File size: 20,025 Bytes
e36aeda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package test

import (
	"math"
	"testing"
)

//go:noinline
func compare1(a, b float64) bool {
	return a < b
}

//go:noinline
func compare2(a, b float32) bool {
	return a < b
}

func TestFloatCompare(t *testing.T) {
	if !compare1(3, 5) {
		t.Errorf("compare1 returned false")
	}
	if !compare2(3, 5) {
		t.Errorf("compare2 returned false")
	}
}

func TestFloatCompareFolded(t *testing.T) {
	// float64 comparisons
	d1, d3, d5, d9 := float64(1), float64(3), float64(5), float64(9)
	if d3 == d5 {
		t.Errorf("d3 == d5 returned true")
	}
	if d3 != d3 {
		t.Errorf("d3 != d3 returned true")
	}
	if d3 > d5 {
		t.Errorf("d3 > d5 returned true")
	}
	if d3 >= d9 {
		t.Errorf("d3 >= d9 returned true")
	}
	if d5 < d1 {
		t.Errorf("d5 < d1 returned true")
	}
	if d9 <= d1 {
		t.Errorf("d9 <= d1 returned true")
	}
	if math.NaN() == math.NaN() {
		t.Errorf("math.NaN() == math.NaN() returned true")
	}
	if math.NaN() >= math.NaN() {
		t.Errorf("math.NaN() >= math.NaN() returned true")
	}
	if math.NaN() <= math.NaN() {
		t.Errorf("math.NaN() <= math.NaN() returned true")
	}
	if math.Copysign(math.NaN(), -1) < math.NaN() {
		t.Errorf("math.Copysign(math.NaN(), -1) < math.NaN() returned true")
	}
	if math.Inf(1) != math.Inf(1) {
		t.Errorf("math.Inf(1) != math.Inf(1) returned true")
	}
	if math.Inf(-1) != math.Inf(-1) {
		t.Errorf("math.Inf(-1) != math.Inf(-1) returned true")
	}
	if math.Copysign(0, -1) != 0 {
		t.Errorf("math.Copysign(0, -1) != 0 returned true")
	}
	if math.Copysign(0, -1) < 0 {
		t.Errorf("math.Copysign(0, -1) < 0 returned true")
	}
	if 0 > math.Copysign(0, -1) {
		t.Errorf("0 > math.Copysign(0, -1) returned true")
	}

	// float32 comparisons
	s1, s3, s5, s9 := float32(1), float32(3), float32(5), float32(9)
	if s3 == s5 {
		t.Errorf("s3 == s5 returned true")
	}
	if s3 != s3 {
		t.Errorf("s3 != s3 returned true")
	}
	if s3 > s5 {
		t.Errorf("s3 > s5 returned true")
	}
	if s3 >= s9 {
		t.Errorf("s3 >= s9 returned true")
	}
	if s5 < s1 {
		t.Errorf("s5 < s1 returned true")
	}
	if s9 <= s1 {
		t.Errorf("s9 <= s1 returned true")
	}
	sPosNaN, sNegNaN := float32(math.NaN()), float32(math.Copysign(math.NaN(), -1))
	if sPosNaN == sPosNaN {
		t.Errorf("sPosNaN == sPosNaN returned true")
	}
	if sPosNaN >= sPosNaN {
		t.Errorf("sPosNaN >= sPosNaN returned true")
	}
	if sPosNaN <= sPosNaN {
		t.Errorf("sPosNaN <= sPosNaN returned true")
	}
	if sNegNaN < sPosNaN {
		t.Errorf("sNegNaN < sPosNaN returned true")
	}
	sPosInf, sNegInf := float32(math.Inf(1)), float32(math.Inf(-1))
	if sPosInf != sPosInf {
		t.Errorf("sPosInf != sPosInf returned true")
	}
	if sNegInf != sNegInf {
		t.Errorf("sNegInf != sNegInf returned true")
	}
	sNegZero := float32(math.Copysign(0, -1))
	if sNegZero != 0 {
		t.Errorf("sNegZero != 0 returned true")
	}
	if sNegZero < 0 {
		t.Errorf("sNegZero < 0 returned true")
	}
	if 0 > sNegZero {
		t.Errorf("0 > sNegZero returned true")
	}
}

//go:noinline
func cvt1(a float64) uint64 {
	return uint64(a)
}

//go:noinline
func cvt2(a float64) uint32 {
	return uint32(a)
}

