Skip to content

Commit

Permalink
Add opaque types for Double and Float
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-bucev committed May 8, 2023
1 parent 98bbc62 commit 20c49db
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
21 changes: 21 additions & 0 deletions frontends/benchmarks/verification/valid/FPFledging.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import stainless.math._

object FPFledging {
def test1(x: Double): Unit = {
assert(Double.longBitsToDouble(Double.doubleToRawLongBits(x)) == x)
}

def test2(x: Long): Unit = {
Double.longBitsToDoublePost(x)
assert(Double.doubleToRawLongBits(Double.longBitsToDouble(x)) == x)
}

def test3(x: Float): Unit = {
assert(Float.intBitsToFloat(Float.floatToRawIntBits(x)) == x)
}

def test4(x: Int): Unit = {
Float.intBitsToFloatPost(x)
assert(Float.floatToRawIntBits(Float.intBitsToFloat(x)) == x)
}
}
26 changes: 26 additions & 0 deletions frontends/library/stainless/math/Double.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package stainless
package math

import lang.StaticChecks._
import annotation._

@library
case class Double(@extern @pure private val value: scala.Double) extends AnyVal

@library
object Double {
@extern @pure
def longBitsToDouble(l: Long): Double = {
Double(java.lang.Double.longBitsToDouble(l))
}

@extern @pure
def longBitsToDoublePost(l: Long): Unit = {
()
} ensuring (_ => doubleToRawLongBits(longBitsToDouble(l)) == l)

@extern @pure
def doubleToRawLongBits(d: Double): Long = {
java.lang.Double.doubleToRawLongBits(d.value)
} ensuring (res => longBitsToDouble(res) == d)
}
26 changes: 26 additions & 0 deletions frontends/library/stainless/math/Float.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package stainless
package math

import lang.StaticChecks._
import annotation._

@library
case class Float(@extern @pure private val value: scala.Float) extends AnyVal

@library
object Float {
@extern @pure
def intBitsToFloat(i: Int): Float = {
Float(java.lang.Float.intBitsToFloat(i))
}

@extern @pure
def intBitsToFloatPost(i: Int): Unit = {
()
} ensuring (_ => floatToRawIntBits(intBitsToFloat(i)) == i)

@extern @pure
def floatToRawIntBits(d: Float): Int = {
java.lang.Float.floatToRawIntBits(d.value)
} ensuring (res => intBitsToFloat(res) == d)
}
2 changes: 2 additions & 0 deletions libfiles.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ stainless/covcollection/Option.scala
stainless/equations/package.scala
stainless/math/BitVectors.scala
stainless/math/Nat.scala
stainless/math/Float.scala
stainless/math/Double.scala
stainless/math/package.scala
stainless/io/FileOutputStream.scala
stainless/io/StdOut.scala
Expand Down

0 comments on commit 20c49db

Please sign in to comment.