Skip to content

Commit 70176c8

Browse files
committed
Add opaque types for Double and Float
1 parent 98bbc62 commit 70176c8

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import stainless.math._
2+
3+
object FPFledging {
4+
def test1(x: Double): Unit = {
5+
assert(Double.longBitsToDouble(Double.doubleToRawLongBits(x)) == x)
6+
}
7+
8+
def test2(x: Long): Unit = {
9+
Double.longBitsToDoublePost(x)
10+
assert(Double.doubleToRawLongBits(Double.longBitsToDouble(x)) == x)
11+
}
12+
13+
def test3(x: Float): Unit = {
14+
assert(Float.intBitsToFloat(Float.floatToRawIntBits(x)) == x)
15+
}
16+
17+
def test4(x: Int): Unit = {
18+
Float.intBitsToFloatPost(x)
19+
assert(Float.floatToRawIntBits(Float.intBitsToFloat(x)) == x)
20+
}
21+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package stainless
2+
package math
3+
4+
import lang.StaticChecks._
5+
import annotation._
6+
7+
@library
8+
case class Double(@extern @pure private val value: scala.Double) extends AnyVal
9+
10+
@library
11+
object Double {
12+
@extern @pure
13+
def longBitsToDouble(l: Long): Double = {
14+
Double(java.lang.Double.longBitsToDouble(l))
15+
}
16+
17+
@extern @pure
18+
def longBitsToDoublePost(l: Long): Unit = {
19+
()
20+
} ensuring (_ => doubleToRawLongBits(longBitsToDouble(l)) == l)
21+
22+
@extern @pure
23+
def doubleToRawLongBits(d: Double): Long = {
24+
java.lang.Double.doubleToRawLongBits(d.value)
25+
} ensuring (res => longBitsToDouble(res) == d)
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package stainless
2+
package math
3+
4+
import lang.StaticChecks._
5+
import annotation._
6+
7+
@library
8+
case class Float(@extern @pure private val value: scala.Float) extends AnyVal
9+
10+
@library
11+
object Float {
12+
@extern @pure
13+
def intBitsToFloat(i: Int): Float = {
14+
Float(java.lang.Float.intBitsToFloat(i))
15+
}
16+
17+
@extern @pure
18+
def intBitsToFloatPost(i: Int): Unit = {
19+
()
20+
} ensuring (_ => floatToRawIntBits(intBitsToFloat(i)) == i)
21+
22+
@extern @pure
23+
def floatToRawIntBits(d: Float): Int = {
24+
java.lang.Float.floatToRawIntBits(d.value)
25+
} ensuring (res => intBitsToFloat(res) == d)
26+
}

0 commit comments

Comments
 (0)