Keith Randall | 3d01f28 | 2016-01-12 12:42:28 -0800 | [diff] [blame] | 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 | func TestFloatConvert(t *testing.T) { |
| 78 | if got := cvt1(3.5); got != 3 { |
| 79 | t.Errorf("cvt1 got %d, wanted 3", got) |
| 80 | } |
| 81 | if got := cvt2(3.5); got != 3 { |
| 82 | t.Errorf("cvt2 got %d, wanted 3", got) |
| 83 | } |
| 84 | if got := cvt3(3.5); got != 3 { |
| 85 | t.Errorf("cvt3 got %d, wanted 3", got) |
| 86 | } |
| 87 | if got := cvt4(3.5); got != 3 { |
| 88 | t.Errorf("cvt4 got %d, wanted 3", got) |
| 89 | } |
| 90 | if got := cvt5(3.5); got != 3 { |
| 91 | t.Errorf("cvt5 got %d, wanted 3", got) |
| 92 | } |
| 93 | if got := cvt6(3.5); got != 3 { |
| 94 | t.Errorf("cvt6 got %d, wanted 3", got) |
| 95 | } |
| 96 | if got := cvt7(3.5); got != 3 { |
| 97 | t.Errorf("cvt7 got %d, wanted 3", got) |
| 98 | } |
| 99 | if got := cvt8(3.5); got != 3 { |
| 100 | t.Errorf("cvt8 got %d, wanted 3", got) |
| 101 | } |
| 102 | } |