Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quick Optimization for showStandard #639

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions Data/ByteString/Builder/RealFloat.hs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,12 @@ module Data.ByteString.Builder.RealFloat
, generic
) where

import Data.ByteString.Builder.Internal (Builder)
import Data.ByteString.Builder.Internal (Builder, shortByteString)
import qualified Data.ByteString.Builder.RealFloat.Internal as R
import qualified Data.ByteString.Builder.RealFloat.F2S as RF
import qualified Data.ByteString.Builder.RealFloat.D2S as RD
import qualified Data.ByteString.Builder.Prim as BP
import qualified Data.ByteString.Short as BS
import GHC.Float (roundTo)
import GHC.Word (Word64)
import GHC.Show (intToDigit)
Expand Down Expand Up @@ -253,19 +254,29 @@ digits w = go [] w
in go ((R.word64ToInt r) : ds) q

-- | Show a floating point value in standard notation. Based on GHC.Float.showFloat
-- TODO: Remove the use of String and lists because it makes this very slow compared
-- to the actual implementation of the Ryu algorithm.
-- TODO: The digits should be found with the look up method described in the Ryu
-- reference algorithm.
showStandard :: Word64 -> Int -> Maybe Int -> Builder
showStandard m e prec =
case prec of
Nothing
| e <= 0 -> char7 '0'
`mappend` char7 '.'
`mappend` string7 (replicate (-e) '0')
`mappend` mconcat (digitsToBuilder ds)
| otherwise ->
let f 0 s rs = mk0 (reverse s) `mappend` char7 '.' `mappend` mk0 rs
f n s [] = f (n-1) (char7 '0':s) []
f n s (r:rs) = f (n-1) (r:s) rs
in f e [] (digitsToBuilder ds)
| e <= 0
-> string7 "0."
<> zeros (-e)
<> buildDigits m
| e >= olength
-> buildDigits m
<> zeros (e - olength)
<> string7 ".0"
| otherwise -> let
wholeDigits = m `div` (10 ^ (olength - e))
fractDigits = m `mod` (10 ^ (olength - e))
in buildDigits wholeDigits
<> char7 '.'
<> zeros (olength - e - R.decimalLength17 fractDigits)
<> buildDigits fractDigits
Just p
| e >= 0 ->
let (ei, is') = roundTo 10 (p' + e) ds
Expand All @@ -285,3 +296,6 @@ showStandard m e prec =
ds = digits m
digitsToBuilder = fmap (char7 . intToDigit)

zeros n = shortByteString $ BS.take n $ BS.replicate 308 48
olength = R.decimalLength17 m
buildDigits = BP.primBounded BP.word64Dec
Loading