1 USING: kernel math math.floats.env math.floats.env.private
2 math.functions math.libm sequences tools.test locals
3 compiler.units kernel.private fry compiler math.private words
5 IN: math.floats.env.tests
7 : set-default-fp-env ( -- )
8 { } { } +round-nearest+ +denormal-keep+ set-fp-env ;
10 ! In case the tests screw up the FP env because of bugs in math.floats.env
13 : test-fp-exception ( exception inputs quot -- quot' )
14 '[ _ [ @ @ ] collect-fp-exceptions nip member? ] ;
16 : test-fp-exception-compiled ( exception inputs quot -- quot' )
17 '[ _ @ [ _ collect-fp-exceptions ] compile-call nip member? ] ;
19 [ t ] +fp-zero-divide+ [ 1.0 0.0 ] [ /f ] test-fp-exception unit-test
20 [ t ] +fp-inexact+ [ 1.0 3.0 ] [ /f ] test-fp-exception unit-test
21 [ t ] +fp-overflow+ [ 1.0e250 1.0e100 ] [ * ] test-fp-exception unit-test
22 [ t ] +fp-underflow+ [ 1.0e-250 1.0e-100 ] [ * ] test-fp-exception unit-test
23 [ t ] +fp-overflow+ [ 2.0 100,000.0 ] [ fpow ] test-fp-exception unit-test
24 [ t ] +fp-invalid-operation+ [ 0.0 0.0 ] [ /f ] test-fp-exception unit-test
25 [ t ] +fp-invalid-operation+ [ -1.0 ] [ fsqrt ] test-fp-exception unit-test
27 [ t ] +fp-zero-divide+ [ 1.0 0.0 ] [ /f ] test-fp-exception-compiled unit-test
28 [ t ] +fp-inexact+ [ 1.0 3.0 ] [ /f ] test-fp-exception-compiled unit-test
29 [ t ] +fp-overflow+ [ 1.0e250 1.0e100 ] [ * ] test-fp-exception-compiled unit-test
30 [ t ] +fp-underflow+ [ 1.0e-250 1.0e-100 ] [ * ] test-fp-exception-compiled unit-test
31 [ t ] +fp-overflow+ [ 2.0 100,000.0 ] [ fpow ] test-fp-exception-compiled unit-test
33 ! No underflow on Linux with this test, just inexact. Reported as an Ubuntu bug:
34 ! https://bugs.launchpad.net/ubuntu/+source/glibc/+bug/429113
35 os linux? cpu x86.64? and [
36 [ t ] +fp-underflow+ [ 2.0 -100,000.0 ] [ fpow ] test-fp-exception unit-test
37 [ t ] +fp-underflow+ [ 2.0 -100,000.0 ] [ fpow ] test-fp-exception-compiled unit-test
40 [ t ] +fp-invalid-operation+ [ 0.0 0.0 ] [ /f ] test-fp-exception-compiled unit-test
41 [ t ] +fp-invalid-operation+ [ -1.0 ] [ fsqrt ] test-fp-exception-compiled unit-test
44 HEX: 3fd5,5555,5555,5555
45 HEX: 3fc9,9999,9999,999a
46 HEX: bfc9,9999,9999,999a
47 HEX: bfd5,5555,5555,5555
50 1.0 3.0 /f double>bits
51 1.0 5.0 /f double>bits
52 -1.0 5.0 /f double>bits
53 -1.0 3.0 /f double>bits
58 HEX: 3fd5,5555,5555,5555
59 HEX: 3fc9,9999,9999,9999
60 HEX: bfc9,9999,9999,999a
61 HEX: bfd5,5555,5555,5556
64 1.0 3.0 /f double>bits
65 1.0 5.0 /f double>bits
66 -1.0 5.0 /f double>bits
67 -1.0 3.0 /f double>bits
72 HEX: 3fd5,5555,5555,5556
73 HEX: 3fc9,9999,9999,999a
74 HEX: bfc9,9999,9999,9999
75 HEX: bfd5,5555,5555,5555
78 1.0 3.0 /f double>bits
79 1.0 5.0 /f double>bits
80 -1.0 5.0 /f double>bits
81 -1.0 3.0 /f double>bits
86 HEX: 3fd5,5555,5555,5555
87 HEX: 3fc9,9999,9999,9999
88 HEX: bfc9,9999,9999,9999
89 HEX: bfd5,5555,5555,5555
92 1.0 3.0 /f double>bits
93 1.0 5.0 /f double>bits
94 -1.0 5.0 /f double>bits
95 -1.0 3.0 /f double>bits
99 ! ensure rounding mode is restored to +round-nearest+
101 HEX: 3fd5,5555,5555,5555
102 HEX: 3fc9,9999,9999,999a
103 HEX: bfc9,9999,9999,999a
104 HEX: bfd5,5555,5555,5555
106 1.0 3.0 /f double>bits
107 1.0 5.0 /f double>bits
108 -1.0 5.0 /f double>bits
109 -1.0 3.0 /f double>bits
112 : test-traps ( traps inputs quot -- quot' )
113 append '[ _ _ with-fp-traps ] ;
115 : test-traps-compiled ( traps inputs quot -- quot' )
116 swapd '[ @ [ _ _ with-fp-traps ] compile-call ] ;
118 { +fp-zero-divide+ } [ 1.0 0.0 ] [ /f ] test-traps must-fail
119 { +fp-inexact+ } [ 1.0 3.0 ] [ /f ] test-traps must-fail
120 { +fp-invalid-operation+ } [ -1.0 ] [ fsqrt ] test-traps must-fail
121 { +fp-overflow+ } [ 2.0 ] [ 100,000.0 ^ ] test-traps must-fail
122 { +fp-underflow+ +fp-inexact+ } [ 2.0 ] [ -100,000.0 ^ ] test-traps must-fail
124 { +fp-zero-divide+ } [ 1.0 0.0 ] [ /f ] test-traps-compiled must-fail
125 { +fp-inexact+ } [ 1.0 3.0 ] [ /f ] test-traps-compiled must-fail
126 { +fp-invalid-operation+ } [ -1.0 ] [ fsqrt ] test-traps-compiled must-fail
127 { +fp-overflow+ } [ 2.0 ] [ 100,000.0 ^ ] test-traps-compiled must-fail
128 { +fp-underflow+ +fp-inexact+ } [ 2.0 ] [ -100,000.0 ^ ] test-traps-compiled must-fail
130 ! Ensure ordered comparisons raise traps
131 :: test-comparison-quot ( word -- quot )
133 { float float } declare
134 { +fp-invalid-operation+ } [ word execute ] with-fp-traps
137 : test-comparison ( inputs word -- quot )
138 test-comparison-quot append ;
140 : test-comparison-compiled ( inputs word -- quot )
141 test-comparison-quot '[ @ _ compile-call ] ;
143 \ float< "intrinsic" word-prop [
144 [ 0/0. -15.0 ] \ < test-comparison must-fail
145 [ 0/0. -15.0 ] \ < test-comparison-compiled must-fail
146 [ -15.0 0/0. ] \ < test-comparison must-fail
147 [ -15.0 0/0. ] \ < test-comparison-compiled must-fail
148 [ 0/0. -15.0 ] \ <= test-comparison must-fail
149 [ 0/0. -15.0 ] \ <= test-comparison-compiled must-fail
150 [ -15.0 0/0. ] \ <= test-comparison must-fail
151 [ -15.0 0/0. ] \ <= test-comparison-compiled must-fail
152 [ 0/0. -15.0 ] \ > test-comparison must-fail
153 [ 0/0. -15.0 ] \ > test-comparison-compiled must-fail
154 [ -15.0 0/0. ] \ > test-comparison must-fail
155 [ -15.0 0/0. ] \ > test-comparison-compiled must-fail
156 [ 0/0. -15.0 ] \ >= test-comparison must-fail
157 [ 0/0. -15.0 ] \ >= test-comparison-compiled must-fail
158 [ -15.0 0/0. ] \ >= test-comparison must-fail
159 [ -15.0 0/0. ] \ >= test-comparison-compiled must-fail
161 [ f ] [ 0/0. -15.0 ] \ u< test-comparison unit-test
162 [ f ] [ 0/0. -15.0 ] \ u< test-comparison-compiled unit-test
163 [ f ] [ -15.0 0/0. ] \ u< test-comparison unit-test
164 [ f ] [ -15.0 0/0. ] \ u< test-comparison-compiled unit-test
165 [ f ] [ 0/0. -15.0 ] \ u<= test-comparison unit-test
166 [ f ] [ 0/0. -15.0 ] \ u<= test-comparison-compiled unit-test
167 [ f ] [ -15.0 0/0. ] \ u<= test-comparison unit-test
168 [ f ] [ -15.0 0/0. ] \ u<= test-comparison-compiled unit-test
169 [ f ] [ 0/0. -15.0 ] \ u> test-comparison unit-test
170 [ f ] [ 0/0. -15.0 ] \ u> test-comparison-compiled unit-test
171 [ f ] [ -15.0 0/0. ] \ u> test-comparison unit-test
172 [ f ] [ -15.0 0/0. ] \ u> test-comparison-compiled unit-test
173 [ f ] [ 0/0. -15.0 ] \ u>= test-comparison unit-test
174 [ f ] [ 0/0. -15.0 ] \ u>= test-comparison-compiled unit-test
175 [ f ] [ -15.0 0/0. ] \ u>= test-comparison unit-test
176 [ f ] [ -15.0 0/0. ] \ u>= test-comparison-compiled unit-test
179 ! Ensure traps get cleared
180 [ 1/0. ] [ 1.0 0.0 /f ] unit-test
182 ! Ensure state is back to normal
183 [ +round-nearest+ ] [ rounding-mode ] unit-test
184 [ +denormal-keep+ ] [ denormal-mode ] unit-test
185 [ { } ] [ fp-traps ] unit-test
187 ! In case the tests screw up the FP env because of bugs in math.floats.env