{-# LANGUAGE ScopedTypeVariables, ExplicitForAll #-}
{-# LANGUAGE BangPatterns, MagicHash, UnboxedTuples #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE RecordWildCards #-}
module Data.ByteString.Builder.RealFloat.Internal
( mask
, NonNumbersAndZero(..)
, toCharsNonNumbersAndZero
, decimalLength9
, decimalLength17
, Mantissa
, pow5bits
, log10pow2
, log10pow5
, pow5_factor
, multipleOfPowerOf5
, multipleOfPowerOf2
, acceptBounds
, BoundsState(..)
, trimTrailing
, trimNoTrailing
, closestCorrectlyRounded
, toCharsScientific
, fquot10
, frem10
, fquot5
, frem5
, dquot10
, dquotRem10
, dquot5
, drem5
, dquot100
, timesWord2
, Addr(..)
, ByteArray(..)
, castDoubleToWord64
, castFloatToWord32
, getWord64At
, getWord128At
, boolToWord32
, boolToWord64
, int32ToInt
, intToInt32
, word32ToInt
, word64ToInt
, word32ToWord64
, word64ToWord32
, module Data.ByteString.Builder.RealFloat.TableGenerator
) where
import Control.Monad (foldM)
import Data.Bits (Bits(..), FiniteBits(..))
import Data.ByteString.Internal (c2w)
import Data.ByteString.Builder.Prim.Internal (BoundedPrim, boundedPrim)
import Data.ByteString.Builder.RealFloat.TableGenerator
import Data.Char (ord)
import GHC.Int (Int(..), Int32(..))
import GHC.Prim
import GHC.Ptr (Ptr(..), plusPtr)
import GHC.ST (ST(..), runST)
import GHC.Types (isTrue#)
import GHC.Word (Word8, Word32(..), Word64(..))
import qualified Foreign.Storable as S (poke)
#include <ghcautoconf.h>
#include "MachDeps.h"
#if WORD_SIZE_IN_BITS < 64 && !MIN_VERSION_ghc_prim(0,8,0)
import GHC.IntWord64
#endif
#if __GLASGOW_HASKELL__ >= 804
import GHC.Float (castFloatToWord32, castDoubleToWord64)
#else
import System.IO.Unsafe (unsafePerformIO)
import Foreign.Marshal.Utils (with)
import Foreign.Ptr (castPtr)
import Foreign.Storable (peek)
{-# NOINLINE castFloatToWord32 #-}
castFloatToWord32 :: Float -> Word32
castFloatToWord32 x = unsafePerformIO (with x (peek . castPtr))
{-# NOINLINE castDoubleToWord64 #-}
castDoubleToWord64 :: Double -> Word64
castDoubleToWord64 x = unsafePerformIO (with x (peek . castPtr))
#endif
{-# INLINABLE mask #-}
mask :: (Bits a, Integral a) => Int -> a
mask :: forall a. (Bits a, Integral a) => Int -> a
mask = forall a b c. (a -> b -> c) -> b -> a -> c
flip (-) a
1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Bits a => a -> Int -> a
unsafeShiftL a
1
{-# INLINABLE boolToWord32 #-}
boolToWord32 :: Bool -> Word32
boolToWord32 :: Bool -> Word32
boolToWord32 = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Enum a => a -> Int
fromEnum
{-# INLINABLE boolToWord64 #-}
boolToWord64 :: Bool -> Word64
boolToWord64 :: Bool -> Word64
boolToWord64 = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Enum a => a -> Int
fromEnum
{-# INLINABLE int32ToInt #-}
int32ToInt :: Int32 -> Int
int32ToInt :: Int32 -> Int
int32ToInt = forall a b. (Integral a, Num b) => a -> b
fromIntegral
{-# INLINABLE intToInt32 #-}
intToInt32 :: Int -> Int32
intToInt32 :: Int -> Int32
intToInt32 = forall a b. (Integral a, Num b) => a -> b
fromIntegral
{-# INLINABLE word32ToInt #-}
word32ToInt :: Word32 -> Int
word32ToInt :: Word32 -> Int
word32ToInt = forall a b. (Integral a, Num b) => a -> b
fromIntegral
{-# INLINABLE word64ToInt #-}
word64ToInt :: Word64 -> Int
word64ToInt :: Word64 -> Int
word64ToInt = forall a b. (Integral a, Num b) => a -> b
fromIntegral
{-# INLINABLE word32ToWord64 #-}
word32ToWord64 :: Word32 -> Word64
word32ToWord64 :: Word32 -> Word64
word32ToWord64 = forall a b. (Integral a, Num b) => a -> b
fromIntegral
{-# INLINABLE word64ToWord32 #-}
word64ToWord32 :: Word64 -> Word32
word64ToWord32 :: Word64 -> Word32
word64ToWord32 = forall a b. (Integral a, Num b) => a -> b
fromIntegral
decimalLength9 :: Word32 -> Int
decimalLength9 :: Word32 -> Int
decimalLength9 Word32
v
| Word32
v forall a. Ord a => a -> a -> Bool
>= Word32
100000000 = Int
9
| Word32
v forall a. Ord a => a -> a -> Bool
>= Word32
10000000 = Int
8
| Word32
v forall a. Ord a => a -> a -> Bool
>= Word32
1000000 = Int
7
| Word32
v forall a. Ord a => a -> a -> Bool
>= Word32
100000 = Int
6
| Word32
v forall a. Ord a => a -> a -> Bool
>= Word32
10000 = Int
5
| Word32
v forall a. Ord a => a -> a -> Bool
>= Word32
1000 = Int
4
| Word32
v forall a. Ord a => a -> a -> Bool
>= Word32
100 = Int
3
| Word32
v forall a. Ord a => a -> a -> Bool
>= Word32
10 = Int
2
| Bool
otherwise = Int
1
decimalLength17 :: Word64 -> Int
decimalLength17 :: Word64 -> Int
decimalLength17 Word64
v
| Word64
v forall a. Ord a => a -> a -> Bool
>= Word64
10000000000000000 = Int
17
| Word64
v forall a. Ord a => a -> a -> Bool
>= Word64
1000000000000000 = Int
16
| Word64
v forall a. Ord a => a -> a -> Bool
>= Word64
100000000000000 = Int
15
| Word64
v forall a. Ord a => a -> a -> Bool
>= Word64
10000000000000 = Int
14
| Word64
v forall a. Ord a => a -> a -> Bool
>= Word64
1000000000000 = Int
13
| Word64
v forall a. Ord a => a -> a -> Bool
>= Word64
100000000000 = Int
12
| Word64
v forall a. Ord a => a -> a -> Bool
>= Word64
10000000000 = Int
11
| Word64
v forall a. Ord a => a -> a -> Bool
>= Word64
1000000000 = Int
10
| Word64
v forall a. Ord a => a -> a -> Bool
>= Word64
100000000 = Int
9
| Word64
v forall a. Ord a => a -> a -> Bool
>= Word64
10000000 = Int
8
| Word64
v forall a. Ord a => a -> a -> Bool
>= Word64
1000000 = Int
7
| Word64
v forall a. Ord a => a -> a -> Bool
>= Word64
100000 = Int
6
| Word64
v forall a. Ord a => a -> a -> Bool
>= Word64
10000 = Int
5
| Word64
v forall a. Ord a => a -> a -> Bool
>= Word64
1000 = Int
4
| Word64
v forall a. Ord a => a -> a -> Bool
>= Word64
100 = Int
3
| Word64
v forall a. Ord a => a -> a -> Bool
>= Word64
10 = Int
2
| Bool
otherwise = Int
1
maxEncodedLength :: Int
maxEncodedLength :: Int
maxEncodedLength = Int
32
pokeAll :: String -> Ptr Word8 -> IO (Ptr Word8)
pokeAll :: String -> Ptr Word8 -> IO (Ptr Word8)
pokeAll String
s Ptr Word8
ptr = forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM forall {b}. Ptr Word8 -> Char -> IO (Ptr b)
pokeOne Ptr Word8
ptr String
s
where pokeOne :: Ptr Word8 -> Char -> IO (Ptr b)
pokeOne Ptr Word8
p Char
c = forall a. Storable a => Ptr a -> a -> IO ()
S.poke Ptr Word8
p (Char -> Word8
c2w Char
c) forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) a. Monad m => a -> m a
return (Ptr Word8
p forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
1)
boundString :: String -> BoundedPrim ()
boundString :: String -> BoundedPrim ()
boundString String
s = forall a.
Int -> (a -> Ptr Word8 -> IO (Ptr Word8)) -> BoundedPrim a
boundedPrim Int
maxEncodedLength forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> a
const (String -> Ptr Word8 -> IO (Ptr Word8)
pokeAll String
s)
data NonNumbersAndZero = NonNumbersAndZero
{ NonNumbersAndZero -> Bool
negative :: Bool
, NonNumbersAndZero -> Bool
exponent_all_one :: Bool
, NonNumbersAndZero -> Bool
mantissa_non_zero :: Bool
}
toCharsNonNumbersAndZero :: NonNumbersAndZero -> BoundedPrim ()
toCharsNonNumbersAndZero :: NonNumbersAndZero -> BoundedPrim ()
toCharsNonNumbersAndZero NonNumbersAndZero{Bool
mantissa_non_zero :: Bool
exponent_all_one :: Bool
negative :: Bool
mantissa_non_zero :: NonNumbersAndZero -> Bool
exponent_all_one :: NonNumbersAndZero -> Bool
negative :: NonNumbersAndZero -> Bool
..}
| Bool
mantissa_non_zero = String -> BoundedPrim ()
boundString String
"NaN"
| Bool
exponent_all_one = String -> BoundedPrim ()
boundString forall a b. (a -> b) -> a -> b
$ String
signStr forall a. [a] -> [a] -> [a]
++ String
"Infinity"
| Bool
otherwise = String -> BoundedPrim ()
boundString forall a b. (a -> b) -> a -> b
$ String
signStr forall a. [a] -> [a] -> [a]
++ String
"0.0e0"
where signStr :: String
signStr = if Bool
negative then String
"-" else String
""
acceptBounds :: Mantissa a => a -> Bool
acceptBounds :: forall a. Mantissa a => a -> Bool
acceptBounds a
_ = Bool
False
pow5bitsUnboxed :: Int# -> Int#
pow5bitsUnboxed :: Int# -> Int#
pow5bitsUnboxed Int#
e = (Int#
e Int# -> Int# -> Int#
*# Int#
1217359#) Int# -> Int# -> Int#
`uncheckedIShiftRL#` Int#
19# Int# -> Int# -> Int#
+# Int#
1#
log10pow2Unboxed :: Int# -> Int#
log10pow2Unboxed :: Int# -> Int#
log10pow2Unboxed Int#
e = (Int#
e Int# -> Int# -> Int#
*# Int#
78913#) Int# -> Int# -> Int#
`uncheckedIShiftRL#` Int#
18#
log10pow5Unboxed :: Int# -> Int#
log10pow5Unboxed :: Int# -> Int#
log10pow5Unboxed Int#
e = (Int#
e Int# -> Int# -> Int#
*# Int#
732923#) Int# -> Int# -> Int#
`uncheckedIShiftRL#` Int#
20#
pow5bits, log10pow2, log10pow5 :: Int -> Int
pow5bits :: Int -> Int
pow5bits = (Int# -> Int#) -> Int -> Int
wrapped Int# -> Int#
pow5bitsUnboxed
log10pow2 :: Int -> Int
log10pow2 = (Int# -> Int#) -> Int -> Int
wrapped Int# -> Int#
log10pow2Unboxed
log10pow5 :: Int -> Int
log10pow5 = (Int# -> Int#) -> Int -> Int
wrapped Int# -> Int#
log10pow5Unboxed
fquot10 :: Word32 -> Word32
fquot10 :: Word32 -> Word32
fquot10 Word32
w = Word64 -> Word32
word64ToWord32 ((Word32 -> Word64
word32ToWord64 Word32
w forall a. Num a => a -> a -> a
* Word64
0xCCCCCCCD) forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
35)
frem10 :: Word32 -> Word32
frem10 :: Word32 -> Word32
frem10 Word32
w = Word32
w forall a. Num a => a -> a -> a
- Word32 -> Word32
fquot10 Word32
w forall a. Num a => a -> a -> a
* Word32
10
fquotRem10 :: Word32 -> (Word32, Word32)
fquotRem10 :: Word32 -> (Word32, Word32)
fquotRem10 Word32
w =
let w' :: Word32
w' = Word32 -> Word32
fquot10 Word32
w
in (Word32
w', Word32
w forall a. Num a => a -> a -> a
- Word32 -> Word32
fquot10 Word32
w forall a. Num a => a -> a -> a
* Word32
10)
fquot100 :: Word32 -> Word32
fquot100 :: Word32 -> Word32
fquot100 Word32
w = Word64 -> Word32
word64ToWord32 ((Word32 -> Word64
word32ToWord64 Word32
w forall a. Num a => a -> a -> a
* Word64
0x51EB851F) forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
37)
fquotRem10000 :: Word32 -> (Word32, Word32)
fquotRem10000 :: Word32 -> (Word32, Word32)
fquotRem10000 Word32
w =
let w' :: Word32
w' = Word64 -> Word32
word64ToWord32 ((Word32 -> Word64
word32ToWord64 Word32
w forall a. Num a => a -> a -> a
* Word64
0xD1B71759) forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
45)
in (Word32
w', Word32
w forall a. Num a => a -> a -> a
- Word32
w' forall a. Num a => a -> a -> a
* Word32
10000)
fquot5 :: Word32 -> Word32
fquot5 :: Word32 -> Word32
fquot5 Word32
w = Word64 -> Word32
word64ToWord32 ((Word32 -> Word64
word32ToWord64 Word32
w forall a. Num a => a -> a -> a
* Word64
0xCCCCCCCD) forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
34)
frem5 :: Word32 -> Word32
frem5 :: Word32 -> Word32
frem5 Word32
w = Word32
w forall a. Num a => a -> a -> a
- Word32 -> Word32
fquot5 Word32
w forall a. Num a => a -> a -> a
* Word32
5
dquot10 :: Word64 -> Word64
dquot10 :: Word64 -> Word64
dquot10 Word64
w =
let !(Word64
rdx, Word64
_) = Word64
w Word64 -> Word64 -> (Word64, Word64)
`timesWord2` Word64
0xCCCCCCCCCCCCCCCD
in Word64
rdx forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
3
dquot100 :: Word64 -> Word64
dquot100 :: Word64 -> Word64
dquot100 Word64
w =
let !(Word64
rdx, Word64
_) = (Word64
w forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
2) Word64 -> Word64 -> (Word64, Word64)
`timesWord2` Word64
0x28F5C28F5C28F5C3
in Word64
rdx forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
2
dquotRem10000 :: Word64 -> (Word64, Word64)
dquotRem10000 :: Word64 -> (Word64, Word64)
dquotRem10000 Word64
w =
let !(Word64
rdx, Word64
_) = Word64
w Word64 -> Word64 -> (Word64, Word64)
`timesWord2` Word64
0x346DC5D63886594B
w' :: Word64
w' = Word64
rdx forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
11
in (Word64
w', Word64
w forall a. Num a => a -> a -> a
- Word64
w' forall a. Num a => a -> a -> a
* Word64
10000)
dquotRem10 :: Word64 -> (Word64, Word64)
dquotRem10 :: Word64 -> (Word64, Word64)
dquotRem10 Word64
w =
let w' :: Word64
w' = Word64 -> Word64
dquot10 Word64
w
in (Word64
w', Word64
w forall a. Num a => a -> a -> a
- Word64
w' forall a. Num a => a -> a -> a
* Word64
10)
dquot5 :: Word64 -> Word64
dquot5 :: Word64 -> Word64
dquot5 Word64
w =
let !(Word64
rdx, Word64
_) = Word64
w Word64 -> Word64 -> (Word64, Word64)
`timesWord2` Word64
0xCCCCCCCCCCCCCCCD
in Word64
rdx forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
2
drem5 :: Word64 -> Word64
drem5 :: Word64 -> Word64
drem5 Word64
w = Word64
w forall a. Num a => a -> a -> a
- Word64 -> Word64
dquot5 Word64
w forall a. Num a => a -> a -> a
* Word64
5
dquotRem5 :: Word64 -> (Word64, Word64)
dquotRem5 :: Word64 -> (Word64, Word64)
dquotRem5 Word64
w =
let w' :: Word64
w' = Word64 -> Word64
dquot5 Word64
w
in (Word64
w', Word64
w forall a. Num a => a -> a -> a
- Word64
w' forall a. Num a => a -> a -> a
* Word64
5)
wrapped :: (Int# -> Int#) -> Int -> Int
wrapped :: (Int# -> Int#) -> Int -> Int
wrapped Int# -> Int#
f (I# Int#
w) = Int# -> Int
I# (Int# -> Int#
f Int#
w)
#if WORD_SIZE_IN_BITS == 32
packWord64 :: Word# -> Word# -> Word64#
packWord64 hi lo =
#if defined(WORDS_BIGENDIAN)
((wordToWord64# lo) `uncheckedShiftL64#` 32#) `or64#` (wordToWord64# hi)
#else
((wordToWord64# hi) `uncheckedShiftL64#` 32#) `or64#` (wordToWord64# lo)
#endif
unpackWord64 :: Word64# -> (# Word#, Word# #)
unpackWord64 w =
#if defined(WORDS_BIGENDIAN)
(# word64ToWord# w
, word64ToWord# (w `uncheckedShiftRL64#` 32#)
#)
#else
(# word64ToWord# (w `uncheckedShiftRL64#` 32#)
, word64ToWord# w
#)
#endif
plusWord64 :: Word64# -> Word64# -> Word64#
plusWord64 x y =
let !(# x_h, x_l #) = unpackWord64 x
!(# y_h, y_l #) = unpackWord64 y
lo = x_l `plusWord#` y_l
carry = int2Word# (lo `ltWord#` x_l)
hi = x_h `plusWord#` y_h `plusWord#` carry
in packWord64 hi lo
#endif
timesWord2 :: Word64 -> Word64 -> (Word64, Word64)
timesWord2 :: Word64 -> Word64 -> (Word64, Word64)
timesWord2 Word64
a Word64
b =
let ra :: WORD64
ra = forall a. Mantissa a => a -> WORD64
raw Word64
a
rb :: WORD64
rb = forall a. Mantissa a => a -> WORD64
raw Word64
b
#if WORD_SIZE_IN_BITS >= 64
#if __GLASGOW_HASKELL__ < 903
!(# WORD64
hi, WORD64
lo #) = WORD64
ra WORD64 -> WORD64 -> (# WORD64, WORD64 #)
`timesWord2#` WORD64
rb
#else
!(# hi_, lo_ #) = word64ToWord# ra `timesWord2#` word64ToWord# rb
hi = wordToWord64# hi_
lo = wordToWord64# lo_
#endif
#else
!(# x_h, x_l #) = unpackWord64 ra
!(# y_h, y_l #) = unpackWord64 rb
!(# phh_h, phh_l #) = x_h `timesWord2#` y_h
!(# phl_h, phl_l #) = x_h `timesWord2#` y_l
!(# plh_h, plh_l #) = x_l `timesWord2#` y_h
!(# pll_h, pll_l #) = x_l `timesWord2#` y_l
phh = packWord64 phh_h phh_l
phl = packWord64 phl_h phl_l
!(# mh, ml #) = unpackWord64 (phl
`plusWord64` (wordToWord64# pll_h)
`plusWord64` (wordToWord64# plh_l))
hi = phh
`plusWord64` (wordToWord64# mh)
`plusWord64` (wordToWord64# plh_h)
lo = packWord64 ml pll_l
#endif
in (WORD64 -> Word64
W64# WORD64
hi, WORD64 -> Word64
W64# WORD64
lo)
type WORD64 =
#if WORD_SIZE_IN_BITS < 64 || __GLASGOW_HASKELL__ >= 903
Word64#
#else
Word#
#endif
pow5_factor :: WORD64 -> Int# -> Int#
pow5_factor :: WORD64 -> Int# -> Int#
pow5_factor WORD64
w Int#
count =
let !(W64# WORD64
q, W64# WORD64
r) = Word64 -> (Word64, Word64)
dquotRem5 (WORD64 -> Word64
W64# WORD64
w)
#if WORD_SIZE_IN_BITS >= 64 && __GLASGOW_HASKELL__ < 903
in case WORD64
r WORD64 -> WORD64 -> Int#
`eqWord#` WORD64
0## of
#else
in case r `eqWord64#` wordToWord64# 0## of
#endif
Int#
0# -> Int#
count
Int#
_ -> WORD64 -> Int# -> Int#
pow5_factor WORD64
q (Int#
count Int# -> Int# -> Int#
+# Int#
1#)
multipleOfPowerOf5 :: Mantissa a => a -> Int -> Bool
multipleOfPowerOf5 :: forall a. Mantissa a => a -> Int -> Bool
multipleOfPowerOf5 a
value (I# Int#
p) = Int# -> Bool
isTrue# (WORD64 -> Int# -> Int#
pow5_factor (forall a. Mantissa a => a -> WORD64
raw a
value) Int#
0# Int# -> Int# -> Int#
>=# Int#
p)
multipleOfPowerOf2 :: Mantissa a => a -> Int -> Bool
multipleOfPowerOf2 :: forall a. Mantissa a => a -> Int -> Bool
multipleOfPowerOf2 a
value Int
p = (a
value forall a. Bits a => a -> a -> a
.&. forall a. (Bits a, Integral a) => Int -> a
mask Int
p) forall a. Eq a => a -> a -> Bool
== a
0
class (FiniteBits a, Integral a) => Mantissa a where
unsafeRaw :: a -> Word#
raw :: a -> WORD64
decimalLength :: a -> Int
boolToWord :: Bool -> a
quotRem10 :: a -> (a, a)
quot10 :: a -> a
quot100 :: a -> a
quotRem100 :: a -> (a, a)
quotRem10000 :: a -> (a, a)
instance Mantissa Word32 where
#if __GLASGOW_HASKELL__ >= 902
unsafeRaw :: Word32 -> WORD64
unsafeRaw (W32# Word32#
w) = Word32# -> WORD64
word32ToWord# Word32#
w
#else
unsafeRaw (W32# w) = w
#endif
#if WORD_SIZE_IN_BITS >= 64 && __GLASGOW_HASKELL__ < 903
raw :: Word32 -> WORD64
raw = forall a. Mantissa a => a -> WORD64
unsafeRaw
#else
raw w = wordToWord64# (unsafeRaw w)
#endif
decimalLength :: Word32 -> Int
decimalLength = Word32 -> Int
decimalLength9
boolToWord :: Bool -> Word32
boolToWord = Bool -> Word32
boolToWord32
{-# INLINE quotRem10 #-}
quotRem10 :: Word32 -> (Word32, Word32)
quotRem10 = Word32 -> (Word32, Word32)
fquotRem10
{-# INLINE quot10 #-}
quot10 :: Word32 -> Word32
quot10 = Word32 -> Word32
fquot10
{-# INLINE quot100 #-}
quot100 :: Word32 -> Word32
quot100 = Word32 -> Word32
fquot100
quotRem100 :: Word32 -> (Word32, Word32)
quotRem100 Word32
w =
let w' :: Word32
w' = Word32 -> Word32
fquot100 Word32
w
in (Word32
w', (Word32
w forall a. Num a => a -> a -> a
- Word32
w' forall a. Num a => a -> a -> a
* Word32
100))
quotRem10000 :: Word32 -> (Word32, Word32)
quotRem10000 = Word32 -> (Word32, Word32)
fquotRem10000
instance Mantissa Word64 where
#if WORD_SIZE_IN_BITS >= 64 && __GLASGOW_HASKELL__ < 903
unsafeRaw :: Word64 -> WORD64
unsafeRaw (W64# WORD64
w) = WORD64
w
#else
unsafeRaw (W64# w) = word64ToWord# w
#endif
raw :: Word64 -> WORD64
raw (W64# WORD64
w) = WORD64
w
decimalLength :: Word64 -> Int
decimalLength = Word64 -> Int
decimalLength17
boolToWord :: Bool -> Word64
boolToWord = Bool -> Word64
boolToWord64
{-# INLINE quotRem10 #-}
quotRem10 :: Word64 -> (Word64, Word64)
quotRem10 = Word64 -> (Word64, Word64)
dquotRem10
{-# INLINE quot10 #-}
quot10 :: Word64 -> Word64
quot10 = Word64 -> Word64
dquot10
{-# INLINE quot100 #-}
quot100 :: Word64 -> Word64
quot100 = Word64 -> Word64
dquot100
quotRem100 :: Word64 -> (Word64, Word64)
quotRem100 Word64
w =
let w' :: Word64
w' = Word64 -> Word64
dquot100 Word64
w
in (Word64
w', (Word64
w forall a. Num a => a -> a -> a
- Word64
w' forall a. Num a => a -> a -> a
* Word64
100))
quotRem10000 :: Word64 -> (Word64, Word64)
quotRem10000 = Word64 -> (Word64, Word64)
dquotRem10000
data BoundsState a = BoundsState
{ forall a. BoundsState a -> a
vu :: !a
, forall a. BoundsState a -> a
vv :: !a
, forall a. BoundsState a -> a
vw :: !a
, forall a. BoundsState a -> a
lastRemovedDigit :: !a
, forall a. BoundsState a -> Bool
vuIsTrailingZeros :: !Bool
, forall a. BoundsState a -> Bool
vvIsTrailingZeros :: !Bool
}
trimTrailing :: (Show a, Mantissa a) => BoundsState a -> (BoundsState a, Int32)
trimTrailing :: forall a.
(Show a, Mantissa a) =>
BoundsState a -> (BoundsState a, Int32)
trimTrailing !BoundsState a
initial = (BoundsState a
res, Int32
r forall a. Num a => a -> a -> a
+ Int32
r')
where
!(BoundsState a
d', Int32
r) = forall {a} {b}.
(Num b, Mantissa a) =>
BoundsState a -> (BoundsState a, b)
trimTrailing' BoundsState a
initial
!(BoundsState a
d'', Int32
r') = if forall a. BoundsState a -> Bool
vuIsTrailingZeros BoundsState a
d' then forall {a} {b}.
(Num b, Mantissa a) =>
BoundsState a -> (BoundsState a, b)
trimTrailing'' BoundsState a
d' else (BoundsState a
d', Int32
0)
res :: BoundsState a
res = if forall a. BoundsState a -> Bool
vvIsTrailingZeros BoundsState a
d'' Bool -> Bool -> Bool
&& forall a. BoundsState a -> a
lastRemovedDigit BoundsState a
d'' forall a. Eq a => a -> a -> Bool
== a
5 Bool -> Bool -> Bool
&& forall a. BoundsState a -> a
vv BoundsState a
d'' forall a. Integral a => a -> a -> a
`rem` a
2 forall a. Eq a => a -> a -> Bool
== a
0
then BoundsState a
d''
else BoundsState a
d''
trimTrailing' :: BoundsState a -> (BoundsState a, b)
trimTrailing' !BoundsState a
d
| a
vw' forall a. Ord a => a -> a -> Bool
> a
vu' =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. Num a => a -> a -> a
(+) b
1) forall b c a. (b -> c) -> (a -> b) -> a -> c
. BoundsState a -> (BoundsState a, b)
trimTrailing' forall a b. (a -> b) -> a -> b
$
BoundsState a
d { vu :: a
vu = a
vu'
, vv :: a
vv = a
vv'
, vw :: a
vw = a
vw'
, lastRemovedDigit :: a
lastRemovedDigit = a
vvRem
, vuIsTrailingZeros :: Bool
vuIsTrailingZeros = forall a. BoundsState a -> Bool
vuIsTrailingZeros BoundsState a
d Bool -> Bool -> Bool
&& a
vuRem forall a. Eq a => a -> a -> Bool
== a
0
, vvIsTrailingZeros :: Bool
vvIsTrailingZeros = forall a. BoundsState a -> Bool
vvIsTrailingZeros BoundsState a
d Bool -> Bool -> Bool
&& forall a. BoundsState a -> a
lastRemovedDigit BoundsState a
d forall a. Eq a => a -> a -> Bool
== a
0
}
| Bool
otherwise = (BoundsState a
d, b
0)
where
!(a
vv', a
vvRem) = forall a. Mantissa a => a -> (a, a)
quotRem10 forall a b. (a -> b) -> a -> b
$ forall a. BoundsState a -> a
vv BoundsState a
d
!(a
vu', a
vuRem) = forall a. Mantissa a => a -> (a, a)
quotRem10 forall a b. (a -> b) -> a -> b
$ forall a. BoundsState a -> a
vu BoundsState a
d
!(a
vw', a
_ ) = forall a. Mantissa a => a -> (a, a)
quotRem10 forall a b. (a -> b) -> a -> b
$ forall a. BoundsState a -> a
vw BoundsState a
d
trimTrailing'' :: BoundsState a -> (BoundsState a, b)
trimTrailing'' !BoundsState a
d
| a
vuRem forall a. Eq a => a -> a -> Bool
== a
0 =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. Num a => a -> a -> a
(+) b
1) forall b c a. (b -> c) -> (a -> b) -> a -> c
. BoundsState a -> (BoundsState a, b)
trimTrailing'' forall a b. (a -> b) -> a -> b
$
BoundsState a
d { vu :: a
vu = a
vu'
, vv :: a
vv = a
vv'
, vw :: a
vw = a
vw'
, lastRemovedDigit :: a
lastRemovedDigit = a
vvRem
, vvIsTrailingZeros :: Bool
vvIsTrailingZeros = forall a. BoundsState a -> Bool
vvIsTrailingZeros BoundsState a
d Bool -> Bool -> Bool
&& forall a. BoundsState a -> a
lastRemovedDigit BoundsState a
d forall a. Eq a => a -> a -> Bool
== a
0
}
| Bool
otherwise = (BoundsState a
d, b
0)
where
!(a
vu', a
vuRem) = forall a. Mantissa a => a -> (a, a)
quotRem10 forall a b. (a -> b) -> a -> b
$ forall a. BoundsState a -> a
vu BoundsState a
d
!(a
vv', a
vvRem) = forall a. Mantissa a => a -> (a, a)
quotRem10 forall a b. (a -> b) -> a -> b
$ forall a. BoundsState a -> a
vv BoundsState a
d
!(a
vw', a
_ ) = forall a. Mantissa a => a -> (a, a)
quotRem10 forall a b. (a -> b) -> a -> b
$ forall a. BoundsState a -> a
vw BoundsState a
d
trimNoTrailing :: Mantissa a => BoundsState a -> (BoundsState a, Int32)
trimNoTrailing :: forall a. Mantissa a => BoundsState a -> (BoundsState a, Int32)
trimNoTrailing !(BoundsState a
u a
v a
w a
ld Bool
_ Bool
_) =
(forall a. a -> a -> a -> a -> Bool -> Bool -> BoundsState a
BoundsState a
ru' a
rv' a
0 a
ld' Bool
False Bool
False, Int32
c)
where
!(a
ru', a
rv', a
ld', Int32
c) = forall {c} {d} {a}.
(Num d, Mantissa a, Mantissa c) =>
a -> c -> a -> c -> d -> (a, c, c, d)
trimNoTrailing' a
u a
v a
w a
ld Int32
0
trimNoTrailing' :: a -> c -> a -> c -> d -> (a, c, c, d)
trimNoTrailing' a
u' c
v' a
w' c
lastRemoved d
count
| a
vw' forall a. Ord a => a -> a -> Bool
> a
vu' =
a -> c -> a -> c -> d -> (a, c, c, d)
trimNoTrailing'' a
vu' c
vv' a
vw' (forall a. Mantissa a => a -> a
quot10 (c
v' forall a. Num a => a -> a -> a
- (c
vv' forall a. Num a => a -> a -> a
* c
100))) (d
count forall a. Num a => a -> a -> a
+ d
2)
| Bool
otherwise =
a -> c -> a -> c -> d -> (a, c, c, d)
trimNoTrailing'' a
u' c
v' a
w' c
lastRemoved d
count
where
!vw' :: a
vw' = forall a. Mantissa a => a -> a
quot100 a
w'
!vu' :: a
vu' = forall a. Mantissa a => a -> a
quot100 a
u'
!vv' :: c
vv' = forall a. Mantissa a => a -> a
quot100 c
v'
trimNoTrailing'' :: a -> c -> a -> c -> d -> (a, c, c, d)
trimNoTrailing'' a
u' c
v' a
w' c
lastRemoved d
count
| a
vw' forall a. Ord a => a -> a -> Bool
> a
vu' = a -> c -> a -> c -> d -> (a, c, c, d)
trimNoTrailing' a
vu' c
vv' a
vw' c
lastRemoved' (d
count forall a. Num a => a -> a -> a
+ d
1)
| Bool
otherwise = (a
u', c
v', c
lastRemoved, d
count)
where
!(c
vv', c
lastRemoved') = forall a. Mantissa a => a -> (a, a)
quotRem10 c
v'
!vu' :: a
vu' = forall a. Mantissa a => a -> a
quot10 a
u'
!vw' :: a
vw' = forall a. Mantissa a => a -> a
quot10 a
w'
{-# INLINE closestCorrectlyRounded #-}
closestCorrectlyRounded :: Mantissa a => Bool -> BoundsState a -> a
closestCorrectlyRounded :: forall a. Mantissa a => Bool -> BoundsState a -> a
closestCorrectlyRounded Bool
acceptBound BoundsState a
s = forall a. BoundsState a -> a
vv BoundsState a
s forall a. Num a => a -> a -> a
+ forall a. Mantissa a => Bool -> a
boolToWord Bool
roundUp
where
outsideBounds :: Bool
outsideBounds = Bool -> Bool
not (forall a. BoundsState a -> Bool
vuIsTrailingZeros BoundsState a
s) Bool -> Bool -> Bool
|| Bool -> Bool
not Bool
acceptBound
roundUp :: Bool
roundUp = (forall a. BoundsState a -> a
vv BoundsState a
s forall a. Eq a => a -> a -> Bool
== forall a. BoundsState a -> a
vu BoundsState a
s Bool -> Bool -> Bool
&& Bool
outsideBounds) Bool -> Bool -> Bool
|| forall a. BoundsState a -> a
lastRemovedDigit BoundsState a
s forall a. Ord a => a -> a -> Bool
>= a
5
asciiRaw :: Int -> Word#
asciiRaw :: Int -> WORD64
asciiRaw (I# Int#
i) = Int# -> WORD64
int2Word# Int#
i
asciiZero :: Int
asciiZero :: Int
asciiZero = Char -> Int
ord Char
'0'
asciiDot :: Int
asciiDot :: Int
asciiDot = Char -> Int
ord Char
'.'
asciiMinus :: Int
asciiMinus :: Int
asciiMinus = Char -> Int
ord Char
'-'
ascii_e :: Int
ascii_e :: Int
ascii_e = Char -> Int
ord Char
'e'
toAscii :: Word# -> Word#
toAscii :: WORD64 -> WORD64
toAscii WORD64
a = WORD64
a WORD64 -> WORD64 -> WORD64
`plusWord#` Int -> WORD64
asciiRaw Int
asciiZero
data Addr = Addr Addr#
{-# INLINE getWord64At #-}
getWord64At :: Addr# -> Int -> Word64
getWord64At :: Addr# -> Int -> Word64
getWord64At Addr#
arr (I# Int#
i) =
#if defined(WORDS_BIGENDIAN)
W64# (byteSwap64# (indexWord64OffAddr# arr i))
#else
WORD64 -> Word64
W64# (Addr# -> Int# -> WORD64
indexWord64OffAddr# Addr#
arr Int#
i)
#endif
{-# INLINE getWord128At #-}
getWord128At :: Addr# -> Int -> (Word64, Word64)
getWord128At :: Addr# -> Int -> (Word64, Word64)
getWord128At Addr#
arr (I# Int#
i) =
#if defined(WORDS_BIGENDIAN)
( W64# (byteSwap64# (indexWord64OffAddr# arr (i *# 2# +# 1#)))
, W64# (byteSwap64# (indexWord64OffAddr# arr (i *# 2#)))
)
#else
( WORD64 -> Word64
W64# (Addr# -> Int# -> WORD64
indexWord64OffAddr# Addr#
arr (Int#
i Int# -> Int# -> Int#
*# Int#
2# Int# -> Int# -> Int#
+# Int#
1#))
, WORD64 -> Word64
W64# (Addr# -> Int# -> WORD64
indexWord64OffAddr# Addr#
arr (Int#
i Int# -> Int# -> Int#
*# Int#
2#))
)
#endif
data ByteArray = ByteArray ByteArray#
packWord16 :: Word# -> Word# -> Word#
packWord16 :: WORD64 -> WORD64 -> WORD64
packWord16 WORD64
l WORD64
h =
#if defined(WORDS_BIGENDIAN)
(h `uncheckedShiftL#` 8#) `or#` l
#else
(WORD64
l WORD64 -> Int# -> WORD64
`uncheckedShiftL#` Int#
8#) WORD64 -> WORD64 -> WORD64
`or#` WORD64
h
#endif
unpackWord16 :: Word# -> (# Word#, Word# #)
unpackWord16 :: WORD64 -> (# WORD64, WORD64 #)
unpackWord16 WORD64
w =
#if defined(WORDS_BIGENDIAN)
(# w `and#` 0xff##, w `uncheckedShiftRL#` 8# #)
#else
(# WORD64
w WORD64 -> Int# -> WORD64
`uncheckedShiftRL#` Int#
8#, WORD64
w WORD64 -> WORD64 -> WORD64
`and#` WORD64
0xff## #)
#endif
digit_table :: ByteArray
digit_table :: ByteArray
digit_table = forall a. (forall s. ST s a) -> a
runST (forall s a. STRep s a -> ST s a
ST forall a b. (a -> b) -> a -> b
$ \State# s
s1 ->
let !(# State# s
s2, MutableByteArray# s
marr #) = forall d. Int# -> State# d -> (# State# d, MutableByteArray# d #)
newByteArray# Int#
200# State# s
s1
go :: Word32
-> (Int# -> State# s -> State# s) -> Int# -> State# s -> State# s
go Word32
y Int# -> State# s -> State# s
r = \Int#
i State# s
s ->
let !(Word32
h, Word32
l) = Word32 -> (Word32, Word32)
fquotRem10 Word32
y
e' :: WORD64
e' = WORD64 -> WORD64 -> WORD64
packWord16 (WORD64 -> WORD64
toAscii (forall a. Mantissa a => a -> WORD64
unsafeRaw Word32
l)) (WORD64 -> WORD64
toAscii (forall a. Mantissa a => a -> WORD64
unsafeRaw Word32
h))
#if __GLASGOW_HASKELL__ >= 902
s' :: State# s
s' = forall d.
MutableByteArray# d -> Int# -> Word16# -> State# d -> State# d
writeWord16Array# MutableByteArray# s
marr Int#
i (WORD64 -> Word16#
wordToWord16# WORD64
e') State# s
s
#else
s' = writeWord16Array# marr i e' s
#endif
in if Int# -> Bool
isTrue# (Int#
i Int# -> Int# -> Int#
==# Int#
99#) then State# s
s' else Int# -> State# s -> State# s
r (Int#
i Int# -> Int# -> Int#
+# Int#
1#) State# s
s'
!(# State# s
s3, ByteArray#
bs #) = forall d.
MutableByteArray# d -> State# d -> (# State# d, ByteArray# #)
unsafeFreezeByteArray# MutableByteArray# s
marr (forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Word32
-> (Int# -> State# s -> State# s) -> Int# -> State# s -> State# s
go (\Int#
_ State# s
s -> State# s
s) [Word32
0..Word32
99] Int#
0# State# s
s2)
in (# State# s
s3, ByteArray# -> ByteArray
ByteArray ByteArray#
bs #))
unsafeAt :: ByteArray -> Int# -> Word#
unsafeAt :: ByteArray -> Int# -> WORD64
unsafeAt (ByteArray ByteArray#
bs) Int#
i =
#if __GLASGOW_HASKELL__ >= 902
Word16# -> WORD64
word16ToWord# (ByteArray# -> Int# -> Word16#
indexWord16Array# ByteArray#
bs Int#
i)
#else
indexWord16Array# bs i
#endif
copyWord16 :: Word# -> Addr# -> State# d -> State# d
copyWord16 :: forall d. WORD64 -> Addr# -> State# d -> State# d
copyWord16 WORD64
w Addr#
a State# d
s =
#if __GLASGOW_HASKELL__ >= 902
forall d. Addr# -> Int# -> Word16# -> State# d -> State# d
writeWord16OffAddr# Addr#
a Int#
0# (WORD64 -> Word16#
wordToWord16# WORD64
w) State# d
s
#else
writeWord16OffAddr# a 0# w s
#endif
poke :: Addr# -> Word# -> State# d -> State# d
poke :: forall d. Addr# -> WORD64 -> State# d -> State# d
poke Addr#
a WORD64
w State# d
s =
#if __GLASGOW_HASKELL__ >= 902
forall d. Addr# -> Int# -> Word8# -> State# d -> State# d
writeWord8OffAddr# Addr#
a Int#
0# (WORD64 -> Word8#
wordToWord8# WORD64
w) State# d
s
#else
writeWord8OffAddr# a 0# w s
#endif
{-# SPECIALIZE writeMantissa :: Addr# -> Int# -> Word32 -> State# d -> (# Addr#, State# d #) #-}
{-# SPECIALIZE writeMantissa :: Addr# -> Int# -> Word64 -> State# d -> (# Addr#, State# d #) #-}
writeMantissa :: forall a d. (Mantissa a) => Addr# -> Int# -> a -> State# d -> (# Addr#, State# d #)
writeMantissa :: forall a d.
Mantissa a =>
Addr# -> Int# -> a -> State# d -> (# Addr#, State# d #)
writeMantissa Addr#
ptr Int#
olength = forall {a} {d}.
Mantissa a =>
Addr# -> a -> State# d -> (# Addr#, State# d #)
go (Addr#
ptr Addr# -> Int# -> Addr#
`plusAddr#` Int#
olength)
where
go :: Addr# -> a -> State# d -> (# Addr#, State# d #)
go Addr#
p a
mantissa State# d
s1
| a
mantissa forall a. Ord a => a -> a -> Bool
>= a
10000 =
let !(a
m', a
c) = forall a. Mantissa a => a -> (a, a)
quotRem10000 a
mantissa
!(a
c1, a
c0) = forall a. Mantissa a => a -> (a, a)
quotRem100 a
c
s2 :: State# d
s2 = forall d. WORD64 -> Addr# -> State# d -> State# d
copyWord16 (ByteArray
digit_table ByteArray -> Int# -> WORD64
`unsafeAt` WORD64 -> Int#
word2Int# (forall a. Mantissa a => a -> WORD64
unsafeRaw a
c0)) (Addr#
p Addr# -> Int# -> Addr#
`plusAddr#` (Int#
-1#)) State# d
s1
s3 :: State# d
s3 = forall d. WORD64 -> Addr# -> State# d -> State# d
copyWord16 (ByteArray
digit_table ByteArray -> Int# -> WORD64
`unsafeAt` WORD64 -> Int#
word2Int# (forall a. Mantissa a => a -> WORD64
unsafeRaw a
c1)) (Addr#
p Addr# -> Int# -> Addr#
`plusAddr#` (Int#
-3#)) State# d
s2
in Addr# -> a -> State# d -> (# Addr#, State# d #)
go (Addr#
p Addr# -> Int# -> Addr#
`plusAddr#` (Int#
-4#)) a
m' State# d
s3
| a
mantissa forall a. Ord a => a -> a -> Bool
>= a
100 =
let !(a
m', a
c) = forall a. Mantissa a => a -> (a, a)
quotRem100 a
mantissa
s2 :: State# d
s2 = forall d. WORD64 -> Addr# -> State# d -> State# d
copyWord16 (ByteArray
digit_table ByteArray -> Int# -> WORD64
`unsafeAt` WORD64 -> Int#
word2Int# (forall a. Mantissa a => a -> WORD64
unsafeRaw a
c)) (Addr#
p Addr# -> Int# -> Addr#
`plusAddr#` (Int#
-1#)) State# d
s1
in forall {a} {d}.
Mantissa a =>
a -> State# d -> (# Addr#, State# d #)
finalize a
m' State# d
s2
| Bool
otherwise = forall {a} {d}.
Mantissa a =>
a -> State# d -> (# Addr#, State# d #)
finalize a
mantissa State# d
s1
finalize :: a -> State# d -> (# Addr#, State# d #)
finalize a
mantissa State# d
s1
| a
mantissa forall a. Ord a => a -> a -> Bool
>= a
10 =
let !bs :: WORD64
bs = ByteArray
digit_table ByteArray -> Int# -> WORD64
`unsafeAt` WORD64 -> Int#
word2Int# (forall a. Mantissa a => a -> WORD64
unsafeRaw a
mantissa)
!(# WORD64
lsb, WORD64
msb #) = WORD64 -> (# WORD64, WORD64 #)
unpackWord16 WORD64
bs
s2 :: State# d
s2 = forall d. Addr# -> WORD64 -> State# d -> State# d
poke (Addr#
ptr Addr# -> Int# -> Addr#
`plusAddr#` Int#
2#) WORD64
lsb State# d
s1
s3 :: State# d
s3 = forall d. Addr# -> WORD64 -> State# d -> State# d
poke (Addr#
ptr Addr# -> Int# -> Addr#
`plusAddr#` Int#
1#) (Int -> WORD64
asciiRaw Int
asciiDot) State# d
s2
s4 :: State# d
s4 = forall d. Addr# -> WORD64 -> State# d -> State# d
poke Addr#
ptr WORD64
msb State# d
s3
in (# Addr#
ptr Addr# -> Int# -> Addr#
`plusAddr#` (Int#
olength Int# -> Int# -> Int#
+# Int#
1#), State# d
s4 #)
| (Int# -> Int
I# Int#
olength) forall a. Ord a => a -> a -> Bool
> Int
1 =
let s2 :: State# d
s2 = forall d. WORD64 -> Addr# -> State# d -> State# d
copyWord16 (WORD64 -> WORD64 -> WORD64
packWord16 (Int -> WORD64
asciiRaw Int
asciiDot) (WORD64 -> WORD64
toAscii (forall a. Mantissa a => a -> WORD64
unsafeRaw a
mantissa))) Addr#
ptr State# d
s1
in (# Addr#
ptr Addr# -> Int# -> Addr#
`plusAddr#` (Int#
olength Int# -> Int# -> Int#
+# Int#
1#), State# d
s2 #)
| Bool
otherwise =
let s2 :: State# d
s2 = forall d. Addr# -> WORD64 -> State# d -> State# d
poke (Addr#
ptr Addr# -> Int# -> Addr#
`plusAddr#` Int#
2#) (Int -> WORD64
asciiRaw Int
asciiZero) State# d
s1
s3 :: State# d
s3 = forall d. Addr# -> WORD64 -> State# d -> State# d
poke (Addr#
ptr Addr# -> Int# -> Addr#
`plusAddr#` Int#
1#) (Int -> WORD64
asciiRaw Int
asciiDot) State# d
s2
s4 :: State# d
s4 = forall d. Addr# -> WORD64 -> State# d -> State# d
poke Addr#
ptr (WORD64 -> WORD64
toAscii (forall a. Mantissa a => a -> WORD64
unsafeRaw a
mantissa)) State# d
s3
in (# Addr#
ptr Addr# -> Int# -> Addr#
`plusAddr#` Int#
3#, State# d
s4 #)
writeExponent :: Addr# -> Int32 -> State# d -> (# Addr#, State# d #)
writeExponent :: forall d. Addr# -> Int32 -> State# d -> (# Addr#, State# d #)
writeExponent Addr#
ptr !Int32
expo State# d
s1
| Int32
expo forall a. Ord a => a -> a -> Bool
>= Int32
100 =
let !(Word32
e1, Word32
e0) = Word32 -> (Word32, Word32)
fquotRem10 (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
expo)
s2 :: State# d
s2 = forall d. WORD64 -> Addr# -> State# d -> State# d
copyWord16 (ByteArray
digit_table ByteArray -> Int# -> WORD64
`unsafeAt` WORD64 -> Int#
word2Int# (forall a. Mantissa a => a -> WORD64
unsafeRaw Word32
e1)) Addr#
ptr State# d
s1
s3 :: State# d
s3 = forall d. Addr# -> WORD64 -> State# d -> State# d
poke (Addr#
ptr Addr# -> Int# -> Addr#
`plusAddr#` Int#
2#) (WORD64 -> WORD64
toAscii (forall a. Mantissa a => a -> WORD64
unsafeRaw Word32
e0)) State# d
s2
in (# Addr#
ptr Addr# -> Int# -> Addr#
`plusAddr#` Int#
3#, State# d
s3 #)
| Int32
expo forall a. Ord a => a -> a -> Bool
>= Int32
10 =
let s2 :: State# d
s2 = forall d. WORD64 -> Addr# -> State# d -> State# d
copyWord16 (ByteArray
digit_table ByteArray -> Int# -> WORD64
`unsafeAt` Int#
e) Addr#
ptr State# d
s1
in (# Addr#
ptr Addr# -> Int# -> Addr#
`plusAddr#` Int#
2#, State# d
s2 #)
| Bool
otherwise =
let s2 :: State# d
s2 = forall d. Addr# -> WORD64 -> State# d -> State# d
poke Addr#
ptr (WORD64 -> WORD64
toAscii (Int# -> WORD64
int2Word# Int#
e)) State# d
s1
in (# Addr#
ptr Addr# -> Int# -> Addr#
`plusAddr#` Int#
1#, State# d
s2 #)
where !(I# Int#
e) = Int32 -> Int
int32ToInt Int32
expo
writeSign :: Addr# -> Bool -> State# d -> (# Addr#, State# d #)
writeSign :: forall d. Addr# -> Bool -> State# d -> (# Addr#, State# d #)
writeSign Addr#
ptr Bool
True State# d
s1 =
let s2 :: State# d
s2 = forall d. Addr# -> WORD64 -> State# d -> State# d
poke Addr#
ptr (Int -> WORD64
asciiRaw Int
asciiMinus) State# d
s1
in (# Addr#
ptr Addr# -> Int# -> Addr#
`plusAddr#` Int#
1#, State# d
s2 #)
writeSign Addr#
ptr Bool
False State# d
s = (# Addr#
ptr, State# d
s #)
{-# INLINABLE toCharsScientific #-}
{-# SPECIALIZE toCharsScientific :: Bool -> Word32 -> Int32 -> BoundedPrim () #-}
{-# SPECIALIZE toCharsScientific :: Bool -> Word64 -> Int32 -> BoundedPrim () #-}
toCharsScientific :: (Mantissa a) => Bool -> a -> Int32 -> BoundedPrim ()
!Bool
sign !a
mantissa !Int32
expo = forall a.
Int -> (a -> Ptr Word8 -> IO (Ptr Word8)) -> BoundedPrim a
boundedPrim Int
maxEncodedLength forall a b. (a -> b) -> a -> b
$ \()
_ !(Ptr Addr#
p0)-> do
let !olength :: Int
olength@(I# Int#
ol) = forall a. Mantissa a => a -> Int
decimalLength a
mantissa
!expo' :: Int32
expo' = Int32
expo forall a. Num a => a -> a -> a
+ Int -> Int32
intToInt32 Int
olength forall a. Num a => a -> a -> a
- Int32
1
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. (forall s. ST s a) -> a
runST (forall s a. STRep s a -> ST s a
ST forall a b. (a -> b) -> a -> b
$ \State# s
s1 ->
let !(# Addr#
p1, State# s
s2 #) = forall d. Addr# -> Bool -> State# d -> (# Addr#, State# d #)
writeSign Addr#
p0 Bool
sign State# s
s1
!(# Addr#
p2, State# s
s3 #) = forall a d.
Mantissa a =>
Addr# -> Int# -> a -> State# d -> (# Addr#, State# d #)
writeMantissa Addr#
p1 Int#
ol a
mantissa State# s
s2
s4 :: State# s
s4 = forall d. Addr# -> WORD64 -> State# d -> State# d
poke Addr#
p2 (Int -> WORD64
asciiRaw Int
ascii_e) State# s
s3
!(# Addr#
p3, State# s
s5 #) = forall d. Addr# -> Bool -> State# d -> (# Addr#, State# d #)
writeSign (Addr#
p2 Addr# -> Int# -> Addr#
`plusAddr#` Int#
1#) (Int32
expo' forall a. Ord a => a -> a -> Bool
< Int32
0) State# s
s4
!(# Addr#
p4, State# s
s6 #) = forall d. Addr# -> Int32 -> State# d -> (# Addr#, State# d #)
writeExponent Addr#
p3 (forall a. Num a => a -> a
abs Int32
expo') State# s
s5
in (# State# s
s6, (forall a. Addr# -> Ptr a
Ptr Addr#
p4) #))