//go:noinline
func cvt3(a float32) uint64 {
	return uint64(a)
}

//go:noinline
func cvt4(a float32) uint32 {
	return uint32(a)
}

//go:noinline
func cvt5(a float64) int64 {
	return int64(a)
}

//go:noinline
func cvt6(a float64) int32 {
	return int32(a)
}

//go:noinline
func cvt7(a float32) int64 {
	return int64(a)
}

//go:noinline
func cvt8(a float32) int32 {
	return int32(a)
}

// make sure to cover int, uint cases (issue #16738)
//
//go:noinline
func cvt9(a float64) int {
	return int(a)
}

//go:noinline
func cvt10(a float64) uint {
	return uint(a)
}

//go:noinline
func cvt11(a float32) int {
	return int(a)
}

//go:noinline
func cvt12(a float32) uint {
	return uint(a)
}

//go:noinline
func f2i64p(v float64) *int64 {
	return ip64(int64(v / 0.1))
}

//go:noinline
func ip64(v int64) *int64 {
	return &v
}

func TestFloatConvert(t *testing.T) {
	if got := cvt1(3.5); got != 3 {
		t.Errorf("cvt1 got %d, wanted 3", got)
	}
	if got := cvt2(3.5); got != 3 {
		t.Errorf("cvt2 got %d, wanted 3", got)
	}
	if got := cvt3(3.5); got != 3 {
		t.Errorf("cvt3 got %d, wanted 3", got)
	}
	if got := cvt4(3.5); got != 3 {
		t.Errorf("cvt4 got %d, wanted 3", got)
	}
	if got := cvt5(3.5); got != 3 {
		t.Errorf("cvt5 got %d, wanted 3", got)
	}
	if got := cvt6(3.5); got != 3 {
		t.Errorf("cvt6 got %d, wanted 3", got)
	}
	if got := cvt7(3.5); got != 3 {
		t.Errorf("cvt7 got %d, wanted 3", got)
	}
	if got := cvt8(3.5); got != 3 {
		t.Errorf("cvt8 got %d, wanted 3", got)
	}
	if got := cvt9(3.5); got != 3 {
		t.Errorf("cvt9 got %d, wanted 3", got)
	}
	if got := cvt10(3.5); got != 3 {
		t.Errorf("cvt10 got %d, wanted 3", got)
	}
	if got := cvt11(3.5); got != 3 {
		t.Errorf("cvt11 got %d, wanted 3", got)
	}
	if got := cvt12(3.5); got != 3 {
		t.Errorf("cvt12 got %d, wanted 3", got)
	}
	if got := *f2i64p(10); got != 100 {
		t.Errorf("f2i64p got %d, wanted 100", got)
	}
}

