| 1 |
/* |
| 2 |
* Copyright (C) 2018 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
import libcore.util.FP16; |
| 18 |
|
| 19 |
public class Main { |
| 20 |
|
| 21 |
public static short FP16_ALT_NAN = (short)(FP16.NaN | 0x0098); |
| 22 |
|
| 23 |
public Main() { |
| 24 |
} |
| 25 |
|
| 26 |
public static int TestFP16ToFloatRawIntBits(short half) { |
| 27 |
float f = FP16.toFloat(half); |
| 28 |
// Since in this test class we need to check the integer representing of |
| 29 |
// the actual float NaN values, the floatToRawIntBits() is used instead of |
| 30 |
// floatToIntBits(). |
| 31 |
return Float.floatToRawIntBits(f); |
| 32 |
} |
| 33 |
|
| 34 |
public static void assertEquals(short expected, short calculated) { |
| 35 |
if (expected != calculated) { |
| 36 |
throw new Error("Expected: " + expected + ", Calculated: " + calculated); |
| 37 |
} |
| 38 |
} |
| 39 |
public static void assertEquals(float expected, float calculated) { |
| 40 |
if (expected != calculated) { |
| 41 |
throw new Error("Expected: " + expected + ", Calculated: " + calculated); |
| 42 |
} |
| 43 |
} |
| 44 |
public static void assertEquals(int expected, int calculated) { |
| 45 |
if (expected != calculated) { |
| 46 |
throw new Error("Expected: " + expected + ", Calculated: " + calculated); |
| 47 |
} |
| 48 |
} |
| 49 |
static public void assertTrue(boolean condition) { |
| 50 |
if (!condition) { |
| 51 |
throw new Error("condition not true"); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
static public void assertFalse(boolean condition) { |
| 56 |
if (condition) { |
| 57 |
throw new Error("condition not false"); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
public static void testHalfToFloatToHalfConversions(){ |
| 62 |
// Test FP16 to float and back to Half for all possible Short values |
| 63 |
for (short h = Short.MIN_VALUE; h < Short.MAX_VALUE; h++) { |
| 64 |
if (FP16.isNaN(h)) { |
| 65 |
// NaN inputs are tested below. |
| 66 |
continue; |
| 67 |
} |
| 68 |
assertEquals(h, FP16.toHalf(FP16.toFloat(h))); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
public static void testToHalf(){ |
| 73 |
// These asserts check some known values and edge cases for FP16.toHalf |
| 74 |
// and have been inspired by the cts HalfTest. |
| 75 |
// Zeroes, NaN and infinities |
| 76 |
assertEquals(FP16.POSITIVE_ZERO, FP16.toHalf(0.0f)); |
| 77 |
assertEquals(FP16.NEGATIVE_ZERO, FP16.toHalf(-0.0f)); |
| 78 |
assertEquals(FP16.NaN, FP16.toHalf(Float.NaN)); |
| 79 |
assertEquals(FP16.POSITIVE_INFINITY, FP16.toHalf(Float.POSITIVE_INFINITY)); |
| 80 |
assertEquals(FP16.NEGATIVE_INFINITY, FP16.toHalf(Float.NEGATIVE_INFINITY)); |
| 81 |
// Known values |
| 82 |
assertEquals((short) 0x3c01, FP16.toHalf(1.0009765625f)); |
| 83 |
assertEquals((short) 0xc000, FP16.toHalf(-2.0f)); |
| 84 |
assertEquals((short) 0x0400, FP16.toHalf(6.10352e-5f)); |
| 85 |
assertEquals((short) 0x7bff, FP16.toHalf(65504.0f)); |
| 86 |
assertEquals((short) 0x3555, FP16.toHalf(1.0f / 3.0f)); |
| 87 |
// Subnormals |
| 88 |
assertEquals((short) 0x03ff, FP16.toHalf(6.09756e-5f)); |
| 89 |
assertEquals(FP16.MIN_VALUE, FP16.toHalf(5.96046e-8f)); |
| 90 |
assertEquals((short) 0x83ff, FP16.toHalf(-6.09756e-5f)); |
| 91 |
assertEquals((short) 0x8001, FP16.toHalf(-5.96046e-8f)); |
| 92 |
// Subnormals (flushed to +/-0) |
| 93 |
assertEquals(FP16.POSITIVE_ZERO, FP16.toHalf(5.96046e-9f)); |
| 94 |
assertEquals(FP16.NEGATIVE_ZERO, FP16.toHalf(-5.96046e-9f)); |
| 95 |
// Test for values that overflow the mantissa bits into exp bits |
| 96 |
assertEquals(0x1000, FP16.toHalf(Float.intBitsToFloat(0x39fff000))); |
| 97 |
assertEquals(0x0400, FP16.toHalf(Float.intBitsToFloat(0x387fe000))); |
| 98 |
// Floats with absolute value above +/-65519 are rounded to +/-inf |
| 99 |
// when using round-to-even |
| 100 |
assertEquals(0x7bff, FP16.toHalf(65519.0f)); |
| 101 |
assertEquals(0x7bff, FP16.toHalf(65519.9f)); |
| 102 |
assertEquals(FP16.POSITIVE_INFINITY, FP16.toHalf(65520.0f)); |
| 103 |
assertEquals(FP16.NEGATIVE_INFINITY, FP16.toHalf(-65520.0f)); |
| 104 |
// Check if numbers are rounded to nearest even when they |
| 105 |
// cannot be accurately represented by Half |
| 106 |
assertEquals(0x6800, FP16.toHalf(2049.0f)); |
| 107 |
assertEquals(0x6c00, FP16.toHalf(4098.0f)); |
| 108 |
assertEquals(0x7000, FP16.toHalf(8196.0f)); |
| 109 |
assertEquals(0x7400, FP16.toHalf(16392.0f)); |
| 110 |
assertEquals(0x7800, FP16.toHalf(32784.0f)); |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
public static void testToFloat(){ |
| 115 |
// FP16 SNaN/QNaN inputs to float |
| 116 |
// The most significant bit of mantissa: |
| 117 |
// V |
| 118 |
// 0xfc01: 1 11111 0000000001 (signaling NaN) |
| 119 |
// 0xfdff: 1 11111 0111111111 (signaling NaN) |
| 120 |
// 0xfe00: 1 11111 1000000000 (quiet NaN) |
| 121 |
// 0xffff: 1 11111 1111111111 (quiet NaN) |
| 122 |
// This test is inspired by Java implementation of android.util.Half.toFloat(), |
| 123 |
// where the implementation performs SNaN->QNaN conversion. |
| 124 |
assert(Float.isNaN(FP16.toFloat((short)0xfc01))); |
| 125 |
assert(Float.isNaN(FP16.toFloat((short)0xfdff))); |
| 126 |
assert(Float.isNaN(FP16.toFloat((short)0xfe00))); |
| 127 |
assert(Float.isNaN(FP16.toFloat((short)0xffff))); |
| 128 |
assertEquals(0xffc02000, TestFP16ToFloatRawIntBits((short)(0xfc01))); // SNaN->QNaN |
| 129 |
assertEquals(0xffffe000, TestFP16ToFloatRawIntBits((short)(0xfdff))); // SNaN->QNaN |
| 130 |
assertEquals(0xffc00000, TestFP16ToFloatRawIntBits((short)(0xfe00))); // QNaN->QNaN |
| 131 |
assertEquals(0xffffe000, TestFP16ToFloatRawIntBits((short)(0xffff))); // QNaN->QNaN |
| 132 |
} |
| 133 |
|
| 134 |
public static void testFloor() { |
| 135 |
// These tests have been taken from the cts HalfTest |
| 136 |
assertEquals(FP16.POSITIVE_INFINITY, FP16.floor(FP16.POSITIVE_INFINITY)); |
| 137 |
assertEquals(FP16.NEGATIVE_INFINITY, FP16.floor(FP16.NEGATIVE_INFINITY)); |
| 138 |
assertEquals(FP16.POSITIVE_ZERO, FP16.floor(FP16.POSITIVE_ZERO)); |
| 139 |
assertEquals(FP16.NEGATIVE_ZERO, FP16.floor(FP16.NEGATIVE_ZERO)); |
| 140 |
assertEquals(FP16.NaN, FP16.floor(FP16.NaN)); |
| 141 |
assertEquals(FP16.LOWEST_VALUE, FP16.floor(FP16.LOWEST_VALUE)); |
| 142 |
assertEquals(FP16.POSITIVE_ZERO, FP16.floor(FP16.MIN_NORMAL)); |
| 143 |
assertEquals(FP16.POSITIVE_ZERO, FP16.floor((short) 0x3ff)); |
| 144 |
assertEquals(FP16.POSITIVE_ZERO, FP16.floor(FP16.toHalf(0.2f))); |
| 145 |
assertEquals(-1.0f, FP16.toFloat(FP16.floor(FP16.toHalf(-0.2f)))); |
| 146 |
assertEquals(-1.0f, FP16.toFloat(FP16.floor(FP16.toHalf(-0.7f)))); |
| 147 |
assertEquals(FP16.POSITIVE_ZERO, FP16.floor(FP16.toHalf(0.7f))); |
| 148 |
assertEquals(124.0f, FP16.toFloat(FP16.floor(FP16.toHalf(124.7f)))); |
| 149 |
assertEquals(-125.0f, FP16.toFloat(FP16.floor(FP16.toHalf(-124.7f)))); |
| 150 |
assertEquals(124.0f, FP16.toFloat(FP16.floor(FP16.toHalf(124.2f)))); |
| 151 |
assertEquals(-125.0f, FP16.toFloat(FP16.floor(FP16.toHalf(-124.2f)))); |
| 152 |
// floor for NaN values |
| 153 |
assertEquals((short) 0x7e01, FP16.floor((short) 0x7c01)); |
| 154 |
assertEquals((short) 0x7f00, FP16.floor((short) 0x7d00)); |
| 155 |
assertEquals((short) 0xfe01, FP16.floor((short) 0xfc01)); |
| 156 |
assertEquals((short) 0xff00, FP16.floor((short) 0xfd00)); |
| 157 |
} |
| 158 |
|
| 159 |
public static void testCeil() { |
| 160 |
// These tests have been taken from the cts HalfTest |
| 161 |
assertEquals(FP16.POSITIVE_INFINITY, FP16.ceil(FP16.POSITIVE_INFINITY)); |
| 162 |
assertEquals(FP16.NEGATIVE_INFINITY, FP16.ceil(FP16.NEGATIVE_INFINITY)); |
| 163 |
assertEquals(FP16.POSITIVE_ZERO, FP16.ceil(FP16.POSITIVE_ZERO)); |
| 164 |
assertEquals(FP16.NEGATIVE_ZERO, FP16.ceil(FP16.NEGATIVE_ZERO)); |
| 165 |
assertEquals(FP16.NaN, FP16.ceil(FP16.NaN)); |
| 166 |
assertEquals(FP16.LOWEST_VALUE, FP16.ceil(FP16.LOWEST_VALUE)); |
| 167 |
assertEquals(1.0f, FP16.toFloat(FP16.ceil(FP16.MIN_NORMAL))); |
| 168 |
assertEquals(1.0f, FP16.toFloat(FP16.ceil((short) 0x3ff))); |
| 169 |
assertEquals(1.0f, FP16.toFloat(FP16.ceil(FP16.toHalf(0.2f)))); |
| 170 |
assertEquals(FP16.NEGATIVE_ZERO, FP16.ceil(FP16.toHalf(-0.2f))); |
| 171 |
assertEquals(1.0f, FP16.toFloat(FP16.ceil(FP16.toHalf(0.7f)))); |
| 172 |
assertEquals(FP16.NEGATIVE_ZERO, FP16.ceil(FP16.toHalf(-0.7f))); |
| 173 |
assertEquals(125.0f, FP16.toFloat(FP16.ceil(FP16.toHalf(124.7f)))); |
| 174 |
assertEquals(-124.0f, FP16.toFloat(FP16.ceil(FP16.toHalf(-124.7f)))); |
| 175 |
assertEquals(125.0f, FP16.toFloat(FP16.ceil(FP16.toHalf(124.2f)))); |
| 176 |
assertEquals(-124.0f, FP16.toFloat(FP16.ceil(FP16.toHalf(-124.2f)))); |
| 177 |
// ceil for NaN values |
| 178 |
assertEquals((short) 0x7e01, FP16.floor((short) 0x7c01)); |
| 179 |
assertEquals((short) 0x7f00, FP16.floor((short) 0x7d00)); |
| 180 |
assertEquals((short) 0xfe01, FP16.floor((short) 0xfc01)); |
| 181 |
assertEquals((short) 0xff00, FP16.floor((short) 0xfd00)); |
| 182 |
} |
| 183 |
|
| 184 |
public static void testRint() { |
| 185 |
assertEquals(FP16.POSITIVE_INFINITY, FP16.rint(FP16.POSITIVE_INFINITY)); |
| 186 |
assertEquals(FP16.NEGATIVE_INFINITY, FP16.rint(FP16.NEGATIVE_INFINITY)); |
| 187 |
assertEquals(FP16.POSITIVE_ZERO, FP16.rint(FP16.POSITIVE_ZERO)); |
| 188 |
assertEquals(FP16.NEGATIVE_ZERO, FP16.rint(FP16.NEGATIVE_ZERO)); |
| 189 |
assertEquals(FP16.NaN, FP16.rint(FP16.NaN)); |
| 190 |
assertEquals(FP16.LOWEST_VALUE, FP16.rint(FP16.LOWEST_VALUE)); |
| 191 |
assertEquals(FP16.POSITIVE_ZERO, FP16.rint(FP16.MIN_VALUE)); |
| 192 |
assertEquals(FP16.POSITIVE_ZERO, FP16.rint((short) 0x200)); |
| 193 |
assertEquals(FP16.POSITIVE_ZERO, FP16.rint((short) 0x3ff)); |
| 194 |
assertEquals(FP16.POSITIVE_ZERO, FP16.rint(FP16.toHalf(0.2f))); |
| 195 |
assertEquals(FP16.NEGATIVE_ZERO, FP16.rint(FP16.toHalf(-0.2f))); |
| 196 |
assertEquals(1.0f, FP16.toFloat(FP16.rint(FP16.toHalf(0.7f)))); |
| 197 |
assertEquals(-1.0f, FP16.toFloat(FP16.rint(FP16.toHalf(-0.7f)))); |
| 198 |
assertEquals(0.0f, FP16.toFloat(FP16.rint(FP16.toHalf(0.5f)))); |
| 199 |
assertEquals(-0.0f, FP16.toFloat(FP16.rint(FP16.toHalf(-0.5f)))); |
| 200 |
assertEquals(125.0f, FP16.toFloat(FP16.rint(FP16.toHalf(124.7f)))); |
| 201 |
assertEquals(-125.0f, FP16.toFloat(FP16.rint(FP16.toHalf(-124.7f)))); |
| 202 |
assertEquals(124.0f, FP16.toFloat(FP16.rint(FP16.toHalf(124.2f)))); |
| 203 |
assertEquals(-124.0f, FP16.toFloat(FP16.rint(FP16.toHalf(-124.2f)))); |
| 204 |
// floor for NaN values |
| 205 |
assertEquals((short) 0x7e01, FP16.floor((short) 0x7c01)); |
| 206 |
assertEquals((short) 0x7f00, FP16.floor((short) 0x7d00)); |
| 207 |
assertEquals((short) 0xfe01, FP16.floor((short) 0xfc01)); |
| 208 |
assertEquals((short) 0xff00, FP16.floor((short) 0xfd00)); |
| 209 |
|
| 210 |
} |
| 211 |
|
| 212 |
public static void testGreater() { |
| 213 |
assertTrue(FP16.greater(FP16.POSITIVE_INFINITY, FP16.NEGATIVE_INFINITY)); |
| 214 |
assertTrue(FP16.greater(FP16.POSITIVE_INFINITY, FP16.MAX_VALUE)); |
| 215 |
assertFalse(FP16.greater(FP16.MAX_VALUE, FP16.POSITIVE_INFINITY)); |
| 216 |
assertFalse(FP16.greater(FP16.NEGATIVE_INFINITY, FP16.LOWEST_VALUE)); |
| 217 |
assertTrue(FP16.greater(FP16.LOWEST_VALUE, FP16.NEGATIVE_INFINITY)); |
| 218 |
assertFalse(FP16.greater(FP16.NEGATIVE_ZERO, FP16.POSITIVE_ZERO)); |
| 219 |
assertFalse(FP16.greater(FP16.POSITIVE_ZERO, FP16.NEGATIVE_ZERO)); |
| 220 |
assertFalse(FP16.greater(FP16.toHalf(12.3f), FP16.NaN)); |
| 221 |
assertFalse(FP16.greater(FP16.NaN, FP16.toHalf(12.3f))); |
| 222 |
assertTrue(FP16.greater(FP16.MIN_NORMAL, FP16.MIN_VALUE)); |
| 223 |
assertFalse(FP16.greater(FP16.MIN_VALUE, FP16.MIN_NORMAL)); |
| 224 |
assertTrue(FP16.greater(FP16.toHalf(12.4f), FP16.toHalf(12.3f))); |
| 225 |
assertFalse(FP16.greater(FP16.toHalf(12.3f), FP16.toHalf(12.4f))); |
| 226 |
assertFalse(FP16.greater(FP16.toHalf(-12.4f), FP16.toHalf(-12.3f))); |
| 227 |
assertTrue(FP16.greater(FP16.toHalf(-12.3f), FP16.toHalf(-12.4f))); |
| 228 |
assertTrue(FP16.greater((short) 0x3ff, FP16.MIN_VALUE)); |
| 229 |
|
| 230 |
assertFalse(FP16.greater(FP16.toHalf(-1.0f), FP16.toHalf(0.0f))); |
| 231 |
assertTrue(FP16.greater(FP16.toHalf(0.0f), FP16.toHalf(-1.0f))); |
| 232 |
assertFalse(FP16.greater(FP16.toHalf(-1.0f), FP16.toHalf(-1.0f))); |
| 233 |
assertFalse(FP16.greater(FP16.toHalf(-1.3f), FP16.toHalf(-1.3f))); |
| 234 |
assertTrue(FP16.greater(FP16.toHalf(1.0f), FP16.toHalf(0.0f))); |
| 235 |
assertFalse(FP16.greater(FP16.toHalf(0.0f), FP16.toHalf(1.0f))); |
| 236 |
assertFalse(FP16.greater(FP16.toHalf(1.0f), FP16.toHalf(1.0f))); |
| 237 |
assertFalse(FP16.greater(FP16.toHalf(1.3f), FP16.toHalf(1.3f))); |
| 238 |
assertFalse(FP16.greater(FP16.toHalf(-0.1f), FP16.toHalf(0.0f))); |
| 239 |
assertTrue(FP16.greater(FP16.toHalf(0.0f), FP16.toHalf(-0.1f))); |
| 240 |
assertFalse(FP16.greater(FP16.toHalf(-0.1f), FP16.toHalf(-0.1f))); |
| 241 |
assertTrue(FP16.greater(FP16.toHalf(0.1f), FP16.toHalf(0.0f))); |
| 242 |
assertFalse(FP16.greater(FP16.toHalf(0.0f), FP16.toHalf(0.1f))); |
| 243 |
assertFalse(FP16.greater(FP16.toHalf(0.1f), FP16.toHalf(0.1f))); |
| 244 |
} |
| 245 |
|
| 246 |
public static void testGreaterEquals() { |
| 247 |
assertTrue(FP16.greaterEquals(FP16.POSITIVE_INFINITY, FP16.NEGATIVE_INFINITY)); |
| 248 |
assertTrue(FP16.greaterEquals(FP16.POSITIVE_INFINITY, FP16.MAX_VALUE)); |
| 249 |
assertFalse(FP16.greaterEquals(FP16.MAX_VALUE, FP16.POSITIVE_INFINITY)); |
| 250 |
assertFalse(FP16.greaterEquals(FP16.NEGATIVE_INFINITY, FP16.LOWEST_VALUE)); |
| 251 |
assertTrue(FP16.greaterEquals(FP16.LOWEST_VALUE, FP16.NEGATIVE_INFINITY)); |
| 252 |
assertTrue(FP16.greaterEquals(FP16.NEGATIVE_ZERO, FP16.POSITIVE_ZERO)); |
| 253 |
assertTrue(FP16.greaterEquals(FP16.POSITIVE_ZERO, FP16.NEGATIVE_ZERO)); |
| 254 |
assertFalse(FP16.greaterEquals(FP16.toHalf(12.3f), FP16.NaN)); |
| 255 |
assertFalse(FP16.greaterEquals(FP16.NaN, FP16.toHalf(12.3f))); |
| 256 |
assertTrue(FP16.greaterEquals(FP16.MIN_NORMAL, FP16.MIN_VALUE)); |
| 257 |
assertFalse(FP16.greaterEquals(FP16.MIN_VALUE, FP16.MIN_NORMAL)); |
| 258 |
assertTrue(FP16.greaterEquals(FP16.toHalf(12.4f), FP16.toHalf(12.3f))); |
| 259 |
assertFalse(FP16.greaterEquals(FP16.toHalf(12.3f), FP16.toHalf(12.4f))); |
| 260 |
assertFalse(FP16.greaterEquals(FP16.toHalf(-12.4f), FP16.toHalf(-12.3f))); |
| 261 |
assertTrue(FP16.greaterEquals(FP16.toHalf(-12.3f), FP16.toHalf(-12.4f))); |
| 262 |
assertTrue(FP16.greaterEquals((short) 0x3ff, FP16.MIN_VALUE)); |
| 263 |
assertTrue(FP16.greaterEquals(FP16.NEGATIVE_INFINITY, FP16.NEGATIVE_INFINITY)); |
| 264 |
assertTrue(FP16.greaterEquals(FP16.POSITIVE_INFINITY, FP16.POSITIVE_INFINITY)); |
| 265 |
assertTrue(FP16.greaterEquals(FP16.toHalf(12.12356f), FP16.toHalf(12.12356f))); |
| 266 |
assertTrue(FP16.greaterEquals(FP16.toHalf(-12.12356f), FP16.toHalf(-12.12356f))); |
| 267 |
|
| 268 |
assertFalse(FP16.greaterEquals(FP16.toHalf(-1.0f), FP16.toHalf(0.0f))); |
| 269 |
assertTrue(FP16.greaterEquals(FP16.toHalf(0.0f), FP16.toHalf(-1.0f))); |
| 270 |
assertTrue(FP16.greaterEquals(FP16.toHalf(-1.0f), FP16.toHalf(-1.0f))); |
| 271 |
assertTrue(FP16.greaterEquals(FP16.toHalf(-1.3f), FP16.toHalf(-1.3f))); |
| 272 |
assertTrue(FP16.greaterEquals(FP16.toHalf(1.0f), FP16.toHalf(0.0f))); |
| 273 |
assertFalse(FP16.greaterEquals(FP16.toHalf(0.0f), FP16.toHalf(1.0f))); |
| 274 |
assertTrue(FP16.greaterEquals(FP16.toHalf(1.0f), FP16.toHalf(1.0f))); |
| 275 |
assertTrue(FP16.greaterEquals(FP16.toHalf(1.3f), FP16.toHalf(1.3f))); |
| 276 |
assertFalse(FP16.greaterEquals(FP16.toHalf(-0.1f), FP16.toHalf(0.0f))); |
| 277 |
assertTrue(FP16.greaterEquals(FP16.toHalf(0.0f), FP16.toHalf(-0.1f))); |
| 278 |
assertTrue(FP16.greaterEquals(FP16.toHalf(-0.1f), FP16.toHalf(-0.1f))); |
| 279 |
assertTrue(FP16.greaterEquals(FP16.toHalf(0.1f), FP16.toHalf(0.0f))); |
| 280 |
assertFalse(FP16.greaterEquals(FP16.toHalf(0.0f), FP16.toHalf(0.1f))); |
| 281 |
assertTrue(FP16.greaterEquals(FP16.toHalf(0.1f), FP16.toHalf(0.1f))); |
| 282 |
} |
| 283 |
|
| 284 |
public static void testLess() { |
| 285 |
assertTrue(FP16.less(FP16.NEGATIVE_INFINITY, FP16.POSITIVE_INFINITY)); |
| 286 |
assertTrue(FP16.less(FP16.MAX_VALUE, FP16.POSITIVE_INFINITY)); |
| 287 |
assertFalse(FP16.less(FP16.POSITIVE_INFINITY, FP16.MAX_VALUE)); |
| 288 |
assertFalse(FP16.less(FP16.LOWEST_VALUE, FP16.NEGATIVE_INFINITY)); |
| 289 |
assertTrue(FP16.less(FP16.NEGATIVE_INFINITY, FP16.LOWEST_VALUE)); |
| 290 |
assertFalse(FP16.less(FP16.POSITIVE_ZERO, FP16.NEGATIVE_ZERO)); |
| 291 |
assertFalse(FP16.less(FP16.NEGATIVE_ZERO, FP16.POSITIVE_ZERO)); |
| 292 |
assertFalse(FP16.less(FP16.NaN, FP16.toHalf(12.3f))); |
| 293 |
assertFalse(FP16.less(FP16.toHalf(12.3f), FP16.NaN)); |
| 294 |
assertTrue(FP16.less(FP16.MIN_VALUE, FP16.MIN_NORMAL)); |
| 295 |
assertFalse(FP16.less(FP16.MIN_NORMAL, FP16.MIN_VALUE)); |
| 296 |
assertTrue(FP16.less(FP16.toHalf(12.3f), FP16.toHalf(12.4f))); |
| 297 |
assertFalse(FP16.less(FP16.toHalf(12.4f), FP16.toHalf(12.3f))); |
| 298 |
assertFalse(FP16.less(FP16.toHalf(-12.3f), FP16.toHalf(-12.4f))); |
| 299 |
assertTrue(FP16.less(FP16.toHalf(-12.4f), FP16.toHalf(-12.3f))); |
| 300 |
assertTrue(FP16.less(FP16.MIN_VALUE, (short) 0x3ff)); |
| 301 |
|
| 302 |
assertTrue(FP16.less(FP16.toHalf(-1.0f), FP16.toHalf(0.0f))); |
| 303 |
assertFalse(FP16.less(FP16.toHalf(0.0f), FP16.toHalf(-1.0f))); |
| 304 |
assertFalse(FP16.less(FP16.toHalf(-1.0f), FP16.toHalf(-1.0f))); |
| 305 |
assertFalse(FP16.less(FP16.toHalf(-1.3f), FP16.toHalf(-1.3f))); |
| 306 |
assertFalse(FP16.less(FP16.toHalf(1.0f), FP16.toHalf(0.0f))); |
| 307 |
assertTrue(FP16.less(FP16.toHalf(0.0f), FP16.toHalf(1.0f))); |
| 308 |
assertFalse(FP16.less(FP16.toHalf(1.0f), FP16.toHalf(1.0f))); |
| 309 |
assertFalse(FP16.less(FP16.toHalf(1.3f), FP16.toHalf(1.3f))); |
| 310 |
assertTrue(FP16.less(FP16.toHalf(-0.1f), FP16.toHalf(0.0f))); |
| 311 |
assertFalse(FP16.less(FP16.toHalf(0.0f), FP16.toHalf(-0.1f))); |
| 312 |
assertFalse(FP16.less(FP16.toHalf(-0.1f), FP16.toHalf(-0.1f))); |
| 313 |
assertFalse(FP16.less(FP16.toHalf(0.1f), FP16.toHalf(0.0f))); |
| 314 |
assertTrue(FP16.less(FP16.toHalf(0.0f), FP16.toHalf(0.1f))); |
| 315 |
assertFalse(FP16.less(FP16.toHalf(0.1f), FP16.toHalf(0.1f))); |
| 316 |
} |
| 317 |
|
| 318 |
public static void testLessEquals() { |
| 319 |
assertTrue(FP16.lessEquals(FP16.NEGATIVE_INFINITY, FP16.POSITIVE_INFINITY)); |
| 320 |
assertTrue(FP16.lessEquals(FP16.MAX_VALUE, FP16.POSITIVE_INFINITY)); |
| 321 |
assertFalse(FP16.lessEquals(FP16.POSITIVE_INFINITY, FP16.MAX_VALUE)); |
| 322 |
assertFalse(FP16.lessEquals(FP16.LOWEST_VALUE, FP16.NEGATIVE_INFINITY)); |
| 323 |
assertTrue(FP16.lessEquals(FP16.NEGATIVE_INFINITY, FP16.LOWEST_VALUE)); |
| 324 |
assertTrue(FP16.lessEquals(FP16.POSITIVE_ZERO, FP16.NEGATIVE_ZERO)); |
| 325 |
assertTrue(FP16.lessEquals(FP16.NEGATIVE_ZERO, FP16.POSITIVE_ZERO)); |
| 326 |
assertFalse(FP16.lessEquals(FP16.NaN, FP16.toHalf(12.3f))); |
| 327 |
assertFalse(FP16.lessEquals(FP16.toHalf(12.3f), FP16.NaN)); |
| 328 |
assertTrue(FP16.lessEquals(FP16.MIN_VALUE, FP16.MIN_NORMAL)); |
| 329 |
assertFalse(FP16.lessEquals(FP16.MIN_NORMAL, FP16.MIN_VALUE)); |
| 330 |
assertTrue(FP16.lessEquals(FP16.toHalf(12.3f), FP16.toHalf(12.4f))); |
| 331 |
assertFalse(FP16.lessEquals(FP16.toHalf(12.4f), FP16.toHalf(12.3f))); |
| 332 |
assertFalse(FP16.lessEquals(FP16.toHalf(-12.3f), FP16.toHalf(-12.4f))); |
| 333 |
assertTrue(FP16.lessEquals(FP16.toHalf(-12.4f), FP16.toHalf(-12.3f))); |
| 334 |
assertTrue(FP16.lessEquals(FP16.MIN_VALUE, (short) 0x3ff)); |
| 335 |
assertTrue(FP16.lessEquals(FP16.NEGATIVE_INFINITY, FP16.NEGATIVE_INFINITY)); |
| 336 |
assertTrue(FP16.lessEquals(FP16.POSITIVE_INFINITY, FP16.POSITIVE_INFINITY)); |
| 337 |
assertTrue(FP16.lessEquals(FP16.toHalf(12.12356f), FP16.toHalf(12.12356f))); |
| 338 |
assertTrue(FP16.lessEquals(FP16.toHalf(-12.12356f), FP16.toHalf(-12.12356f))); |
| 339 |
|
| 340 |
assertTrue(FP16.lessEquals(FP16.toHalf(-1.0f), FP16.toHalf(0.0f))); |
| 341 |
assertFalse(FP16.lessEquals(FP16.toHalf(0.0f), FP16.toHalf(-1.0f))); |
| 342 |
assertTrue(FP16.lessEquals(FP16.toHalf(-1.0f), FP16.toHalf(-1.0f))); |
| 343 |
assertTrue(FP16.lessEquals(FP16.toHalf(-1.3f), FP16.toHalf(-1.3f))); |
| 344 |
assertFalse(FP16.lessEquals(FP16.toHalf(1.0f), FP16.toHalf(0.0f))); |
| 345 |
assertTrue(FP16.lessEquals(FP16.toHalf(0.0f), FP16.toHalf(1.0f))); |
| 346 |
assertTrue(FP16.lessEquals(FP16.toHalf(1.0f), FP16.toHalf(1.0f))); |
| 347 |
assertTrue(FP16.lessEquals(FP16.toHalf(1.3f), FP16.toHalf(1.3f))); |
| 348 |
assertTrue(FP16.lessEquals(FP16.toHalf(-0.1f), FP16.toHalf(0.0f))); |
| 349 |
assertFalse(FP16.lessEquals(FP16.toHalf(0.0f), FP16.toHalf(-0.1f))); |
| 350 |
assertTrue(FP16.lessEquals(FP16.toHalf(-0.1f), FP16.toHalf(-0.1f))); |
| 351 |
assertFalse(FP16.lessEquals(FP16.toHalf(0.1f), FP16.toHalf(0.0f))); |
| 352 |
assertTrue(FP16.lessEquals(FP16.toHalf(0.0f), FP16.toHalf(0.1f))); |
| 353 |
assertTrue(FP16.lessEquals(FP16.toHalf(0.1f), FP16.toHalf(0.1f))); |
| 354 |
} |
| 355 |
|
| 356 |
public static void testCompare() { |
| 357 |
assertEquals(0, FP16.compare(FP16.NaN, FP16.NaN)); |
| 358 |
assertEquals(0, FP16.compare(FP16.NaN, FP16_ALT_NAN)); |
| 359 |
assertEquals(0, FP16.compare(FP16_ALT_NAN, FP16.NaN)); |
| 360 |
assertEquals(1, FP16.compare(FP16.NaN, FP16.POSITIVE_INFINITY)); |
| 361 |
assertEquals(-1, FP16.compare(FP16.POSITIVE_INFINITY, FP16.NaN)); |
| 362 |
|
| 363 |
assertEquals(0, FP16.compare(FP16.POSITIVE_INFINITY, FP16.POSITIVE_INFINITY)); |
| 364 |
assertEquals(0, FP16.compare(FP16.NEGATIVE_INFINITY, FP16.NEGATIVE_INFINITY)); |
| 365 |
assertEquals(1, FP16.compare(FP16.POSITIVE_INFINITY, FP16.NEGATIVE_INFINITY)); |
| 366 |
assertEquals(-1, FP16.compare(FP16.NEGATIVE_INFINITY, FP16.POSITIVE_INFINITY)); |
| 367 |
|
| 368 |
assertEquals(0, FP16.compare(FP16.POSITIVE_ZERO, FP16.POSITIVE_ZERO)); |
| 369 |
assertEquals(0, FP16.compare(FP16.NEGATIVE_ZERO, FP16.NEGATIVE_ZERO)); |
| 370 |
assertEquals(1, FP16.compare(FP16.POSITIVE_ZERO, FP16.NEGATIVE_ZERO)); |
| 371 |
assertEquals(-1, FP16.compare(FP16.NEGATIVE_ZERO, FP16.POSITIVE_ZERO)); |
| 372 |
|
| 373 |
assertEquals(0, FP16.compare(FP16.toHalf(12.462f), FP16.toHalf(12.462f))); |
| 374 |
assertEquals(0, FP16.compare(FP16.toHalf(-12.462f), FP16.toHalf(-12.462f))); |
| 375 |
assertEquals(1, FP16.compare(FP16.toHalf(12.462f), FP16.toHalf(-12.462f))); |
| 376 |
assertEquals(-1, FP16.compare(FP16.toHalf(-12.462f), FP16.toHalf(12.462f))); |
| 377 |
|
| 378 |
assertEquals(1, FP16.compare(FP16.NaN, FP16.toHalf(12.462f))); |
| 379 |
assertEquals(1, FP16.compare(FP16.NaN, FP16.toHalf(-12.462f))); |
| 380 |
assertEquals(1, FP16.compare(FP16.NaN, FP16.EPSILON)); |
| 381 |
assertEquals(1, FP16.compare(FP16.NaN, FP16.LOWEST_VALUE)); |
| 382 |
assertEquals(1, FP16.compare(FP16.NaN, FP16.MAX_VALUE)); |
| 383 |
assertEquals(1, FP16.compare(FP16.NaN, FP16.MIN_NORMAL)); |
| 384 |
assertEquals(1, FP16.compare(FP16.NaN, FP16.MIN_VALUE)); |
| 385 |
assertEquals(1, FP16.compare(FP16.NaN, FP16.NEGATIVE_INFINITY)); |
| 386 |
assertEquals(1, FP16.compare(FP16.NaN, FP16.NEGATIVE_ZERO)); |
| 387 |
assertEquals(1, FP16.compare(FP16.NaN, FP16.POSITIVE_ZERO)); |
| 388 |
|
| 389 |
assertEquals(-1, FP16.compare(FP16.toHalf(12.462f), FP16.NaN)); |
| 390 |
assertEquals(-1, FP16.compare(FP16.toHalf(-12.462f), FP16.NaN)); |
| 391 |
assertEquals(-1, FP16.compare(FP16.EPSILON, FP16.NaN)); |
| 392 |
assertEquals(-1, FP16.compare(FP16.LOWEST_VALUE, FP16.NaN)); |
| 393 |
assertEquals(-1, FP16.compare(FP16.MAX_VALUE, FP16.NaN)); |
| 394 |
assertEquals(-1, FP16.compare(FP16.MIN_NORMAL, FP16.NaN)); |
| 395 |
assertEquals(-1, FP16.compare(FP16.MIN_VALUE, FP16.NaN)); |
| 396 |
assertEquals(-1, FP16.compare(FP16.NEGATIVE_INFINITY, FP16.NaN)); |
| 397 |
assertEquals(-1, FP16.compare(FP16.NEGATIVE_ZERO, FP16.NaN)); |
| 398 |
assertEquals(-1, FP16.compare(FP16.POSITIVE_ZERO, FP16.NaN)); |
| 399 |
|
| 400 |
assertEquals(1, FP16.compare(FP16.POSITIVE_INFINITY, FP16.NEGATIVE_ZERO)); |
| 401 |
assertEquals(1, FP16.compare(FP16.POSITIVE_INFINITY, FP16.POSITIVE_ZERO)); |
| 402 |
assertEquals(1, FP16.compare(FP16.POSITIVE_INFINITY, FP16.toHalf(12.462f))); |
| 403 |
assertEquals(1, FP16.compare(FP16.POSITIVE_INFINITY, FP16.toHalf(-12.462f))); |
| 404 |
assertEquals(-1, FP16.compare(FP16.NEGATIVE_INFINITY, FP16.toHalf(12.462f))); |
| 405 |
assertEquals(-1, FP16.compare(FP16.NEGATIVE_INFINITY, FP16.toHalf(-12.462f))); |
| 406 |
assertEquals(-1, FP16.compare(FP16.NEGATIVE_INFINITY, FP16.NEGATIVE_ZERO)); |
| 407 |
assertEquals(-1, FP16.compare(FP16.NEGATIVE_INFINITY, FP16.POSITIVE_ZERO)); |
| 408 |
|
| 409 |
assertEquals(-1, FP16.compare(FP16.NEGATIVE_ZERO, FP16.toHalf(12.462f))); |
| 410 |
assertEquals(1, FP16.compare(FP16.NEGATIVE_ZERO, FP16.toHalf(-12.462f))); |
| 411 |
} |
| 412 |
|
| 413 |
/// CHECK-START-ARM64: void Main.testCheckCompare() disassembly (after) |
| 414 |
/// CHECK-IF: hasIsaFeature("fp16") |
| 415 |
/// CHECK: InvokeStaticOrDirect intrinsic:FP16Compare |
| 416 |
/// CHECK: fcmp {{h\d+}}, {{h\d+}} |
| 417 |
/// CHECK-ELSE: |
| 418 |
/// CHECK: InvokeStaticOrDirect intrinsic:FP16Compare |
| 419 |
/// CHECK-NOT: fcmp {{h\d+}}, {{h\d+}} |
| 420 |
/// CHECK-FI: |
| 421 |
|
| 422 |
public static void testCheckCompare() { |
| 423 |
assertEquals(0, FP16.compare(FP16.toHalf(12.462f), FP16.toHalf(12.462f))); |
| 424 |
} |
| 425 |
|
| 426 |
public static void assertMinPermutations(short small, short large){ |
| 427 |
assertEquals(small, FP16.min(small, large)); |
| 428 |
assertEquals(small, FP16.min(large, small)); |
| 429 |
|
| 430 |
assertEquals(small, FP16.min(small, small)); |
| 431 |
assertEquals(large, FP16.min(large, large)); |
| 432 |
} |
| 433 |
|
| 434 |
public static void testMin() { |
| 435 |
assertMinPermutations(FP16.NEGATIVE_INFINITY, FP16.POSITIVE_INFINITY); |
| 436 |
assertMinPermutations(FP16.NEGATIVE_ZERO,FP16.POSITIVE_ZERO); |
| 437 |
assertMinPermutations(FP16.NEGATIVE_INFINITY, FP16.LOWEST_VALUE); |
| 438 |
assertMinPermutations(FP16.MAX_VALUE, FP16.POSITIVE_INFINITY); |
| 439 |
assertMinPermutations(FP16.MIN_VALUE, FP16.MIN_NORMAL); |
| 440 |
assertMinPermutations(FP16.POSITIVE_ZERO, FP16.MIN_VALUE); |
| 441 |
assertMinPermutations(FP16.POSITIVE_ZERO, FP16.MIN_NORMAL); |
| 442 |
assertMinPermutations(FP16.toHalf(-3.456f), FP16.toHalf(-3.453f)); |
| 443 |
assertMinPermutations(FP16.toHalf(3.453f), FP16.toHalf(3.456f)); |
| 444 |
assertMinPermutations(FP16.toHalf(-3.456f), FP16.toHalf(3.456f)); |
| 445 |
assertMinPermutations(FP16.NaN, FP16.LOWEST_VALUE); |
| 446 |
|
| 447 |
assertEquals(FP16.NaN, FP16.min(FP16.NaN, FP16_ALT_NAN)); |
| 448 |
assertEquals(FP16.NaN, FP16.min(FP16_ALT_NAN, FP16.NaN)); |
| 449 |
assertEquals(FP16.NaN, FP16.min(FP16.NaN, FP16.NaN)); |
| 450 |
assertEquals(FP16.NaN, FP16.min(FP16_ALT_NAN, FP16_ALT_NAN)); |
| 451 |
} |
| 452 |
|
| 453 |
/// CHECK-START-ARM64: void Main.testCheckMin() disassembly (after) |
| 454 |
/// CHECK-IF: hasIsaFeature("fp16") |
| 455 |
/// CHECK: InvokeStaticOrDirect intrinsic:FP16Min |
| 456 |
/// CHECK: fcmp {{h\d+}}, {{h\d+}} |
| 457 |
/// CHECK-ELSE: |
| 458 |
/// CHECK: InvokeStaticOrDirect intrinsic:FP16Min |
| 459 |
/// CHECK-NOT: fcmp {{h\d+}}, {{h\d+}} |
| 460 |
/// CHECK-FI: |
| 461 |
public static void testCheckMin() { |
| 462 |
assertEquals(FP16.toHalf(-3.456f), FP16.min(FP16.toHalf(-3.456f), FP16.toHalf(-3.453f))); |
| 463 |
} |
| 464 |
|
| 465 |
public static void assertMaxPermutations(short small, short large){ |
| 466 |
assertEquals(large, FP16.max(small, large)); |
| 467 |
assertEquals(large, FP16.max(large, small)); |
| 468 |
|
| 469 |
assertEquals(small, FP16.max(small, small)); |
| 470 |
assertEquals(large, FP16.max(large, large)); |
| 471 |
} |
| 472 |
|
| 473 |
public static void testMax() { |
| 474 |
assertMaxPermutations(FP16.NEGATIVE_INFINITY, FP16.POSITIVE_INFINITY); |
| 475 |
assertMaxPermutations(FP16.NEGATIVE_ZERO,FP16.POSITIVE_ZERO); |
| 476 |
assertMaxPermutations(FP16.NEGATIVE_INFINITY, FP16.LOWEST_VALUE); |
| 477 |
assertMaxPermutations(FP16.MAX_VALUE, FP16.POSITIVE_INFINITY); |
| 478 |
assertMaxPermutations(FP16.MIN_VALUE, FP16.MIN_NORMAL); |
| 479 |
assertMaxPermutations(FP16.POSITIVE_ZERO, FP16.MIN_VALUE); |
| 480 |
assertMaxPermutations(FP16.POSITIVE_ZERO, FP16.MIN_NORMAL); |
| 481 |
assertMaxPermutations(FP16.toHalf(-3.456f), FP16.toHalf(-3.453f)); |
| 482 |
assertMaxPermutations(FP16.toHalf(3.453f), FP16.toHalf(3.456f)); |
| 483 |
assertMaxPermutations(FP16.toHalf(-3.456f), FP16.toHalf(3.456f)); |
| 484 |
assertMaxPermutations(FP16.MAX_VALUE, FP16.NaN); |
| 485 |
|
| 486 |
assertEquals(FP16.NaN, FP16.max(FP16.NaN, FP16_ALT_NAN)); |
| 487 |
assertEquals(FP16.NaN, FP16.max(FP16_ALT_NAN, FP16.NaN)); |
| 488 |
assertEquals(FP16.NaN, FP16.max(FP16.NaN, FP16.NaN)); |
| 489 |
assertEquals(FP16.NaN, FP16.max(FP16_ALT_NAN, FP16_ALT_NAN)); |
| 490 |
} |
| 491 |
|
| 492 |
/// CHECK-START-ARM64: void Main.testCheckMax() disassembly (after) |
| 493 |
/// CHECK-IF: hasIsaFeature("fp16") |
| 494 |
/// CHECK: InvokeStaticOrDirect intrinsic:FP16Max |
| 495 |
/// CHECK: fcmp {{h\d+}}, {{h\d+}} |
| 496 |
/// CHECK-ELSE: |
| 497 |
/// CHECK: InvokeStaticOrDirect intrinsic:FP16Max |
| 498 |
/// CHECK-NOT: fcmp {{h\d+}}, {{h\d+}} |
| 499 |
/// CHECK-FI: |
| 500 |
public static void testCheckMax() { |
| 501 |
assertEquals(FP16.toHalf(-3.453f), FP16.max(FP16.toHalf(-3.456f), FP16.toHalf(-3.453f))); |
| 502 |
} |
| 503 |
|
| 504 |
public static void main(String args[]) { |
| 505 |
testHalfToFloatToHalfConversions(); |
| 506 |
testToHalf(); |
| 507 |
testToFloat(); |
| 508 |
testFloor(); |
| 509 |
testCeil(); |
| 510 |
testRint(); |
| 511 |
testGreater(); |
| 512 |
testGreaterEquals(); |
| 513 |
testLessEquals(); |
| 514 |
testLess(); |
| 515 |
testCompare(); |
| 516 |
testCheckCompare(); |
| 517 |
testMin(); |
| 518 |
testCheckMin(); |
| 519 |
testMax(); |
| 520 |
testCheckMax(); |
| 521 |
} |
| 522 |
} |