Skip to content

Commit 753edbc

Browse files
author
Adam Hrbac
committed
Rewrite some bitshifts using >>>
1 parent bdff557 commit 753edbc

File tree

1 file changed

+5
-9
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/contextvars

1 file changed

+5
-9
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/contextvars/Hamt.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@
5252
*
5353
* It may make sense to use a sealed interface here eventually, rather than dispatching manually
5454
* with instanceof.
55-
*
56-
* Care must be taken when using right shift (>>), as it sign-extends, It is generally needed to use
57-
* bitmasking, unlike in the cpython implementation/original paper
5855
*/
5956

6057
public final class Hamt {
@@ -102,8 +99,7 @@ private Hamt(TreePart root) {
10299
}
103100

104101
private static int hashIdx(int hash, int hashShift) {
105-
// since we never access the bits the shift adds here, it is fine that it is adding ones to
106-
// the start
102+
// Since we mask off the high 2 bits of the hash, it is always positive
107103
return hashTail(hash >> hashShift);
108104
}
109105

@@ -121,9 +117,9 @@ private static int popcount(int n) {
121117
}
122118

123119
private static int bitmapToIdx(int bitmap, int position) {
124-
if ((bitmap & (1 << position)) != 0) {
125-
// java cannot do unsigned shifts, so we need to mask off the low bits instead
126-
return popcount(bitmap & (-1 << position)) - 1;
120+
int shiftedBitmap = bitmap >>> position;
121+
if ((shiftedBitmap & 1) == 1) {
122+
return popcount(shiftedBitmap) - 1;
127123
} else {
128124
return -1;
129125
}
@@ -166,7 +162,7 @@ private static TreePart partWithEntry(TreePart original, Entry newEntry, int has
166162
newElems[position] = newEntry;
167163
int elemsI = originalLength - 1;
168164
for (int i = 0; i < 32; ++i) {
169-
if (((existing.bitmap) & (1 << i)) != 0) {
165+
if (((existing.bitmap >>> i) & 1) == 1) {
170166
newElems[i] = existing.elems[elemsI--];
171167
}
172168
}

0 commit comments

Comments
 (0)