{-# LANGUAGE CPP #-}
module Unicode.Internal.Bits
( lookupBit64,
lookupIntN
) where
#include "MachDeps.h"
import Data.Bits (finiteBitSize, popCount)
import GHC.Exts
(Addr#, Int(..), Word(..),
indexWordOffAddr#, indexWord8OffAddr#,
andI#, uncheckedIShiftRL#,
and#, word2Int#, uncheckedShiftL#)
#if MIN_VERSION_base(4,16,0)
import GHC.Exts (word8ToWord#)
#endif
#ifdef WORDS_BIGENDIAN
import GHC.Exts (byteSwap#)
#endif
lookupBit64 :: Addr# -> Int -> Bool
lookupBit64 :: Addr# -> Int -> Bool
lookupBit64 Addr#
addr# (I# Int#
index#) = Word# -> Word
W# (Word#
word## Word# -> Word# -> Word#
`and#` Word#
bitMask##) Word -> Word -> Bool
forall a. Eq a => a -> a -> Bool
/= Word
0
where
!fbs :: Int
fbs@(I# Int#
fbs#) = Word -> Int
forall b. FiniteBits b => b -> Int
finiteBitSize (Word
0 :: Word) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
!(I# Int#
logFbs#) = case Int
fbs of
Int
31 -> Int
5
Int
63 -> Int
6
Int
_ -> Int -> Int
forall a. Bits a => a -> Int
popCount Int
fbs
wordIndex# :: Int#
wordIndex# = Int#
index# Int# -> Int# -> Int#
`uncheckedIShiftRL#` Int#
logFbs#
#ifdef WORDS_BIGENDIAN
word## = byteSwap# (indexWordOffAddr# addr# wordIndex#)
#else
word## :: Word#
word## = Addr# -> Int# -> Word#
indexWordOffAddr# Addr#
addr# Int#
wordIndex#
#endif
bitIndex# :: Int#
bitIndex# = Int#
index# Int# -> Int# -> Int#
`andI#` Int#
fbs#
bitMask## :: Word#
bitMask## = Word#
1## Word# -> Int# -> Word#
`uncheckedShiftL#` Int#
bitIndex#
lookupIntN
:: Addr#
-> Int
-> Int
lookupIntN :: Addr# -> Int -> Int
lookupIntN Addr#
addr# (I# Int#
index#) = Int# -> Int
I# (Word# -> Int#
word2Int# Word#
word##)
where
#if MIN_VERSION_base(4,16,0)
word## :: Word#
word## = Word8# -> Word#
word8ToWord# (Addr# -> Int# -> Word8#
indexWord8OffAddr# Addr#
addr# Int#
index#)
#else
word## = indexWord8OffAddr# addr# index#
#endif