File: BitMask.hs

package info (click to toggle)
haskell-integer-roots 1.0.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 264 kB
  • sloc: haskell: 1,591; makefile: 3
file content (56 lines) | stat: -rw-r--r-- 1,532 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
-- |
-- Module:      Math.NumberTheory.Utils.BitMask
-- Copyright:   (c) 2020 Andrew Lelechenko
-- Licence:     MIT
-- Maintainer:  Andrew Lelechenko <andrew.lelechenko@gmail.com>
--
-- Bit mask utilities.
--

{-# LANGUAGE CPP          #-}
{-# LANGUAGE MagicHash    #-}

module Math.NumberTheory.Utils.BitMask
  ( indexBitSet
  -- , vectorToAddrLiteral
  ) where

#include "MachDeps.h"

import Data.Bits (countTrailingZeros, finiteBitSize, testBit, (.&.))
import GHC.Exts (Int(..), Word(..), Ptr(..), iShiftRL#, indexWordOffAddr#)
#ifdef WORDS_BIGENDIAN
import GHC.Exts (byteSwap#)
#endif

-- import Data.Bits (unsafeShiftL)
-- import Data.List (unfoldr)
-- import Data.Char (chr)

indexBitSet :: Ptr Word -> Int -> Bool
indexBitSet (Ptr addr#) i@(I# i#) = word `testBit` (i .&. (fbs - 1))
  where
    fbs = finiteBitSize (0 :: Word)
    logFbs# = case countTrailingZeros fbs of
      I# l# -> l#
    word# = indexWordOffAddr# addr# (i# `iShiftRL#` logFbs#)
#ifdef WORDS_BIGENDIAN
    word = W# (byteSwap# word#)
#else
    word = W# word#
#endif

-- vectorToAddrLiteral :: [Bool] -> String
-- vectorToAddrLiteral = map (chr . toByte . padTo8) . unfoldr go
--   where
--     go :: [a] -> Maybe ([a], [a])
--     go [] = Nothing
--     go xs = Just (take 8 xs, drop 8 xs)

--     padTo8 :: [Bool] -> [Bool]
--     padTo8 xs
--       | length xs >= 8 = xs
--       | otherwise = xs ++ replicate (8 - length xs) False

--     toByte :: [Bool] -> Int
--     toByte xs = sum $ map (\i -> if xs !! i then 1 `unsafeShiftL` i else 0) [0..7]