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.round32(float) intrinsics_recognition (after)
     20   /// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect intrinsic:MathRoundFloat
     21   /// CHECK-DAG:                 Return [<<Result>>]
     22   private static int round32(float f) {
     23     return Math.round(f);
     24   }
     25 
     26   /// CHECK-START: long Main.round64(double) intrinsics_recognition (after)
     27   /// CHECK-DAG: <<Result:j\d+>> InvokeStaticOrDirect intrinsic:MathRoundDouble
     28   /// CHECK-DAG:                 Return [<<Result>>]
     29   private static long round64(double d) {
     30     return Math.round(d);
     31   }
     32 
     33   public static void main(String args[]) {
     34     // A few obvious numbers.
     35     expectEquals32(-2147483648, round32(Float.NEGATIVE_INFINITY));
     36     expectEquals32(-2, round32(-1.51f));
     37     expectEquals32(-1, round32(-1.2f));
     38     expectEquals32(-1, round32(-1.0f));
     39     expectEquals32(-1, round32(-0.5000001f));
     40     expectEquals32(0, round32(-0.5f));
     41     expectEquals32(0, round32(-0.2f));
     42     expectEquals32(0, round32(-0.0f));
     43     expectEquals32(0, round32(+0.0f));
     44     expectEquals32(0, round32(+0.2f));
     45     expectEquals32(1, round32(+0.5f));
     46     expectEquals32(1, round32(+1.0f));
     47     expectEquals32(1, round32(+1.2f));
     48     expectEquals32(2, round32(+1.5f));
     49     expectEquals32(2147483647, round32(Float.POSITIVE_INFINITY));
     50 
     51     // Near minint.
     52     expectEquals32(-2147483648, round32(Math.nextAfter(-2147483648.0f, Float.NEGATIVE_INFINITY)));
     53     expectEquals32(-2147483648, round32(-2147483648.0f));
     54     expectEquals32(-2147483520, round32(Math.nextAfter(-2147483648.0f, Float.POSITIVE_INFINITY)));
     55 
     56     // Near maxint.
     57     expectEquals32(2147483520, round32(Math.nextAfter(2147483648.0f, Float.NEGATIVE_INFINITY)));
     58     expectEquals32(2147483647, round32(2147483648.0f));
     59     expectEquals32(2147483647, round32(Math.nextAfter(2147483648.0f, Float.POSITIVE_INFINITY)));
     60 
     61     // Some others.
     62     for (int i = -100; i <= 100; ++i) {
     63       expectEquals32(i - 1, round32((float) i - 0.51f));
     64       expectEquals32(i, round32((float) i - 0.5f));
     65       expectEquals32(i, round32((float) i));
     66       expectEquals32(i + 1, round32((float) i + 0.5f));
     67       expectEquals32(i + 1, round32((float) i + 0.51f));
     68     }
     69     for (float f = -1.5f; f <= -1.499f; f = Math.nextAfter(f, Float.POSITIVE_INFINITY)) {
     70       expectEquals32(-1, round32(f));
     71     }
     72 
     73     // Some harder.
     74     float[] fvals = {
     75       -16777215.5f,
     76       -16777215.0f,
     77       -0.49999998f,
     78       -0.4999999701976776123046875f,
     79       0.4999999701976776123046875f,
     80       0.49999998f,
     81       16777215.0f,
     82       16777215.5f
     83     };
     84     int[] ivals = {
     85       -16777216,
     86       -16777215,
     87       0,
     88       0,
     89       0,
     90       0,
     91       16777215,
     92       16777216
     93     };
     94     for (int i = 0; i < fvals.length; i++) {
     95       expectEquals32(ivals[i], round32(fvals[i]));
     96     }
     97 
     98     // A few NaN numbers.
     99     float[] fnans = {
    100       Float.intBitsToFloat(0x7f800001),
    101       Float.intBitsToFloat(0x7fa00000),
    102       Float.intBitsToFloat(0x7fc00000),
    103       Float.intBitsToFloat(0x7fffffff),
    104       Float.intBitsToFloat(0xff800001),
    105       Float.intBitsToFloat(0xffa00000),
    106       Float.intBitsToFloat(0xffc00000),
    107       Float.intBitsToFloat(0xffffffff)
    108     };
    109     for (int i = 0; i < fnans.length; i++) {
    110       expectEquals32(0, round32(fnans[i]));
    111     }
    112 
    113     // A few obvious numbers.
    114     expectEquals64(-9223372036854775808L, round64(Double.NEGATIVE_INFINITY));
    115     expectEquals64(-2L, round64(-1.51d));
    116     expectEquals64(-1L, round64(-1.2d));
    117     expectEquals64(-1L, round64(-1.0d));
    118     expectEquals64(-1L, round64(-0.5000001f));
    119     expectEquals64(0L, round64(-0.5d));
    120     expectEquals64(0L, round64(-0.2d));
    121     expectEquals64(0L, round64(-0.0d));
    122     expectEquals64(0L, round64(+0.0d));
    123     expectEquals64(0L, round64(+0.2d));
    124     expectEquals64(1L, round64(+0.5d));
    125     expectEquals64(1L, round64(+1.0d));
    126     expectEquals64(1L, round64(+1.2d));
    127     expectEquals64(2L, round64(+1.5d));
    128     expectEquals64(9223372036854775807L, round64(Double.POSITIVE_INFINITY));
    129 
    130     // Near minlong.
    131     expectEquals64(-9223372036854775808L,
    132         round64(Math.nextAfter(-9223372036854775808.0, Double.NEGATIVE_INFINITY)));
    133     expectEquals64(-9223372036854775808L, round64(-9223372036854775808.0));
    134     expectEquals64(-9223372036854774784L,
    135         round64(Math.nextAfter(-9223372036854775809.0, Double.POSITIVE_INFINITY)));
    136 
    137     // Near maxlong.
    138     expectEquals64(9223372036854774784L,
    139         round64(Math.nextAfter(9223372036854775808.0, Double.NEGATIVE_INFINITY)));
    140     expectEquals64(9223372036854775807L, round64(9223372036854775808.0));
    141     expectEquals64(9223372036854775807L,
    142         round64(Math.nextAfter(9223372036854775808.0, Double.POSITIVE_INFINITY)));
    143 
    144     // Some others.
    145     for (long l = -100; l <= 100; ++l) {
    146       expectEquals64(l - 1, round64((double) l - 0.51d));
    147       expectEquals64(l, round64((double) l - 0.5d));
    148       expectEquals64(l, round64((double) l));
    149       expectEquals64(l + 1, round64((double) l + 0.5d));
    150       expectEquals64(l + 1, round64((double) l + 0.51d));
    151     }
    152     for (double d = -1.5d; d <= -1.49999999999d; d = Math.nextAfter(d, Double.POSITIVE_INFINITY)) {
    153       expectEquals64(-1L, round64(d));
    154     }
    155 
    156     // Some harder.
    157     double[] dvals = {
    158       -9007199254740991.5d,
    159       -9007199254740991.0d,
    160       -0.49999999999999997d,
    161       -0.49999999999999994d,
    162       0.49999999999999994d,
    163       0.49999999999999997d,
    164       9007199254740991.0d,
    165       9007199254740991.5d
    166     };
    167     long[] lvals = {
    168       -9007199254740992L,
    169       -9007199254740991L,
    170       0L,
    171       0L,
    172       0L,
    173       0L,
    174       9007199254740991L,
    175       9007199254740992L
    176     };
    177     for (int i = 0; i < dvals.length; i++) {
    178       expectEquals64(lvals[i], round64(dvals[i]));
    179     }
    180 
    181     // A few NaN numbers.
    182     double[] dnans = {
    183       Double.longBitsToDouble(0x7ff0000000000001L),
    184       Double.longBitsToDouble(0x7ff4000000000000L),
    185       Double.longBitsToDouble(0x7ff8000000000000L),
    186       Double.longBitsToDouble(0x7fffffffffffffffL),
    187       Double.longBitsToDouble(0xfff0000000000001L),
    188       Double.longBitsToDouble(0xfff4000000000000L),
    189       Double.longBitsToDouble(0xfff8000000000000L),
    190       Double.longBitsToDouble(0xffffffffffffffffL)
    191     };
    192     for (int i = 0; i < dnans.length; i++) {
    193       expectEquals64(0L, round64(dnans[i]));
    194     }
    195 
    196     System.out.println("passed");
    197   }
    198 
    199   private static void expectEquals32(int expected, int result) {
    200     if (expected != result) {
    201       throw new Error("Expected: " + expected + ", found: " + result);
    202     }
    203   }
    204 
    205   private static void expectEquals64(long expected, long result) {
    206     if (expected != result) {
    207       throw new Error("Expected: " + expected + ", found: " + result);
    208     }
    209   }
    210 }
    211