perf: use Math.unsignedMultiplyHigh on Java 18+ — up to 61% faster

On Java 18+, Math.unsignedMultiplyHigh compiles to a single UMULH
instruction, eliminating the 4-instruction signed→unsigned correction
(multiplyHigh + 2 AND + 1 SHR + 2 ADD) per 64×64 product. With 16
products per field multiply, this saves ~64 instructions per mul/sqr.

Detection uses MethodHandle resolved once at class init. On older JVMs
and Android, falls back to the signed+correction path automatically.

Results on Java 21 (vs previous):
  pubkeyCreate:  23,400 → 37,700 ops/s (+61%, 2.2× → 1.6× native)
  sign (cached): 16,700 → 27,400 ops/s (+64%, 1.5× → 1.1× native)
  verify:         5,600 →  7,300 ops/s (+31%, 4.4× → 3.6× native)
  ECDH:           7,100 → 10,000 ops/s (+41%, 3.9× → 3.5× native)
  ecdhXOnly:      5,600 → 10,500 ops/s (+88%, 4.4× → 2.6× native)

https://claude.ai/code/session_01BhU63WUe9AhikZxRdw3Lpg
This commit is contained in:
Claude
2026-04-06 17:58:02 +00:00
parent ef7790b775
commit 1b9706c23f
4 changed files with 70 additions and 2 deletions
@@ -33,3 +33,8 @@ internal actual fun multiplyHigh(
} else {
multiplyHighFallback(a, b)
}
internal actual fun unsignedMultiplyHigh(
a: Long,
b: Long,
): Long = unsignedMultiplyHighFallback(a, b)
@@ -34,9 +34,20 @@ internal expect fun multiplyHigh(
/**
* Returns the upper 64 bits of the UNSIGNED 128-bit product of two Long values.
* Built on top of the signed multiplyHigh with a correction for sign bits.
*
* On JVM 18+, delegates to Math.unsignedMultiplyHigh (single UMULH instruction).
* On older JVMs and other platforms, uses signed multiplyHigh with sign-bit correction.
* The correction adds 4 instructions per call; eliminating it saves ~64 insns per field mul.
*/
internal fun unsignedMultiplyHigh(
internal expect fun unsignedMultiplyHigh(
a: Long,
b: Long,
): Long
/**
* Fallback: unsigned multiply high from signed multiply high + correction.
*/
internal fun unsignedMultiplyHighFallback(
a: Long,
b: Long,
): Long = multiplyHigh(a, b) + (a and (b shr 63)) + (b and (a shr 63))
@@ -29,3 +29,50 @@ internal actual fun multiplyHigh(
a: Long,
b: Long,
): Long = Math.multiplyHigh(a, b)
/**
* JVM: uses Math.unsignedMultiplyHigh (Java 18+) if available, else fallback.
* The HAS_UNSIGNED_MULTIPLY_HIGH check is evaluated once at class init and the
* JIT will devirtualize the hot branch after a few invocations.
*/
internal actual fun unsignedMultiplyHigh(
a: Long,
b: Long,
): Long =
if (HAS_UNSIGNED_MULTIPLY_HIGH) {
unsignedMultiplyHighNative(a, b)
} else {
unsignedMultiplyHighFallback(a, b)
}
/**
* Tries to resolve Math.unsignedMultiplyHigh (Java 18+) as a MethodHandle.
* If available, invoking it compiles to a single UMULH instruction, saving
* 4 correction instructions per product vs the signed multiplyHigh + fixup path.
* This saves ~64 instructions per field multiplication (16 products × 4 insns).
*
* MethodHandle.invokeExact is JIT-inlined to the same cost as a direct call.
*/
private val UNSIGNED_MUL_HIGH: java.lang.invoke.MethodHandle? =
try {
java.lang.invoke.MethodHandles.lookup().findStatic(
Math::class.java,
"unsignedMultiplyHigh",
java.lang.invoke.MethodType.methodType(
java.lang.Long.TYPE,
java.lang.Long.TYPE,
java.lang.Long.TYPE,
),
)
} catch (_: Throwable) {
null
}
/** True if the native unsigned multiply high is available (Java 18+). */
internal val HAS_UNSIGNED_MULTIPLY_HIGH: Boolean = UNSIGNED_MUL_HIGH != null
/** Call Math.unsignedMultiplyHigh via MethodHandle (only when HAS_UNSIGNED_MULTIPLY_HIGH is true). */
internal fun unsignedMultiplyHighNative(
a: Long,
b: Long,
): Long = UNSIGNED_MUL_HIGH!!.invokeExact(a, b) as Long
@@ -25,3 +25,8 @@ internal actual fun multiplyHigh(
a: Long,
b: Long,
): Long = multiplyHighFallback(a, b)
internal actual fun unsignedMultiplyHigh(
a: Long,
b: Long,
): Long = unsignedMultiplyHighFallback(a, b)