Skip to content

Commit

Permalink
WASM: Intrinsify numberOfLeadingZeros, numberOfTrailingZeros, and bit…
Browse files Browse the repository at this point in the history
…Count (#978)
  • Loading branch information
lax1dude authored Dec 3, 2024
1 parent 516602d commit 61e8c85
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/src/main/java/org/teavm/backend/wasm/WasmTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
import org.teavm.backend.wasm.render.WasmCRenderer;
import org.teavm.backend.wasm.render.WasmRenderer;
import org.teavm.backend.wasm.runtime.WasmSupport;
import org.teavm.backend.wasm.transformation.BaseClassesTransformation;
import org.teavm.backend.wasm.transformation.IndirectCallTraceTransformation;
import org.teavm.backend.wasm.transformation.MemoryAccessTraceTransformation;
import org.teavm.backend.wasm.transformation.WasiFileSystemProviderTransformer;
Expand Down Expand Up @@ -241,6 +242,7 @@ public VariableCategoryProvider variableCategoryProvider() {
public List<ClassHolderTransformer> getTransformers() {
List<ClassHolderTransformer> transformers = new ArrayList<>();
transformers.add(new ClassPatch());
transformers.add(new BaseClassesTransformation());
transformers.add(new WasmDependencyListener());
if (runtimeType == WasmRuntimeType.WASI) {
transformers.add(new WasiSupportClassTransformer());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.teavm.backend.wasm.model.expression.WasmIntBinary;
import org.teavm.backend.wasm.model.expression.WasmIntBinaryOperation;
import org.teavm.backend.wasm.model.expression.WasmIntType;
import org.teavm.backend.wasm.model.expression.WasmIntUnary;
import org.teavm.backend.wasm.model.expression.WasmIntUnaryOperation;
import org.teavm.model.MethodReference;

public class IntegerIntrinsic implements WasmIntrinsic {
Expand All @@ -38,6 +40,9 @@ public boolean isApplicable(MethodReference methodReference) {
case "divideUnsigned":
case "remainderUnsigned":
case "compareUnsigned":
case "numberOfLeadingZeros":
case "numberOfTrailingZeros":
case "bitCount":
return true;
default:
return false;
Expand All @@ -59,6 +64,15 @@ public WasmExpression apply(InvocationExpr invocation, WasmIntrinsicManager mana
return new WasmCall(manager.getFunctions().forStaticMethod(COMPARE_UNSIGNED),
manager.generate(invocation.getArguments().get(0)),
manager.generate(invocation.getArguments().get(1)));
case "numberOfLeadingZeros":
return new WasmIntUnary(WasmIntType.INT32, WasmIntUnaryOperation.CLZ,
manager.generate(invocation.getArguments().get(0)));
case "numberOfTrailingZeros":
return new WasmIntUnary(WasmIntType.INT32, WasmIntUnaryOperation.CTZ,
manager.generate(invocation.getArguments().get(0)));
case "bitCount":
return new WasmIntUnary(WasmIntType.INT32, WasmIntUnaryOperation.POPCNT,
manager.generate(invocation.getArguments().get(0)));
default:
throw new AssertionError();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@

import org.teavm.ast.InvocationExpr;
import org.teavm.backend.wasm.WasmRuntime;
import org.teavm.backend.wasm.model.WasmNumType;
import org.teavm.backend.wasm.model.expression.WasmCall;
import org.teavm.backend.wasm.model.expression.WasmConversion;
import org.teavm.backend.wasm.model.expression.WasmExpression;
import org.teavm.backend.wasm.model.expression.WasmIntBinary;
import org.teavm.backend.wasm.model.expression.WasmIntBinaryOperation;
import org.teavm.backend.wasm.model.expression.WasmIntType;
import org.teavm.backend.wasm.model.expression.WasmIntUnary;
import org.teavm.backend.wasm.model.expression.WasmIntUnaryOperation;
import org.teavm.model.MethodReference;

public class LongIntrinsic implements WasmIntrinsic {
Expand All @@ -38,6 +42,9 @@ public boolean isApplicable(MethodReference methodReference) {
case "divideUnsigned":
case "remainderUnsigned":
case "compareUnsigned":
case "numberOfLeadingZeros":
case "numberOfTrailingZeros":
case "bitCount":
return true;
default:
return false;
Expand All @@ -59,8 +66,21 @@ public WasmExpression apply(InvocationExpr invocation, WasmIntrinsicManager mana
return new WasmCall(manager.getFunctions().forStaticMethod(COMPARE_UNSIGNED),
manager.generate(invocation.getArguments().get(0)),
manager.generate(invocation.getArguments().get(1)));
case "numberOfLeadingZeros":
return castToInt(new WasmIntUnary(WasmIntType.INT64, WasmIntUnaryOperation.CLZ,
manager.generate(invocation.getArguments().get(0))));
case "numberOfTrailingZeros":
return castToInt(new WasmIntUnary(WasmIntType.INT64, WasmIntUnaryOperation.CTZ,
manager.generate(invocation.getArguments().get(0))));
case "bitCount":
return castToInt(new WasmIntUnary(WasmIntType.INT64, WasmIntUnaryOperation.POPCNT,
manager.generate(invocation.getArguments().get(0))));
default:
throw new AssertionError();
}
}

private WasmExpression castToInt(WasmExpression expr) {
return new WasmConversion(WasmNumType.INT64, WasmNumType.INT32, false, expr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@

import org.teavm.ast.InvocationExpr;
import org.teavm.backend.wasm.WasmRuntime;
import org.teavm.backend.wasm.model.WasmNumType;
import org.teavm.backend.wasm.model.expression.WasmCall;
import org.teavm.backend.wasm.model.expression.WasmConversion;
import org.teavm.backend.wasm.model.expression.WasmExpression;
import org.teavm.backend.wasm.model.expression.WasmIntBinary;
import org.teavm.backend.wasm.model.expression.WasmIntBinaryOperation;
import org.teavm.backend.wasm.model.expression.WasmIntType;
import org.teavm.backend.wasm.model.expression.WasmIntUnary;
import org.teavm.backend.wasm.model.expression.WasmIntUnaryOperation;
import org.teavm.model.MethodReference;

public class IntNumIntrinsic implements WasmGCIntrinsic {
Expand All @@ -48,8 +52,25 @@ public WasmExpression apply(InvocationExpr invocation, WasmGCIntrinsicContext co
return new WasmCall(context.functions().forStaticMethod(compareUnsigned),
context.generate(invocation.getArguments().get(0)),
context.generate(invocation.getArguments().get(1)));
case "numberOfLeadingZeros":
return castToInt(new WasmIntUnary(wasmType, WasmIntUnaryOperation.CLZ,
context.generate(invocation.getArguments().get(0))));
case "numberOfTrailingZeros":
return castToInt(new WasmIntUnary(wasmType, WasmIntUnaryOperation.CTZ,
context.generate(invocation.getArguments().get(0))));
case "bitCount":
return castToInt(new WasmIntUnary(wasmType, WasmIntUnaryOperation.POPCNT,
context.generate(invocation.getArguments().get(0))));
default:
throw new AssertionError();
}
}

private WasmExpression castToInt(WasmExpression expr) {
if (wasmType == WasmIntType.INT64) {
return new WasmConversion(WasmNumType.INT64, WasmNumType.INT32, false, expr);
} else {
return expr;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ private void fillIntNum(Class<?> javaClass, Class<?> wrapperClass, WasmIntType w
add(new MethodReference(wrapperClass, "divideUnsigned", javaClass, javaClass, javaClass), intrinsic);
add(new MethodReference(wrapperClass, "remainderUnsigned", javaClass, javaClass, javaClass), intrinsic);
add(new MethodReference(wrapperClass, "compareUnsigned", javaClass, javaClass, int.class), intrinsic);
add(new MethodReference(wrapperClass, "numberOfLeadingZeros", javaClass, int.class), intrinsic);
add(new MethodReference(wrapperClass, "numberOfTrailingZeros", javaClass, int.class), intrinsic);
add(new MethodReference(wrapperClass, "bitCount", javaClass, int.class), intrinsic);
}

private void fillFloat() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2024 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.backend.wasm.transformation;

import org.teavm.model.ClassHolder;
import org.teavm.model.ClassHolderTransformer;
import org.teavm.model.ClassHolderTransformerContext;
import org.teavm.model.ElementModifier;

public class BaseClassesTransformation implements ClassHolderTransformer {

@Override
public void transformClass(ClassHolder cls, ClassHolderTransformerContext context) {
if (cls.getName().equals("java.lang.Integer") || cls.getName().equals("java.lang.Long")) {
for (var method : cls.getMethods()) {
switch (method.getName()) {
case "numberOfLeadingZeros":
case "numberOfTrailingZeros":
case "bitCount":
method.setProgram(null);
method.getModifiers().add(ElementModifier.NATIVE);
break;
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ public void transformClass(ClassHolder cls, ClassHolderTransformerContext contex
var clear = cls.getMethod(new MethodDescriptor("clear", void.class));
clear.getModifiers().add(ElementModifier.NATIVE);
clear.setProgram(null);
} else if (cls.getName().equals("java.lang.Integer") || cls.getName().equals("java.lang.Long")) {
for (var method : cls.getMethods()) {
switch (method.getName()) {
case "numberOfLeadingZeros":
case "numberOfTrailingZeros":
case "bitCount":
method.setProgram(null);
method.getModifiers().add(ElementModifier.NATIVE);
break;
}
}
}
}

Expand Down
29 changes: 29 additions & 0 deletions tests/src/test/java/org/teavm/classlib/java/lang/LongTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,35 @@ public void bitsReversed() {
assertEquals(0xF63BA00000000000L, Long.reverse(0x5DC6F));
}

@Test
public void numberOfLeadingZerosComputed() {
assertEquals(0, Long.numberOfLeadingZeros(-1L));
assertEquals(1, Long.numberOfLeadingZeros(0x4000000000000000L));
assertEquals(1, Long.numberOfLeadingZeros(0x4000000000000123L));
assertEquals(1, Long.numberOfLeadingZeros(0x7FFFFFFFFFFFFFFFL));
assertEquals(63, Long.numberOfLeadingZeros(1L));
assertEquals(62, Long.numberOfLeadingZeros(2L));
assertEquals(62, Long.numberOfLeadingZeros(3L));
assertEquals(0, Long.numberOfLeadingZeros(0x8000000000000000L));
assertEquals(0, Long.numberOfLeadingZeros(0x8000000000000123L));
assertEquals(0, Long.numberOfLeadingZeros(0xFFFFFFFFFFFFFFFFL));
assertEquals(64, Long.numberOfLeadingZeros(0L));
}

@Test
public void numberOfTrailingZerosComputed() {
assertEquals(1, Long.numberOfTrailingZeros(0xFFFFFFFFFFFFFFFEL));
assertEquals(1, Long.numberOfTrailingZeros(0x4000000000000002L));
assertEquals(1, Long.numberOfTrailingZeros(0x0000000000000002L));
assertEquals(63, Long.numberOfTrailingZeros(0x8000000000000000L));
assertEquals(62, Long.numberOfTrailingZeros(0x4000000000000000L));
assertEquals(62, Long.numberOfTrailingZeros(0xC000000000000000L));
assertEquals(0, Long.numberOfTrailingZeros(0x0000000000000001L));
assertEquals(0, Long.numberOfTrailingZeros(0x1230000000000003L));
assertEquals(0, Long.numberOfTrailingZeros(0xFFFFFFFFFFFFFFFFL));
assertEquals(64, Long.numberOfTrailingZeros(0L));
}

@Test
public void highestOneBit() {
assertEquals(1L << 63, Long.highestOneBit(-1L));
Expand Down

0 comments on commit 61e8c85

Please sign in to comment.