func TestFloatConvertFolded(t *testing.T) {
	// Assign constants to variables so that they are (hopefully) constant folded
	// by the SSA backend rather than the frontend.
	u64, u32, u16, u8 := uint64(1<<63), uint32(1<<31), uint16(1<<15), uint8(1<<7)
	i64, i32, i16, i8 := int64(-1<<63), int32(-1<<31), int16(-1<<15), int8(-1<<7)
	du64, du32, du16, du8 := float64(1<<63), float64(1<<31), float64(1<<15), float64(1<<7)
	di64, di32, di16, di8 := float64(-1<<63), float64(-1<<31), float64(-1<<15), float64(-1<<7)
	su64, su32, su16, su8 := float32(1<<63), float32(1<<31), float32(1<<15), float32(1<<7)
	si64, si32, si16, si8 := float32(-1<<63), float32(-1<<31), float32(-1<<15), float32(-1<<7)

	// integer to float
	if float64(u64) != du64 {
		t.Errorf("float64(u64) != du64")
	}
	if float64(u32) != du32 {
		t.Errorf("float64(u32) != du32")
	}
	if float64(u16) != du16 {
		t.Errorf("float64(u16) != du16")
	}
	if float64(u8) != du8 {
		t.Errorf("float64(u8) != du8")
	}
	if float64(i64) != di64 {
		t.Errorf("float64(i64) != di64")
	}
	if float64(i32) != di32 {
		t.Errorf("float64(i32) != di32")
	}
	if float64(i16) != di16 {
		t.Errorf("float64(i16) != di16")
	}
	if float64(i8) != di8 {
		t.Errorf("float64(i8) != di8")
	}
	if float32(u64) != su64 {
		t.Errorf("float32(u64) != su64")
	}
	if float32(u32) != su32 {
		t.Errorf("float32(u32) != su32")
	}
	if float32(u16) != su16 {
		t.Errorf("float32(u16) != su16")
	}
	if float32(u8) != su8 {
		t.Errorf("float32(u8) != su8")
	}
	if float32(i64) != si64 {
		t.Errorf("float32(i64) != si64")
	}
	if float32(i32) != si32 {
		t.Errorf("float32(i32) != si32")
	}
	if float32(i16) != si16 {
		t.Errorf("float32(i16) != si16")
	}
	if float32(i8) != si8 {
		t.Errorf("float32(i8) != si8")
	}

	// float to integer
	if uint64(du64) != u64 {
		t.Errorf("uint64(du64) != u64")
	}
	if uint32(du32) != u32 {
		t.Errorf("uint32(du32) != u32")
	}
	if uint16(du16) != u16 {
		t.Errorf("uint16(du16) != u16")
	}
	if uint8(du8) != u8 {
		t.Errorf("uint8(du8) != u8")
	}
	if int64(di64) != i64 {
		t.Errorf("int64(di64) != i64")
	}
	if int32(di32) != i32 {
		t.Errorf("int32(di32) != i32")
	}
	if int16(di16) != i16 {
		t.Errorf("int16(di16) != i16")
	}
	if int8(di8) != i8 {
		t.Errorf("int8(di8) != i8")
	}
	if uint64(su64) != u64 {
		t.Errorf("uint64(su64) != u64")
	}
	if uint32(su32) != u32 {
		t.Errorf("uint32(su32) != u32")
	}
	if uint16(su16) != u16 {
		t.Errorf("uint16(su16) != u16")
	}
	if uint8(su8) != u8 {
		t.Errorf("uint8(su8) != u8")
	}
	if int64(si64) != i64 {
		t.Errorf("int64(si64) != i64")
	}
	if int32(si32) != i32 {
		t.Errorf("int32(si32) != i32")
	}
	if int16(si16) != i16 {
		t.Errorf("int16(si16) != i16")
	}
	if int8(si8) != i8 {
		t.Errorf("int8(si8) != i8")
	}
}

func TestFloat32StoreToLoadConstantFold(t *testing.T) {
	// Test that math.Float32{,from}bits constant fold correctly.
	// In particular we need to be careful that signaling NaN (sNaN) values
	// are not converted to quiet NaN (qNaN) values during compilation.
	// See issue #27193 for more information.

	// signaling NaNs
	{
		const nan = uint32(0x7f800001) // sNaN
		if x := math.Float32bits(math.Float32frombits(nan)); x != nan {
			t.Errorf("got %#x, want %#x", x, nan)
		}
	}
	{
		const nan = uint32(0x7fbfffff) // sNaN
		if x := math.Float32bits(math.Float32frombits(nan)); x != nan {
			t.Errorf("got %#x, want %#x", x, nan)
		}
	}
	{
		const nan = uint32(0xff800001) // sNaN
		if x := math.Float32bits(math.Float32frombits(nan)); x != nan {
			t.Errorf("got %#x, want %#x", x, nan)
		}
	}
	{
		const nan = uint32(0xffbfffff) // sNaN
		if x := math.Float32bits(math.Float32frombits(nan)); x != nan {
			t.Errorf("got %#x, want %#x", x, nan)
		}
	}

	// quiet NaNs
	{
		const nan = uint32(0x7fc00000) // qNaN
		if x := math.Float32bits(math.Float32frombits(nan)); x != nan {
			t.Errorf("got %#x, want %#x", x, nan)
		}
	}
	{
		const nan = uint32(0x7fffffff) // qNaN
		if x := math.Float32bits(math.Float32frombits(nan)); x != nan {
			t.Errorf("got %#x, want %#x", x, nan)
		}
	}
	{
		const nan = uint32(0x8fc00000) // qNaN
		if x := math.Float32bits(math.Float32frombits(nan)); x != nan {
			t.Errorf("got %#x, want %#x", x, nan)
		}
	}
	{
		const nan = uint32(0x8fffffff) // qNaN
		if x := math.Float32bits(math.Float32frombits(nan)); x != nan {
			t.Errorf("got %#x, want %#x", x, nan)
		}
	}

	// infinities
	{
		const inf = uint32(0x7f800000) // +∞
		if x := math.Float32bits(math.Float32frombits(inf)); x != inf {
			t.Errorf("got %#x, want %#x", x, inf)
		}
	}
	{
		const negInf = uint32(0xff800000) // -∞
		if x := math.Float32bits(math.Float32frombits(negInf)); x != negInf {
			t.Errorf("got %#x, want %#x", x, negInf)
		}
	}

	// numbers
	{
		const zero = uint32(0) // +0.0
		if x := math.Float32bits(math.Float32frombits(zero)); x != zero {
			t.Errorf("got %#x, want %#x", x, zero)
		}
	}
	{
		const negZero = uint32(1 << 31) // -0.0
		if x := math.Float32bits(math.Float32frombits(negZero)); x != negZero {
			t.Errorf("got %#x, want %#x", x, negZero)
		}
	}
	{
		const one = uint32(0x3f800000) // 1.0
		if x := math.Float32bits(math.Float32frombits(one)); x != one {
			t.Errorf("got %#x, want %#x", x, one)
		}
	}
	{
		const negOne = uint32(0xbf800000) // -1.0
		if x := math.Float32bits(math.Float32frombits(negOne)); x != negOne {
			t.Errorf("got %#x, want %#x", x, negOne)
		}
	}
	{
		const frac = uint32(0x3fc00000) // +1.5
		if x := math.Float32bits(math.Float32frombits(frac)); x != frac {
			t.Errorf("got %#x, want %#x", x, frac)
		}
	}
	{
		const negFrac = uint32(0xbfc00000) // -1.5
		if x := math.Float32bits(math.Float32frombits(negFrac)); x != negFrac {
			t.Errorf("got %#x, want %#x", x, negFrac)
		}
	}
}

