Skip to content

Commit

Permalink
Added support for compiling in Java 8 and running in Java 21
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-lawrey committed Apr 15, 2024
1 parent 9d416be commit caaa54c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 9 deletions.
38 changes: 35 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<parent>
<groupId>net.openhft</groupId>
<artifactId>java-parent-pom</artifactId>
<version>1.25.4</version>
<version>1.26.0-SNAPSHOT</version>
<relativePath />
</parent>

Expand Down Expand Up @@ -332,7 +332,7 @@
<profile>
<id>jdk9+-profile</id>
<activation>
<jdk>[9,)</jdk>
<jdk>[9,21)</jdk>
</activation>
<properties>
<maven.compiler.release>${project.target.release}</maven.compiler.release>
Expand All @@ -356,7 +356,39 @@
</plugins>
</build>
</profile>

<profile>
<id>jdk21-profile</id>
<activation>
<jdk>[21,)</jdk>
</activation>
<properties>
<maven.compiler.release>8</maven.compiler.release>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<failOnWarning>false</failOnWarning>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>test-without-compact-string</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<argLine>@{argLine} -XX:-CompactStrings ${jvm.requiredArgs}</argLine>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>sonar</id>
<build>
Expand Down
32 changes: 26 additions & 6 deletions src/main/java/net/openhft/hashing/Maths.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@

import org.jetbrains.annotations.NotNull;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Method;

class Maths {
@NotNull
private static final Maths INSTANCE;

static {
boolean hasMultiplyHigh = true;
Maths maths = null;
try {
Math.multiplyHigh(0, 0);
Method multiplyHigh = Math.class.getDeclaredMethod("multiplyHigh", int.class, int.class);
MethodHandle multiplyHighMH = MethodHandles.lookup().unreflect(multiplyHigh);
maths = new MathsJDK9(multiplyHighMH);
} catch (final Throwable ignore) {
hasMultiplyHigh = false;
maths = new Maths();
}
INSTANCE = hasMultiplyHigh ? new MathsJDK9() : new Maths();
INSTANCE = maths;
}

public static long unsignedLongMulXorFold(final long lhs, final long rhs) {
Expand Down Expand Up @@ -62,16 +68,30 @@ long unsignedLongMulHighImp(final long lhs, final long rhs) {
}

class MathsJDK9 extends Maths {
private final MethodHandle multiplyHighMH;

public MathsJDK9(MethodHandle multiplyHighMH) {
this.multiplyHighMH = multiplyHighMH;
}

// Math.multiplyHigh() is intrinsified from JDK 10. But JDK 9 is out of life, we always prefer
// this version to the scalar one.
@Override
long unsignedLongMulXorFoldImp(final long lhs, final long rhs) {
final long upper = Math.multiplyHigh(lhs, rhs) + ((lhs >> 63) & rhs) + ((rhs >> 63) & lhs);
final long upper = invokeExact(lhs, rhs) + ((lhs >> 63) & rhs) + ((rhs >> 63) & lhs);
final long lower = lhs * rhs;
return lower ^ upper;
}
@Override
long unsignedLongMulHighImp(final long lhs, final long rhs) {
return Math.multiplyHigh(lhs, rhs) + ((lhs >> 63) & rhs) + ((rhs >> 63) & lhs);
return invokeExact(lhs, rhs) + ((lhs >> 63) & rhs) + ((rhs >> 63) & lhs);
}

private long invokeExact(long lhs, long rhs) {
try {
return (long) multiplyHighMH.invokeExact(lhs, rhs);
} catch (Throwable e) {
throw new AssertionError(e);
}
}
}

0 comments on commit caaa54c

Please sign in to comment.