Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2016 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   /// CHECK-START: int Main.f2int(float) instruction_simplifier (before)
     20   /// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect intrinsic:FloatFloatToIntBits
     21   /// CHECK-DAG: Return [<<Result>>]
     22   //
     23   /// CHECK-START: int Main.f2int(float) instruction_simplifier (after)
     24   // Note: The ArtMethod* (typed as int or long) is optional after sharpening.
     25   /// CHECK-DAG: <<Raw:i\d+>> InvokeStaticOrDirect [<<Arg:f\d+>>{{(,[ij]\d+)?}}] intrinsic:FloatFloatToRawIntBits
     26   /// CHECK-DAG: <<Cond:z\d+>> NotEqual [<<Arg>>,<<Arg>>]
     27   /// CHECK-DAG: <<Result:i\d+>> Select [<<Raw>>,{{i\d+}},<<Cond>>]
     28   /// CHECK-DAG: Return [<<Result>>]
     29   private static int f2int(float f) {
     30     return Float.floatToIntBits(f);
     31   }
     32 
     33   /// CHECK-START: long Main.d2long(double) instruction_simplifier (before)
     34   /// CHECK-DAG: <<Result:j\d+>> InvokeStaticOrDirect intrinsic:DoubleDoubleToLongBits
     35   /// CHECK-DAG: Return [<<Result>>]
     36   //
     37   /// CHECK-START: long Main.d2long(double) instruction_simplifier (after)
     38   // Note: The ArtMethod* (typed as int or long) is optional after sharpening.
     39   /// CHECK-DAG: <<Raw:j\d+>> InvokeStaticOrDirect [<<Arg:d\d+>>{{(,[ij]\d+)?}}] intrinsic:DoubleDoubleToRawLongBits
     40   /// CHECK-DAG: <<Cond:z\d+>> NotEqual [<<Arg>>,<<Arg>>]
     41   /// CHECK-DAG: <<Result:j\d+>> Select [<<Raw>>,{{j\d+}},<<Cond>>]
     42   /// CHECK-DAG: Return [<<Result>>]
     43   private static long d2long(double d) {
     44     return Double.doubleToLongBits(d);
     45   }
     46 
     47   public static void main(String args[]) {
     48     // A few distinct numbers.
     49     expectEquals32(0xff800000, f2int(Float.NEGATIVE_INFINITY));
     50     expectEquals32(0xbf800000, f2int(-1.0f));
     51     expectEquals32(0x80000000, f2int(-0.0f));
     52     expectEquals32(0x00000000, f2int(+0.0f));
     53     expectEquals32(0x3f800000, f2int(+1.0f));
     54     expectEquals32(0x7f800000, f2int(Float.POSITIVE_INFINITY));
     55 
     56     // A few others.
     57     for (int i = 0; i <= 100; i++) {
     58       expectEquals32(i, f2int(Float.intBitsToFloat(i)));
     59     }
     60 
     61     // A few NaN numbers.
     62     float[] fvals = {
     63       Float.intBitsToFloat(0x7f800001),
     64       Float.intBitsToFloat(0x7fa00000),
     65       Float.intBitsToFloat(0x7fc00000),
     66       Float.intBitsToFloat(0x7fffffff),
     67       Float.intBitsToFloat(0xff800001),
     68       Float.intBitsToFloat(0xffa00000),
     69       Float.intBitsToFloat(0xffc00000),
     70       Float.intBitsToFloat(0xffffffff)
     71     };
     72     for (int i = 0; i < fvals.length; i++) {
     73       expectEquals32(0x7fc00000, f2int(fvals[i]));
     74     }
     75 
     76     // A few distinct numbers.
     77     expectEquals64(0xfff0000000000000L, d2long(Double.NEGATIVE_INFINITY));
     78     expectEquals64(0xbff0000000000000L, d2long(-1.0d));
     79     expectEquals64(0x8000000000000000L, d2long(-0.0d));
     80     expectEquals64(0x0000000000000000L, d2long(+0.0d));
     81     expectEquals64(0x3ff0000000000000L, d2long(+1.0d));
     82     expectEquals64(0x7ff0000000000000L, d2long(Double.POSITIVE_INFINITY));
     83 
     84     // A few others.
     85     for (long l = 0; l <= 100; l++) {
     86       expectEquals64(l, d2long(Double.longBitsToDouble(l)));
     87     }
     88 
     89     // A few NaN numbers.
     90     double[] dvals = {
     91       Double.longBitsToDouble(0x7ff0000000000001L),
     92       Double.longBitsToDouble(0x7ff4000000000000L),
     93       Double.longBitsToDouble(0x7ff8000000000000L),
     94       Double.longBitsToDouble(0x7fffffffffffffffL),
     95       Double.longBitsToDouble(0xfff0000000000001L),
     96       Double.longBitsToDouble(0xfff4000000000000L),
     97       Double.longBitsToDouble(0xfff8000000000000L),
     98       Double.longBitsToDouble(0xffffffffffffffffL)
     99     };
    100     for (int i = 0; i < dvals.length; i++) {
    101       expectEquals64(0x7ff8000000000000L, d2long(dvals[i]));
    102     }
    103 
    104     System.out.println("passed");
    105   }
    106 
    107   private static void expectEquals32(int expected, int result) {
    108     if (expected != result) {
    109       throw new Error("Expected: "
    110           + Integer.toHexString(expected)
    111           + ", found: "
    112           + Integer.toHexString(result));
    113     }
    114   }
    115 
    116   private static void expectEquals64(long expected, long result) {
    117     if (expected != result) {
    118       throw new Error("Expected: "
    119           + Long.toHexString(expected)
    120           + ", found: "
    121           + Long.toHexString(result));
    122     }
    123   }
    124 }
    125