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

more asthetics. no functional changes. fixes #4. #9

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions floating-bits.cabal
Original file line number Diff line number Diff line change
@@ -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
Expand Down
26 changes: 19 additions & 7 deletions src/Data/Bits/Floating.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
-----------------------------------------------------------------------------
-- |
Expand All @@ -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
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/Data/Bits/Floating/Prim.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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#))

Expand All @@ -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#)
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/Data/Bits/Floating/Ulp.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down