// Signaling NaN values as constants.
const (
	snan32bits uint32 = 0x7f800001
	snan64bits uint64 = 0x7ff0000000000001
)

// Signaling NaNs as variables.
var snan32bitsVar uint32 = snan32bits
var snan64bitsVar uint64 = snan64bits

func TestFloatSignalingNaN(t *testing.T) {
	// Make sure we generate a signaling NaN from a constant properly.
	// See issue 36400.
	f32 := math.Float32frombits(snan32bits)
	g32 := math.Float32frombits(snan32bitsVar)
	x32 := math.Float32bits(f32)
	y32 := math.Float32bits(g32)
	if x32 != y32 {
		t.Errorf("got %x, want %x (diff=%x)", x32, y32, x32^y32)
	}

	f64 := math.Float64frombits(snan64bits)
	g64 := math.Float64frombits(snan64bitsVar)
	x64 := math.Float64bits(f64)
	y64 := math.Float64bits(g64)
	if x64 != y64 {
		t.Errorf("got %x, want %x (diff=%x)", x64, y64, x64^y64)
	}
}

func TestFloatSignalingNaNConversion(t *testing.T) {
	// Test to make sure when we convert a signaling NaN, we get a NaN.
	// (Ideally we want a quiet NaN, but some platforms don't agree.)
	// See issue 36399.
	s32 := math.Float32frombits(snan32bitsVar)
	if s32 == s32 {
		t.Errorf("converting a NaN did not result in a NaN")
	}
	s64 := math.Float64frombits(snan64bitsVar)
	if s64 == s64 {
		t.Errorf("converting a NaN did not result in a NaN")
	}
}

func TestFloatSignalingNaNConversionConst(t *testing.T) {
	// Test to make sure when we convert a signaling NaN, it converts to a NaN.
	// (Ideally we want a quiet NaN, but some platforms don't agree.)
	// See issue 36399 and 36400.
	s32 := math.Float32frombits(snan32bits)
	if s32 == s32 {
		t.Errorf("converting a NaN did not result in a NaN")
	}
	s64 := math.Float64frombits(snan64bits)
	if s64 == s64 {
		t.Errorf("converting a NaN did not result in a NaN")
	}
}

//go:noinline
func isPosInf(x float64) bool {
	return math.IsInf(x, 1)
}

//go:noinline
func isPosInfEq(x float64) bool {
	return x == math.Inf(1)
}

//go:noinline
func isPosInfCmp(x float64) bool {
	return x > math.MaxFloat64
}

//go:noinline
func isNotPosInf(x float64) bool {
	return !math.IsInf(x, 1)
}

//go:noinline
func isNotPosInfEq(x float64) bool {
	return x != math.Inf(1)
}

//go:noinline
func isNotPosInfCmp(x float64) bool {
	return x <= math.MaxFloat64
}

