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.hi32(int) intrinsics_recognition (after)
     20   /// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect intrinsic:IntegerHighestOneBit
     21   /// CHECK-DAG:                 Return [<<Result>>]
     22   private static int hi32(int x) {
     23     return Integer.highestOneBit(x);
     24   }
     25 
     26   /// CHECK-START: int Main.lo32(int) intrinsics_recognition (after)
     27   /// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect intrinsic:IntegerLowestOneBit
     28   /// CHECK-DAG:                 Return [<<Result>>]
     29   private static int lo32(int x) {
     30     return Integer.lowestOneBit(x);
     31   }
     32 
     33   /// CHECK-START: long Main.hi64(long) intrinsics_recognition (after)
     34   /// CHECK-DAG: <<Result:j\d+>> InvokeStaticOrDirect intrinsic:LongHighestOneBit
     35   /// CHECK-DAG:                 Return [<<Result>>]
     36   private static long hi64(long x) {
     37     return Long.highestOneBit(x);
     38   }
     39 
     40   /// CHECK-START: long Main.lo64(long) intrinsics_recognition (after)
     41   /// CHECK-DAG: <<Result:j\d+>> InvokeStaticOrDirect intrinsic:LongLowestOneBit
     42   /// CHECK-DAG:                 Return [<<Result>>]
     43   private static long lo64(long x) {
     44     return Long.lowestOneBit(x);
     45   }
     46 
     47   public static void main(String args[]) {
     48     // Hidden zeros.
     49     int[] xi = new int[32];
     50     long[] xj = new long[64];
     51 
     52     expectEquals32(0x00000000, hi32(0x00000000));
     53     expectEquals32(0x00000000, lo32(0x00000000));
     54     expectEquals32(0x00010000, hi32(0x00010000));
     55     expectEquals32(0x00010000, lo32(0x00010000));
     56     expectEquals32(0x00800000, hi32(0x00FF0000));
     57     expectEquals32(0x00010000, lo32(0x00FF0000));
     58     expectEquals32(0x80000000, hi32(0xFFFFFFFF));
     59     expectEquals32(0x00000001, lo32(0xFFFFFFFF));
     60 
     61     for (int i = 0; i < 32; i++) {
     62       expectEquals32(0, hi32(xi[i]));
     63       expectEquals32(0, lo32(xi[i]));
     64       expectEquals32(1 << i, hi32(1 << i));
     65       expectEquals32(1 << i, lo32(1 << i));
     66       int expected = i < 29 ? 0x8 << i : 0x80000000;
     67       expectEquals32(expected, hi32(0xF << i));
     68       expectEquals32(0x1 << i, lo32(0xF << i));
     69     }
     70 
     71     expectEquals64(0x0000000000000000L, hi64(0x0000000000000000L));
     72     expectEquals64(0x0000000000000000L, lo64(0x0000000000000000L));
     73     expectEquals64(0x0000000100000000L, hi64(0x0000000100000000L));
     74     expectEquals64(0x0000000100000000L, lo64(0x0000000100000000L));
     75     expectEquals64(0x0000008000000000L, hi64(0x000000FF00000000L));
     76     expectEquals64(0x0000000100000000L, lo64(0x000000FF00000000L));
     77     expectEquals64(0x8000000000000000L, hi64(0xFFFFFFFFFFFFFFFFL));
     78     expectEquals64(0x0000000000000001L, lo64(0xFFFFFFFFFFFFFFFFL));
     79 
     80     for (int i = 0; i < 64; i++) {
     81       expectEquals64(0L, hi64(xj[i]));
     82       expectEquals64(0L, lo64(xj[i]));
     83       expectEquals64(1L << i, hi64(1L << i));
     84       expectEquals64(1L << i, lo64(1L << i));
     85       long expected = i < 61 ? 0x8L << i : 0x8000000000000000L;
     86       expectEquals64(expected, hi64(0xFL << i));
     87       expectEquals64(0x1L << i, lo64(0xFL << i));
     88     }
     89 
     90     System.out.println("passed");
     91   }
     92 
     93   private static void expectEquals32(int expected, int result) {
     94     if (expected != result) {
     95       throw new Error("Expected: " + expected + ", found: " + result);
     96     }
     97   }
     98   private static void expectEquals64(long expected, long result) {
     99     if (expected != result) {
    100       throw new Error("Expected: " + expected + ", found: " + result);
    101     }
    102   }
    103 }
    104