{-# LANGUAGE CPP, MagicHash, UnboxedTuples, BangPatterns #-}
{-# OPTIONS_HADDOCK hide #-}
module Math.NumberTheory.Utils
( shiftToOddCount
, shiftToOdd
, shiftToOdd#
, shiftToOddCount#
, bitCountWord
, bitCountInt
, bitCountWord#
, uncheckedShiftR
, splitOff
, splitOff#
, mergeBy
, recipMod
, toWheel30
, fromWheel30
) where
#include "MachDeps.h"
import Prelude hiding (mod, quotRem)
import qualified Prelude as P
import GHC.Base
import GHC.Integer.GMP.Internals
import GHC.Natural
import Data.Bits
import Math.NumberTheory.Euclidean
uncheckedShiftR :: Word -> Int -> Word
uncheckedShiftR (W# w#) (I# i#) = W# (uncheckedShiftRL# w# i#)
{-# RULES
"shiftToOddCount/Int" shiftToOddCount = shiftOCInt
"shiftToOddCount/Word" shiftToOddCount = shiftOCWord
"shiftToOddCount/Integer" shiftToOddCount = shiftOCInteger
"shiftToOddCount/Natural" shiftToOddCount = shiftOCNatural
#-}
{-# INLINE [1] shiftToOddCount #-}
shiftToOddCount :: Integral a => a -> (Word, a)
shiftToOddCount n = case shiftOCInteger (fromIntegral n) of
(z, o) -> (z, fromInteger o)
shiftOCWord :: Word -> (Word, Word)
shiftOCWord (W# w#) = case shiftToOddCount# w# of
(# z# , u# #) -> (W# z#, W# u#)
shiftOCInt :: Int -> (Word, Int)
shiftOCInt (I# i#) = case shiftToOddCount# (int2Word# i#) of
(# z#, u# #) -> (W# z#, I# (word2Int# u#))
shiftOCInteger :: Integer -> (Word, Integer)
shiftOCInteger n@(S# i#) =
case shiftToOddCount# (int2Word# i#) of
(# 0##, _ #) -> (0, n)
(# z#, w# #) -> (W# z#, wordToInteger w#)
shiftOCInteger n@(Jp# bn#) = case bigNatZeroCount bn# of
0## -> (0, n)
z# -> (W# z#, bigNatToInteger (bn# `shiftRBigNat` (word2Int# z#)))
shiftOCInteger n@(Jn# bn#) = case bigNatZeroCount bn# of
0## -> (0, n)
z# -> (W# z#, bigNatToNegInteger (bn# `shiftRBigNat` (word2Int# z#)))
shiftOCNatural :: Natural -> (Word, Natural)
shiftOCNatural n@(NatS# i#) =
case shiftToOddCount# i# of
(# 0##, _ #) -> (0, n)
(# z#, w# #) -> (W# z#, NatS# w#)
shiftOCNatural n@(NatJ# bn#) = case bigNatZeroCount bn# of
0## -> (0, n)
z# -> (W# z#, bigNatToNatural (bn# `shiftRBigNat` (word2Int# z#)))
bigNatZeroCount :: BigNat -> Word#
bigNatZeroCount bn# = count 0## 0#
where
count a# i# =
case indexBigNat# bn# i# of
0## -> count (a# `plusWord#` WORD_SIZE_IN_BITS##) (i# +# 1#)
w# -> a# `plusWord#` ctz# w#
{-# RULES
"shiftToOdd/Int" shiftToOdd = shiftOInt
"shiftToOdd/Word" shiftToOdd = shiftOWord
"shiftToOdd/Integer" shiftToOdd = shiftOInteger
#-}
{-# INLINE [1] shiftToOdd #-}
shiftToOdd :: Integral a => a -> a
shiftToOdd n = fromInteger (shiftOInteger (fromIntegral n))
shiftOInt :: Int -> Int
shiftOInt (I# i#) = I# (word2Int# (shiftToOdd# (int2Word# i#)))
shiftOWord :: Word -> Word
shiftOWord (W# w#) = W# (shiftToOdd# w#)
shiftOInteger :: Integer -> Integer
shiftOInteger (S# i#) = wordToInteger (shiftToOdd# (int2Word# i#))
shiftOInteger n@(Jp# bn#) = case bigNatZeroCount bn# of
0## -> n
z# -> bigNatToInteger (bn# `shiftRBigNat` (word2Int# z#))
shiftOInteger n@(Jn# bn#) = case bigNatZeroCount bn# of
0## -> n
z# -> bigNatToNegInteger (bn# `shiftRBigNat` (word2Int# z#))
shiftToOdd# :: Word# -> Word#
shiftToOdd# w# = uncheckedShiftRL# w# (word2Int# (ctz# w#))
shiftToOddCount# :: Word# -> (# Word#, Word# #)
shiftToOddCount# w# = case ctz# w# of
k# -> (# k#, uncheckedShiftRL# w# (word2Int# k#) #)
bitCountWord# :: Word# -> Int#
bitCountWord# w# = case bitCountWord (W# w#) of
I# i# -> i#
bitCountWord :: Word -> Int
bitCountWord = popCount
bitCountInt :: Int -> Int
bitCountInt = popCount
splitOff :: Euclidean a => a -> a -> (Word, a)
splitOff _ 0 = (0, 0)
splitOff p n = go 0 n
where
go !k m = case m `quotRem` p of
(q, 0) -> go (k + 1) q
_ -> (k, m)
{-# INLINABLE splitOff #-}
splitOff# :: Word# -> Word# -> (# Word#, Word# #)
splitOff# _ 0## = (# 0##, 0## #)
splitOff# p n = go 0## n
where
go k m = case m `quotRemWord#` p of
(# q, 0## #) -> go (k `plusWord#` 1##) q
_ -> (# k, m #)
{-# INLINABLE splitOff# #-}
mergeBy :: (a -> a -> Ordering) -> [a] -> [a] -> [a]
mergeBy cmp = loop
where
loop [] ys = ys
loop xs [] = xs
loop (x:xs) (y:ys)
= case cmp x y of
GT -> y : loop (x:xs) ys
_ -> x : loop xs (y:ys)
recipMod :: Integer -> Integer -> Maybe Integer
recipMod x m = case recipModInteger (x `mod` m) m of
0 -> Nothing
y -> Just y
bigNatToNatural :: BigNat -> Natural
bigNatToNatural bn
| isTrue# (sizeofBigNat# bn ==# 1#) = NatS# (bigNatToWord bn)
| otherwise = NatJ# bn
toWheel30 :: (Integral a, Bits a) => a -> a
toWheel30 i = q `shiftL` 3 + (r + r `shiftR` 4) `shiftR` 2
where
(q, r) = i `P.quotRem` 30
fromWheel30 :: (Num a, Bits a) => a -> a
fromWheel30 i = ((i `shiftL` 2 - i `shiftR` 2) .|. 1)
+ ((i `shiftL` 1 - i `shiftR` 1) .&. 2)