ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a.@apache.org
Subject ignite git commit: IGNITE-5859 IgniteUtils.ceilPow2 overflow for values greater than 2^30
Date Tue, 01 Aug 2017 14:03:00 GMT
Repository: ignite
Updated Branches:
  refs/heads/master 5224c9de4 -> 160dab095


IGNITE-5859 IgniteUtils.ceilPow2 overflow for values greater than 2^30


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/160dab09
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/160dab09
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/160dab09

Branch: refs/heads/master
Commit: 160dab09587a4c6ebdcfd71368360cfcb153575b
Parents: 5224c9d
Author: Vitaliy Biryukov <BiryukovVitaliy92@gmail.com>
Authored: Tue Aug 1 17:02:53 2017 +0300
Committer: Anton Vinogradov <av@apache.org>
Committed: Tue Aug 1 17:02:53 2017 +0300

----------------------------------------------------------------------
 .../ignite/internal/util/IgniteUtils.java       |  4 ++-
 .../internal/util/IgniteUtilsSelfTest.java      | 26 ++++++++++++++++++++
 2 files changed, 29 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/160dab09/modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java b/modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java
index 4116d07..524286c 100755
--- a/modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java
@@ -987,7 +987,9 @@ public abstract class IgniteUtils {
      * @return Nearest power of 2.
      */
     public static int ceilPow2(int v) {
-        return Integer.highestOneBit(v - 1) << 1;
+        int i = v - 1;
+
+        return Integer.highestOneBit(i) << 1 - (i >>> 30 ^ v >> 31);
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/ignite/blob/160dab09/modules/core/src/test/java/org/apache/ignite/internal/util/IgniteUtilsSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/util/IgniteUtilsSelfTest.java
b/modules/core/src/test/java/org/apache/ignite/internal/util/IgniteUtilsSelfTest.java
index 057d343..963c1d9 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/util/IgniteUtilsSelfTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/util/IgniteUtilsSelfTest.java
@@ -822,6 +822,32 @@ public class IgniteUtilsSelfTest extends GridCommonAbstractTest {
     }
 
     /**
+     *
+     */
+    public void testCeilPow2() throws Exception {
+        assertEquals(2, U.ceilPow2(2));
+        assertEquals(4, U.ceilPow2(3));
+        assertEquals(4, U.ceilPow2(4));
+        assertEquals(8, U.ceilPow2(5));
+        assertEquals(8, U.ceilPow2(6));
+        assertEquals(8, U.ceilPow2(7));
+        assertEquals(8, U.ceilPow2(8));
+        assertEquals(16, U.ceilPow2(9));
+        assertEquals(1 << 15, U.ceilPow2((1 << 15) - 1));
+        assertEquals(1 << 15, U.ceilPow2(1 << 15));
+        assertEquals(1 << 16, U.ceilPow2((1 << 15) + 1));
+        assertEquals(1 << 26, U.ceilPow2((1 << 26) - 100));
+        assertEquals(1 << 26, U.ceilPow2(1 << 26));
+        assertEquals(1 << 27, U.ceilPow2((1 << 26) + 100));
+
+        for (int i = (int)Math.pow(2, 30); i < Integer.MAX_VALUE; i++)
+            assertEquals((int)Math.pow(2, 30), U.ceilPow2(i));
+
+        for (int i = Integer.MIN_VALUE; i < 0; i++)
+            assertEquals(0, U.ceilPow2(i));
+    }
+
+    /**
      * Test enum.
      */
     private enum TestEnum {


Mime
View raw message