From 50feec6887f46c6ca5d2ee8e2063f6e28876c407 Mon Sep 17 00:00:00 2001 From: Julia Longtin Date: Tue, 19 Dec 2023 18:33:29 +0000 Subject: [PATCH] more asthetics. no functional changes. (#9) --- floating-bits.cabal | 8 ++++---- src/Data/Bits/Floating.hs | 26 +++++++++++++++++++------- src/Data/Bits/Floating/Prim.hs | 6 +++++- src/Data/Bits/Floating/Ulp.hs | 6 ++++++ 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/floating-bits.cabal b/floating-bits.cabal index 8bd2700..870b3a7 100644 --- a/floating-bits.cabal +++ b/floating-bits.cabal @@ -1,12 +1,12 @@ name: floating-bits -version: 0.3.0.0 -synopsis: Conversions between floating and integral values. +version: 0.3.1.0 +synopsis: Bitwise accurate floating point conversion, and Unit of Lease Precision calculation. description: A small library to cast floating point values to integral values and back preserving the bit-pattern. license: BSD3 license-file: LICENSE author: Anselm Jonas Scholl -maintainer: anselm.scholl@tu-harburg.de -copyright: (c) 2015 Anselm Jonas Scholl +maintainer: julia.longtin@gmail.com +copyright: (c) 2015 Anselm Jonas Scholl, (c) 2023 Julia Longtin category: Data build-type: Simple cabal-version: >=1.10 diff --git a/src/Data/Bits/Floating.hs b/src/Data/Bits/Floating.hs index 9b7a66b..468cd36 100644 --- a/src/Data/Bits/Floating.hs +++ b/src/Data/Bits/Floating.hs @@ -1,4 +1,3 @@ -{-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE FunctionalDependencies #-} ----------------------------------------------------------------------------- -- | @@ -24,13 +23,26 @@ module Data.Bits.Floating ( ,fromCDouble ) where -import Data.Bits -import Data.Bits.Floating.Prim -import Data.Bits.Floating.Ulp -import Data.Word +-- Our base library. +import Prelude (Double, Float, Floating, Integral, ShowS, String, (.), (/=), (++), id, isNaN, otherwise, shows) + +-- Our bitwise and operator. +import Data.Bits ((.&.)) + +-- Conversion wrappers. +import Data.Bits.Floating.Prim (double2WordBitwise, float2WordBitwise, word2DoubleBitwise, word2FloatBitwise) + +-- Functions for getting ULPs. +import Data.Bits.Floating.Ulp (doubleNextUlp, doublePrevUlp, doubleUlp, floatNextUlp, floatPrevUlp, floatUlp) + +-- Our byte formats. +import Data.Word (Word32, Word64) + +-- A function to dump hexidecimal. import Numeric (showHex) -import Foreign.C.Types +-- Types corresponding to C's Double and Float. +import Foreign.C.Types (CDouble(CDouble), CFloat(CFloat)) class (Floating f, Integral w) => FloatingBits f w | f -> w where -- | Coerce a floating point number to an integral number preserving the @@ -51,7 +63,7 @@ class (Floating f, Integral w) => FloatingBits f w | f -> w where nextUp :: f -> f -- | Return the next floating point value in the direction of -INF. -- If the argument is NaN, NaN is returned. If the argument is -INF, - -- +INF is returned. If the argument is 0.0, the minimum value smaller than + -- +INF is returned. If the argument is 0.0, the maximum value smaller than -- 0.0 is returned. nextDown :: f -> f -- | Return the size of an ulp of the argument. If the argument is NaN, NaN diff --git a/src/Data/Bits/Floating/Prim.hs b/src/Data/Bits/Floating/Prim.hs index 6052f40..c8fcd71 100644 --- a/src/Data/Bits/Floating/Prim.hs +++ b/src/Data/Bits/Floating/Prim.hs @@ -25,6 +25,9 @@ module Data.Bits.Floating.Prim ( word2DoubleBitwise, word2FloatBitwise) where +-- We use nothing from the Base library here. +import Prelude () + import GHC.Exts (Double#, Double(D#), Float#, Float(F#)) import GHC.Word (Word32(W32#), Word64(W64#)) @@ -33,6 +36,7 @@ import GHC.Word (Word32(W32#), Word64(W64#)) import GHC.Exts (Word64#, Word32#) #define WORD64 Word64 #define WORD32 Word32 +-- FIXME: this is wrong. what's a better test here? #elif WORD_SIZE_IN_BITS == 64 -- Earlier than GHC 9.4? then Word is our 64 bit type. import GHC.Exts (Word#) @@ -42,7 +46,7 @@ import GHC.Exts (Word#) import GHC.Exts (Word32#) #define WORD32 Word32 #else --- Earlier than GHC9? then use Word for both 32 and 64 bit FFI calls. +-- Earlier than GHC9.2? then use Word for both 32 and 64 bit FFI calls. #define WORD32 Word #endif #else diff --git a/src/Data/Bits/Floating/Ulp.hs b/src/Data/Bits/Floating/Ulp.hs index bdb7954..53efa7e 100644 --- a/src/Data/Bits/Floating/Ulp.hs +++ b/src/Data/Bits/Floating/Ulp.hs @@ -76,6 +76,12 @@ floatExpBitMask = 0x7F800000 -- * Generic implementation --------------------------- +-- | Advance a RealFloat by one unit in the last place (ULP). +-- +-- If the argument is NaN, NaN is returned. +-- If the argument is +INF, +INF is returned. +-- If the argument is (-)0.0, the minimum value greater than 0.0 is returned. +-- if the argument is -INF, -INF is returned. {-# INLINE genericUp #-} genericUp :: (RealFloat f, Num w) => (f -> w) -> (w -> f) -> f -> f genericUp mkW mkF f