github.com/tidwall/go@v0.0.0-20170415222209-6694a6888b7d/src/cmd/compile/internal/gc/float_test.go (about) 1 // Copyright 2016 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package gc 6 7 import "testing" 8 9 // For GO386=387, make sure fucomi* opcodes are not used 10 // for comparison operations. 11 // Note that this test will fail only on a Pentium MMX 12 // processor (with GOARCH=386 GO386=387), as it just runs 13 // some code and looks for an unimplemented instruction fault. 14 15 //go:noinline 16 func compare1(a, b float64) bool { 17 return a < b 18 } 19 20 //go:noinline 21 func compare2(a, b float32) bool { 22 return a < b 23 } 24 25 func TestFloatCompare(t *testing.T) { 26 if !compare1(3, 5) { 27 t.Errorf("compare1 returned false") 28 } 29 if !compare2(3, 5) { 30 t.Errorf("compare2 returned false") 31 } 32 } 33 34 // For GO386=387, make sure fucomi* opcodes are not used 35 // for float->int conversions. 36 37 //go:noinline 38 func cvt1(a float64) uint64 { 39 return uint64(a) 40 } 41 42 //go:noinline 43 func cvt2(a float64) uint32 { 44 return uint32(a) 45 } 46 47 //go:noinline 48 func cvt3(a float32) uint64 { 49 return uint64(a) 50 } 51 52 //go:noinline 53 func cvt4(a float32) uint32 { 54 return uint32(a) 55 } 56 57 //go:noinline 58 func cvt5(a float64) int64 { 59 return int64(a) 60 } 61 62 //go:noinline 63 func cvt6(a float64) int32 { 64 return int32(a) 65 } 66 67 //go:noinline 68 func cvt7(a float32) int64 { 69 return int64(a) 70 } 71 72 //go:noinline 73 func cvt8(a float32) int32 { 74 return int32(a) 75 } 76 77 // make sure to cover int, uint cases (issue #16738) 78 //go:noinline 79 func cvt9(a float64) int { 80 return int(a) 81 } 82 83 //go:noinline 84 func cvt10(a float64) uint { 85 return uint(a) 86 } 87 88 //go:noinline 89 func cvt11(a float32) int { 90 return int(a) 91 } 92 93 //go:noinline 94 func cvt12(a float32) uint { 95 return uint(a) 96 } 97 98 func TestFloatConvert(t *testing.T) { 99 if got := cvt1(3.5); got != 3 { 100 t.Errorf("cvt1 got %d, wanted 3", got) 101 } 102 if got := cvt2(3.5); got != 3 { 103 t.Errorf("cvt2 got %d, wanted 3", got) 104 } 105 if got := cvt3(3.5); got != 3 { 106 t.Errorf("cvt3 got %d, wanted 3", got) 107 } 108 if got := cvt4(3.5); got != 3 { 109 t.Errorf("cvt4 got %d, wanted 3", got) 110 } 111 if got := cvt5(3.5); got != 3 { 112 t.Errorf("cvt5 got %d, wanted 3", got) 113 } 114 if got := cvt6(3.5); got != 3 { 115 t.Errorf("cvt6 got %d, wanted 3", got) 116 } 117 if got := cvt7(3.5); got != 3 { 118 t.Errorf("cvt7 got %d, wanted 3", got) 119 } 120 if got := cvt8(3.5); got != 3 { 121 t.Errorf("cvt8 got %d, wanted 3", got) 122 } 123 if got := cvt9(3.5); got != 3 { 124 t.Errorf("cvt9 got %d, wanted 3", got) 125 } 126 if got := cvt10(3.5); got != 3 { 127 t.Errorf("cvt10 got %d, wanted 3", got) 128 } 129 if got := cvt11(3.5); got != 3 { 130 t.Errorf("cvt11 got %d, wanted 3", got) 131 } 132 if got := cvt12(3.5); got != 3 { 133 t.Errorf("cvt12 got %d, wanted 3", got) 134 } 135 } 136 137 var sinkFloat float64 138 139 func BenchmarkMul2(b *testing.B) { 140 for i := 0; i < b.N; i++ { 141 var m float64 = 1 142 for j := 0; j < 500; j++ { 143 m *= 2 144 } 145 sinkFloat = m 146 } 147 } 148 func BenchmarkMulNeg2(b *testing.B) { 149 for i := 0; i < b.N; i++ { 150 var m float64 = 1 151 for j := 0; j < 500; j++ { 152 m *= -2 153 } 154 sinkFloat = m 155 } 156 }