Ningsheng Jian | 675e09b | 2014-10-23 13:48:36 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | public class Main { |
| 18 | |
| 19 | static double dPi = Math.PI; |
| 20 | static float fPi = (float)Math.PI; |
| 21 | |
| 22 | public static void expectEquals(long expected, long result) { |
| 23 | if (expected != result) { |
| 24 | throw new Error("Expected: " + expected + ", found: " + result); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | public static void expectEquals(int expected, int result) { |
| 29 | if (expected != result) { |
| 30 | throw new Error("Expected: " + expected + ", found: " + result); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | public static void divDoubleTest() { |
| 35 | double d1 = 0x1.0p1023; |
| 36 | double d2 = -2.0; |
| 37 | double d3 = 0.0; |
| 38 | double d4 = Double.MIN_NORMAL; |
| 39 | double d5 = Double.POSITIVE_INFINITY; |
| 40 | double d6 = Double.NEGATIVE_INFINITY; |
| 41 | double d7 = -0.0; |
| 42 | double d8 = Double.MAX_VALUE; |
| 43 | double d9 = Double.MIN_VALUE; |
Andreas Gampe | 7d9e5a9 | 2015-03-09 19:29:48 -0700 | [diff] [blame] | 44 | double dNaN = Double.NaN; |
Ningsheng Jian | 675e09b | 2014-10-23 13:48:36 +0800 | [diff] [blame] | 45 | |
| 46 | expectEquals(Double.doubleToRawLongBits(dPi/d1), 0x1921fb54442d18L); |
| 47 | expectEquals(Double.doubleToRawLongBits(dPi/d2), 0xbff921fb54442d18L); |
| 48 | expectEquals(Double.doubleToRawLongBits(dPi/d3), 0x7ff0000000000000L); |
| 49 | expectEquals(Double.doubleToRawLongBits(dPi/d4), 0x7fe921fb54442d18L); |
| 50 | expectEquals(Double.doubleToRawLongBits(dPi/d5), 0x0L); |
| 51 | expectEquals(Double.doubleToRawLongBits(dPi/d6), 0x8000000000000000L); |
| 52 | expectEquals(Double.doubleToRawLongBits(dPi/d7), 0xfff0000000000000L); |
| 53 | |
| 54 | expectEquals(Double.doubleToRawLongBits(dPi/d8), 0xc90fdaa22168cL); |
| 55 | expectEquals(Double.doubleToRawLongBits(dPi/d9), 0x7ff0000000000000L); |
Andreas Gampe | 7d9e5a9 | 2015-03-09 19:29:48 -0700 | [diff] [blame] | 56 | |
| 57 | // Not-a-number computation. Use doubleToLongBits to get canonical NaN. The literal value |
| 58 | // is the canonical NaN (see Double.doubleToLongBits). |
| 59 | expectEquals(Double.doubleToLongBits(dPi/dNaN), 0x7ff8000000000000L); |
Ningsheng Jian | 675e09b | 2014-10-23 13:48:36 +0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | public static void divFloatTest() { |
| 63 | float f1 = 0x1.0p127f; |
| 64 | float f2 = -2.0f; |
| 65 | float f3 = 0.0f; |
| 66 | float f4 = Float.MIN_NORMAL; |
| 67 | float f5 = Float.POSITIVE_INFINITY; |
| 68 | float f6 = Float.NEGATIVE_INFINITY; |
| 69 | float f7 = -0.0f; |
| 70 | float f8 = Float.MAX_VALUE; |
| 71 | float f9 = Float.MIN_VALUE; |
Andreas Gampe | 7d9e5a9 | 2015-03-09 19:29:48 -0700 | [diff] [blame] | 72 | float fNaN = Float.NaN; |
Ningsheng Jian | 675e09b | 2014-10-23 13:48:36 +0800 | [diff] [blame] | 73 | |
| 74 | expectEquals(Float.floatToRawIntBits(fPi/f1), 0xc90fdb); |
| 75 | expectEquals(Float.floatToRawIntBits(fPi/f2), 0xbfc90fdb); |
| 76 | expectEquals(Float.floatToRawIntBits(fPi/f3), 0x7f800000); |
| 77 | expectEquals(Float.floatToRawIntBits(fPi/f4), 0x7f490fdb); |
| 78 | expectEquals(Float.floatToRawIntBits(fPi/f5), 0x0); |
| 79 | expectEquals(Float.floatToRawIntBits(fPi/f6), 0x80000000); |
| 80 | expectEquals(Float.floatToRawIntBits(fPi/f7), 0xff800000); |
| 81 | |
| 82 | expectEquals(Float.floatToRawIntBits(fPi/f8), 0x6487ee); |
| 83 | expectEquals(Float.floatToRawIntBits(fPi/f9), 0x7f800000); |
Andreas Gampe | 7d9e5a9 | 2015-03-09 19:29:48 -0700 | [diff] [blame] | 84 | |
| 85 | // Not-a-number computation. Use floatToIntBits to get canonical NaN. The literal value |
| 86 | // is the canonical NaN (see Float.floatToIntBits). |
| 87 | expectEquals(Float.floatToIntBits(fPi/fNaN), 0x7fc00000); |
Ningsheng Jian | 675e09b | 2014-10-23 13:48:36 +0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | public static void main(String[] args) { |
| 91 | divDoubleTest(); |
| 92 | divFloatTest(); |
| 93 | System.out.println("Done!"); |
| 94 | } |
| 95 | |
| 96 | } |