Skip to content

Commit

Permalink
Allow :exhaust to work directly on floating-point values
Browse files Browse the repository at this point in the history
by enumerating all bitpatterns.

Note, this overcounts somewhat the number of distinct floating point
values there are, since all NaNs are considered identical
as `Float` values.  We could be more careful here to avoid
generating more NaN values than required, but I'm not sure it's worth
the effort.

Fixes #1051
  • Loading branch information
robdockins committed Feb 9, 2021
1 parent 38a9c58 commit b413294
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/Cryptol/Testing/Random.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import qualified Data.Sequence as Seq
import System.Random (RandomGen, split, random, randomR)

import Cryptol.Backend (Backend(..), SRational(..))
import Cryptol.Backend.FloatHelpers (floatFromBits)
import Cryptol.Backend.Monad (runEval,Eval,EvalErrorEx(..))
import Cryptol.Backend.Concrete

Expand Down Expand Up @@ -353,7 +354,7 @@ typeSize ty = case ty of
TVInteger -> Nothing
TVRational -> Nothing
TVIntMod n -> Just n
TVFloat{} -> Nothing -- TODO?
TVFloat e p -> Just (2 ^ (e+p))
TVArray{} -> Nothing
TVStream{} -> Nothing
TVSeq n el -> (^ n) <$> typeSize el
Expand All @@ -368,13 +369,13 @@ for types where 'typeSize' returned 'Nothing'. -}
typeValues :: TValue -> [Value]
typeValues ty =
case ty of
TVBit -> [ VBit False, VBit True ]
TVInteger -> []
TVRational -> []
TVIntMod n -> [ VInteger x | x <- [ 0 .. (n-1) ] ]
TVFloat{} -> [] -- TODO?
TVArray{} -> []
TVStream{} -> []
TVBit -> [ VBit False, VBit True ]
TVInteger -> []
TVRational -> []
TVIntMod n -> [ VInteger x | x <- [ 0 .. (n-1) ] ]
TVFloat e p -> [ VFloat (floatFromBits e p v) | v <- [0 .. 2^(e+p) - 1] ]
TVArray{} -> []
TVStream{} -> []
TVSeq n TVBit ->
[ VWord n (pure (WordVal (BV n x)))
| x <- [ 0 .. 2^n - 1 ]
Expand Down
1 change: 1 addition & 0 deletions tests/issues/issue1049.icry
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
:m Float
:exhaust (\x -> (y != y \/ x == fpToBits y where y = fpFromBits x : Float16))
:exhaust (\x -> (x : Float16) =.= fpFromBits (fpToBits x))
3 changes: 3 additions & 0 deletions tests/issues/issue1049.icry.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ Loading module Float
Using exhaustive testing.
Testing... Passed 65536 tests.
Q.E.D.
Using exhaustive testing.
Testing... Passed 65536 tests.
Q.E.D.

0 comments on commit b413294

Please sign in to comment.