{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE MagicHash #-}
module Math.NumberTheory.Roots.Cubes
( integerCubeRoot
, integerCubeRoot'
, exactCubeRoot
, isCube
, isCube'
, isPossibleCube
) where
import Data.Bits (finiteBitSize, (.&.))
import GHC.Exts (Int#, Ptr(..), quotInt#, int2Double#, double2Int#, isTrue#, (/##), (**##), (<#), (*#), (-#))
import GHC.Integer.GMP.Internals (Integer(..), shiftLInteger, shiftRInteger, sizeofBigNat#)
import GHC.Integer.Logarithms (integerLog2#)
import Numeric.Natural (Natural)
import Math.NumberTheory.Utils.BitMask (indexBitSet)
{-# SPECIALISE integerCubeRoot :: Int -> Int,
Word -> Word,
Integer -> Integer,
Natural -> Natural
#-}
integerCubeRoot :: Integral a => a -> a
integerCubeRoot 0 = 0
integerCubeRoot n
| n > 0 = integerCubeRoot' n
| otherwise =
let m = negate n
r = if m < 0
then negate . fromInteger $ integerCubeRoot' (negate $ fromIntegral n)
else negate (integerCubeRoot' m)
in if r*r*r == n then r else (r-1)
{-# RULES
"integerCubeRoot'/Int" integerCubeRoot' = cubeRootInt'
"integerCubeRoot'/Word" integerCubeRoot' = cubeRootWord
"integerCubeRoot'/Integer" integerCubeRoot' = cubeRootIgr
#-}
{-# INLINE [1] integerCubeRoot' #-}
integerCubeRoot' :: Integral a => a -> a
integerCubeRoot' 0 = 0
integerCubeRoot' n = newton3 n (approxCuRt n)
{-# SPECIALISE exactCubeRoot :: Int -> Maybe Int,
Word -> Maybe Word,
Integer -> Maybe Integer,
Natural -> Maybe Natural
#-}
exactCubeRoot :: Integral a => a -> Maybe a
exactCubeRoot 0 = Just 0
exactCubeRoot n
| n < 0 =
if m < 0
then fmap (negate . fromInteger) $ exactCubeRoot (negate $ fromIntegral n)
else fmap negate (exactCubeRoot m)
| isPossibleCube n && r*r*r == n = Just r
| otherwise = Nothing
where
m = negate n
r = integerCubeRoot' n
{-# SPECIALISE isCube :: Int -> Bool,
Word -> Bool,
Integer -> Bool,
Natural -> Bool
#-}
isCube :: Integral a => a -> Bool
isCube 0 = True
isCube n
| n > 0 = isCube' n
| m > 0 = isCube' m
| otherwise = isCube' (negate (fromIntegral n) :: Integer)
where
m = negate n
{-# SPECIALISE isCube' :: Int -> Bool,
Word -> Bool,
Integer -> Bool,
Natural -> Bool
#-}
isCube' :: Integral a => a -> Bool
isCube' !n = isPossibleCube n
&& (r*r*r == n)
where
r = integerCubeRoot' n
{-# SPECIALISE isPossibleCube :: Int -> Bool,
Word -> Bool,
Integer -> Bool,
Natural -> Bool
#-}
isPossibleCube :: Integral a => a -> Bool
isPossibleCube n'
= indexBitSet mask512 (fromInteger (n .&. 511))
&& indexBitSet mask837 (fromInteger (n `rem` 837))
&& indexBitSet mask637 (fromInteger (n `rem` 637))
&& indexBitSet mask703 (fromInteger (n `rem` 703))
where
n = toInteger n'
cubeRootInt' :: Int -> Int
cubeRootInt' 0 = 0
cubeRootInt' n
| n < c || c < 0 = r-1
| 0 < d && d < n = r+1
| otherwise = r
where
x = fromIntegral n :: Double
r = truncate (x ** (1/3))
c = r*r*r
d = c+3*r*(r+1)
cubeRootWordLimit :: Word
cubeRootWordLimit = if finiteBitSize (0 :: Word) == 64 then 2642245 else 1625
cubeRootWord :: Word -> Word
cubeRootWord 0 = 0
cubeRootWord w
| r > cubeRootWordLimit = cubeRootWordLimit
| w < c = r-1
| c < w && e < w && c < e = r+1
| otherwise = r
where
r = truncate ((fromIntegral w) ** (1/3) :: Double)
c = r*r*r
d = 3*r*(r+1)
e = c+d
cubeRootIgr :: Integer -> Integer
cubeRootIgr 0 = 0
cubeRootIgr n = newton3 n (approxCuRt n)
{-# SPECIALISE newton3 :: Integer -> Integer -> Integer #-}
newton3 :: Integral a => a -> a -> a
newton3 n a = go (step a)
where
step k = (2*k + n `quot` (k*k)) `quot` 3
go k
| m < k = go m
| otherwise = k
where
m = step k
{-# SPECIALISE approxCuRt :: Integer -> Integer #-}
approxCuRt :: Integral a => a -> a
approxCuRt 0 = 0
approxCuRt n = fromInteger $ appCuRt (fromIntegral n)
appCuRt :: Integer -> Integer
appCuRt (S# i#) = case double2Int# (int2Double# i# **## (1.0## /## 3.0##)) of
r# -> S# r#
appCuRt n@(Jp# bn#)
| isTrue# ((sizeofBigNat# bn#) <# thresh#) =
floor (fromInteger n ** (1.0/3.0) :: Double)
| otherwise = case integerLog2# n of
l# -> case (l# `quotInt#` 3#) -# 51# of
h# -> case shiftRInteger n (3# *# h#) of
m -> case floor (fromInteger m ** (1.0/3.0) :: Double) of
r -> shiftLInteger r h#
where
thresh# :: Int#
thresh# = if finiteBitSize (0 :: Word) == 64 then 5# else 9#
appCuRt _ = error "integerCubeRoot': negative argument"
mask512 :: Ptr Word
mask512 = Ptr "\171\171\170\171\170\171\170\171\171\171\170\171\170\171\170\171\170\171\170\171\170\171\170\171\171\171\170\171\170\171\170\171\170\171\170\171\170\171\170\171\171\171\170\171\170\171\170\171\170\171\170\171\170\171\170\171\171\171\170\171\170\171\170\171"#
mask837 :: Ptr Word
mask837 = Ptr "\ETX\SOH\NUL\b\b@@@\SOH\NUL\NUL\n\NUL0\DLE \NUL\NUL\NUL\EOT\b\EOT\NULP\NUL\NUL\128\ETX\NUL\STX\DLE\NUL\NUL\128@\129\NUL\NUL\NUL\EOT \NUL\160\NUL\NUL\NUL\ENQ\NUL\DLE\b0\NUL\NUL\NUL\ETX\EOT\STX\NUL(\NUL\NUL@\SOH\NUL\SOH\b\NUL\NUL@\160@\NUL\NUL\NUL\STX\DLE\NULp\NUL\NUL\128\STX\NUL\b\EOT\b\NUL\NUL\NUL\SOH\STX\ETX\NUL\DC4\NUL\NUL\160\128\128\NUL\EOT\EOT\NUL \DLE"#
mask637 :: Ptr Word
mask637 = Ptr "\ETX!\NUL\b\EOT\NUL\NUL\STX\SOH@\b\DC4\b\SOH@ \NUL\NUL\DLE\b\NULB\160@\CAN\NUL\STX\SOH\NUL\128@\NUL\DLE\STX\ENQB@\DLE\b\NUL\NUL\EOT\130\128\DLE(\DLE\STX\128@\NUL\NUL \DLE\NUL\134@\129\DLE\NUL\EOT\STX\NUL\NUL\129\NUL \EOT\n\132\NUL \DLE\NUL\NUL\b\EOT\NUL!\DLE"#
mask703 :: Ptr Word
mask703 = Ptr "\ETX\t\NUL\140` \NUL\NUL\DC1\b\DLE\SOH\128\NUL\NUL&@\DLE\NUL\128\NUL\b\b\128\NUL\NUL\SOH!\DLE\DLE\NUL\SOH\f\n\STX`\NUL\ETX\SOH\NUL\f@\160\NUL\NUL\ENQ\STX0\NUL\128\192\NUL\ACK@P0\128\NUL\b\b\132\128\NUL\NUL\SOH\DLE\DLE\NUL\SOH\NUL\b\STXd\NUL\NUL\SOH\128\b\DLE\136\NUL\NUL\EOT\ACK1\NUL\144@"#