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

added shortest format mode to RealFloat that prints the shortest possible string #635

Open
wants to merge 35 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
332bcf6
improved RealFloat benchmarks
BebeSparkelSparkel Jan 9, 2024
36241cc
better names for RealFloat tests
BebeSparkelSparkel Jan 9, 2024
b3a2276
averaged realfloat specal strings instead of checking each special va…
BebeSparkelSparkel Jan 12, 2024
9a86e45
improved test and add bench for small doubles
BebeSparkelSparkel Jan 12, 2024
a0998bf
improved tests for FStandard
BebeSparkelSparkel Jan 15, 2024
ce7d20e
added better labels to real float tests
BebeSparkelSparkel Jan 15, 2024
b98fc5d
differencated special values from basic values and Float from Double
BebeSparkelSparkel Jan 15, 2024
edd64cd
put float test in the correct group
BebeSparkelSparkel Jan 16, 2024
b20d7a6
combined FloatFormat and FormatMode
BebeSparkelSparkel Jan 7, 2024
b22b6b3
customized FGeneric exponent range
BebeSparkelSparkel Jan 7, 2024
37e8d22
FScientific now has a selectable case E
BebeSparkelSparkel Jan 7, 2024
1b16c67
generaized FloatingDecimal and intermediate
BebeSparkelSparkel Jan 7, 2024
d3cdedc
generailized decimalLength
BebeSparkelSparkel Jan 7, 2024
e11b303
generalized mantissa to Word64
BebeSparkelSparkel Jan 7, 2024
aa15ac2
generalized f2s and d2s
BebeSparkelSparkel Jan 7, 2024
053f87a
added formatFloating which combines the logic of formatFlat and forma…
BebeSparkelSparkel Jan 7, 2024
40f90a4
added SpecialStrings for scientific non-normal float values
BebeSparkelSparkel Jan 7, 2024
b1e3e30
added SpecialStrings to standard floating point non-normal values
BebeSparkelSparkel Jan 7, 2024
945916b
RealFloat optimizations
BebeSparkelSparkel Jan 9, 2024
08c5050
generalized breakdown
BebeSparkelSparkel Jan 9, 2024
d87507b
added some INLINABLE to RealFloat.Internal
BebeSparkelSparkel Jan 9, 2024
0ea0a35
toCharsNonNumbersAndZero now accepts the sign, mantissa, and exponent…
BebeSparkelSparkel Jan 9, 2024
e181e2a
toCharsNonNumberAndZero now takes the float and only uses bit operati…
BebeSparkelSparkel Jan 11, 2024
cbeeef8
removed f2s d2s
BebeSparkelSparkel Jan 12, 2024
bf287a5
removed f2s f2s' f2Intermediate
BebeSparkelSparkel Jan 12, 2024
f399638
removed specialStr and replaced with improved version of toCharsNonNu…
BebeSparkelSparkel Jan 11, 2024
d9ebd68
clean up
BebeSparkelSparkel Jan 12, 2024
bd2b685
cleaned up function formatFloating
BebeSparkelSparkel Jan 12, 2024
a9cbf58
fixed precison printing of zero and neg zero for FStandard
BebeSparkelSparkel Jan 14, 2024
bbd4f76
labels for RealFloat format parameters
BebeSparkelSparkel Jan 16, 2024
a1e556e
fix possible overflow error when converting String to Builder
BebeSparkelSparkel Jan 16, 2024
182d76f
specialized maxEncodeLength to Float and Double
BebeSparkelSparkel Jan 16, 2024
8479796
moved FloatFormat to Internal so that it can be exported and users ca…
BebeSparkelSparkel Jan 16, 2024
67c4cb4
removed mappend
BebeSparkelSparkel Jan 19, 2024
00c66a7
added shortest format mode to RealFloat that prints the shortest poss…
BebeSparkelSparkel Dec 28, 2023
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
Prev Previous commit
Next Next commit
removed specialStr and replaced with improved version of toCharsNonNu…
…mbersAndZero
BebeSparkelSparkel committed Jan 16, 2024
commit f399638f2b09e4612d3287d9ba8273c9d3aef9f5
33 changes: 15 additions & 18 deletions Data/ByteString/Builder/RealFloat.hs
Original file line number Diff line number Diff line change
@@ -225,41 +225,38 @@ formatDouble = formatFloating
{-# SPECIALIZE formatFloating :: FloatFormat -> Double -> Builder #-}
formatFloating :: forall a mw ew ei.
-- a
--( ToS a
( ToD a
, Num a
, Ord a
, RealFloat a
, R.CastToWord a
, R.MantissaBits a
, R.ExponentBits a
, Bits (R.ExponentWord a)
, R.MantissaBits a
, R.CastToWord a
-- mantissa
, mw ~ R.MantissaWord a
, R.Mantissa mw
, ToWord64 mw
, R.DecimalLength mw
-- exponent
, ew ~ R.ExponentWord a
, Integral (R.ExponentWord a)
, Integral ew
, Bits ew
, ei ~ R.ExponentInt a
, R.ToInt ei
, Integral ei
, R.FromInt ei
) => FloatFormat -> a -> Builder
formatFloating fmt f = case fmt of
FGeneric eE prec (minExpo,maxExpo) ss ->
case R.toCharsNonNumbersAndZero ss f of
Just b -> BP.primBounded b ()
Nothing ->
if e' >= minExpo && e' <= maxExpo
then printSign f `mappend` showStandard (toWord64 m) e' prec
else BP.primBounded (sci eE) ()
FScientific eE ss -> flip BP.primBounded ()
$ fromMaybe (sci eE) (R.toCharsNonNumbersAndZero ss f)
FStandard prec ss ->
case R.toCharsNonNumbersAndZero ss f of
Just b -> BP.primBounded b ()
Nothing -> printSign f `mappend` showStandard (toWord64 m) e' prec
FGeneric eE prec (minExpo,maxExpo) ss -> flip fromMaybe (R.toCharsNonNumbersAndZero ss f) $
if e' >= minExpo && e' <= maxExpo
then printSign f `mappend` showStandard (toWord64 m) e' prec
else sci eE
FScientific eE ss -> fromMaybe (sci eE) (R.toCharsNonNumbersAndZero ss f)
FStandard prec ss -> flip fromMaybe (R.toCharsNonNumbersAndZero ss f) $
printSign f `mappend` showStandard (toWord64 m) e' prec
where
sci eE = R.toCharsScientific @a Proxy eE sign m e
sci eE = BP.primBounded (R.toCharsScientific @a Proxy eE sign m e) ()
e' = R.toInt e + R.decimalLength m
R.FloatingDecimal m e = toD @a mantissa expo
(sign, mantissa, expo) = R.breakdown f
32 changes: 23 additions & 9 deletions Data/ByteString/Builder/RealFloat/Internal.hs
Original file line number Diff line number Diff line change
@@ -6,7 +6,6 @@
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE BlockArguments #-}
@@ -89,9 +88,11 @@ module Data.ByteString.Builder.RealFloat.Internal
import Control.Monad (foldM)
import Data.Bits (Bits(..), FiniteBits(..))
import Data.ByteString.Internal (c2w)
import Data.ByteString.Builder.Internal (Builder)
import Data.ByteString.Builder.Prim.Internal (BoundedPrim, boundedPrim)
import Data.ByteString.Builder.RealFloat.TableGenerator
import Data.ByteString.Utils.UnalignedWrite
import qualified Data.ByteString.Builder.Prim as BP
import Data.Char (ord)
import Data.Proxy (Proxy)
import Foreign.C.Types
@@ -265,19 +266,31 @@ boundString s = boundedPrim maxEncodedLength $ const (pokeAll s)
-- * biased exponent = all 0 bits.
-- * fraction = all 0 bits.
{-# INLINABLE toCharsNonNumbersAndZero #-}
{-# SPECIALIZE toCharsNonNumbersAndZero :: SpecialStrings -> Float -> Maybe (BoundedPrim ()) #-}
{-# SPECIALIZE toCharsNonNumbersAndZero :: SpecialStrings -> Double -> Maybe (BoundedPrim ()) #-}
toCharsNonNumbersAndZero :: forall a mw.
( CastToWord a
, MantissaBits a
{-# SPECIALIZE toCharsNonNumbersAndZero :: SpecialStrings -> Float -> Maybe Builder #-}
{-# SPECIALIZE toCharsNonNumbersAndZero :: SpecialStrings -> Double -> Maybe Builder #-}
toCharsNonNumbersAndZero :: forall a mw ew.
( ExponentBits a
, mw ~ MantissaWord a
, Ord mw
, Num mw
, ew ~ ExponentWord a
, Ord ew
, Num ew
, Bits ew
, Integral ew

, ExponentBits a
, MantissaBits a
, CastToWord a
, mw ~ MantissaWord a
, Bits mw
, Eq mw
, Integral mw
, ExponentBits a
) => SpecialStrings -> a -> Maybe (BoundedPrim ())
toCharsNonNumbersAndZero SpecialStrings{..} f = boundString <$>
, ew ~ ExponentWord a
, Num ew

) => SpecialStrings -> a -> Maybe Builder
toCharsNonNumbersAndZero SpecialStrings{..} f = flip BP.primBounded () . boundString <$>
if w .&. expoMantissaBits == 0
then Just if w == signBit then negativeZero else positiveZero
else if w .&. expoMask == expoMask
@@ -943,6 +956,7 @@ breakdown :: forall a mw ew.
, mw ~ MantissaWord a
, Bits mw
, Integral mw
, ew ~ ExponentWord a
, Num ew
) => a -> (Bool, mw, ew)
breakdown f = (sign, mantissa, expo)