Implement CountLeadingZeros for x86
Generate Long and Integer numberOfLeadingZeros for x86 and x86_64. Uses
'bsr' instruction to find the first one bit, and then corrects the
result.
Added some more tests with constant values to test constant folding.
Also add a runtime test with 0 as the input.
Change-Id: I920b21bb00069bccf5f921f8f87a77e334114926
Signed-off-by: Mark Mendell <mark.p.mendell@intel.com>
diff --git a/test/082-inline-execute/src/Main.java b/test/082-inline-execute/src/Main.java
index 77c1a99..bd606a6 100644
--- a/test/082-inline-execute/src/Main.java
+++ b/test/082-inline-execute/src/Main.java
@@ -1043,8 +1043,20 @@
return (r1 / i1) + (r2 / i2) + i3 + i4 + i5 + i6 + i7 + i8;
}
+ public static boolean doThrow = false;
+
+ public static int $noinline$return_int_zero() {
+ if (doThrow) {
+ throw new Error();
+ }
+ return 0;
+ }
+
public static void test_Integer_numberOfLeadingZeros() {
Assert.assertEquals(Integer.numberOfLeadingZeros(0), Integer.SIZE);
+ Assert.assertEquals(Integer.numberOfLeadingZeros(1), Integer.SIZE - 1);
+ Assert.assertEquals(Integer.numberOfLeadingZeros(1 << (Integer.SIZE-1)), 0);
+ Assert.assertEquals(Integer.numberOfLeadingZeros($noinline$return_int_zero()), Integer.SIZE);
for (int i = 0; i < Integer.SIZE; i++) {
Assert.assertEquals(Integer.numberOfLeadingZeros(1 << i), Integer.SIZE - 1 - i);
Assert.assertEquals(Integer.numberOfLeadingZeros((1 << i) | 1), Integer.SIZE - 1 - i);
@@ -1052,8 +1064,19 @@
}
}
+ public static long $noinline$return_long_zero() {
+ if (doThrow) {
+ throw new Error();
+ }
+ return 0;
+ }
+
public static void test_Long_numberOfLeadingZeros() {
Assert.assertEquals(Long.numberOfLeadingZeros(0L), Long.SIZE);
+ Assert.assertEquals(Long.numberOfLeadingZeros(1L), Long.SIZE - 1);
+ Assert.assertEquals(Long.numberOfLeadingZeros(1L << ((Long.SIZE/2)-1)), Long.SIZE/2);
+ Assert.assertEquals(Long.numberOfLeadingZeros(1L << (Long.SIZE-1)), 0);
+ Assert.assertEquals(Long.numberOfLeadingZeros($noinline$return_long_zero()), Long.SIZE);
for (int i = 0; i < Long.SIZE; i++) {
Assert.assertEquals(Long.numberOfLeadingZeros(1L << i), Long.SIZE - 1 - i);
Assert.assertEquals(Long.numberOfLeadingZeros((1L << i) | 1L), Long.SIZE - 1 - i);