{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples #-}
{-# OPTIONS_HADDOCK hide #-}
module Data.Unicode.Internal.Division
( quotRem21
, quotRem28
) where
import Data.Bits (Bits(..), FiniteBits(..))
import GHC.Exts (Word(..), timesWord2#)
highMul :: Word -> Word -> Word
highMul :: Word -> Word -> Word
highMul (W# x# :: Word#
x#) (W# y# :: Word#
y#) = Word# -> Word
W# Word#
high#
where
!(# high# :: Word#
high#, _ #) = Word# -> Word# -> (# Word#, Word# #)
timesWord2# Word#
x# Word#
y#
{-# INLINE quotRem21 #-}
quotRem21 :: Int -> (Int, Int)
quotRem21 :: Int -> (Int, Int)
quotRem21 n :: Int
n
| Word -> Int
forall b. FiniteBits b => b -> Int
finiteBitSize (0 :: Word) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= 64
= Int
n Int -> Int -> (Int, Int)
forall a. Integral a => a -> a -> (a, a)
`quotRem` 21
| Bool
otherwise
= (Word -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
q, Word -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word
w Word -> Word -> Word
forall a. Num a => a -> a -> a
- 21 Word -> Word -> Word
forall a. Num a => a -> a -> a
* Word
q))
where
w :: Word
w = Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n
high :: Word
high = Word -> Word -> Word
highMul Word
w 14054662151397753613
q :: Word
q = Word
high Word -> Int -> Word
forall a. Bits a => a -> Int -> a
`shiftR` 4
{-# INLINE quotRem28 #-}
quotRem28 :: Int -> (Int, Int)
quotRem28 :: Int -> (Int, Int)
quotRem28 n :: Int
n
| Word -> Int
forall b. FiniteBits b => b -> Int
finiteBitSize (0 :: Word) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= 64
= Int
n Int -> Int -> (Int, Int)
forall a. Integral a => a -> a -> (a, a)
`quotRem` 28
| Bool
otherwise
= (Word -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
q, Word -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
r)
where
w :: Word
w = Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n
high :: Word
high = Word -> Word -> Word
highMul Word
w 5270498306774157605
q :: Word
q = Word
high Word -> Int -> Word
forall a. Bits a => a -> Int -> a
`shiftR` 3
prod :: Word
prod = (Word
q Word -> Int -> Word
forall a. Bits a => a -> Int -> a
`shiftL` 3 Word -> Word -> Word
forall a. Num a => a -> a -> a
- Word
q) Word -> Int -> Word
forall a. Bits a => a -> Int -> a
`shiftL` 2
r :: Word
r = Word
w Word -> Word -> Word
forall a. Num a => a -> a -> a
- Word
prod