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

Add opaque types for Double and Float #1413

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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