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

Update cabal file #13

Merged
merged 5 commits into from
Dec 23, 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
2 changes: 1 addition & 1 deletion floating-bits.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: A small library to cast floating point values to integral v
license: BSD3
license-file: LICENSE
author: Anselm Jonas Scholl
maintainer: julia.longtin@gmail.com
maintainer: Julia Longtin <julia.longtin@gmail.com>
copyright: (c) 2015 Anselm Jonas Scholl, (c) 2023 Julia Longtin
category: Data
build-type: Simple
Expand Down
32 changes: 18 additions & 14 deletions src/Data/Bits/Floating.hs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{-# LANGUAGE FunctionalDependencies #-}
-----------------------------------------------------------------------------
-- |
-- Copyright : (C) 2015 Anselm Jonas Scholl
-- Copyright : (C) 2015 Anselm Jonas Scholl, (C) 2023 Julia Longtin
-- License : BSD3
-- Maintainer : Anselm Jonas Scholl <anselm.scholl@tu-harburg.de>
-- Maintainer : Julia Longtin <Julia.Longtin@gmail.com>
-- Stability : experimental
-- Portability : GHC-specific
--
-- Conversions between floating point values and integral values preserving
-- the bit-patterns.
-- Functions for performing conversions between floating point values and
-- Integral values, for retrieving the Unit of Least Precision of a floating
-- point value, and for incrementing / decrementing a value by one ULP..
----------------------------------------------------------------------------
module Data.Bits.Floating (

Expand Down Expand Up @@ -57,20 +58,23 @@ class (Floating f, Integral w) => FloatingBits f w | f -> w where
-- @'coerceToWord' ('coerceToFloat' w) /= w@.
coerceToFloat :: w -> 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 greater than
-- 0.0 is returned.
-- 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.
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 maximum value smaller than
-- 0.0 is returned.
-- If the argument is NaN, NaN is returned.
-- If the argument is +INF, +INF is returned.
-- If the argument is 0.0, the maximum value smaller than 0.0 is returned.
-- If the argument is -INF, -INF is returned.
nextDown :: f -> f
-- | Return the size of an ulp of the argument. If the argument is NaN, NaN
-- is returned. If the argument is +INF or -INF, +INF is returned. If
-- the argument is 0.0, the minimum value greater than 0.0 is returned.
-- | Return the size of the Unit of Least Precision of the argument.
-- If the argument is NaN, NaN is returned.
-- If the argument is +INF or -INF, +INF is returned.
-- If the argument is 0.0, the minimum value greater than 0.0 is returned.
--
-- If @x@ is not NaN, @'ulp' x == 'ulp' (-x)@ holds.
-- If @x@ is not NaN or one of the infinities, @'ulp' x == 'ulp' (-x)@ holds.
ulp :: f -> f

class ShowFloat f where
Expand Down
7 changes: 5 additions & 2 deletions src/Data/Bits/Floating/Prim.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
-----------------------------------------------------------------------------
-- |
-- Copyright : (C) 2015 Anselm Jonas Scholl
-- Copyright : (C) 2015 Anselm Jonas Scholl, (C) 2023 Julia Longtin
-- License : BSD3
-- Maintainer : Anselm Jonas Scholl <anselm.scholl@tu-harburg.de>
-- Maintainer : Julia Longtin
-- Stability : experimental
-- Portability : GHC-specific
--
Expand All @@ -17,6 +17,7 @@
{-# LANGUAGE UnliftedFFITypes #-}
{-# LANGUAGE CPP #-}

-- Note: HLint fails to find this, and will always warn.
#include "MachDeps.h"

module Data.Bits.Floating.Prim (
Expand All @@ -31,6 +32,7 @@ import Prelude ()
import GHC.Exts (Double#, Double(D#), Float#, Float(F#))
import GHC.Word (Word32(W32#), Word64(W64#))

#if defined(WORD_SIZE_IN_BITS)
#if WORD_SIZE_IN_BITS == 64 && MIN_VERSION_base(4,17,0)
-- The name of Word# changed to Word64# in base 4.17.0 (GHC 9.4.1)
import GHC.Exts (Word64#, Word32#)
Expand All @@ -52,6 +54,7 @@ import GHC.Exts (Word32#)
#else
#error non-X86_64 architectures not supported
#endif
#endif

foreign import prim "double2WordBwzh"
double2WordBitwise# :: Double# -> WORD64#
Expand Down
20 changes: 16 additions & 4 deletions test/Bench.hs
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
-----------------------------------------------------------------------------
-- |
-- Copyright : (C) 2015 Anselm Jonas Scholl, (C) 2023 Julia Longtin
-- License : BSD3
-- Maintainer : Julia Longtin <Julia.Longtin@gmail.com>
-- Stability : experimental
-- Portability : GHC-specific
--

module Main where

import TestUtils
import Prelude (Double, Float, IO, ($), (++), concatMap, map, shows, unzip)

import TestUtils (refDoubleDown, refDoubleToWord, refDoubleUp, refDoubleUlp, refFloatDown, refFloatToWord, refFloatUp, refFloatUlp, refWordToDouble, refWordToFloat, showW, testD, testF)

import Data.Word (Word32, Word64)

import Data.Word
import Data.Bits.Floating
import Data.Bits.Floating (coerceToFloat, coerceToWord, nextDown, nextUp, ulp)

import Criterion.Main
import Criterion.Main (Benchmark, bench, bgroup, defaultMain, nf)

mkSingleBenchmarks :: ((Float, Word32) -> [Benchmark]) -> ((Double, Word64) -> [Benchmark]) -> [Benchmark]
mkSingleBenchmarks mkFTest mkDTest = concatMap mkFTest testF ++ concatMap mkDTest testD
Expand Down
28 changes: 21 additions & 7 deletions test/Test.hs
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
-----------------------------------------------------------------------------
-- |
-- Copyright : (C) 2015 Anselm Jonas Scholl, (C) 2023 Julia Longtin
-- License : BSD3
-- Maintainer : Julia Longtin <Julia.Longtin@gmail.com>
-- Stability : experimental
-- Portability : GHC-specific
--

{-# LANGUAGE BangPatterns #-}

{-# LANGUAGE ScopedTypeVariables #-}

module Main where

import TestUtils
import Prelude(Bool(False), Double, Float, Integral, IO, MonadFail, RealFloat, Show, String, (.), (<), ($), (+), (-), (&&), (++), (<=), (>=), (/=), (==), (||), (<$>), either, fail, fromIntegral, fst, isInfinite, isNaN, isNegativeZero, mapM_, maxBound, minBound, not, otherwise, putStrLn, realToFrac, rem, return, quot, show, shows, snd)

import TestUtils (refDoubleDown, refDoubleToWord, refDoubleUp, refDoubleUlp, refFloatDown, refFloatToWord, refFloatUlp, refFloatUp, refWordToDouble, refWordToFloat, showW, testD, testF)

import Data.Word (Word32, Word64)

import Data.Word
import Data.Bits
import Data.Bits.Floating
import Data.Bits ((.&.), complement)
import Data.Bits.Floating (FloatingBits, ShowFloat, coerceToFloat, coerceToWord, nextDown, nextUp, showFloat, ulp)

import Control.Concurrent
import Control.Exception
import Control.Monad
import Control.Concurrent (MVar, forkIO, getNumCapabilities, newEmptyMVar, putMVar, takeMVar)
import Control.Exception (SomeException, throwIO, try)
import Control.Monad (forM_, join, replicateM_, unless, void, when)

main :: IO ()
main = do
Expand Down
16 changes: 14 additions & 2 deletions test/TestUtils.hs
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
-----------------------------------------------------------------------------
-- |
-- Copyright : (C) 2015 Anselm Jonas Scholl, (C) 2023 Julia Longtin
-- License : BSD3
-- Maintainer : Julia Longtin <Julia.Longtin@gmail.com>
-- Stability : experimental
-- Portability : GHC-specific
--

{-# LANGUAGE ForeignFunctionInterface #-}

module TestUtils where

import Data.Word
import Prelude (Double, Float, Integral, Show, String, (/), pi, sqrt)

import Data.Word (Word32, Word64)

import Numeric
import Numeric (showHex)

-- we do not test NaNs here because their binary representation can be changed
-- without changing their value and some processors have been observed to do that.
Expand Down