1
2
3
4
5 package test
6
7 import (
8 "math"
9 "testing"
10 )
11
12
13 func compare1(a, b float64) bool {
14 return a < b
15 }
16
17
18 func compare2(a, b float32) bool {
19 return a < b
20 }
21
22 func TestFloatCompare(t *testing.T) {
23 if !compare1(3, 5) {
24 t.Errorf("compare1 returned false")
25 }
26 if !compare2(3, 5) {
27 t.Errorf("compare2 returned false")
28 }
29 }
30
31 func TestFloatCompareFolded(t *testing.T) {
32
33 d1, d3, d5, d9 := float64(1), float64(3), float64(5), float64(9)
34 if d3 == d5 {
35 t.Errorf("d3 == d5 returned true")
36 }
37 if d3 != d3 {
38 t.Errorf("d3 != d3 returned true")
39 }
40 if d3 > d5 {
41 t.Errorf("d3 > d5 returned true")
42 }
43 if d3 >= d9 {
44 t.Errorf("d3 >= d9 returned true")
45 }
46 if d5 < d1 {
47 t.Errorf("d5 < d1 returned true")
48 }
49 if d9 <= d1 {
50 t.Errorf("d9 <= d1 returned true")
51 }
52 if math.NaN() == math.NaN() {
53 t.Errorf("math.NaN() == math.NaN() returned true")
54 }
55 if math.NaN() >= math.NaN() {
56 t.Errorf("math.NaN() >= math.NaN() returned true")
57 }
58 if math.NaN() <= math.NaN() {
59 t.Errorf("math.NaN() <= math.NaN() returned true")
60 }
61 if math.Copysign(math.NaN(), -1) < math.NaN() {
62 t.Errorf("math.Copysign(math.NaN(), -1) < math.NaN() returned true")
63 }
64 if math.Inf(1) != math.Inf(1) {
65 t.Errorf("math.Inf(1) != math.Inf(1) returned true")
66 }
67 if math.Inf(-1) != math.Inf(-1) {
68 t.Errorf("math.Inf(-1) != math.Inf(-1) returned true")
69 }
70 if math.Copysign(0, -1) != 0 {
71 t.Errorf("math.Copysign(0, -1) != 0 returned true")
72 }
73 if math.Copysign(0, -1) < 0 {
74 t.Errorf("math.Copysign(0, -1) < 0 returned true")
75 }
76 if 0 > math.Copysign(0, -1) {
77 t.Errorf("0 > math.Copysign(0, -1) returned true")
78 }
79
80
81 s1, s3, s5, s9 := float32(1), float32(3), float32(5), float32(9)
82 if s3 == s5 {
83 t.Errorf("s3 == s5 returned true")
84 }
85 if s3 != s3 {
86 t.Errorf("s3 != s3 returned true")
87 }
88 if s3 > s5 {
89 t.Errorf("s3 > s5 returned true")
90 }
91 if s3 >= s9 {
92 t.Errorf("s3 >= s9 returned true")
93 }
94 if s5 < s1 {
95 t.Errorf("s5 < s1 returned true")
96 }
97 if s9 <= s1 {
98 t.Errorf("s9 <= s1 returned true")
99 }
100 sPosNaN, sNegNaN := float32(math.NaN()), float32(math.Copysign(math.NaN(), -1))
101 if sPosNaN == sPosNaN {
102 t.Errorf("sPosNaN == sPosNaN returned true")
103 }
104 if sPosNaN >= sPosNaN {
105 t.Errorf("sPosNaN >= sPosNaN returned true")
106 }
107 if sPosNaN <= sPosNaN {
108 t.Errorf("sPosNaN <= sPosNaN returned true")
109 }
110 if sNegNaN < sPosNaN {
111 t.Errorf("sNegNaN < sPosNaN returned true")
112 }
113 sPosInf, sNegInf := float32(math.Inf(1)), float32(math.Inf(-1))
114 if sPosInf != sPosInf {
115 t.Errorf("sPosInf != sPosInf returned true")
116 }
117 if sNegInf != sNegInf {
118 t.Errorf("sNegInf != sNegInf returned true")
119 }
120 sNegZero := float32(math.Copysign(0, -1))
121 if sNegZero != 0 {
122 t.Errorf("sNegZero != 0 returned true")
123 }
124 if sNegZero < 0 {
125 t.Errorf("sNegZero < 0 returned true")
126 }
127 if 0 > sNegZero {
128 t.Errorf("0 > sNegZero returned true")
129 }
130 }
131
132
133 func cvt1(a float64) uint64 {
134 return uint64(a)
135 }
136
137
138 func cvt2(a float64) uint32 {
139 return uint32(a)
140 }
141
142
143 func cvt3(a float32) uint64 {
144 return uint64(a)
145 }
146
147
148 func cvt4(a float32) uint32 {
149 return uint32(a)
150 }
151
152
153 func cvt5(a float64) int64 {
154 return int64(a)
155 }
156
157
158 func cvt6(a float64) int32 {
159 return int32(a)
160 }
161
162
163 func cvt7(a float32) int64 {
164 return int64(a)
165 }
166
167
168 func cvt8(a float32) int32 {
169 return int32(a)
170 }
171
172
173
174 func cvt9(a float64) int {
175 return int(a)
176 }
177
178
179 func cvt10(a float64) uint {
180 return uint(a)
181 }
182
183
184 func cvt11(a float32) int {
185 return int(a)
186 }
187
188
189 func cvt12(a float32) uint {
190 return uint(a)
191 }
192
193
194 func f2i64p(v float64) *int64 {
195 return ip64(int64(v / 0.1))
196 }
197
198
199 func ip64(v int64) *int64 {
200 return &v
201 }
202
203 func TestFloatConvert(t *testing.T) {
204 if got := cvt1(3.5); got != 3 {
205 t.Errorf("cvt1 got %d, wanted 3", got)
206 }
207 if got := cvt2(3.5); got != 3 {
208 t.Errorf("cvt2 got %d, wanted 3", got)
209 }
210 if got := cvt3(3.5); got != 3 {
211 t.Errorf("cvt3 got %d, wanted 3", got)
212 }
213 if got := cvt4(3.5); got != 3 {
214 t.Errorf("cvt4 got %d, wanted 3", got)
215 }
216 if got := cvt5(3.5); got != 3 {
217 t.Errorf("cvt5 got %d, wanted 3", got)
218 }
219 if got := cvt6(3.5); got != 3 {
220 t.Errorf("cvt6 got %d, wanted 3", got)
221 }
222 if got := cvt7(3.5); got != 3 {
223 t.Errorf("cvt7 got %d, wanted 3", got)
224 }
225 if got := cvt8(3.5); got != 3 {
226 t.Errorf("cvt8 got %d, wanted 3", got)
227 }
228 if got := cvt9(3.5); got != 3 {
229 t.Errorf("cvt9 got %d, wanted 3", got)
230 }
231 if got := cvt10(3.5); got != 3 {
232 t.Errorf("cvt10 got %d, wanted 3", got)
233 }
234 if got := cvt11(3.5); got != 3 {
235 t.Errorf("cvt11 got %d, wanted 3", got)
236 }
237 if got := cvt12(3.5); got != 3 {
238 t.Errorf("cvt12 got %d, wanted 3", got)
239 }
240 if got := *f2i64p(10); got != 100 {
241 t.Errorf("f2i64p got %d, wanted 100", got)
242 }
243 }
244
245 func TestFloatConvertFolded(t *testing.T) {
246
247
248 u64, u32, u16, u8 := uint64(1<<63), uint32(1<<31), uint16(1<<15), uint8(1<<7)
249 i64, i32, i16, i8 := int64(-1<<63), int32(-1<<31), int16(-1<<15), int8(-1<<7)
250 du64, du32, du16, du8 := float64(1<<63), float64(1<<31), float64(1<<15), float64(1<<7)
251 di64, di32, di16, di8 := float64(-1<<63), float64(-1<<31), float64(-1<<15), float64(-1<<7)
252 su64, su32, su16, su8 := float32(1<<63), float32(1<<31), float32(1<<15), float32(1<<7)
253 si64, si32, si16, si8 := float32(-1<<63), float32(-1<<31), float32(-1<<15), float32(-1<<7)
254
255
256 if float64(u64) != du64 {
257 t.Errorf("float64(u64) != du64")
258 }
259 if float64(u32) != du32 {
260 t.Errorf("float64(u32) != du32")
261 }
262 if float64(u16) != du16 {
263 t.Errorf("float64(u16) != du16")
264 }
265 if float64(u8) != du8 {
266 t.Errorf("float64(u8) != du8")
267 }
268 if float64(i64) != di64 {
269 t.Errorf("float64(i64) != di64")
270 }
271 if float64(i32) != di32 {
272 t.Errorf("float64(i32) != di32")
273 }
274 if float64(i16) != di16 {
275 t.Errorf("float64(i16) != di16")
276 }
277 if float64(i8) != di8 {
278 t.Errorf("float64(i8) != di8")
279 }
280 if float32(u64) != su64 {
281 t.Errorf("float32(u64) != su64")
282 }
283 if float32(u32) != su32 {
284 t.Errorf("float32(u32) != su32")
285 }
286 if float32(u16) != su16 {
287 t.Errorf("float32(u16) != su16")
288 }
289 if float32(u8) != su8 {
290 t.Errorf("float32(u8) != su8")
291 }
292 if float32(i64) != si64 {
293 t.Errorf("float32(i64) != si64")
294 }
295 if float32(i32) != si32 {
296 t.Errorf("float32(i32) != si32")
297 }
298 if float32(i16) != si16 {
299 t.Errorf("float32(i16) != si16")
300 }
301 if float32(i8) != si8 {
302 t.Errorf("float32(i8) != si8")
303 }
304
305
306 if uint64(du64) != u64 {
307 t.Errorf("uint64(du64) != u64")
308 }
309 if uint32(du32) != u32 {
310 t.Errorf("uint32(du32) != u32")
311 }
312 if uint16(du16) != u16 {
313 t.Errorf("uint16(du16) != u16")
314 }
315 if uint8(du8) != u8 {
316 t.Errorf("uint8(du8) != u8")
317 }
318 if int64(di64) != i64 {
319 t.Errorf("int64(di64) != i64")
320 }
321 if int32(di32) != i32 {
322 t.Errorf("int32(di32) != i32")
323 }
324 if int16(di16) != i16 {
325 t.Errorf("int16(di16) != i16")
326 }
327 if int8(di8) != i8 {
328 t.Errorf("int8(di8) != i8")
329 }
330 if uint64(su64) != u64 {
331 t.Errorf("uint64(su64) != u64")
332 }
333 if uint32(su32) != u32 {
334 t.Errorf("uint32(su32) != u32")
335 }
336 if uint16(su16) != u16 {
337 t.Errorf("uint16(su16) != u16")
338 }
339 if uint8(su8) != u8 {
340 t.Errorf("uint8(su8) != u8")
341 }
342 if int64(si64) != i64 {
343 t.Errorf("int64(si64) != i64")
344 }
345 if int32(si32) != i32 {
346 t.Errorf("int32(si32) != i32")
347 }
348 if int16(si16) != i16 {
349 t.Errorf("int16(si16) != i16")
350 }
351 if int8(si8) != i8 {
352 t.Errorf("int8(si8) != i8")
353 }
354 }
355
356 func TestFloat32StoreToLoadConstantFold(t *testing.T) {
357
358
359
360
361
362
363 {
364 const nan = uint32(0x7f800001)
365 if x := math.Float32bits(math.Float32frombits(nan)); x != nan {
366 t.Errorf("got %#x, want %#x", x, nan)
367 }
368 }
369 {
370 const nan = uint32(0x7fbfffff)
371 if x := math.Float32bits(math.Float32frombits(nan)); x != nan {
372 t.Errorf("got %#x, want %#x", x, nan)
373 }
374 }
375 {
376 const nan = uint32(0xff800001)
377 if x := math.Float32bits(math.Float32frombits(nan)); x != nan {
378 t.Errorf("got %#x, want %#x", x, nan)
379 }
380 }
381 {
382 const nan = uint32(0xffbfffff)
383 if x := math.Float32bits(math.Float32frombits(nan)); x != nan {
384 t.Errorf("got %#x, want %#x", x, nan)
385 }
386 }
387
388
389 {
390 const nan = uint32(0x7fc00000)
391 if x := math.Float32bits(math.Float32frombits(nan)); x != nan {
392 t.Errorf("got %#x, want %#x", x, nan)
393 }
394 }
395 {
396 const nan = uint32(0x7fffffff)
397 if x := math.Float32bits(math.Float32frombits(nan)); x != nan {
398 t.Errorf("got %#x, want %#x", x, nan)
399 }
400 }
401 {
402 const nan = uint32(0x8fc00000)
403 if x := math.Float32bits(math.Float32frombits(nan)); x != nan {
404 t.Errorf("got %#x, want %#x", x, nan)
405 }
406 }
407 {
408 const nan = uint32(0x8fffffff)
409 if x := math.Float32bits(math.Float32frombits(nan)); x != nan {
410 t.Errorf("got %#x, want %#x", x, nan)
411 }
412 }
413
414
415 {
416 const inf = uint32(0x7f800000)
417 if x := math.Float32bits(math.Float32frombits(inf)); x != inf {
418 t.Errorf("got %#x, want %#x", x, inf)
419 }
420 }
421 {
422 const negInf = uint32(0xff800000)
423 if x := math.Float32bits(math.Float32frombits(negInf)); x != negInf {
424 t.Errorf("got %#x, want %#x", x, negInf)
425 }
426 }
427
428
429 {
430 const zero = uint32(0)
431 if x := math.Float32bits(math.Float32frombits(zero)); x != zero {
432 t.Errorf("got %#x, want %#x", x, zero)
433 }
434 }
435 {
436 const negZero = uint32(1 << 31)
437 if x := math.Float32bits(math.Float32frombits(negZero)); x != negZero {
438 t.Errorf("got %#x, want %#x", x, negZero)
439 }
440 }
441 {
442 const one = uint32(0x3f800000)
443 if x := math.Float32bits(math.Float32frombits(one)); x != one {
444 t.Errorf("got %#x, want %#x", x, one)
445 }
446 }
447 {
448 const negOne = uint32(0xbf800000)
449 if x := math.Float32bits(math.Float32frombits(negOne)); x != negOne {
450 t.Errorf("got %#x, want %#x", x, negOne)
451 }
452 }
453 {
454 const frac = uint32(0x3fc00000)
455 if x := math.Float32bits(math.Float32frombits(frac)); x != frac {
456 t.Errorf("got %#x, want %#x", x, frac)
457 }
458 }
459 {
460 const negFrac = uint32(0xbfc00000)
461 if x := math.Float32bits(math.Float32frombits(negFrac)); x != negFrac {
462 t.Errorf("got %#x, want %#x", x, negFrac)
463 }
464 }
465 }
466
467
468 const (
469 snan32bits uint32 = 0x7f800001
470 snan64bits uint64 = 0x7ff0000000000001
471 )
472
473
474 var snan32bitsVar uint32 = snan32bits
475 var snan64bitsVar uint64 = snan64bits
476
477 func TestFloatSignalingNaN(t *testing.T) {
478
479
480 f32 := math.Float32frombits(snan32bits)
481 g32 := math.Float32frombits(snan32bitsVar)
482 x32 := math.Float32bits(f32)
483 y32 := math.Float32bits(g32)
484 if x32 != y32 {
485 t.Errorf("got %x, want %x (diff=%x)", x32, y32, x32^y32)
486 }
487
488 f64 := math.Float64frombits(snan64bits)
489 g64 := math.Float64frombits(snan64bitsVar)
490 x64 := math.Float64bits(f64)
491 y64 := math.Float64bits(g64)
492 if x64 != y64 {
493 t.Errorf("got %x, want %x (diff=%x)", x64, y64, x64^y64)
494 }
495 }
496
497 func TestFloatSignalingNaNConversion(t *testing.T) {
498
499
500
501 s32 := math.Float32frombits(snan32bitsVar)
502 if s32 == s32 {
503 t.Errorf("converting a NaN did not result in a NaN")
504 }
505 s64 := math.Float64frombits(snan64bitsVar)
506 if s64 == s64 {
507 t.Errorf("converting a NaN did not result in a NaN")
508 }
509 }
510
511 func TestFloatSignalingNaNConversionConst(t *testing.T) {
512
513
514
515 s32 := math.Float32frombits(snan32bits)
516 if s32 == s32 {
517 t.Errorf("converting a NaN did not result in a NaN")
518 }
519 s64 := math.Float64frombits(snan64bits)
520 if s64 == s64 {
521 t.Errorf("converting a NaN did not result in a NaN")
522 }
523 }
524
525 var sinkFloat float64
526
527 func BenchmarkMul2(b *testing.B) {
528 for i := 0; i < b.N; i++ {
529 var m float64 = 1
530 for j := 0; j < 500; j++ {
531 m *= 2
532 }
533 sinkFloat = m
534 }
535 }
536 func BenchmarkMulNeg2(b *testing.B) {
537 for i := 0; i < b.N; i++ {
538 var m float64 = 1
539 for j := 0; j < 500; j++ {
540 m *= -2
541 }
542 sinkFloat = m
543 }
544 }
545
View as plain text