summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2018-05-13 01:15:17 -0700
committerOri Bernstein <ori@eigenstate.org>2018-05-13 01:15:17 -0700
commit7cd4f442cf238c5de486b54deb0bfd3a3f7a0516 (patch)
treeec9829057b700af1d34ec0c62470c308486beec7
parentf52ac3cb5ada4ec50019cf8a09c981c040f56e4d (diff)
downloadmc-7cd4f442cf238c5de486b54deb0bfd3a3f7a0516.tar.gz
Add float trapping controls.
This both allows us to error earlier when math goes wonky, and allows us to test all our code on 9front.
-rw-r--r--lib/math/bld.sub2
-rw-r--r--lib/math/ftrap-impl+plan9-x64.s14
-rw-r--r--lib/math/ftrap-impl+posixy-x64.s17
-rw-r--r--lib/math/ftrap.myr3
-rw-r--r--lib/math/test/exp-impl.myr1
-rw-r--r--lib/math/test/fma-impl.myr1
-rw-r--r--lib/math/test/sqrt-impl.myr1
-rw-r--r--lib/math/test/trunc-impl.myr1
-rw-r--r--lib/std/blat.myr8
-rw-r--r--lib/std/test/fltbits.myr72
10 files changed, 76 insertions, 44 deletions
diff --git a/lib/math/bld.sub b/lib/math/bld.sub
index 382ecef..df857d5 100644
--- a/lib/math/bld.sub
+++ b/lib/math/bld.sub
@@ -31,6 +31,8 @@ lib math =
# util
util.myr
+ ftrap.myr
+ ftrap-impl+plan9-x64.s
lib ../std:std
;;
diff --git a/lib/math/ftrap-impl+plan9-x64.s b/lib/math/ftrap-impl+plan9-x64.s
new file mode 100644
index 0000000..3374f4c
--- /dev/null
+++ b/lib/math/ftrap-impl+plan9-x64.s
@@ -0,0 +1,14 @@
+TEXT math$fptrap(SB),$0
+ SUBQ $4,SP
+ WAIT
+ STMXCSR (SP)
+ MOVL (SP),AX
+ ANDL $~0x1f80,AX
+ TESTB DI,DI
+ JNZ .apply
+ ORL $0x1f80,AX
+.apply:
+ MOVL AX,(SP)
+ LDMXCSR (SP)
+ ADDQ $4,SP
+ RET
diff --git a/lib/math/ftrap-impl+posixy-x64.s b/lib/math/ftrap-impl+posixy-x64.s
new file mode 100644
index 0000000..e713a9f
--- /dev/null
+++ b/lib/math/ftrap-impl+posixy-x64.s
@@ -0,0 +1,17 @@
+.globl _math$fptrap
+.globl math$fptrap
+_math$fptrap:
+math$fptrap
+ subq $4,%rsp
+ wait
+ stmxcsr (%rsp)
+ movl (%rsp),%rax
+ andl $~0x1f80,%rax
+ testb %rdi,%rdi
+ jnz .apply
+ orl $0x1f80,%rax
+.apply:
+ movl %rax,(rsp)
+ ldmxcsr (%rsp)
+ addq $4,%rsp
+ ret
diff --git a/lib/math/ftrap.myr b/lib/math/ftrap.myr
new file mode 100644
index 0000000..2be8970
--- /dev/null
+++ b/lib/math/ftrap.myr
@@ -0,0 +1,3 @@
+pkg math =
+ extern const fptrap : (f : bool -> void)
+;;
diff --git a/lib/math/test/exp-impl.myr b/lib/math/test/exp-impl.myr
index a009978..e84d5ab 100644
--- a/lib/math/test/exp-impl.myr
+++ b/lib/math/test/exp-impl.myr
@@ -7,6 +7,7 @@ use testr
are tested extensively in expm101 and expm102.
*/
const main = {
+ math.fptrap(false)
testr.run([
[.name="exp-01", .fn = exp01],
[.name="exp-02", .fn = exp02],
diff --git a/lib/math/test/fma-impl.myr b/lib/math/test/fma-impl.myr
index 0bb30bb..f5d7472 100644
--- a/lib/math/test/fma-impl.myr
+++ b/lib/math/test/fma-impl.myr
@@ -3,6 +3,7 @@ use math
use testr
const main = {
+ math.fptrap(false)
testr.run([
[.name="fma-01", .fn = fma01],
[.name="fma-02", .fn = fma02],
diff --git a/lib/math/test/sqrt-impl.myr b/lib/math/test/sqrt-impl.myr
index a84f15b..79765b0 100644
--- a/lib/math/test/sqrt-impl.myr
+++ b/lib/math/test/sqrt-impl.myr
@@ -3,6 +3,7 @@ use math
use testr
const main = {
+ math.fptrap(false)
testr.run([
[.name="sqrt-01", .fn = sqrt01],
[.name="sqrt-02", .fn = sqrt02],
diff --git a/lib/math/test/trunc-impl.myr b/lib/math/test/trunc-impl.myr
index d86f25d..328b8dd 100644
--- a/lib/math/test/trunc-impl.myr
+++ b/lib/math/test/trunc-impl.myr
@@ -3,6 +3,7 @@ use math
use testr
const main = {
+ math.fptrap(false)
testr.run([
[.name = "trunc-01", .fn = trunc01],
[.name = "trunc-02", .fn = trunc02],
diff --git a/lib/std/blat.myr b/lib/std/blat.myr
index f68f089..46a6e3e 100644
--- a/lib/std/blat.myr
+++ b/lib/std/blat.myr
@@ -8,8 +8,12 @@ pkg std =
const blat = {path, buf, perm
match openmode(path, Ocreat|Owrite, perm)
- | `Ok fd: -> fblat(fd, buf)
- | `Err e: -> false
+ | `Ok fd:
+ var r = fblat(fd, buf)
+ close(fd)
+ -> r
+ | `Err e:
+ -> false
;;
}
diff --git a/lib/std/test/fltbits.myr b/lib/std/test/fltbits.myr
index c517043..38d3a43 100644
--- a/lib/std/test/fltbits.myr
+++ b/lib/std/test/fltbits.myr
@@ -1,18 +1,9 @@
use std
use testr
-
-var testnan
+use math
const main = {
- var si
-
- /* Floating point mode traps on 9front. */
- std.getsysinfo(&si)
- match si.system
- | "Plan9": testnan = false
- | _: testnan = true
- ;;
-
+ math.fptrap(false)
testr.run([
[.name = "isnan", .fn = isnan01],
[.name = "bits-roundtrip-32", .fn = bitsround32],
@@ -25,26 +16,24 @@ const main = {
}
const isnan01 = {c
- if testnan
- testr.check(c, std.isnan(std.flt64nan()), "std.flt64nan() should give a NaN")
- testr.check(c, std.isnan(std.flt32nan()), "std.flt32nan() should give a NaN")
-
- /*
- a NaN should be {any sign bit}, then {8 or 11 exponent
- bits, all 1}, then {any non-zero sequence of 23 or 52
- bits}
- */
- testr.check(c, std.isnan(std.flt64frombits(0xfff0000500000000ul)), "0xfff0000500000000 should be a NaN")
- testr.check(c, std.isnan(std.flt64frombits(0x7ff0000500000000ul)), "0x7ff0000500000000 should be a NaN")
- testr.check(c, std.isnan(std.flt32frombits(0xff800090)), "0xff800090 should be a NaN")
- testr.check(c, std.isnan(std.flt32frombits(0x7f800090)), "0x7f800090 should be a NaN")
-
- /* if the significand bits are all 0, it's an infinity instead */
- testr.check(c, !std.isnan(std.flt64frombits(0x7ff0000000000000ul)), "Infinities[1] should not be NaNs")
- testr.check(c, !std.isnan(std.flt64frombits(0xfff0000000000000ul)), "Infinities[2] should not be NaNs")
- testr.check(c, !std.isnan(std.flt32frombits(0xff800000)), "Infinities[3] should not be NaNs")
- testr.check(c, !std.isnan(std.flt32frombits(0x7f800000)), "Infinities[4] should not be NaNs")
- ;;
+ testr.check(c, std.isnan(std.flt64nan()), "std.flt64nan() should give a NaN")
+ testr.check(c, std.isnan(std.flt32nan()), "std.flt32nan() should give a NaN")
+
+ /*
+ a NaN should be {any sign bit}, then {8 or 11 exponent
+ bits, all 1}, then {any non-zero sequence of 23 or 52
+ bits}
+ */
+ testr.check(c, std.isnan(std.flt64frombits(0xfff0000500000000ul)), "0xfff0000500000000 should be a NaN")
+ testr.check(c, std.isnan(std.flt64frombits(0x7ff0000500000000ul)), "0x7ff0000500000000 should be a NaN")
+ testr.check(c, std.isnan(std.flt32frombits(0xff800090)), "0xff800090 should be a NaN")
+ testr.check(c, std.isnan(std.flt32frombits(0x7f800090)), "0x7f800090 should be a NaN")
+
+ /* if the significand bits are all 0, it's an infinity instead */
+ testr.check(c, !std.isnan(std.flt64frombits(0x7ff0000000000000ul)), "Infinities[1] should not be NaNs")
+ testr.check(c, !std.isnan(std.flt64frombits(0xfff0000000000000ul)), "Infinities[2] should not be NaNs")
+ testr.check(c, !std.isnan(std.flt32frombits(0xff800000)), "Infinities[3] should not be NaNs")
+ testr.check(c, !std.isnan(std.flt32frombits(0x7f800000)), "Infinities[4] should not be NaNs")
}
const bitsround32 = {c
@@ -58,11 +47,9 @@ const bitsround32 = {c
testr.check(c, u == v, "bits -> flt -> bits non-identity: {} != {}", u, v)
;;
- if testnan
- var nan_f = std.flt32frombits(0xff800090)
- var nan_g = std.flt32frombits(std.flt32bits(nan_f))
- testr.check(c, nan_f == nan_g, "flt -> bits -> flt non-identity for nan")
- ;;
+ var nan_f = std.flt32frombits(0xff800090)
+ var nan_g = std.flt32frombits(std.flt32bits(nan_f))
+ testr.check(c, nan_f == nan_g, "flt -> bits -> flt non-identity for nan")
var inf_f = std.flt32frombits(0x7f800000)
var inf_g = std.flt32frombits(std.flt32bits(inf_f))
@@ -80,11 +67,9 @@ const bitsround64 = {c
testr.check(c, u == v, "bits -> flt -> bits non-identity: {} != {}", u, v)
;;
- if testnan
- var nan_f = std.flt64frombits(0x7ff000000000a000ul)
- var nan_g = std.flt64frombits(std.flt64bits(nan_f))
- testr.check(c, nan_f == nan_g, "flt -> bits -> flt non-identity for nan")
- ;;
+ var nan_f = std.flt64frombits(0x7ff000000000a000ul)
+ var nan_g = std.flt64frombits(std.flt64bits(nan_f))
+ testr.check(c, nan_f == nan_g, "flt -> bits -> flt non-identity for nan")
var inf_f = std.flt64frombits(0xfff0000000000000ul)
var inf_g = std.flt64frombits(std.flt64bits(inf_f))
@@ -117,7 +102,10 @@ const flt64bits = {c
}
const exploderound32 = {c
- for f : [1.0, 0.00001, 123.45, 1111111111111111.2, -1.9, -0.0001, 0.000000000000000000000000000000000000006054601, std.flt32nan()][:]
+ var vals
+ vals = [1.0, 0.00001, 123.45, 1111111111111111.2, -1.9, -0.0001, 0.000000000000000000000000000000000000006054601, std.flt32nan()][:]
+
+ for f : vals
var n, e, s
(n, e, s) = std.flt32explode(f)
var g = std.flt32assem(n, e, s)