diff --git a/user/super/com/google/gwt/emul/java/lang/Integer.java b/user/super/com/google/gwt/emul/java/lang/Integer.java index a56e31e423..d349bdab09 100644 --- a/user/super/com/google/gwt/emul/java/lang/Integer.java +++ b/user/super/com/google/gwt/emul/java/lang/Integer.java @@ -184,24 +184,15 @@ public static int reverseBytes(int i) { } public static int rotateLeft(int i, int distance) { - while (distance-- > 0) { - i = i << 1 | ((i < 0) ? 1 : 0); - } - return i; + int lowerBits = i >>> (SIZE - distance); + int upperBits = i << distance; + return upperBits | lowerBits; } public static int rotateRight(int i, int distance) { - int ui = i & MAX_VALUE; // avoid sign extension - int carry = (i < 0) ? 0x40000000 : 0; // MIN_VALUE rightshifted 1 - while (distance-- > 0) { - int nextcarry = ui & 1; - ui = carry | (ui >> 1); - carry = (nextcarry == 0) ? 0 : 0x40000000; - } - if (carry != 0) { - ui = ui | MIN_VALUE; - } - return ui; + int upperBits = i << (SIZE - distance); + int lowerBits = i >>> distance; + return upperBits | lowerBits; } public static int signum(int i) { diff --git a/user/super/com/google/gwt/emul/java/lang/Long.java b/user/super/com/google/gwt/emul/java/lang/Long.java index 8950a41236..33622aa6b4 100644 --- a/user/super/com/google/gwt/emul/java/lang/Long.java +++ b/user/super/com/google/gwt/emul/java/lang/Long.java @@ -128,24 +128,15 @@ public static long reverseBytes(long l) { } public static long rotateLeft(long i, int distance) { - while (distance-- > 0) { - i = i << 1 | ((i < 0) ? 1 : 0); - } - return i; + long lowerBits = i >>> (SIZE - distance); + long upperBits = i << distance; + return upperBits | lowerBits; } public static long rotateRight(long i, int distance) { - long ui = i & MAX_VALUE; // avoid sign extension - long carry = (i < 0) ? 0x4000000000000000L : 0; // MIN_VALUE rightshifted 1 - while (distance-- > 0) { - long nextcarry = ui & 1; - ui = carry | (ui >> 1); - carry = (nextcarry == 0) ? 0 : 0x4000000000000000L; - } - if (carry != 0) { - ui = ui | MIN_VALUE; - } - return ui; + long upperBits = i << (SIZE - distance); + long lowerBits = i >>> distance; + return upperBits | lowerBits; } public static int signum(long i) { diff --git a/user/test/com/google/gwt/emultest/java/lang/IntegerTest.java b/user/test/com/google/gwt/emultest/java/lang/IntegerTest.java index 4f93f55aa9..64c9d1395e 100644 --- a/user/test/com/google/gwt/emultest/java/lang/IntegerTest.java +++ b/user/test/com/google/gwt/emultest/java/lang/IntegerTest.java @@ -234,6 +234,9 @@ public void testRotateLeft() { assertEquals(-1, Integer.rotateLeft(-1, 4)); assertEquals(Integer.MIN_VALUE, Integer.rotateLeft(0x40000000, 1)); assertEquals(1, Integer.rotateLeft(Integer.MIN_VALUE, 1)); + assertEquals(Integer.MIN_VALUE, Integer.rotateLeft(Integer.MIN_VALUE, 0)); + assertEquals(Integer.MIN_VALUE, Integer.rotateLeft(1, -1)); + assertEquals(-1, Integer.rotateLeft(-1, 1000)); } public void testRotateRight() { @@ -241,6 +244,9 @@ public void testRotateRight() { assertEquals(Integer.MIN_VALUE, Integer.rotateRight(1, 1)); assertEquals(0x10000000, Integer.rotateRight(1, 4)); assertEquals(-1, Integer.rotateRight(-1, 4)); + assertEquals(Integer.MIN_VALUE, Integer.rotateLeft(Integer.MIN_VALUE, 0)); + assertEquals(1, Integer.rotateRight(Integer.MIN_VALUE, -1)); + assertEquals(-1, Integer.rotateRight(-1, 1000)); } public void testSignum() { diff --git a/user/test/com/google/gwt/emultest/java/lang/LongTest.java b/user/test/com/google/gwt/emultest/java/lang/LongTest.java index 37fb27d634..d4f30a8b2b 100644 --- a/user/test/com/google/gwt/emultest/java/lang/LongTest.java +++ b/user/test/com/google/gwt/emultest/java/lang/LongTest.java @@ -224,6 +224,9 @@ public void testRotateLeft() { assertEquals(-1, Long.rotateLeft(-1, 4)); assertEquals(Long.MIN_VALUE, Long.rotateLeft(0x4000000000000000L, 1)); assertEquals(1, Long.rotateLeft(Long.MIN_VALUE, 1)); + assertEquals(Long.MIN_VALUE, Long.rotateLeft(Long.MIN_VALUE, 0)); + assertEquals(Long.MIN_VALUE, Long.rotateLeft(1, -1)); + assertEquals(-1, Long.rotateLeft(-1, 1000)); } public void testRotateRight() { @@ -231,6 +234,9 @@ public void testRotateRight() { assertEquals(Long.MIN_VALUE, Long.rotateRight(1, 1)); assertEquals(0x1000000000000000L, Long.rotateRight(1, 4)); assertEquals(-1, Long.rotateRight(-1, 4)); + assertEquals(Long.MIN_VALUE, Long.rotateRight(Long.MIN_VALUE, 0)); + assertEquals(1, Long.rotateRight(Long.MIN_VALUE, -1)); + assertEquals(-1, Long.rotateRight(-1, 1000)); } public void testSignum() {