| 1 |
// Copyright 2020 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 runtime_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"testing" |
| 9 |
) |
| 10 |
|
| 11 |
var res int64 |
| 12 |
var ures uint64 |
| 13 |
|
| 14 |
// TODO: This test probably should be in a different place. |
| 15 |
|
| 16 |
func TestFloatTruncation(t *testing.T) { |
| 17 |
testdata := []struct { |
| 18 |
input float64 |
| 19 |
convInt64 int64 |
| 20 |
convUInt64 uint64 |
| 21 |
overflow bool |
| 22 |
}{ |
| 23 |
// max +- 1 |
| 24 |
{ |
| 25 |
input: 0x7fffffffffffffff, |
| 26 |
convInt64: 0x7fffffffffffffff, |
| 27 |
convUInt64: 0x8000000000000000, |
| 28 |
}, |
| 29 |
// For out-of-bounds conversion, the result is implementation-dependent. |
| 30 |
// This test verifies the implementation of wasm architecture, which is, |
| 31 |
// saturating to the min/max value. |
| 32 |
{ |
| 33 |
input: 0x8000000000000000, |
| 34 |
convInt64: 0x7fffffffffffffff, |
| 35 |
convUInt64: 0x8000000000000000, |
| 36 |
}, |
| 37 |
{ |
| 38 |
input: 0x7ffffffffffffffe, |
| 39 |
convInt64: 0x7fffffffffffffff, |
| 40 |
convUInt64: 0x8000000000000000, |
| 41 |
}, |
| 42 |
// neg max +- 1 |
| 43 |
{ |
| 44 |
input: -0x8000000000000000, |
| 45 |
convInt64: -0x8000000000000000, |
| 46 |
convUInt64: 0, |
| 47 |
}, |
| 48 |
{ |
| 49 |
input: -0x8000000000000001, |
| 50 |
convInt64: -0x8000000000000000, |
| 51 |
convUInt64: 0, |
| 52 |
}, |
| 53 |
{ |
| 54 |
input: -0x7fffffffffffffff, |
| 55 |
convInt64: -0x8000000000000000, |
| 56 |
convUInt64: 0, |
| 57 |
}, |
| 58 |
// trunc point +- 1 |
| 59 |
{ |
| 60 |
input: 0x7ffffffffffffdff, |
| 61 |
convInt64: 0x7ffffffffffffc00, |
| 62 |
convUInt64: 0x7ffffffffffffc00, |
| 63 |
}, |
| 64 |
{ |
| 65 |
input: 0x7ffffffffffffe00, |
| 66 |
convInt64: 0x7fffffffffffffff, |
| 67 |
convUInt64: 0x8000000000000000, |
| 68 |
}, |
| 69 |
{ |
| 70 |
input: 0x7ffffffffffffdfe, |
| 71 |
convInt64: 0x7ffffffffffffc00, |
| 72 |
convUInt64: 0x7ffffffffffffc00, |
| 73 |
}, |
| 74 |
// neg trunc point +- 1 |
| 75 |
{ |
| 76 |
input: -0x7ffffffffffffdff, |
| 77 |
convInt64: -0x7ffffffffffffc00, |
| 78 |
convUInt64: 0, |
| 79 |
}, |
| 80 |
{ |
| 81 |
input: -0x7ffffffffffffe00, |
| 82 |
convInt64: -0x8000000000000000, |
| 83 |
convUInt64: 0, |
| 84 |
}, |
| 85 |
{ |
| 86 |
input: -0x7ffffffffffffdfe, |
| 87 |
convInt64: -0x7ffffffffffffc00, |
| 88 |
convUInt64: 0, |
| 89 |
}, |
| 90 |
// umax +- 1 |
| 91 |
{ |
| 92 |
input: 0xffffffffffffffff, |
| 93 |
convInt64: 0x7fffffffffffffff, |
| 94 |
convUInt64: 0xffffffffffffffff, |
| 95 |
}, |
| 96 |
{ |
| 97 |
input: 0x10000000000000000, |
| 98 |
convInt64: 0x7fffffffffffffff, |
| 99 |
convUInt64: 0xffffffffffffffff, |
| 100 |
}, |
| 101 |
{ |
| 102 |
input: 0xfffffffffffffffe, |
| 103 |
convInt64: 0x7fffffffffffffff, |
| 104 |
convUInt64: 0xffffffffffffffff, |
| 105 |
}, |
| 106 |
// umax trunc +- 1 |
| 107 |
{ |
| 108 |
input: 0xfffffffffffffbff, |
| 109 |
convInt64: 0x7fffffffffffffff, |
| 110 |
convUInt64: 0xfffffffffffff800, |
| 111 |
}, |
| 112 |
{ |
| 113 |
input: 0xfffffffffffffc00, |
| 114 |
convInt64: 0x7fffffffffffffff, |
| 115 |
convUInt64: 0xffffffffffffffff, |
| 116 |
}, |
| 117 |
{ |
| 118 |
input: 0xfffffffffffffbfe, |
| 119 |
convInt64: 0x7fffffffffffffff, |
| 120 |
convUInt64: 0xfffffffffffff800, |
| 121 |
}, |
| 122 |
} |
| 123 |
for _, item := range testdata { |
| 124 |
if got, want := int64(item.input), item.convInt64; got != want { |
| 125 |
t.Errorf("int64(%f): got %x, want %x", item.input, got, want) |
| 126 |
} |
| 127 |
if got, want := uint64(item.input), item.convUInt64; got != want { |
| 128 |
t.Errorf("uint64(%f): got %x, want %x", item.input, got, want) |
| 129 |
} |
| 130 |
} |
| 131 |
} |