//go:noinline
func isNegInf(x float64) bool {
	return math.IsInf(x, -1)
}

//go:noinline
func isNegInfEq(x float64) bool {
	return x == math.Inf(-1)
}

//go:noinline
func isNegInfCmp(x float64) bool {
	return x < -math.MaxFloat64
}

//go:noinline
func isNotNegInf(x float64) bool {
	return !math.IsInf(x, -1)
}

//go:noinline
func isNotNegInfEq(x float64) bool {
	return x != math.Inf(-1)
}

//go:noinline
func isNotNegInfCmp(x float64) bool {
	return x >= -math.MaxFloat64
}

func TestInf(t *testing.T) {
	tests := []struct {
		value    float64
		isPosInf bool
		isNegInf bool
		isNaN    bool
	}{
		{value: math.Inf(1), isPosInf: true},
		{value: math.MaxFloat64},
		{value: math.Inf(-1), isNegInf: true},
		{value: -math.MaxFloat64},
		{value: math.NaN(), isNaN: true},
	}

	check := func(name string, f func(x float64) bool, value float64, want bool) {
		got := f(value)
		if got != want {
			t.Errorf("%v(%g): want %v, got %v", name, value, want, got)
		}
	}

	for _, test := range tests {
		check("isPosInf", isPosInf, test.value, test.isPosInf)
		check("isPosInfEq", isPosInfEq, test.value, test.isPosInf)
		check("isPosInfCmp", isPosInfCmp, test.value, test.isPosInf)

		check("isNotPosInf", isNotPosInf, test.value, !test.isPosInf)
		check("isNotPosInfEq", isNotPosInfEq, test.value, !test.isPosInf)
		check("isNotPosInfCmp", isNotPosInfCmp, test.value, !test.isPosInf && !test.isNaN)

		check("isNegInf", isNegInf, test.value, test.isNegInf)
		check("isNegInfEq", isNegInfEq, test.value, test.isNegInf)
		check("isNegInfCmp", isNegInfCmp, test.value, test.isNegInf)

		check("isNotNegInf", isNotNegInf, test.value, !test.isNegInf)
		check("isNotNegInfEq", isNotNegInfEq, test.value, !test.isNegInf)
		check("isNotNegInfCmp", isNotNegInfCmp, test.value, !test.isNegInf && !test.isNaN)
	}
}

//go:noinline
func isNaNOrGtZero64(x float64) bool {
	return math.IsNaN(x) || x > 0
}

//go:noinline
func isNaNOrGteZero64(x float64) bool {
	return x >= 0 || math.IsNaN(x)
}

//go:noinline
func isNaNOrLtZero64(x float64) bool {
	return x < 0 || math.IsNaN(x)
}

//go:noinline
func isNaNOrLteZero64(x float64) bool {
	return math.IsNaN(x) || x <= 0
}

func TestFusedNaNChecks64(t *testing.T) {
	tests := []struct {
		value             float64
		isZero            bool
		isGreaterThanZero bool
		isLessThanZero    bool
		isNaN             bool
	}{
		{value: 0.0, isZero: true},
		{value: math.Copysign(0, -1), isZero: true},
		{value: 1.0, isGreaterThanZero: true},
		{value: -1.0, isLessThanZero: true},
		{value: math.Inf(1), isGreaterThanZero: true},
		{value: math.Inf(-1), isLessThanZero: true},
		{value: math.NaN(), isNaN: true},
	}

	check := func(name string, f func(x float64) bool, value float64, want bool) {
		got := f(value)
		if got != want {
			t.Errorf("%v(%g): want %v, got %v", name, value, want, got)
		}
	}

	for _, test := range tests {
		check("isNaNOrGtZero64", isNaNOrGtZero64, test.value, test.isNaN || test.isGreaterThanZero)
		check("isNaNOrGteZero64", isNaNOrGteZero64, test.value, test.isNaN || test.isGreaterThanZero || test.isZero)
		check("isNaNOrLtZero64", isNaNOrLtZero64, test.value, test.isNaN || test.isLessThanZero)
		check("isNaNOrLteZero64", isNaNOrLteZero64, test.value, test.isNaN || test.isLessThanZero || test.isZero)
	}
}

//go:noinline
func isNaNOrGtZero32(x float32) bool {
	return x > 0 || x != x
}

//go:noinline
func isNaNOrGteZero32(x float32) bool {
	return x != x || x >= 0
}

//go:noinline
func isNaNOrLtZero32(x float32) bool {
	return x != x || x < 0
}

