{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE BangPatterns, MagicHash #-}
-- |
-- Module      : Data.ByteString.Builder.RealFloat.F2S
-- Copyright   : (c) Lawrence Wu 2021
-- License     : BSD-style
-- Maintainer  : lawrencejwu@gmail.com
--
-- Implementation of float-to-string conversion

module Data.ByteString.Builder.RealFloat.F2S
    ( FloatingDecimal(..)
    , f2s
    , f2Intermediate
    ) where

import Control.Arrow (first)
import Data.Bits ((.|.), (.&.), unsafeShiftL, unsafeShiftR)
import Data.ByteString.Builder.Internal (Builder)
import Data.ByteString.Builder.Prim (primBounded)
import Data.ByteString.Builder.RealFloat.Internal
import GHC.Int (Int32(..))
import GHC.Word (Word32(..), Word64(..))

-- See Data.ByteString.Builder.RealFloat.TableGenerator for a high-level
-- explanation of the ryu algorithm

-- | Table of 2^k / 5^q + 1
-- Byte-swapped version of
-- > fmap (finv float_pow5_inv_bitcount) [0..float_max_inv_split]
--
-- Displayed here as 2 Word64 table values per line
float_pow5_inv_split :: Addr
float_pow5_inv_split :: Addr
float_pow5_inv_split = Addr# -> Addr
Addr
  Addr#
"\x01\x00\x00\x00\x00\x00\x00\x08\x67\x66\x66\x66\x66\x66\x66\x06\
  \\xb9\x1e\x85\xeb\x51\xb8\x1e\x05\xfa\x7e\x6a\xbc\x74\x93\x18\x04\
  \\x2a\xcb\x10\xc7\xba\xb8\x8d\x06\x22\x3c\xda\x38\x62\x2d\x3e\x05\
  \\x4e\x63\x7b\x2d\xe8\xbd\x31\x04\x16\xd2\x2b\xaf\xa6\xfc\xb5\x06\
  \\x78\x0e\x23\x8c\xb8\x63\x5e\x05\x2d\xa5\xb5\x09\xfa\x82\x4b\x04\
  \\xae\x6e\xef\x75\xf6\x37\xdf\x06\x58\x25\x59\x5e\xf8\x5f\x7f\x05\
  \\x47\x84\x7a\x4b\x60\xe6\x65\x04\x71\xa0\x5d\x12\x9a\x70\x09\x07\
  \\xc1\xe6\x4a\xa8\xe1\x26\xa1\x05\x67\x85\xd5\xb9\xe7\xeb\x80\x04\
  \\x0b\x6f\x22\xf6\xa5\xac\x34\x07\xa3\x25\xb5\x91\x51\xbd\xc3\x05\
  \\xe9\xea\x90\x74\x74\x97\x9c\x04\x0e\xab\xb4\xed\x53\xf2\x60\x07\
  \\xd8\x88\x90\x24\x43\x28\xe7\x05\xe0\xd3\xa6\x83\x02\xed\xb8\x04\
  \\x66\xb9\xd7\x05\x04\x48\x8e\x07\x52\x94\xac\x04\xd0\x6c\x0b\x06\
  \\xdb\xa9\x23\x6a\xa6\xf0\xd5\x04\x2b\x76\x9f\x76\x3d\xb4\xbc\x07\
  \\xef\xc4\xb2\x2b\x31\x90\x30\x06\xf3\x03\x8f\xbc\x8d\xa6\xf3\x04\
  \\x51\x06\x18\x94\xaf\x3d\xec\x07\xda\xd1\xac\xa9\xbf\x97\x56\x06\
  \\xe2\xa7\xf0\xba\xff\x12\x12\x05"#

-- | Table of 5^(-e2-q) / 2^k + 1
-- Byte-swapped version of
-- > fmap (fnorm float_pow5_bitcount) [0..float_max_split]
--
-- Displayed here as 2 Word64 table values per line
float_pow5_split :: Addr
float_pow5_split :: Addr
float_pow5_split = Addr# -> Addr
Addr
  Addr#
"\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00\x00\x00\x00\x00\x14\
  \\x00\x00\x00\x00\x00\x00\x00\x19\x00\x00\x00\x00\x00\x00\x40\x1f\
  \\x00\x00\x00\x00\x00\x00\x88\x13\x00\x00\x00\x00\x00\x00\x6a\x18\
  \\x00\x00\x00\x00\x00\x80\x84\x1e\x00\x00\x00\x00\x00\xd0\x12\x13\
  \\x00\x00\x00\x00\x00\x84\xd7\x17\x00\x00\x00\x00\x00\x65\xcd\x1d\
  \\x00\x00\x00\x00\x20\x5f\xa0\x12\x00\x00\x00\x00\xe8\x76\x48\x17\
  \\x00\x00\x00\x00\xa2\x94\x1a\x1d\x00\x00\x00\x40\xe5\x9c\x30\x12\
  \\x00\x00\x00\x90\x1e\xc4\xbc\x16\x00\x00\x00\x34\x26\xf5\x6b\x1c\
  \\x00\x00\x80\xe0\x37\x79\xc3\x11\x00\x00\xa0\xd8\x85\x57\x34\x16\
  \\x00\x00\xc8\x4e\x67\x6d\xc1\x1b\x00\x00\x3d\x91\x60\xe4\x58\x11\
  \\x00\x40\x8c\xb5\x78\x1d\xaf\x15\x00\x50\xef\xe2\xd6\xe4\x1a\x1b\
  \\x00\x92\xd5\x4d\x06\xcf\xf0\x10\x80\xf6\x4a\xe1\xc7\x02\x2d\x15\
  \\x20\xb4\x9d\xd9\x79\x43\x78\x1a\x94\x90\x02\x28\x2c\x2a\x8b\x10\
  \\xb9\x34\x03\x32\xb7\xf4\xad\x14\xe7\x01\x84\xfe\xe4\x71\xd9\x19\
  \\x30\x81\x12\x1f\x2f\xe7\x27\x10\x7c\x21\xd7\xe6\xfa\xe0\x31\x14\
  \\xdb\xe9\x8c\xa0\x39\x59\x3e\x19\x52\x24\xb0\x08\x88\xef\x8d\x1f\
  \\xb3\x16\x6e\x05\xb5\xb5\xb8\x13\x60\x9c\xc9\x46\x22\xe3\xa6\x18\
  \\x78\x03\x7c\xd8\xea\x9b\xd0\x1e\x2b\x82\x4d\xc7\x72\x61\x42\x13\
  \\xb6\xe2\x20\x79\xcf\xf9\x12\x18\x64\x1b\x69\x57\x43\xb8\x17\x1e\
  \\x1e\xb1\xa1\x16\x2a\xd3\xce\x12\x66\x1d\x4a\x9c\xf4\x87\x82\x17\
  \\xbf\xa4\x5c\xc3\xf1\x29\x63\x1d\xf7\xe6\x19\x1a\x37\xfa\x5d\x12\
  \\xb5\x60\xa0\xe0\xc4\x78\xf5\x16\xe3\x78\xc8\x18\xf6\xd6\xb2\x1c\
  \\x8d\x4b\x7d\xcf\x59\xc6\xef\x11\x71\x9e\x5c\x43\xf0\xb7\x6b\x16\
  \\x0d\xc6\x33\x54\xec\xa5\x06\x1c"#

-- | Number of mantissa bits of a 32-bit float. The number of significant bits
-- (floatDigits (undefined :: Float)) is 24 since we have a leading 1 for
-- normal floats and 0 for subnormal floats
float_mantissa_bits :: Int
float_mantissa_bits :: Int
float_mantissa_bits = Int
23

-- | Number of exponent bits of a 32-bit float
float_exponent_bits :: Int
float_exponent_bits :: Int
float_exponent_bits = Int
8

-- | Bias in encoded 32-bit float representation (2^7 - 1)
float_bias :: Int
float_bias :: Int
float_bias = Int
127

data FloatingDecimal = FloatingDecimal
  { FloatingDecimal -> Word32
fmantissa :: !Word32
  , FloatingDecimal -> Int32
fexponent :: !Int32
  } deriving (Int -> FloatingDecimal -> ShowS
[FloatingDecimal] -> ShowS
FloatingDecimal -> String
(Int -> FloatingDecimal -> ShowS)
-> (FloatingDecimal -> String)
-> ([FloatingDecimal] -> ShowS)
-> Show FloatingDecimal
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> FloatingDecimal -> ShowS
showsPrec :: Int -> FloatingDecimal -> ShowS
$cshow :: FloatingDecimal -> String
show :: FloatingDecimal -> String
$cshowList :: [FloatingDecimal] -> ShowS
showList :: [FloatingDecimal] -> ShowS
Show, FloatingDecimal -> FloatingDecimal -> Bool
(FloatingDecimal -> FloatingDecimal -> Bool)
-> (FloatingDecimal -> FloatingDecimal -> Bool)
-> Eq FloatingDecimal
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: FloatingDecimal -> FloatingDecimal -> Bool
== :: FloatingDecimal -> FloatingDecimal -> Bool
$c/= :: FloatingDecimal -> FloatingDecimal -> Bool
/= :: FloatingDecimal -> FloatingDecimal -> Bool
Eq)

-- | Multiply a 32-bit number with a 64-bit number while keeping the upper 64
-- bits. Then shift by specified amount minus 32
mulShift32 :: Word32 -> Word64 -> Int -> Word32
mulShift32 :: Word32 -> Word64 -> Int -> Word32
mulShift32 Word32
m Word64
factor Int
shift =
  let factorLo :: Word64
factorLo = Word64
factor Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Int -> Word64
forall a. (Bits a, Integral a) => Int -> a
mask Int
32
      factorHi :: Word64
factorHi = Word64
factor Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
32
      bits0 :: Word64
bits0 = Word32 -> Word64
word32ToWord64 Word32
m Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
* Word64
factorLo
      bits1 :: Word64
bits1 = Word32 -> Word64
word32ToWord64 Word32
m Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
* Word64
factorHi
      total :: Word64
total  = (Word64
bits0 Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
32) Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
+ Word64
bits1
   in Word64 -> Word32
word64ToWord32 (Word64 -> Word32) -> Word64 -> Word32
forall a b. (a -> b) -> a -> b
$ Word64
total Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`unsafeShiftR` (Int
shift Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
32)

-- | Index into the 64-bit word lookup table float_pow5_inv_split
get_float_pow5_inv_split :: Int -> Word64
get_float_pow5_inv_split :: Int -> Word64
get_float_pow5_inv_split =
  let !(Addr Addr#
arr) = Addr
float_pow5_inv_split
   in Addr# -> Int -> Word64
getWord64At Addr#
arr

-- | Index into the 64-bit word lookup table float_pow5_split
get_float_pow5_split :: Int -> Word64
get_float_pow5_split :: Int -> Word64
get_float_pow5_split =
  let !(Addr Addr#
arr) = Addr
float_pow5_split
   in Addr# -> Int -> Word64
getWord64At Addr#
arr

-- | Take the high bits of m * 2^k / 5^q / 2^-e2+q+k
mulPow5InvDivPow2 :: Word32 -> Int -> Int -> Word32
mulPow5InvDivPow2 :: Word32 -> Int -> Int -> Word32
mulPow5InvDivPow2 Word32
m Int
q Int
j = Word32 -> Word64 -> Int -> Word32
mulShift32 Word32
m (Int -> Word64
get_float_pow5_inv_split Int
q) Int
j

-- | Take the high bits of m * 5^-e2-q / 2^k / 2^q-k
mulPow5DivPow2 :: Word32 -> Int -> Int -> Word32
mulPow5DivPow2 :: Word32 -> Int -> Int -> Word32
mulPow5DivPow2 Word32
m Int
i Int
j = Word32 -> Word64 -> Int -> Word32
mulShift32 Word32
m (Int -> Word64
get_float_pow5_split Int
i) Int
j

-- | Handle case e2 >= 0
f2dGT :: Int32 -> Word32 -> Word32 -> Word32 -> (BoundsState Word32, Int32)
f2dGT :: Int32 -> Word32 -> Word32 -> Word32 -> (BoundsState Word32, Int32)
f2dGT Int32
e2' Word32
u Word32
v Word32
w =
  let e2 :: Int
e2 = Int32 -> Int
int32ToInt Int32
e2'
      -- q = e10 = log_10(2^e2)
      q :: Int
q = Int -> Int
log10pow2 Int
e2
      -- k = B0 + log_2(5^q)
      k :: Int
k = Int
float_pow5_inv_bitcount Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int -> Int
pow5bits Int
q Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
      i :: Int
i = -Int
e2 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
q Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
k
      -- (u, v, w) * 2^k / 5^q / 2^-e2+q+k
      u' :: Word32
u' = Word32 -> Int -> Int -> Word32
mulPow5InvDivPow2 Word32
u Int
q Int
i
      v' :: Word32
v' = Word32 -> Int -> Int -> Word32
mulPow5InvDivPow2 Word32
v Int
q Int
i
      w' :: Word32
w' = Word32 -> Int -> Int -> Word32
mulPow5InvDivPow2 Word32
w Int
q Int
i
      !lastRemoved :: Word32
lastRemoved =
        if Int
q Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
0 Bool -> Bool -> Bool
&& Word32 -> Word32
fquot10 (Word32
w' Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Word32
1) Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word32 -> Word32
fquot10 Word32
u'
          -- We need to know one removed digit even if we are not going to loop
          -- below. We could use q = X - 1 above, except that would require 33
          -- bits for the result, and we've found that 32-bit arithmetic is
          -- faster even on 64-bit machines.
          then let l :: Int
l = Int
float_pow5_inv_bitcount Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int -> Int
pow5bits (Int
q Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
                in Word32 -> Word32
frem10 (Word32 -> Int -> Int -> Word32
mulPow5InvDivPow2 Word32
v (Int
q Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) (-Int
e2 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
q Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
l))
          else Word32
0
      !(Bool
vvTrailing, Bool
vuTrailing, Word32
vw') =
        case () of
          ()
_ | Int
q Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
9 Bool -> Bool -> Bool
&& Word32 -> Word32
frem5 Word32
v Word32 -> Word32 -> Bool
forall a. Eq a => a -> a -> Bool
== Word32
0
                -> (Word32 -> Int -> Bool
forall a. Mantissa a => a -> Int -> Bool
multipleOfPowerOf5 Word32
v Int
q, Bool
False, Word32
w')
            | Int
q Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
9 Bool -> Bool -> Bool
&& Word32 -> Bool
forall a. Mantissa a => a -> Bool
acceptBounds Word32
v
                -> (Bool
False, Word32 -> Int -> Bool
forall a. Mantissa a => a -> Int -> Bool
multipleOfPowerOf5 Word32
u Int
q, Word32
w')
            | Int
q Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
9
                -> (Bool
False, Bool
False, Word32
w' Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Bool -> Word32
boolToWord32 (Word32 -> Int -> Bool
forall a. Mantissa a => a -> Int -> Bool
multipleOfPowerOf5 Word32
w Int
q))
            | Bool
otherwise
                -> (Bool
False, Bool
False, Word32
w')
   in (Word32
-> Word32 -> Word32 -> Word32 -> Bool -> Bool -> BoundsState Word32
forall a. a -> a -> a -> a -> Bool -> Bool -> BoundsState a
BoundsState Word32
u' Word32
v' Word32
vw' Word32
lastRemoved Bool
vuTrailing Bool
vvTrailing, Int -> Int32
intToInt32 Int
q)

-- | Handle case e2 < 0
f2dLT :: Int32 -> Word32 -> Word32 -> Word32 -> (BoundsState Word32, Int32)
f2dLT :: Int32 -> Word32 -> Word32 -> Word32 -> (BoundsState Word32, Int32)
f2dLT Int32
e2' Word32
u Word32
v Word32
w =
  let e2 :: Int
e2 = Int32 -> Int
int32ToInt Int32
e2'
      q :: Int
q = Int -> Int
log10pow5 (-Int
e2)
      e10 :: Int
e10 = Int
q Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
e2
      i :: Int
i = (-Int
e2) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
q
      -- k = log_2(5^-e2-q) - B1
      k :: Int
k = Int -> Int
pow5bits Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
float_pow5_bitcount
      j :: Int
j = Int
q Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
k
      -- (u, v, w) * 5^-e2-q / 2^k / 2^q-k
      u' :: Word32
u' = Word32 -> Int -> Int -> Word32
mulPow5DivPow2 Word32
u Int
i Int
j
      v' :: Word32
v' = Word32 -> Int -> Int -> Word32
mulPow5DivPow2 Word32
v Int
i Int
j
      w' :: Word32
w' = Word32 -> Int -> Int -> Word32
mulPow5DivPow2 Word32
w Int
i Int
j
      !lastRemoved :: Word32
lastRemoved =
        if Int
q Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
0 Bool -> Bool -> Bool
&& Word32 -> Word32
fquot10 (Word32
w' Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Word32
1) Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word32 -> Word32
fquot10 Word32
u'
          then let j' :: Int
j' = Int
q Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
- (Int -> Int
pow5bits (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
float_pow5_bitcount)
                in Word32 -> Word32
frem10 (Word32 -> Int -> Int -> Word32
mulPow5DivPow2 Word32
v (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
j')
          else Word32
0
      !(Bool
vvTrailing , Bool
vuTrailing, Word32
vw') =
        case () of
          ()
_ | Int
q Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
1 Bool -> Bool -> Bool
&& Word32 -> Bool
forall a. Mantissa a => a -> Bool
acceptBounds Word32
v
                -> (Bool
True, Word32
v Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Word32
u Word32 -> Word32 -> Bool
forall a. Eq a => a -> a -> Bool
== Word32
2, Word32
w') -- mmShift == 1
            | Int
q Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
1
                -> (Bool
True, Bool
False, Word32
w' Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Word32
1)
            | Int
q Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
31
                -> (Word32 -> Int -> Bool
forall a. Mantissa a => a -> Int -> Bool
multipleOfPowerOf2 Word32
v (Int
q Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1), Bool
False, Word32
w')
            | Bool
otherwise
                -> (Bool
False, Bool
False, Word32
w')
   in (Word32
-> Word32 -> Word32 -> Word32 -> Bool -> Bool -> BoundsState Word32
forall a. a -> a -> a -> a -> Bool -> Bool -> BoundsState a
BoundsState Word32
u' Word32
v' Word32
vw' Word32
lastRemoved Bool
vuTrailing Bool
vvTrailing, Int -> Int32
intToInt32 Int
e10)

-- | Returns the decimal representation of the given mantissa and exponent of a
-- 32-bit Float using the ryu algorithm.
f2d :: Word32 -> Word32 -> FloatingDecimal
f2d :: Word32 -> Word32 -> FloatingDecimal
f2d Word32
m Word32
e =
  let !mf :: Word32
mf = if Word32
e Word32 -> Word32 -> Bool
forall a. Eq a => a -> a -> Bool
== Word32
0
              then Word32
m
              else (Word32
1 Word32 -> Int -> Word32
forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
float_mantissa_bits) Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.|. Word32
m
      !ef :: Int32
ef = Int -> Int32
intToInt32 (Int -> Int32) -> Int -> Int32
forall a b. (a -> b) -> a -> b
$ if Word32
e Word32 -> Word32 -> Bool
forall a. Eq a => a -> a -> Bool
== Word32
0
              then Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
- (Int
float_bias Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
float_mantissa_bits)
              else Word32 -> Int
word32ToInt Word32
e Int -> Int -> Int
forall a. Num a => a -> a -> a
- (Int
float_bias Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
float_mantissa_bits)
      !e2 :: Int32
e2 = Int32
ef Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
2
      -- Step 2. 3-tuple (u, v, w) * 2**e2
      !u :: Word32
u = Word32
4 Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
* Word32
mf Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Word32
1 Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Bool -> Word32
boolToWord32 (Word32
m Word32 -> Word32 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word32
0 Bool -> Bool -> Bool
|| Word32
e Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word32
1)
      !v :: Word32
v = Word32
4 Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
* Word32
mf
      !w :: Word32
w = Word32
4 Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
* Word32
mf Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
+ Word32
2
      -- Step 3. convert to decimal power base
      !(BoundsState Word32
state, Int32
e10) =
        if Int32
e2 Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
>= Int32
0
           then Int32 -> Word32 -> Word32 -> Word32 -> (BoundsState Word32, Int32)
f2dGT Int32
e2 Word32
u Word32
v Word32
w
           else Int32 -> Word32 -> Word32 -> Word32 -> (BoundsState Word32, Int32)
f2dLT Int32
e2 Word32
u Word32
v Word32
w
      -- Step 4: Find the shortest decimal representation in the interval of
      -- valid representations.
      !(Word32
output, Int32
removed) =
        let rounded :: BoundsState Word32 -> Word32
rounded = Bool -> BoundsState Word32 -> Word32
forall a. Mantissa a => Bool -> BoundsState a -> a
closestCorrectlyRounded (Word32 -> Bool
forall a. Mantissa a => a -> Bool
acceptBounds Word32
v)
         in (BoundsState Word32 -> Word32)
-> (BoundsState Word32, Int32) -> (Word32, Int32)
forall b c d. (b -> c) -> (b, d) -> (c, d)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first BoundsState Word32 -> Word32
rounded ((BoundsState Word32, Int32) -> (Word32, Int32))
-> (BoundsState Word32, Int32) -> (Word32, Int32)
forall a b. (a -> b) -> a -> b
$ if BoundsState Word32 -> Bool
forall a. BoundsState a -> Bool
vvIsTrailingZeros BoundsState Word32
state Bool -> Bool -> Bool
|| BoundsState Word32 -> Bool
forall a. BoundsState a -> Bool
vuIsTrailingZeros BoundsState Word32
state
           then BoundsState Word32 -> (BoundsState Word32, Int32)
forall a.
(Show a, Mantissa a) =>
BoundsState a -> (BoundsState a, Int32)
trimTrailing BoundsState Word32
state
           else BoundsState Word32 -> (BoundsState Word32, Int32)
forall a. Mantissa a => BoundsState a -> (BoundsState a, Int32)
trimNoTrailing BoundsState Word32
state
      !e' :: Int32
e' = Int32
e10 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
removed
   in Word32 -> Int32 -> FloatingDecimal
FloatingDecimal Word32
output Int32
e'

-- | Split a Float into (sign, mantissa, exponent)
breakdown :: Float -> (Bool, Word32, Word32)
breakdown :: Float -> (Bool, Word32, Word32)
breakdown Float
f =
  let bits :: Word32
bits = Float -> Word32
castFloatToWord32 Float
f
      sign :: Bool
sign = ((Word32
bits Word32 -> Int -> Word32
forall a. Bits a => a -> Int -> a
`unsafeShiftR` (Int
float_mantissa_bits Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
float_exponent_bits)) Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.&. Word32
1) Word32 -> Word32 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word32
0
      mantissa :: Word32
mantissa = Word32
bits Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.&. Int -> Word32
forall a. (Bits a, Integral a) => Int -> a
mask Int
float_mantissa_bits
      expo :: Word32
expo = (Word32
bits Word32 -> Int -> Word32
forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
float_mantissa_bits) Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.&. Int -> Word32
forall a. (Bits a, Integral a) => Int -> a
mask Int
float_exponent_bits
   in (Bool
sign, Word32
mantissa, Word32
expo)

-- | Dispatches to `f2d` and applies the given formatters
{-# INLINE f2s' #-}
f2s' :: (Bool -> Word32 -> Int32 -> a) -> (NonNumbersAndZero -> a) -> Float -> a
f2s' :: forall a.
(Bool -> Word32 -> Int32 -> a)
-> (NonNumbersAndZero -> a) -> Float -> a
f2s' Bool -> Word32 -> Int32 -> a
formatter NonNumbersAndZero -> a
specialFormatter Float
f =
  let (Bool
sign, Word32
mantissa, Word32
expo) = Float -> (Bool, Word32, Word32)
breakdown Float
f
   in if (Word32
expo Word32 -> Word32 -> Bool
forall a. Eq a => a -> a -> Bool
== Int -> Word32
forall a. (Bits a, Integral a) => Int -> a
mask Int
float_exponent_bits) Bool -> Bool -> Bool
|| (Word32
expo Word32 -> Word32 -> Bool
forall a. Eq a => a -> a -> Bool
== Word32
0 Bool -> Bool -> Bool
&& Word32
mantissa Word32 -> Word32 -> Bool
forall a. Eq a => a -> a -> Bool
== Word32
0)
         then NonNumbersAndZero -> a
specialFormatter NonNumbersAndZero
                  { negative :: Bool
negative=Bool
sign
                  , exponent_all_one :: Bool
exponent_all_one=Word32
expo Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
> Word32
0
                  , mantissa_non_zero :: Bool
mantissa_non_zero=Word32
mantissa Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
> Word32
0 }
         else let FloatingDecimal Word32
m Int32
e = Word32 -> Word32 -> FloatingDecimal
f2d Word32
mantissa Word32
expo
               in Bool -> Word32 -> Int32 -> a
formatter Bool
sign Word32
m Int32
e

-- | Render a Float in scientific notation
f2s :: Float -> Builder
f2s :: Float -> Builder
f2s Float
f = BoundedPrim () -> () -> Builder
forall a. BoundedPrim a -> a -> Builder
primBounded ((Bool -> Word32 -> Int32 -> BoundedPrim ())
-> (NonNumbersAndZero -> BoundedPrim ()) -> Float -> BoundedPrim ()
forall a.
(Bool -> Word32 -> Int32 -> a)
-> (NonNumbersAndZero -> a) -> Float -> a
f2s' Bool -> Word32 -> Int32 -> BoundedPrim ()
forall a. Mantissa a => Bool -> a -> Int32 -> BoundedPrim ()
toCharsScientific NonNumbersAndZero -> BoundedPrim ()
toCharsNonNumbersAndZero Float
f) ()

-- | Returns the decimal representation of a Float. NaN and Infinity will
-- return `FloatingDecimal 0 0`
f2Intermediate :: Float -> FloatingDecimal
f2Intermediate :: Float -> FloatingDecimal
f2Intermediate = (Bool -> Word32 -> Int32 -> FloatingDecimal)
-> (NonNumbersAndZero -> FloatingDecimal)
-> Float
-> FloatingDecimal
forall a.
(Bool -> Word32 -> Int32 -> a)
-> (NonNumbersAndZero -> a) -> Float -> a
f2s' ((Word32 -> Int32 -> FloatingDecimal)
-> Bool -> Word32 -> Int32 -> FloatingDecimal
forall a b. a -> b -> a
const Word32 -> Int32 -> FloatingDecimal
FloatingDecimal) (FloatingDecimal -> NonNumbersAndZero -> FloatingDecimal
forall a b. a -> b -> a
const (FloatingDecimal -> NonNumbersAndZero -> FloatingDecimal)
-> FloatingDecimal -> NonNumbersAndZero -> FloatingDecimal
forall a b. (a -> b) -> a -> b
$ Word32 -> Int32 -> FloatingDecimal
FloatingDecimal Word32
0 Int32
0)