{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE BangPatterns #-}
module Numeric.Floating.IEEE.Internal.NextFloat where
import Data.Bits
import GHC.Float.Compat (castDoubleToWord64, castFloatToWord32,
castWord32ToFloat, castWord64ToDouble)
import MyPrelude
import Numeric.Floating.IEEE.Internal.Base
default ()
nextUp :: RealFloat a => a -> a
nextUp :: forall a. RealFloat a => a -> a
nextUp a
x | Bool -> Bool
not (forall a. RealFloat a => a -> Bool
isIEEE a
x) = forall a. HasCallStack => [Char] -> a
error [Char]
"non-IEEE numbers are not supported"
| forall a. RealFloat a => a -> Bool
isNaN a
x Bool -> Bool -> Bool
|| (forall a. RealFloat a => a -> Bool
isInfinite a
x Bool -> Bool -> Bool
&& a
x forall a. Ord a => a -> a -> Bool
> a
0) = a
x forall a. Num a => a -> a -> a
+ a
x
| a
x forall a. Ord a => a -> a -> Bool
>= a
0 = forall a. RealFloat a => a -> a
nextUp_positive a
x
| Bool
otherwise = - forall a. RealFloat a => a -> a
nextDown_positive (- a
x)
{-# INLINE [1] nextUp #-}
nextDown :: RealFloat a => a -> a
nextDown :: forall a. RealFloat a => a -> a
nextDown a
x | Bool -> Bool
not (forall a. RealFloat a => a -> Bool
isIEEE a
x) = forall a. HasCallStack => [Char] -> a
error [Char]
"non-IEEE numbers are not supported"
| forall a. RealFloat a => a -> Bool
isNaN a
x Bool -> Bool -> Bool
|| (forall a. RealFloat a => a -> Bool
isInfinite a
x Bool -> Bool -> Bool
&& a
x forall a. Ord a => a -> a -> Bool
< a
0) = a
x forall a. Num a => a -> a -> a
+ a
x
| a
x forall a. Ord a => a -> a -> Bool
>= a
0 = forall a. RealFloat a => a -> a
nextDown_positive a
x
| Bool
otherwise = - forall a. RealFloat a => a -> a
nextUp_positive (- a
x)
{-# INLINE [1] nextDown #-}
nextTowardZero :: RealFloat a => a -> a
nextTowardZero :: forall a. RealFloat a => a -> a
nextTowardZero a
x | Bool -> Bool
not (forall a. RealFloat a => a -> Bool
isIEEE a
x) = forall a. HasCallStack => [Char] -> a
error [Char]
"non-IEEE numbers are not supported"
| forall a. RealFloat a => a -> Bool
isNaN a
x Bool -> Bool -> Bool
|| a
x forall a. Eq a => a -> a -> Bool
== a
0 = a
x forall a. Num a => a -> a -> a
+ a
x
| a
x forall a. Ord a => a -> a -> Bool
>= a
0 = forall a. RealFloat a => a -> a
nextDown_positive a
x
| Bool
otherwise = - forall a. RealFloat a => a -> a
nextDown_positive (- a
x)
{-# INLINE [1] nextTowardZero #-}
nextUp_positive :: RealFloat a => a -> a
nextUp_positive :: forall a. RealFloat a => a -> a
nextUp_positive a
x
| forall a. RealFloat a => a -> Bool
isNaN a
x Bool -> Bool -> Bool
|| a
x forall a. Ord a => a -> a -> Bool
< a
0 = forall a. HasCallStack => [Char] -> a
error [Char]
"nextUp_positive"
| forall a. RealFloat a => a -> Bool
isInfinite a
x = a
x
| a
x forall a. Eq a => a -> a -> Bool
== a
0 = forall a. RealFloat a => Integer -> Int -> a
encodeFloat Integer
1 (Int
expMin forall a. Num a => a -> a -> a
- Int
d)
| Bool
otherwise = let m :: Integer
e :: Int
(Integer
m,Int
e) = forall a. RealFloat a => a -> (Integer, Int)
decodeFloat a
x
in if Int
expMin forall a. Num a => a -> a -> a
- Int
d forall a. Ord a => a -> a -> Bool
<= Int
e then
if Integer
m forall a. Num a => a -> a -> a
+ Integer
1 forall a. Eq a => a -> a -> Bool
== Integer
base Integer -> Int -> Integer
^! Int
d Bool -> Bool -> Bool
&& Int
e forall a. Eq a => a -> a -> Bool
== Int
expMax forall a. Num a => a -> a -> a
- Int
d then
a
1 forall a. Fractional a => a -> a -> a
/ a
0
else
forall a. RealFloat a => Integer -> Int -> a
encodeFloat (Integer
m forall a. Num a => a -> a -> a
+ Integer
1) Int
e
else
let m' :: Integer
m' = if Integer
base forall a. Eq a => a -> a -> Bool
== Integer
2 then
Integer
m forall a. Bits a => a -> Int -> a
`unsafeShiftR` (Int
expMin forall a. Num a => a -> a -> a
- Int
d forall a. Num a => a -> a -> a
- Int
e)
else
Integer
m forall a. Integral a => a -> a -> a
`quot` (Integer
base forall a b. (Num a, Integral b) => a -> b -> a
^ (Int
expMin forall a. Num a => a -> a -> a
- Int
d forall a. Num a => a -> a -> a
- Int
e))
in forall a. RealFloat a => Integer -> Int -> a
encodeFloat (Integer
m' forall a. Num a => a -> a -> a
+ Integer
1) (Int
expMin forall a. Num a => a -> a -> a
- Int
d)
where
d, expMin :: Int
base :: Integer
base = forall a. RealFloat a => a -> Integer
floatRadix a
x
d :: Int
d = forall a. RealFloat a => a -> Int
floatDigits a
x
(Int
expMin,Int
expMax) = forall a. RealFloat a => a -> (Int, Int)
floatRange a
x
{-# INLINE nextUp_positive #-}
nextDown_positive :: RealFloat a => a -> a
nextDown_positive :: forall a. RealFloat a => a -> a
nextDown_positive a
x
| forall a. RealFloat a => a -> Bool
isNaN a
x Bool -> Bool -> Bool
|| a
x forall a. Ord a => a -> a -> Bool
< a
0 = forall a. HasCallStack => [Char] -> a
error [Char]
"nextDown_positive"
| forall a. RealFloat a => a -> Bool
isInfinite a
x = forall a. RealFloat a => a
maxFinite
| a
x forall a. Eq a => a -> a -> Bool
== a
0 = forall a. RealFloat a => Integer -> Int -> a
encodeFloat (-Integer
1) (Int
expMin forall a. Num a => a -> a -> a
- Int
d)
| Bool
otherwise = let m :: Integer
e :: Int
(Integer
m,Int
e) = forall a. RealFloat a => a -> (Integer, Int)
decodeFloat a
x
in if Int
expMin forall a. Num a => a -> a -> a
- Int
d forall a. Ord a => a -> a -> Bool
<= Int
e then
let m1 :: Integer
m1 = Integer
m forall a. Num a => a -> a -> a
- Integer
1
in if Integer
m forall a. Eq a => a -> a -> Bool
== Integer
base Integer -> Int -> Integer
^! (Int
d forall a. Num a => a -> a -> a
- Int
1) Bool -> Bool -> Bool
&& Int
expMin forall a. Num a => a -> a -> a
- Int
d forall a. Eq a => a -> a -> Bool
/= Int
e then
forall a. RealFloat a => Integer -> Int -> a
encodeFloat (Integer
base forall a. Num a => a -> a -> a
* Integer
m forall a. Num a => a -> a -> a
- Integer
1) (Int
e forall a. Num a => a -> a -> a
- Int
1)
else
forall a. RealFloat a => Integer -> Int -> a
encodeFloat Integer
m1 Int
e
else
let m' :: Integer
m' = if Integer
base forall a. Eq a => a -> a -> Bool
== Integer
2 then
Integer
m forall a. Bits a => a -> Int -> a
`unsafeShiftR` (Int
expMin forall a. Num a => a -> a -> a
- Int
d forall a. Num a => a -> a -> a
- Int
e)
else
Integer
m forall a. Integral a => a -> a -> a
`quot` (Integer
base forall a b. (Num a, Integral b) => a -> b -> a
^ (Int
expMin forall a. Num a => a -> a -> a
- Int
d forall a. Num a => a -> a -> a
- Int
e))
in forall a. RealFloat a => Integer -> Int -> a
encodeFloat (Integer
m' forall a. Num a => a -> a -> a
- Integer
1) (Int
expMin forall a. Num a => a -> a -> a
- Int
d)
where
d, expMin :: Int
base :: Integer
base = forall a. RealFloat a => a -> Integer
floatRadix a
x
d :: Int
d = forall a. RealFloat a => a -> Int
floatDigits a
x
(Int
expMin,Int
_expMax) = forall a. RealFloat a => a -> (Int, Int)
floatRange a
x
{-# INLINE nextDown_positive #-}
{-# RULES
"nextUp/Float" nextUp = nextUpFloat
"nextUp/Double" nextUp = nextUpDouble
"nextDown/Float" nextDown = nextDownFloat
"nextDown/Double" nextDown = nextDownDouble
"nextTowardZero/Float" nextTowardZero = nextTowardZeroFloat
"nextTowardZero/Double" nextTowardZero = nextTowardZeroDouble
#-}
nextUpFloat :: Float -> Float
nextUpFloat :: Float -> Float
nextUpFloat Float
x =
case Float -> Word32
castFloatToWord32 Float
x of
Word32
w | Word32
w forall a. Bits a => a -> a -> a
.&. Word32
0x7f80_0000 forall a. Eq a => a -> a -> Bool
== Word32
0x7f80_0000
, Word32
w forall a. Eq a => a -> a -> Bool
/= Word32
0xff80_0000 -> Float
x forall a. Num a => a -> a -> a
+ Float
x
Word32
0x8000_0000 -> forall a. RealFloat a => a
minPositive
Word32
w | forall a. Bits a => a -> Int -> Bool
testBit Word32
w Int
31 -> Word32 -> Float
castWord32ToFloat (Word32
w forall a. Num a => a -> a -> a
- Word32
1)
| Bool
otherwise -> Word32 -> Float
castWord32ToFloat (Word32
w forall a. Num a => a -> a -> a
+ Word32
1)
where
!() = if Bool
isFloatBinary32 then () else forall a. HasCallStack => [Char] -> a
error [Char]
"Numeric.Floating.IEEE assumes Float is IEEE binary32"
nextUpDouble :: Double -> Double
nextUpDouble :: Double -> Double
nextUpDouble Double
x =
case Double -> Word64
castDoubleToWord64 Double
x of
Word64
w | Word64
w forall a. Bits a => a -> a -> a
.&. Word64
0x7ff0_0000_0000_0000 forall a. Eq a => a -> a -> Bool
== Word64
0x7ff0_0000_0000_0000
, Word64
w forall a. Eq a => a -> a -> Bool
/= Word64
0xfff0_0000_0000_0000 -> Double
x forall a. Num a => a -> a -> a
+ Double
x
Word64
0x8000_0000_0000_0000 -> forall a. RealFloat a => a
minPositive
Word64
w | forall a. Bits a => a -> Int -> Bool
testBit Word64
w Int
63 -> Word64 -> Double
castWord64ToDouble (Word64
w forall a. Num a => a -> a -> a
- Word64
1)
| Bool
otherwise -> Word64 -> Double
castWord64ToDouble (Word64
w forall a. Num a => a -> a -> a
+ Word64
1)
where
!() = if Bool
isDoubleBinary64 then () else forall a. HasCallStack => [Char] -> a
error [Char]
"Numeric.Floating.IEEE assumes Double is IEEE binary64"
nextDownFloat :: Float -> Float
nextDownFloat :: Float -> Float
nextDownFloat Float
x =
case Float -> Word32
castFloatToWord32 Float
x of
Word32
w | Word32
w forall a. Bits a => a -> a -> a
.&. Word32
0x7f80_0000 forall a. Eq a => a -> a -> Bool
== Word32
0x7f80_0000
, Word32
w forall a. Eq a => a -> a -> Bool
/= Word32
0x7f80_0000 -> Float
x forall a. Num a => a -> a -> a
+ Float
x
Word32
0x0000_0000 -> - forall a. RealFloat a => a
minPositive
Word32
w | forall a. Bits a => a -> Int -> Bool
testBit Word32
w Int
31 -> Word32 -> Float
castWord32ToFloat (Word32
w forall a. Num a => a -> a -> a
+ Word32
1)
| Bool
otherwise -> Word32 -> Float
castWord32ToFloat (Word32
w forall a. Num a => a -> a -> a
- Word32
1)
where
!() = if Bool
isFloatBinary32 then () else forall a. HasCallStack => [Char] -> a
error [Char]
"Numeric.Floating.IEEE assumes Float is IEEE binary32"
nextDownDouble :: Double -> Double
nextDownDouble :: Double -> Double
nextDownDouble Double
x =
case Double -> Word64
castDoubleToWord64 Double
x of
Word64
w | Word64
w forall a. Bits a => a -> a -> a
.&. Word64
0x7ff0_0000_0000_0000 forall a. Eq a => a -> a -> Bool
== Word64
0x7ff0_0000_0000_0000
, Word64
w forall a. Eq a => a -> a -> Bool
/= Word64
0x7ff0_0000_0000_0000 -> Double
x forall a. Num a => a -> a -> a
+ Double
x
Word64
0x0000_0000_0000_0000 -> - forall a. RealFloat a => a
minPositive
Word64
w | forall a. Bits a => a -> Int -> Bool
testBit Word64
w Int
63 -> Word64 -> Double
castWord64ToDouble (Word64
w forall a. Num a => a -> a -> a
+ Word64
1)
| Bool
otherwise -> Word64 -> Double
castWord64ToDouble (Word64
w forall a. Num a => a -> a -> a
- Word64
1)
where
!() = if Bool
isDoubleBinary64 then () else forall a. HasCallStack => [Char] -> a
error [Char]
"Numeric.Floating.IEEE assumes Double is IEEE binary64"
nextTowardZeroFloat :: Float -> Float
nextTowardZeroFloat :: Float -> Float
nextTowardZeroFloat Float
x =
case Float -> Word32
castFloatToWord32 Float
x of
Word32
w | Word32
w forall a. Bits a => a -> a -> a
.&. Word32
0x7f80_0000 forall a. Eq a => a -> a -> Bool
== Word32
0x7f80_0000
, Word32
w forall a. Bits a => a -> a -> a
.&. Word32
0x007f_ffff forall a. Eq a => a -> a -> Bool
/= Word32
0 -> Float
x forall a. Num a => a -> a -> a
+ Float
x
Word32
0x8000_0000 -> Float
x
Word32
0x0000_0000 -> Float
x
Word32
w -> Word32 -> Float
castWord32ToFloat (Word32
w forall a. Num a => a -> a -> a
- Word32
1)
where
!() = if Bool
isFloatBinary32 then () else forall a. HasCallStack => [Char] -> a
error [Char]
"Numeric.Floating.IEEE assumes Float is IEEE binary32"
nextTowardZeroDouble :: Double -> Double
nextTowardZeroDouble :: Double -> Double
nextTowardZeroDouble Double
x =
case Double -> Word64
castDoubleToWord64 Double
x of
Word64
w | Word64
w forall a. Bits a => a -> a -> a
.&. Word64
0x7ff0_0000_0000_0000 forall a. Eq a => a -> a -> Bool
== Word64
0x7ff0_0000_0000_0000
, Word64
w forall a. Bits a => a -> a -> a
.&. Word64
0x000f_ffff_ffff_ffff forall a. Eq a => a -> a -> Bool
/= Word64
0 -> Double
x forall a. Num a => a -> a -> a
+ Double
x
Word64
0x8000_0000_0000_0000 -> Double
x
Word64
0x0000_0000_0000_0000 -> Double
x
Word64
w -> Word64 -> Double
castWord64ToDouble (Word64
w forall a. Num a => a -> a -> a
- Word64
1)
where
!() = if Bool
isDoubleBinary64 then () else forall a. HasCallStack => [Char] -> a
error [Char]
"Numeric.Floating.IEEE assumes Double is IEEE binary64"