Skip to content

Commit 34291e3

Browse files
committed
Dispatch to slow cast when builtin unavailable
- GHC.Float exports them starting from base-4.10.0.0 which starts with with ghc 8.2.1
1 parent 11d7d90 commit 34291e3

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

Data/ByteString/Builder/RealFloat/D2S.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import Data.ByteString.Builder.RealFloat.Internal
1414
import Data.ByteString.Builder.RealFloat.TableGenerator
1515
import Data.Maybe (fromMaybe)
1616
import GHC.Exts
17-
import GHC.Float (castDoubleToWord64)
1817
import GHC.Int (Int32(..), Int64(..))
1918
import GHC.ST (ST(..), runST)
2019
import GHC.Word (Word32(..), Word64(..))

Data/ByteString/Builder/RealFloat/F2S.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import Data.ByteString.Builder.Prim (primBounded)
1313
import Data.ByteString.Builder.RealFloat.Internal
1414
import Data.ByteString.Builder.RealFloat.TableGenerator
1515
import GHC.Exts
16-
import GHC.Float (castFloatToWord32)
1716
import GHC.Int (Int32(..))
1817
import GHC.ST (ST(..), runST)
1918
import GHC.Word (Word32(..), Word64(..))

Data/ByteString/Builder/RealFloat/Internal.hs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{-# LANGUAGE ScopedTypeVariables, ExplicitForAll #-}
22
{-# LANGUAGE BangPatterns, MagicHash, UnboxedTuples #-}
3+
{-# LANGUAGE CPP #-}
34

45
module Data.ByteString.Builder.RealFloat.Internal
56
( (.>>)
@@ -42,6 +43,8 @@ module Data.ByteString.Builder.RealFloat.Internal
4243
, box
4344
, unbox
4445
, ByteArray(..)
46+
, castDoubleToWord64
47+
, castFloatToWord32
4548
) where
4649

4750
import Control.Monad (foldM)
@@ -56,6 +59,23 @@ import GHC.Word (Word8, Word32(..), Word64(..))
5659
import Foreign.Ptr (plusPtr)
5760
import qualified Foreign.Storable as S (poke)
5861

62+
#if __GLASGOW_HASKELL__ >= 802
63+
import GHC.Float (castFloatToWord32, castDoubleToWord64)
64+
#else
65+
import System.IO.Unsafe (unsafePerformIO)
66+
import Foreign.Marshal.Utils (with)
67+
import Foreign.Ptr (castPtr)
68+
import Foreign.Storable (peek)
69+
{-# NOINLINE castFloatToWord32 #-}
70+
castFloatToWord32 :: Float -> Word32
71+
castFloatToWord32 x = unsafePerformIO (with x (peek . castPtr))
72+
73+
-- | Convert a 'Double' to a 'Word64'.
74+
{-# NOINLINE castDoubleToWord64 #-}
75+
castDoubleToWord64 :: Double -> Word64
76+
castDoubleToWord64 x = unsafePerformIO (with x (peek . castPtr))
77+
#endif
78+
5979
{-# INLINABLE (.>>) #-}
6080
(.>>) :: (Bits a, Integral b) => a -> b -> a
6181
a .>> s = unsafeShiftR a (fromIntegral s)

0 commit comments

Comments
 (0)