Skip to content

Commit a201f53

Browse files
committed
Avoid using Vector API's BIT_COUNT
1 parent 80e0f2a commit a201f53

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

wasm/src/org.graalvm.wasm.jdk25/src/org/graalvm/wasm/api/Vector128OpsVectorAPI.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ public ByteVector unary(ByteVector xVec, int vectorOpcode) {
323323
case Bytecode.VECTOR_V128_NOT -> unop(x, I8X16, VectorOperators.NOT);
324324
case Bytecode.VECTOR_I8X16_ABS -> unop(x, I8X16, VectorOperators.ABS);
325325
case Bytecode.VECTOR_I8X16_NEG -> unop(x, I8X16, VectorOperators.NEG);
326-
case Bytecode.VECTOR_I8X16_POPCNT -> unop(x, I8X16, VectorOperators.BIT_COUNT);
326+
case Bytecode.VECTOR_I8X16_POPCNT -> i8x16_popcnt(x); // GR-68892
327327
case Bytecode.VECTOR_I16X8_EXTADD_PAIRWISE_I8X16_S -> extadd_pairwise(x, I8X16, VectorOperators.B2S);
328328
case Bytecode.VECTOR_I16X8_EXTADD_PAIRWISE_I8X16_U -> extadd_pairwise(x, I8X16, VectorOperators.ZERO_EXTEND_B2S);
329329
case Bytecode.VECTOR_I16X8_EXTEND_LOW_I8X16_S -> extend(x, 0, I8X16, VectorOperators.B2S);
@@ -747,6 +747,13 @@ private static <E> ByteVector unop(ByteVector xBytes, Shape<E> shape, VectorOper
747747
return result.reinterpretAsBytes();
748748
}
749749

750+
private static ByteVector i8x16_popcnt(ByteVector x) {
751+
// Based on the same approach as Integer#bitCount
752+
ByteVector popcnt = x.sub(x.lanewise(VectorOperators.LSHR, 1).and((byte) 0x55));
753+
popcnt = popcnt.and((byte) 0x33).add(popcnt.lanewise(VectorOperators.LSHR, 2).and((byte) 0x33));
754+
return popcnt.add(popcnt.lanewise(VectorOperators.LSHR, 4)).and((byte) 0x0F);
755+
}
756+
750757
private static <E, F> ByteVector extadd_pairwise(ByteVector xBytes, Shape<E> shape, VectorOperators.Conversion<E, F> conv) {
751758
Vector<E> x = shape.reinterpret(xBytes);
752759
Vector<F> evens = x.compress(shape.evensMask).convert(conv, 0);

0 commit comments

Comments
 (0)