Movatterモバイル変換
[0]ホーム
{-# LANGUAGE BangPatterns #-}{-# LANGUAGE CPP #-}{-# LANGUAGE NoImplicitPrelude #-}{-# LANGUAGE MagicHash #-}{-# LANGUAGE Trustworthy #-}{-# OPTIONS_HADDOCK not-home #-}------------------------------------------------------------------------------- |-- Module : GHC.Bits-- Copyright : (c) The University of Glasgow 2001-- License : BSD-style (see the file libraries/base/LICENSE)---- Maintainer : libraries@haskell.org-- Stability : stable-- Portability : portable---- This module defines bitwise operations for signed and unsigned-- integers. Instances of the class 'Bits' for the 'Int' and-- 'Integer' types are available from this module, and instances for-- explicitly sized integral types are available from the-- "Data.Int" and "Data.Word" modules.-------------------------------------------------------------------------------moduleGHC.Bits(Bits((.&.),(.|.),xor,complement,shift,rotate,zeroBits,bit,setBit,clearBit,complementBit,testBit,bitSizeMaybe,bitSize,isSigned,shiftL,shiftR,unsafeShiftL,unsafeShiftR,rotateL,rotateR,popCount),FiniteBits(finiteBitSize,countLeadingZeros,countTrailingZeros),bitDefault,testBitDefault,popCountDefault,toIntegralSized,)where-- Defines the @Bits@ class containing bit-based operations.-- See library document for details on the semantics of the-- individual operations.#include "MachDeps.h"importData.MaybeimportGHC.NumimportGHC.BaseimportGHC.Realinfixl8`shift`,`rotate`,`shiftL`,`shiftR`,`rotateL`,`rotateR`infixl7.&.infixl6`xor`infixl5.|.{-# DEPRECATEDbitSize"Use 'bitSizeMaybe' or 'finiteBitSize' instead"#-}-- deprecated in 7.8-- | The 'Bits' class defines bitwise operations over integral types.---- * Bits are numbered from 0 with bit 0 being the least-- significant bit.classEqa=>Bitsawhere{-# MINIMAL(.&.),(.|.),xor,complement,(shift|(shiftL,shiftR)),(rotate|(rotateL,rotateR)),bitSize,bitSizeMaybe,isSigned,testBit,bit,popCount#-}-- | Bitwise \"and\"(.&.)::a->a->a-- | Bitwise \"or\"(.|.)::a->a->a-- | Bitwise \"xor\"xor::a->a->a{-| Reverse all the bits in the argument -}complement::a->a{-| @'shift' x i@ shifts @x@ left by @i@ bits if @i@ is positive, or right by @-i@ bits otherwise. Right shifts perform sign extension on signed number types; i.e. they fill the top bits with 1 if the @x@ is negative and with 0 otherwise. An instance can define either this unified 'shift' or 'shiftL' and 'shiftR', depending on which is more convenient for the type in question. -}shift::a->Int->aax`shift`Inti|IntiInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<Int0=axa -> Int -> aforall a. Bits a => a -> Int -> a`shiftR`(-Inti)|IntiInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>Int0=axa -> Int -> aforall a. Bits a => a -> Int -> a`shiftL`Inti|Boolotherwise=ax{-| @'rotate' x i@ rotates @x@ left by @i@ bits if @i@ is positive, or right by @-i@ bits otherwise. For unbounded types like 'Integer', 'rotate' is equivalent to 'shift'. An instance can define either this unified 'rotate' or 'rotateL' and 'rotateR', depending on which is more convenient for the type in question. -}rotate::a->Int->aax`rotate`Inti|IntiInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<Int0=axa -> Int -> aforall a. Bits a => a -> Int -> a`rotateR`(-Inti)|IntiInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>Int0=axa -> Int -> aforall a. Bits a => a -> Int -> a`rotateL`Inti|Boolotherwise=ax{- -- Rotation can be implemented in terms of two shifts, but care is -- needed for negative values. This suggested implementation assumes -- 2's-complement arithmetic. It is commented out because it would -- require an extra context (Ord a) on the signature of 'rotate'. x `rotate` i | i<0 && isSigned x && x<0 = let left = i+bitSize x in ((x `shift` i) .&. complement ((-1) `shift` left)) .|. (x `shift` left) | i<0 = (x `shift` i) .|. (x `shift` (i+bitSize x)) | i==0 = x | i>0 = (x `shift` i) .|. (x `shift` (i-bitSize x)) -}-- | 'zeroBits' is the value with all bits unset.---- The following laws ought to hold (for all valid bit indices @/n/@):---- * @'clearBit' 'zeroBits' /n/ == 'zeroBits'@-- * @'setBit' 'zeroBits' /n/ == 'bit' /n/@-- * @'testBit' 'zeroBits' /n/ == False@-- * @'popCount' 'zeroBits' == 0@---- This method uses @'clearBit' ('bit' 0) 0@ as its default-- implementation (which ought to be equivalent to 'zeroBits' for-- types which possess a 0th bit).---- @since 4.7.0.0zeroBits::azeroBits=a -> Int -> aforall a. Bits a => a -> Int -> aclearBit(Int -> aforall a. Bits a => Int -> abitInt0)Int0-- | @bit /i/@ is a value with the @/i/@th bit set and all other bits clear.---- Can be implemented using `bitDefault' if @a@ is also an-- instance of 'Num'.---- See also 'zeroBits'.bit::Int->a-- | @x \`setBit\` i@ is the same as @x .|. bit i@setBit::a->Int->a-- | @x \`clearBit\` i@ is the same as @x .&. complement (bit i)@clearBit::a->Int->a-- | @x \`complementBit\` i@ is the same as @x \`xor\` bit i@complementBit::a->Int->a{-| @x \`testBit\` i@ is the same as @x .&. bit n /= 0@ In other words it returns True if the bit at offset @n is set. Can be implemented using `testBitDefault' if @a@ is also an instance of 'Num'. -}testBit::a->Int->Bool{-| Return the number of bits in the type of the argument. The actual value of the argument is ignored. Returns Nothing for types that do not have a fixed bitsize, like 'Integer'. @since 4.7.0.0 -}bitSizeMaybe::a->MaybeInt{-| Return the number of bits in the type of the argument. The actual value of the argument is ignored. The function 'bitSize' is undefined for types that do not have a fixed bitsize, like 'Integer'. Default implementation based upon 'bitSizeMaybe' provided since 4.12.0.0. -}bitSize::a->IntbitSizeab=Int -> Maybe Int -> Intforall a. a -> Maybe a -> afromMaybe([Char] -> Intforall a. HasCallStack => [Char] -> aerror[Char]"bitSize is undefined")(a -> Maybe Intforall a. Bits a => a -> Maybe IntbitSizeMaybeab){-| Return 'True' if the argument is a signed type. The actual value of the argument is ignored -}isSigned::a->Bool{-# INLINEsetBit#-}{-# INLINEclearBit#-}{-# INLINEcomplementBit#-}ax`setBit`Inti=axa -> a -> aforall a. Bits a => a -> a -> a.|.Int -> aforall a. Bits a => Int -> abitIntiax`clearBit`Inti=axa -> a -> aforall a. Bits a => a -> a -> a.&.a -> aforall a. Bits a => a -> acomplement(Int -> aforall a. Bits a => Int -> abitInti)ax`complementBit`Inti=axa -> a -> aforall a. Bits a => a -> a -> a`xor`Int -> aforall a. Bits a => Int -> abitInti{-| Shift the argument left by the specified number of bits (which must be non-negative). Some instances may throw an 'Control.Exception.Overflow' exception if given a negative input. An instance can define either this and 'shiftR' or the unified 'shift', depending on which is more convenient for the type in question. -}shiftL::a->Int->a{-# INLINEshiftL#-}ax`shiftL`Inti=axa -> Int -> aforall a. Bits a => a -> Int -> a`shift`Inti{-| Shift the argument left by the specified number of bits. The result is undefined for negative shift amounts and shift amounts greater or equal to the 'bitSize'. Defaults to 'shiftL' unless defined explicitly by an instance. @since 4.5.0.0 -}unsafeShiftL::a->Int->a{-# INLINEunsafeShiftL#-}ax`unsafeShiftL`Inti=axa -> Int -> aforall a. Bits a => a -> Int -> a`shiftL`Inti{-| Shift the first argument right by the specified number of bits. The result is undefined for negative shift amounts and shift amounts greater or equal to the 'bitSize'. Some instances may throw an 'Control.Exception.Overflow' exception if given a negative input. Right shifts perform sign extension on signed number types; i.e. they fill the top bits with 1 if the @x@ is negative and with 0 otherwise. An instance can define either this and 'shiftL' or the unified 'shift', depending on which is more convenient for the type in question. -}shiftR::a->Int->a{-# INLINEshiftR#-}ax`shiftR`Inti=axa -> Int -> aforall a. Bits a => a -> Int -> a`shift`(-Inti){-| Shift the first argument right by the specified number of bits, which must be non-negative and smaller than the number of bits in the type. Right shifts perform sign extension on signed number types; i.e. they fill the top bits with 1 if the @x@ is negative and with 0 otherwise. Defaults to 'shiftR' unless defined explicitly by an instance. @since 4.5.0.0 -}unsafeShiftR::a->Int->a{-# INLINEunsafeShiftR#-}ax`unsafeShiftR`Inti=axa -> Int -> aforall a. Bits a => a -> Int -> a`shiftR`Inti{-| Rotate the argument left by the specified number of bits (which must be non-negative). An instance can define either this and 'rotateR' or the unified 'rotate', depending on which is more convenient for the type in question. -}rotateL::a->Int->a{-# INLINErotateL#-}ax`rotateL`Inti=axa -> Int -> aforall a. Bits a => a -> Int -> a`rotate`Inti{-| Rotate the argument right by the specified number of bits (which must be non-negative). An instance can define either this and 'rotateL' or the unified 'rotate', depending on which is more convenient for the type in question. -}rotateR::a->Int->a{-# INLINErotateR#-}ax`rotateR`Inti=axa -> Int -> aforall a. Bits a => a -> Int -> a`rotate`(-Inti){-| Return the number of set bits in the argument. This number is known as the population count or the Hamming weight. Can be implemented using `popCountDefault' if @a@ is also an instance of 'Num'. @since 4.5.0.0 -}popCount::a->Int-- |The 'FiniteBits' class denotes types with a finite, fixed number of bits.---- @since 4.7.0.0classBitsb=>FiniteBitsbwhere-- | Return the number of bits in the type of the argument.-- The actual value of the argument is ignored. Moreover, 'finiteBitSize'-- is total, in contrast to the deprecated 'bitSize' function it replaces.---- @-- 'finiteBitSize' = 'bitSize'-- 'bitSizeMaybe' = 'Just' . 'finiteBitSize'-- @---- @since 4.7.0.0finiteBitSize::b->Int-- | Count number of zero bits preceding the most significant set bit.---- @-- 'countLeadingZeros' ('zeroBits' :: a) = finiteBitSize ('zeroBits' :: a)-- @---- 'countLeadingZeros' can be used to compute log base 2 via---- @-- logBase2 x = 'finiteBitSize' x - 1 - 'countLeadingZeros' x-- @---- Note: The default implementation for this method is intentionally-- naive. However, the instances provided for the primitive-- integral types are implemented using CPU specific machine-- instructions.---- @since 4.8.0.0countLeadingZeros::b->IntcountLeadingZerosbx=(IntwInt -> Int -> Intforall a. Num a => a -> a -> a-Int1)Int -> Int -> Intforall a. Num a => a -> a -> a-Int -> Intgo(IntwInt -> Int -> Intforall a. Num a => a -> a -> a-Int1)wherego :: Int -> IntgoInti|IntiInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<Int0=Inti-- no bit set|b -> Int -> Boolforall a. Bits a => a -> Int -> BooltestBitbxInti=Inti|Boolotherwise=Int -> Intgo(IntiInt -> Int -> Intforall a. Num a => a -> a -> a-Int1)w :: Intw=b -> Intforall b. FiniteBits b => b -> IntfiniteBitSizebx-- | Count number of zero bits following the least significant set bit.---- @-- 'countTrailingZeros' ('zeroBits' :: a) = finiteBitSize ('zeroBits' :: a)-- 'countTrailingZeros' . 'negate' = 'countTrailingZeros'-- @---- The related-- <http://en.wikipedia.org/wiki/Find_first_set find-first-set operation>-- can be expressed in terms of 'countTrailingZeros' as follows---- @-- findFirstSet x = 1 + 'countTrailingZeros' x-- @---- Note: The default implementation for this method is intentionally-- naive. However, the instances provided for the primitive-- integral types are implemented using CPU specific machine-- instructions.---- @since 4.8.0.0countTrailingZeros::b->IntcountTrailingZerosbx=Int -> IntgoInt0wherego :: Int -> IntgoInti|IntiInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Intw=Inti|b -> Int -> Boolforall a. Bits a => a -> Int -> BooltestBitbxInti=Inti|Boolotherwise=Int -> Intgo(IntiInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)w :: Intw=b -> Intforall b. FiniteBits b => b -> IntfiniteBitSizebx-- The defaults below are written with lambdas so that e.g.-- bit = bitDefault-- is fully applied, so inlining will happen-- | Default implementation for 'bit'.---- Note that: @bitDefault i = 1 `shiftL` i@---- @since 4.6.0.0bitDefault::(Bitsa,Numa)=>Int->abitDefault :: forall a. (Bits a, Num a) => Int -> abitDefault=\Inti->a1a -> Int -> aforall a. Bits a => a -> Int -> a`shiftL`Inti{-# INLINEbitDefault#-}-- | Default implementation for 'testBit'.---- Note that: @testBitDefault x i = (x .&. bit i) /= 0@---- @since 4.6.0.0testBitDefault::(Bitsa,Numa)=>a->Int->BooltestBitDefault :: forall a. (Bits a, Num a) => a -> Int -> BooltestBitDefault=\axInti->(axa -> a -> aforall a. Bits a => a -> a -> a.&.Int -> aforall a. Bits a => Int -> abitInti)a -> a -> Boolforall a. Eq a => a -> a -> Bool/=a0{-# INLINEtestBitDefault#-}-- | Default implementation for 'popCount'.---- This implementation is intentionally naive. Instances are expected to provide-- an optimized implementation for their size.---- @since 4.6.0.0popCountDefault::(Bitsa,Numa)=>a->IntpopCountDefault :: forall a. (Bits a, Num a) => a -> IntpopCountDefault=Int -> a -> Intforall {t} {t}. (Num t, Num t, Bits t) => t -> t -> tgoInt0wherego :: t -> t -> tgo!tct0=tcgotctw=t -> t -> tgo(tct -> t -> tforall a. Num a => a -> a -> a+t1)(twt -> t -> tforall a. Bits a => a -> a -> a.&.(twt -> t -> tforall a. Num a => a -> a -> a-t1))-- clear the least significant{-# INLINABLEpopCountDefault#-}-- | Interpret 'Bool' as 1-bit bit-field---- @since 4.7.0.0instanceBitsBoolwhere.&. :: Bool -> Bool -> Bool(.&.)=Bool -> Bool -> Bool(&&).|. :: Bool -> Bool -> Bool(.|.)=Bool -> Bool -> Bool(||)xor :: Bool -> Bool -> Boolxor=Bool -> Bool -> Boolforall a. Eq a => a -> a -> Bool(/=)complement :: Bool -> Boolcomplement=Bool -> Boolnotshift :: Bool -> Int -> BoolshiftBoolxInt0=BoolxshiftBool_Int_=BoolFalserotate :: Bool -> Int -> BoolrotateBoolxInt_=Boolxbit :: Int -> BoolbitInt0=BoolTruebitInt_=BoolFalsetestBit :: Bool -> Int -> BooltestBitBoolxInt0=BoolxtestBitBool_Int_=BoolFalsebitSizeMaybe :: Bool -> Maybe IntbitSizeMaybeBool_=Int -> Maybe Intforall a. a -> Maybe aJustInt1bitSize :: Bool -> IntbitSizeBool_=Int1isSigned :: Bool -> BoolisSignedBool_=BoolFalsepopCount :: Bool -> IntpopCountBoolFalse=Int0popCountBoolTrue=Int1-- | @since 4.7.0.0instanceFiniteBitsBoolwherefiniteBitSize :: Bool -> IntfiniteBitSizeBool_=Int1countTrailingZeros :: Bool -> IntcountTrailingZerosBoolx=ifBoolxthenInt0elseInt1countLeadingZeros :: Bool -> IntcountLeadingZerosBoolx=ifBoolxthenInt0elseInt1-- | @since 2.01instanceBitsIntwhere{-# INLINEshift#-}{-# INLINEbit#-}{-# INLINEtestBit#-}-- We want popCnt# to be inlined in user code so that `ghc -msse4.2`-- can compile it down to a popcnt instruction without an extra function call{-# INLINEpopCount#-}zeroBits :: IntzeroBits=Int0bit :: Int -> Intbit=Int -> Intforall a. (Bits a, Num a) => Int -> abitDefaulttestBit :: Int -> Int -> BooltestBit=Int -> Int -> Boolforall a. (Bits a, Num a) => a -> Int -> BooltestBitDefault(I#Int#x#).&. :: Int -> Int -> Int.&.(I#Int#y#)=Int# -> IntI#(Int#x#Int# -> Int# -> Int#`andI#`Int#y#)(I#Int#x#).|. :: Int -> Int -> Int.|.(I#Int#y#)=Int# -> IntI#(Int#x#Int# -> Int# -> Int#`orI#`Int#y#)(I#Int#x#)xor :: Int -> Int -> Int`xor`(I#Int#y#)=Int# -> IntI#(Int#x#Int# -> Int# -> Int#`xorI#`Int#y#)complement :: Int -> Intcomplement(I#Int#x#)=Int# -> IntI#(Int# -> Int#notI#Int#x#)(I#Int#x#)shift :: Int -> Int -> Int`shift`(I#Int#i#)|Int# -> BoolisTrue#(Int#i#Int# -> Int# -> Int#>=#Int#0#)=Int# -> IntI#(Int#x#Int# -> Int# -> Int#`iShiftL#`Int#i#)|Boolotherwise=Int# -> IntI#(Int#x#Int# -> Int# -> Int#`iShiftRA#`Int# -> Int#negateInt#Int#i#)(I#Int#x#)shiftL :: Int -> Int -> Int`shiftL`(I#Int#i#)|Int# -> BoolisTrue#(Int#i#Int# -> Int# -> Int#>=#Int#0#)=Int# -> IntI#(Int#x#Int# -> Int# -> Int#`iShiftL#`Int#i#)|Boolotherwise=Intforall a. aoverflowError(I#Int#x#)unsafeShiftL :: Int -> Int -> Int`unsafeShiftL`(I#Int#i#)=Int# -> IntI#(Int#x#Int# -> Int# -> Int#`uncheckedIShiftL#`Int#i#)(I#Int#x#)shiftR :: Int -> Int -> Int`shiftR`(I#Int#i#)|Int# -> BoolisTrue#(Int#i#Int# -> Int# -> Int#>=#Int#0#)=Int# -> IntI#(Int#x#Int# -> Int# -> Int#`iShiftRA#`Int#i#)|Boolotherwise=Intforall a. aoverflowError(I#Int#x#)unsafeShiftR :: Int -> Int -> Int`unsafeShiftR`(I#Int#i#)=Int# -> IntI#(Int#x#Int# -> Int# -> Int#`uncheckedIShiftRA#`Int#i#){-# INLINErotate#-}-- See Note [Constant folding for rotate](I#Int#x#)rotate :: Int -> Int -> Int`rotate`(I#Int#i#)=Int# -> IntI#((Int#x#Int# -> Int# -> Int#`uncheckedIShiftL#`Int#i'#)Int# -> Int# -> Int#`orI#`(Int#x#Int# -> Int# -> Int#`uncheckedIShiftRL#`(Int#wsibInt# -> Int# -> Int#-#Int#i'#)))where!i'# :: Int#i'#=Int#i#Int# -> Int# -> Int#`andI#`(Int#wsibInt# -> Int# -> Int#-#Int#1#)!wsib :: Int#wsib=WORD_SIZE_IN_BITS#{- work around preprocessor problem (??) -}bitSizeMaybe :: Int -> Maybe IntbitSizeMaybeInti=Int -> Maybe Intforall a. a -> Maybe aJust(Int -> Intforall b. FiniteBits b => b -> IntfiniteBitSizeInti)bitSize :: Int -> IntbitSizeInti=Int -> Intforall b. FiniteBits b => b -> IntfiniteBitSizeIntipopCount :: Int -> IntpopCount(I#Int#x#)=Int# -> IntI#(Word# -> Int#word2Int#(Word# -> Word#popCnt#(Int# -> Word#int2Word#Int#x#)))isSigned :: Int -> BoolisSignedInt_=BoolTrue-- | @since 4.6.0.0instanceFiniteBitsIntwherefiniteBitSize :: Int -> IntfiniteBitSizeInt_=WORD_SIZE_IN_BITScountLeadingZeros :: Int -> IntcountLeadingZeros(I#Int#x#)=Int# -> IntI#(Word# -> Int#word2Int#(Word# -> Word#clz#(Int# -> Word#int2Word#Int#x#))){-# INLINEcountLeadingZeros#-}countTrailingZeros :: Int -> IntcountTrailingZeros(I#Int#x#)=Int# -> IntI#(Word# -> Int#word2Int#(Word# -> Word#ctz#(Int# -> Word#int2Word#Int#x#))){-# INLINEcountTrailingZeros#-}-- | @since 2.01instanceBitsWordwhere{-# INLINEshift#-}{-# INLINEbit#-}{-# INLINEtestBit#-}{-# INLINEpopCount#-}(W#Word#x#).&. :: Word -> Word -> Word.&.(W#Word#y#)=Word# -> WordW#(Word#x#Word# -> Word# -> Word#`and#`Word#y#)(W#Word#x#).|. :: Word -> Word -> Word.|.(W#Word#y#)=Word# -> WordW#(Word#x#Word# -> Word# -> Word#`or#`Word#y#)(W#Word#x#)xor :: Word -> Word -> Word`xor`(W#Word#y#)=Word# -> WordW#(Word#x#Word# -> Word# -> Word#`xor#`Word#y#)complement :: Word -> Wordcomplement(W#Word#x#)=Word# -> WordW#(Word# -> Word#not#Word#x#)(W#Word#x#)shift :: Word -> Int -> Word`shift`(I#Int#i#)|Int# -> BoolisTrue#(Int#i#Int# -> Int# -> Int#>=#Int#0#)=Word# -> WordW#(Word#x#Word# -> Int# -> Word#`shiftL#`Int#i#)|Boolotherwise=Word# -> WordW#(Word#x#Word# -> Int# -> Word#`shiftRL#`Int# -> Int#negateInt#Int#i#)(W#Word#x#)shiftL :: Word -> Int -> Word`shiftL`(I#Int#i#)|Int# -> BoolisTrue#(Int#i#Int# -> Int# -> Int#>=#Int#0#)=Word# -> WordW#(Word#x#Word# -> Int# -> Word#`shiftL#`Int#i#)|Boolotherwise=Wordforall a. aoverflowError(W#Word#x#)unsafeShiftL :: Word -> Int -> Word`unsafeShiftL`(I#Int#i#)=Word# -> WordW#(Word#x#Word# -> Int# -> Word#`uncheckedShiftL#`Int#i#)(W#Word#x#)shiftR :: Word -> Int -> Word`shiftR`(I#Int#i#)|Int# -> BoolisTrue#(Int#i#Int# -> Int# -> Int#>=#Int#0#)=Word# -> WordW#(Word#x#Word# -> Int# -> Word#`shiftRL#`Int#i#)|Boolotherwise=Wordforall a. aoverflowError(W#Word#x#)unsafeShiftR :: Word -> Int -> Word`unsafeShiftR`(I#Int#i#)=Word# -> WordW#(Word#x#Word# -> Int# -> Word#`uncheckedShiftRL#`Int#i#)(W#Word#x#)rotate :: Word -> Int -> Word`rotate`(I#Int#i#)|Int# -> BoolisTrue#(Int#i'#Int# -> Int# -> Int#==#Int#0#)=Word# -> WordW#Word#x#|Boolotherwise=Word# -> WordW#((Word#x#Word# -> Int# -> Word#`uncheckedShiftL#`Int#i'#)Word# -> Word# -> Word#`or#`(Word#x#Word# -> Int# -> Word#`uncheckedShiftRL#`(Int#wsibInt# -> Int# -> Int#-#Int#i'#)))where!i'# :: Int#i'#=Int#i#Int# -> Int# -> Int#`andI#`(Int#wsibInt# -> Int# -> Int#-#Int#1#)!wsib :: Int#wsib=WORD_SIZE_IN_BITS#{- work around preprocessor problem (??) -}bitSizeMaybe :: Word -> Maybe IntbitSizeMaybeWordi=Int -> Maybe Intforall a. a -> Maybe aJust(Word -> Intforall b. FiniteBits b => b -> IntfiniteBitSizeWordi)bitSize :: Word -> IntbitSizeWordi=Word -> Intforall b. FiniteBits b => b -> IntfiniteBitSizeWordiisSigned :: Word -> BoolisSignedWord_=BoolFalsepopCount :: Word -> IntpopCount(W#Word#x#)=Int# -> IntI#(Word# -> Int#word2Int#(Word# -> Word#popCnt#Word#x#))bit :: Int -> Wordbit=Int -> Wordforall a. (Bits a, Num a) => Int -> abitDefaulttestBit :: Word -> Int -> BooltestBit=Word -> Int -> Boolforall a. (Bits a, Num a) => a -> Int -> BooltestBitDefault-- | @since 4.6.0.0instanceFiniteBitsWordwherefiniteBitSize :: Word -> IntfiniteBitSizeWord_=WORD_SIZE_IN_BITScountLeadingZeros :: Word -> IntcountLeadingZeros(W#Word#x#)=Int# -> IntI#(Word# -> Int#word2Int#(Word# -> Word#clz#Word#x#)){-# INLINEcountLeadingZeros#-}countTrailingZeros :: Word -> IntcountTrailingZeros(W#Word#x#)=Int# -> IntI#(Word# -> Int#word2Int#(Word# -> Word#ctz#Word#x#)){-# INLINEcountTrailingZeros#-}-- | @since 2.01instanceBitsIntegerwhere.&. :: Integer -> Integer -> Integer(.&.)=Integer -> Integer -> IntegerintegerAnd.|. :: Integer -> Integer -> Integer(.|.)=Integer -> Integer -> IntegerintegerOrxor :: Integer -> Integer -> Integerxor=Integer -> Integer -> IntegerintegerXorcomplement :: Integer -> Integercomplement=Integer -> IntegerintegerComplementunsafeShiftR :: Integer -> Int -> IntegerunsafeShiftRIntegerxInti=Integer -> Word -> IntegerintegerShiftRIntegerx(Int -> Wordforall a b. (Integral a, Num b) => a -> bfromIntegralInti)unsafeShiftL :: Integer -> Int -> IntegerunsafeShiftLIntegerxInti=Integer -> Word -> IntegerintegerShiftLIntegerx(Int -> Wordforall a b. (Integral a, Num b) => a -> bfromIntegralInti)shiftR :: Integer -> Int -> IntegershiftRIntegerxi :: Inti@(I#Int#i#)|Int# -> BoolisTrue#(Int#i#Int# -> Int# -> Int#>=#Int#0#)=Integer -> Int -> Integerforall a. Bits a => a -> Int -> aunsafeShiftRIntegerxInti|Boolotherwise=Integerforall a. aoverflowErrorshiftL :: Integer -> Int -> IntegershiftLIntegerxi :: Inti@(I#Int#i#)|Int# -> BoolisTrue#(Int#i#Int# -> Int# -> Int#>=#Int#0#)=Integer -> Int -> Integerforall a. Bits a => a -> Int -> aunsafeShiftLIntegerxInti|Boolotherwise=Integerforall a. aoverflowErrorshift :: Integer -> Int -> IntegershiftIntegerxInti|IntiInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Int0=Integer -> Word -> IntegerintegerShiftLIntegerx(Int -> Wordforall a b. (Integral a, Num b) => a -> bfromIntegralInti)|Boolotherwise=Integer -> Word -> IntegerintegerShiftRIntegerx(Int -> Wordforall a b. (Integral a, Num b) => a -> bfromIntegral(Int -> Intforall a. Num a => a -> anegateInti))testBit :: Integer -> Int -> BooltestBitIntegerxInti=Integer -> Word -> BoolintegerTestBitIntegerx(Int -> Wordforall a b. (Integral a, Num b) => a -> bfromIntegralInti)zeroBits :: IntegerzeroBits=IntegerintegerZerobit :: Int -> Integerbit(I#Int#i)=Word# -> IntegerintegerBit#(Int# -> Word#int2Word#Int#i)popCount :: Integer -> IntpopCountIntegerx=Int# -> IntI#(Integer -> Int#integerPopCount#Integerx)rotate :: Integer -> Int -> IntegerrotateIntegerxInti=Integer -> Int -> Integerforall a. Bits a => a -> Int -> ashiftIntegerxInti-- since an Integer never wraps aroundbitSizeMaybe :: Integer -> Maybe IntbitSizeMaybeInteger_=Maybe Intforall a. Maybe aNothingbitSize :: Integer -> IntbitSizeInteger_=[Char] -> Intforall a. [Char] -> aerrorWithoutStackTrace[Char]"Data.Bits.bitSize(Integer)"isSigned :: Integer -> BoolisSignedInteger_=BoolTrue-- | @since 4.8.0instanceBitsNaturalwhere.&. :: Natural -> Natural -> Natural(.&.)=Natural -> Natural -> NaturalnaturalAnd.|. :: Natural -> Natural -> Natural(.|.)=Natural -> Natural -> NaturalnaturalOrxor :: Natural -> Natural -> Naturalxor=Natural -> Natural -> NaturalnaturalXorcomplement :: Natural -> NaturalcomplementNatural_=[Char] -> Naturalforall a. [Char] -> aerrorWithoutStackTrace[Char]"Bits.complement: Natural complement undefined"unsafeShiftR :: Natural -> Int -> NaturalunsafeShiftRNaturalxInti=Natural -> Word -> NaturalnaturalShiftRNaturalx(Int -> Wordforall a b. (Integral a, Num b) => a -> bfromIntegralInti)unsafeShiftL :: Natural -> Int -> NaturalunsafeShiftLNaturalxInti=Natural -> Word -> NaturalnaturalShiftLNaturalx(Int -> Wordforall a b. (Integral a, Num b) => a -> bfromIntegralInti)shiftR :: Natural -> Int -> NaturalshiftRNaturalxi :: Inti@(I#Int#i#)|Int# -> BoolisTrue#(Int#i#Int# -> Int# -> Int#>=#Int#0#)=Natural -> Int -> Naturalforall a. Bits a => a -> Int -> aunsafeShiftRNaturalxInti|Boolotherwise=Naturalforall a. aoverflowErrorshiftL :: Natural -> Int -> NaturalshiftLNaturalxi :: Inti@(I#Int#i#)|Int# -> BoolisTrue#(Int#i#Int# -> Int# -> Int#>=#Int#0#)=Natural -> Int -> Naturalforall a. Bits a => a -> Int -> aunsafeShiftLNaturalxInti|Boolotherwise=Naturalforall a. aoverflowErrorshift :: Natural -> Int -> NaturalshiftNaturalxInti|IntiInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Int0=Natural -> Word -> NaturalnaturalShiftLNaturalx(Int -> Wordforall a b. (Integral a, Num b) => a -> bfromIntegralInti)|Boolotherwise=Natural -> Word -> NaturalnaturalShiftRNaturalx(Int -> Wordforall a b. (Integral a, Num b) => a -> bfromIntegral(Int -> Intforall a. Num a => a -> anegateInti))testBit :: Natural -> Int -> BooltestBitNaturalxInti=Natural -> Word -> BoolnaturalTestBitNaturalx(Int -> Wordforall a b. (Integral a, Num b) => a -> bfromIntegralInti)zeroBits :: NaturalzeroBits=NaturalnaturalZerosetBit :: Natural -> Int -> NaturalsetBitNaturalxInti=Natural -> Word -> NaturalnaturalSetBitNaturalx(Int -> Wordforall a b. (Integral a, Num b) => a -> bfromIntegralInti)clearBit :: Natural -> Int -> NaturalclearBitNaturalxInti=Natural -> Word -> NaturalnaturalClearBitNaturalx(Int -> Wordforall a b. (Integral a, Num b) => a -> bfromIntegralInti)complementBit :: Natural -> Int -> NaturalcomplementBitNaturalxInti=Natural -> Word -> NaturalnaturalComplementBitNaturalx(Int -> Wordforall a b. (Integral a, Num b) => a -> bfromIntegralInti)bit :: Int -> Naturalbit(I#Int#i)=Word# -> NaturalnaturalBit#(Int# -> Word#int2Word#Int#i)popCount :: Natural -> IntpopCountNaturalx=Int# -> IntI#(Word# -> Int#word2Int#(Natural -> Word#naturalPopCount#Naturalx))rotate :: Natural -> Int -> NaturalrotateNaturalxInti=Natural -> Int -> Naturalforall a. Bits a => a -> Int -> ashiftNaturalxInti-- since an Natural never wraps aroundbitSizeMaybe :: Natural -> Maybe IntbitSizeMaybeNatural_=Maybe Intforall a. Maybe aNothingbitSize :: Natural -> IntbitSizeNatural_=[Char] -> Intforall a. [Char] -> aerrorWithoutStackTrace[Char]"Data.Bits.bitSize(Natural)"isSigned :: Natural -> BoolisSignedNatural_=BoolFalse------------------------------------------------------------------------------- | Attempt to convert an 'Integral' type @a@ to an 'Integral' type @b@ using-- the size of the types as measured by 'Bits' methods.---- A simpler version of this function is:---- > toIntegral :: (Integral a, Integral b) => a -> Maybe b-- > toIntegral x-- > | toInteger x == toInteger y = Just y-- > | otherwise = Nothing-- > where-- > y = fromIntegral x---- This version requires going through 'Integer', which can be inefficient.-- However, @toIntegralSized@ is optimized to allow GHC to statically determine-- the relative type sizes (as measured by 'bitSizeMaybe' and 'isSigned') and-- avoid going through 'Integer' for many types. (The implementation uses-- 'fromIntegral', which is itself optimized with rules for @base@ types but may-- go through 'Integer' for some type pairs.)---- @since 4.8.0.0toIntegralSized::(Integrala,Integralb,Bitsa,Bitsb)=>a->MaybebtoIntegralSized :: forall a b.(Integral a, Integral b, Bits a, Bits b) =>a -> Maybe btoIntegralSizedax-- See Note [toIntegralSized optimization]|Bool -> (a -> Bool) -> Maybe a -> Boolforall b a. b -> (a -> b) -> Maybe a -> bmaybeBoolTrue(a -> a -> Boolforall a. Ord a => a -> a -> Bool<=ax)Maybe ayMinBound,Bool -> (a -> Bool) -> Maybe a -> Boolforall b a. b -> (a -> b) -> Maybe a -> bmaybeBoolTrue(axa -> a -> Boolforall a. Ord a => a -> a -> Bool<=)Maybe ayMaxBound=b -> Maybe bforall a. a -> Maybe aJustby|Boolotherwise=Maybe bforall a. Maybe aNothingwherey :: by=a -> bforall a b. (Integral a, Num b) => a -> bfromIntegralaxxWidth :: Maybe IntxWidth=a -> Maybe Intforall a. Bits a => a -> Maybe IntbitSizeMaybeaxyWidth :: Maybe IntyWidth=b -> Maybe Intforall a. Bits a => a -> Maybe IntbitSizeMaybebyyMinBound :: Maybe ayMinBound|a -> b -> Boolforall a b. (Bits a, Bits b) => a -> b -> BoolisBitSubTypeaxby=Maybe aforall a. Maybe aNothing|a -> Boolforall a. Bits a => a -> BoolisSignedax,Bool -> Boolnot(b -> Boolforall a. Bits a => a -> BoolisSignedby)=a -> Maybe aforall a. a -> Maybe aJusta0|a -> Boolforall a. Bits a => a -> BoolisSignedax,b -> Boolforall a. Bits a => a -> BoolisSignedby,JustIntyW<-Maybe IntyWidth=a -> Maybe aforall a. a -> Maybe aJust(a -> aforall a. Num a => a -> anegate(a -> a) -> a -> aforall a b. (a -> b) -> a -> b$Int -> aforall a. Bits a => Int -> abit(IntyWInt -> Int -> Intforall a. Num a => a -> a -> a-Int1))-- Assumes sub-type|Boolotherwise=Maybe aforall a. Maybe aNothingyMaxBound :: Maybe ayMaxBound|a -> b -> Boolforall a b. (Bits a, Bits b) => a -> b -> BoolisBitSubTypeaxby=Maybe aforall a. Maybe aNothing|a -> Boolforall a. Bits a => a -> BoolisSignedax,Bool -> Boolnot(b -> Boolforall a. Bits a => a -> BoolisSignedby),JustIntxW<-Maybe IntxWidth,JustIntyW<-Maybe IntyWidth,IntxWInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<=IntyWInt -> Int -> Intforall a. Num a => a -> a -> a+Int1=Maybe aforall a. Maybe aNothing-- Max bound beyond a's domain|JustIntyW<-Maybe IntyWidth=ifb -> Boolforall a. Bits a => a -> BoolisSignedbythena -> Maybe aforall a. a -> Maybe aJust(Int -> aforall a. Bits a => Int -> abit(IntyWInt -> Int -> Intforall a. Num a => a -> a -> a-Int1)a -> a -> aforall a. Num a => a -> a -> a-a1)elsea -> Maybe aforall a. a -> Maybe aJust(Int -> aforall a. Bits a => Int -> abitIntyWa -> a -> aforall a. Num a => a -> a -> a-a1)|Boolotherwise=Maybe aforall a. Maybe aNothing{-# INLINABLEtoIntegralSized#-}-- | 'True' if the size of @a@ is @<=@ the size of @b@, where size is measured-- by 'bitSizeMaybe' and 'isSigned'.isBitSubType::(Bitsa,Bitsb)=>a->b->BoolisBitSubType :: forall a b. (Bits a, Bits b) => a -> b -> BoolisBitSubTypeaxby-- Reflexive|Maybe IntxWidthMaybe Int -> Maybe Int -> Boolforall a. Eq a => a -> a -> Bool==Maybe IntyWidth,BoolxSignedBool -> Bool -> Boolforall a. Eq a => a -> a -> Bool==BoolySigned=BoolTrue-- Every integer is a subset of 'Integer'|BoolySigned,Maybe Intforall a. Maybe aNothingMaybe Int -> Maybe Int -> Boolforall a. Eq a => a -> a -> Bool==Maybe IntyWidth=BoolTrue|Bool -> BoolnotBoolxSigned,Bool -> BoolnotBoolySigned,Maybe Intforall a. Maybe aNothingMaybe Int -> Maybe Int -> Boolforall a. Eq a => a -> a -> Bool==Maybe IntyWidth=BoolTrue-- Sub-type relations between fixed-with types|BoolxSignedBool -> Bool -> Boolforall a. Eq a => a -> a -> Bool==BoolySigned,JustIntxW<-Maybe IntxWidth,JustIntyW<-Maybe IntyWidth=IntxWInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<=IntyW|Bool -> BoolnotBoolxSigned,BoolySigned,JustIntxW<-Maybe IntxWidth,JustIntyW<-Maybe IntyWidth=IntxWInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<IntyW|Boolotherwise=BoolFalsewherexWidth :: Maybe IntxWidth=a -> Maybe Intforall a. Bits a => a -> Maybe IntbitSizeMaybeaxxSigned :: BoolxSigned=a -> Boolforall a. Bits a => a -> BoolisSignedaxyWidth :: Maybe IntyWidth=b -> Maybe Intforall a. Bits a => a -> Maybe IntbitSizeMaybebyySigned :: BoolySigned=b -> Boolforall a. Bits a => a -> BoolisSignedby{-# INLINEisBitSubType#-}{-Note [Constant folding for rotate]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~The INLINE on the Int instance of rotate enables it to be constantfolded. For example: sumU . mapU (`rotate` 3) . replicateU 10000000 $ (7 :: Int)goes to: Main.$wfold = \ (ww_sO7 :: Int#) (ww1_sOb :: Int#) -> case ww1_sOb of wild_XM { __DEFAULT -> Main.$wfold (+# ww_sO7 56) (+# wild_XM 1); 10000000 -> ww_sO7whereas before it was left as a call to $wrotate.All other Bits instances seem to inline well enough on theirown to enable constant folding; for example 'shift': sumU . mapU (`shift` 3) . replicateU 10000000 $ (7 :: Int) goes to: Main.$wfold = \ (ww_sOb :: Int#) (ww1_sOf :: Int#) -> case ww1_sOf of wild_XM { __DEFAULT -> Main.$wfold (+# ww_sOb 56) (+# wild_XM 1); 10000000 -> ww_sOb }-}-- Note [toIntegralSized optimization]-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-- The code in 'toIntegralSized' relies on GHC optimizing away statically-- decidable branches.---- If both integral types are statically known, GHC will be able optimize the-- code significantly (for @-O1@ and better).---- For instance (as of GHC 7.8.1) the following definitions:---- > w16_to_i32 = toIntegralSized :: Word16 -> Maybe Int32-- >-- > i16_to_w16 = toIntegralSized :: Int16 -> Maybe Word16---- are translated into the following (simplified) /GHC Core/ language:---- > w16_to_i32 = \x -> Just (case x of _ { W16# x# -> I32# (word2Int# x#) })-- >-- > i16_to_w16 = \x -> case eta of _-- > { I16# b1 -> case tagToEnum# (<=# 0 b1) of _-- > { False -> Nothing-- > ; True -> Just (W16# (narrow16Word# (int2Word# b1)))-- > }-- > }
[8]ページ先頭