//go:noinline
func isNaNOrLteZero32(x float32) bool {
	return x <= 0 || x != x
}

func TestFusedNaNChecks32(t *testing.T) {
	tests := []struct {
		value             float32
		isZero            bool
		isGreaterThanZero bool
		isLessThanZero    bool
		isNaN             bool
	}{
		{value: 0.0, isZero: true},
		{value: float32(math.Copysign(0, -1)), isZero: true},
		{value: 1.0, isGreaterThanZero: true},
		{value: -1.0, isLessThanZero: true},
		{value: float32(math.Inf(1)), isGreaterThanZero: true},
		{value: float32(math.Inf(-1)), isLessThanZero: true},
		{value: float32(math.NaN()), isNaN: true},
	}

	check := func(name string, f func(x float32) bool, value float32, want bool) {
		got := f(value)
		if got != want {
			t.Errorf("%v(%g): want %v, got %v", name, value, want, got)
		}
	}

	for _, test := range tests {
		check("isNaNOrGtZero32", isNaNOrGtZero32, test.value, test.isNaN || test.isGreaterThanZero)
		check("isNaNOrGteZero32", isNaNOrGteZero32, test.value, test.isNaN || test.isGreaterThanZero || test.isZero)
		check("isNaNOrLtZero32", isNaNOrLtZero32, test.value, test.isNaN || test.isLessThanZero)
		check("isNaNOrLteZero32", isNaNOrLteZero32, test.value, test.isNaN || test.isLessThanZero || test.isZero)
	}
}

// minNormal64 is the smallest float64 value that is not subnormal.
const minNormal64 = 2.2250738585072014e-308

//go:noinline
func isAbsLessThanMinNormal64(x float64) bool {
	return math.Abs(x) < minNormal64
}

//go:noinline
func isLessThanMinNormal64(x float64) bool {
	return x < minNormal64
}

//go:noinline
func isGreaterThanNegMinNormal64(x float64) bool {
	return x > -minNormal64
}

//go:noinline
func isGreaterThanOrEqualToMinNormal64(x float64) bool {
	return math.Abs(x) >= minNormal64
}

func TestSubnormalComparisons(t *testing.T) {
	tests := []struct {
		value                  float64
		isAbsLessThanMinNormal bool
		isPositive             bool
		isNegative             bool
		isNaN                  bool
	}{
		{value: math.Inf(1), isPositive: true},
		{value: math.MaxFloat64, isPositive: true},
		{value: math.Inf(-1), isNegative: true},
		{value: -math.MaxFloat64, isNegative: true},
		{value: math.NaN(), isNaN: true},
		{value: minNormal64, isPositive: true},
		{value: minNormal64 / 2, isAbsLessThanMinNormal: true, isPositive: true},
		{value: -minNormal64, isNegative: true},
		{value: -minNormal64 / 2, isAbsLessThanMinNormal: true, isNegative: true},
		{value: 0, isAbsLessThanMinNormal: true, isPositive: true},
		{value: math.Copysign(0, -1), isAbsLessThanMinNormal: true, isNegative: true},
	}

	check := func(name string, f func(x float64) bool, value float64, want bool) {
		got := f(value)
		if got != want {
			t.Errorf("%v(%g): want %v, got %v", name, value, want, got)
		}
	}

	for _, test := range tests {
		check("isAbsLessThanMinNormal64", isAbsLessThanMinNormal64, test.value, test.isAbsLessThanMinNormal)
		check("isLessThanMinNormal64", isLessThanMinNormal64, test.value, test.isAbsLessThanMinNormal || test.isNegative)
		check("isGreaterThanNegMinNormal64", isGreaterThanNegMinNormal64, test.value, test.isAbsLessThanMinNormal || test.isPositive)
		check("isGreaterThanOrEqualToMinNormal64", isGreaterThanOrEqualToMinNormal64, test.value, !test.isAbsLessThanMinNormal && !test.isNaN)
	}
}

var sinkFloat float64

func BenchmarkMul2(b *testing.B) {
	for i := 0; i < b.N; i++ {
		var m float64 = 1
		for j := 0; j < 500; j++ {
			m *= 2
		}
		sinkFloat = m
	}
}
func BenchmarkMulNeg2(b *testing.B) {
	for i := 0; i < b.N; i++ {
		var m float64 = 1
		for j := 0; j < 500; j++ {
			m *= -2
		}
		sinkFloat = m
	}
}