← Back to Pull requests

perf: simd double math binary ops #20875

Open
clintropolis opened this pull request with 1747 lines diff across 25 files
View on GitHub
clintropolis clintropolis commented on Nov 10, 2026 10:37 UTC
Author

Description


Adds simd wiring for POWER, ATAN2, and HYPOT (as well as SQL binding for hypot)

Benchmarks:
| # | Op | Shape | Scalar (ms/op) | SIMD (ms/op) | Speedup |
|---|---|---|---:|---:|---:|
| 97 | POWER | dd | 45.31 | 39.91 | 1.14× |
| 98 | POWER | dl | 46.79 | 40.30 | 1.16× |
| 99 | POWER | ld | 54.36 | 36.07 | 1.51× |
| 100 | POWER | ll | 55.62 | 36.21 | 1.54× |
| 101 | ATAN2 | dd | 54.39 | 28.79 | 1.89× |
| 102 | ATAN2 | dl | 34.59 | 25.34 | 1.37× |
| 103 | ATAN2 | ld | 42.43 | 25.20 | 1.68× |
| 104 | ATAN2 | ll | 43.22 | 24.83 | 1.74× |
| 105 | HYPOT | dd | 43.98 | 23.42 | 1.88× |
| 106 | HYPOT | dl | 29.60 | 19.61 | 1.51× |
| 107 | HYPOT | ld | 30.43 | 19.59 | 1.55× |
| 108 | HYPOT | ll | 29.31 | 19.64 | 1.49× |

Diff: 1747 lines (+1719 / -28) across 25 files Reviewer: amaechler Review duration: 8781m
All Vouch review confidence checks passed
Residual risk 0.06 = Change Risk (0.72) × (1 − Review Confidence (0.91)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.72 · 1747 lines diff across 25 files
0.72
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.91 · 5 substantive comment(s)
0.91
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
91%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.400
Mass File Touch mass_file_touch
0.080
Real PR Conversation Timeline
12 items
FrankChen021 FrankChen021 commented on code on Nov 10, 2026 12:33 UTC
Code Review
processing/src/test/java/org/apache/druid/math/expr/VectorExprResultConsistencyTest.java:1
@@ -77,16 +79,22 @@ public class VectorExprResultConsistencyTest extends InitializedNullHandlingTest
 
   // Regexes for detecting VO_MATHLIB function calls in an expression string so the ulp tolerance below only kicks in
   // where the SIMD path can actually diverge from scalar Math. Everything else stays on strict equality. Derived from
-  // SimdSupportedUnaryOp so future ops picked up by the enum's isMathLib() classification get covered automatically.
-  // This part could probably be done nicer if we exposed Simd stuff via the Expr itself instead of this semi-fragile
-  // regex.
+  // SimdSupported{Unary,Binary}Op so future ops picked up by the enum's isMathLib() classification get covered
+  // automatically. This part could probably be done nicer if we exposed Simd stuff via the Expr itself instead of this
+  // semi-fragile regex.
   private static final Set<String> HYPERBOLIC_FN_NAMES = Set.of("sinh", "cosh", "tanh");
   private static final Pattern HYPERBOLIC_FN_PATTERN = functionNamePattern(HYPERBOLIC_FN_NAMES);
+  private static final Set<String> ATAN2_FN_NAMES = Set.of("atan2");
+  private static final Pattern ATAN2_FN_PATTERN = functionNamePattern(ATAN2_FN_NAMES);
   private static final Pattern MATH_LIB_FN_PATTERN = functionNamePattern(
-      Arrays.stream(SimdSupportedUnaryOp.values())
-            .filter(SimdSupportedUnaryOp::isMathLib)
-            .map(op -> op.name().toLowerCase(Locale.ROOT))
-            .collect(Collectors.toSet())
+      Stream.concat(
+          Arrays.stream(SimdSupportedUnaryOp.values())
+                .filter(SimdSupportedUnaryOp::isMathLib)
+                .map(op -> op.name().toLowerCase(Locale.ROOT)),
+          Arrays.stream(SimdSupportedBinaryOp.values())

[P2] Floating-point power operator remains exact-equality checked

Finding: SimdSupportedBinaryOp.POW now routes floating-point ^ expressions through VectorOperators.POW, and the new parity test explicitly allows a two-ULP SIMD-vs-Math delta. This matcher only recognizes function-call spellings such as pow(...); the existing testBinaryMathOperators cases using ^ therefore receive zero tolerance and can fail when C2/SVML produces a permitted non-bit-identical result.

Suggestion: Teach allowedUlpsFor to recognize the binary ^ form when it produces a double, or use parsed-expression metadata, and add a regression case for floating-point power.

FrankChen021 FrankChen021 commented on Nov 10, 2026 12:33 UTC
Reviewer

🟡 Changes recommended

Update the vector/scalar consistency tolerance for the existing ^ power syntax before merging. The new SIMD POW path permits up to a two-ULP difference, but the tolerance matcher only recognizes function-call forms such as pow(...); floating-point ^ expressions can therefore fail exact-equality assertions.

Reviewed 25 of 25 changed files; findings in 1 file.

Coverage included SIMD processors and factories, expression wiring, SQL registration and tests, benchmarks, documentation, and surrounding consistency tests.

Validation: git apply --check passed. Narrow JDK 25 Vector API smoke checks confirmed operand order, long-to-double conversion, and basic POW/ATAN2/HYPOT behavior. No project tests, builds, installs, or formatters were run.

| Severity | Findings |
| --- | ---: |
| P0 | 0 |
| P1 | 0 |
| P2 | 1 |
| P3 | 0 |
| Total | 1 |

<!-- mergelens:review -->

---

This is an automated review by Codex GPT-5.6-Luna(max)

After addressing the findings or replying to the comments, you can request another review from me to trigger a new automated review.

FrankChen021 FrankChen021 commented on Nov 12, 2026 12:38 UTC
Reviewer

🟢 Approval recommended

No actionable issues were found. The prior power-operator tolerance finding is resolved by traversing parsed BinPowExpr nodes and adding dedicated ^ coverage; SIMD dispatch, operand widening, tails, null handling, SQL registration, documentation, benchmarks, and parity tests are consistent across the current diff.

Reviewed 25 of 25 changed files. Validation: git diff --check passed. No tests or builds were run.

<!-- mergelens:review -->

---

This is an automated review by Codex GPT-5.6-Luna(max)

amaechler amaechler commented on code on Nov 12, 2026 16:29 UTC
Code Review
processing/src/main/java/org/apache/druid/math/expr/vector/simd/SimdDoubleDoublePowProcessor.java:1
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.druid.math.expr.vector.simd;
+
+import jdk.incubator.vector.DoubleVector;
+import jdk.incubator.vector.VectorMask;
+import jdk.incubator.vector.VectorOperators;
+import org.apache.druid.math.expr.vector.ExprVectorProcessor;
+import org.apache.druid.math.expr.vector.functional.DoubleBivariateDoublesFunction;
+
+/**
+ * SIMD specialization of {@code (double[], double[]) -> double[]} {@code pow(x, y)}.
+ * {@code lanewise(VectorOperators.POW)} is emitted with the operator literally inline; the JDK routes this to a
+ * vectorized math library (Intel SVML on x86, SLEEF on Arm) rather than a single hardware instruction. Speedup is
+ * JVM/build/hardware dependent, see the {@link SimdSupportedBinaryOp#POW} javadoc.
+ */
+public final class SimdDoubleDoublePowProcessor extends SimdDoubleDoubleProcessor
+{
+  public SimdDoubleDoublePowProcessor(
+      ExprVectorProcessor<?> left,
+      ExprVectorProcessor<?> right,
+      DoubleBivariateDoublesFunction scalarFallback
+  )
+  {
+    super(left, right, scalarFallback);
+  }
+
+  @Override
+  protected void processVector(
+      double[] leftInput,
+      double[] rightInput,
+      boolean[] leftNulls,
+      boolean[] rightNulls,
+      int currentSize
+  )
+  {
+    final int laneCount = SPECIES.length();
+    final int upperBound = SPECIES.loopBound(currentSize);
+    int i = 0;
+    for (; i < upperBound; i += laneCount) {
+      final DoubleVector va = DoubleVector.fromArray(SPECIES, leftInput, i);
+      final DoubleVector vb = DoubleVector.fromArray(SPECIES, rightInput, i);
+      va.lanewise(VectorOperators.POW, vb).intoArray(outValues, i);

VectorOperators.POW follows the C99 special cases rather than Math.pow: pow(1, NaN) and pow(-1, ±Infinity) return 1.0 instead of NaN. I reproduced this on JDK 25 with SLEEF after C2 warmup (libraryBinaryOp intrinsified). A NaN in a double column is enough to hit it, and it is not the few-ulp drift useVectorMathApi documents. The parity test misses it because the POW domain only has positive finite bases.

Blending NaN back in for those lanes fixes it. I checked this against Math.pow over a 12×12 grid of special values (NaN, ±Infinity, ±0, ±1, negative bases), and the only difference left was 1 ulp:

      va.lanewise(VectorOperators.POW, vb)
        .blend(Double.NaN, vb.test(VectorOperators.IS_NAN).or(va.abs().eq(1.0).and(vb.test(VectorOperators.IS_INFINITE))))
        .intoArray(outValues, i);

The same is needed for the masked tail and the other three POW processors, so a small static helper may be nicer than eight copies. It would also be worth adding NaN, ±Infinity and negative-base cases to SimdVoMathlibParityTest.

amaechler amaechler commented on code on Nov 12, 2026 16:29 UTC
Code Review
processing/src/main/java/org/apache/druid/math/expr/vector/simd/SimdSupportedBinaryOp.java:1
@@ -45,7 +45,22 @@ public enum SimdSupportedBinaryOp
    * division by zero would throw {@link ArithmeticException} in the middle of a chunk. So the {@code long × long}
    * combo falls through to the scalar processor for DIV; only the double-output combos are SIMD-specialized.
    */
-  DIV(false, false);
+  DIV(false, false),
+  /**
+   * Exponentiation. VO_MATHLIB code path. {@code VectorOperators.POW} returns a
+   * {@link jdk.incubator.vector.DoubleVector} only, so the {@code long × long -> long} combo (produced by
+   * {@code LongMath.pow} in the scalar factory) is not SIMD-specialized here; the three double-returning combos are.

DoublePower (the pow() function) now sends long, long through makeLongLongToDouble, so all four double-output combos are SIMD-specialized. Only ^ on two longs stays scalar.

   * Exponentiation. VO_MATHLIB code path. {@code VectorOperators.POW} returns a
   * {@link jdk.incubator.vector.DoubleVector} only, so the {@code long × long -> long} combo (produced by
   * {@code LongMath.pow} for the {@code ^} operator) is not SIMD-specialized here; every double-returning combo is,
   * including {@code long × long -> double} for the {@code pow} function.

amaechler amaechler commented on Nov 12, 2026 16:39 UTC
Reviewer

Cool stuff! Buddy Claude had two comments I think are valuable (one small docs, one around pow() edge-cases) to consider.

FrankChen021 FrankChen021 commented on Nov 14, 2026 12:47 UTC
Reviewer

🟢 Approval recommended

No actionable issues found. The incremental POW fix restores Math.pow NaN behavior for the relevant C99 corner cases, while the full SIMD wiring, type dispatch, null handling, tails, SQL registration, tests, benchmarks, and documentation remain consistent.

Reviewed 25 of 25 changed files. Updated-head focus covered the 5 files changed since the previous review, then reconciled them with the complete PR diff and surrounding code.

Validation: git diff --check passed. No project tests, builds, installs, or formatters were run.

<!-- mergelens:review -->

---

This is an automated review by Codex GPT-5.6-Luna(max)

amaechler amaechler commented on code on Nov 14, 2026 16:22 UTC
Code Review
processing/src/test/java/org/apache/druid/math/expr/vector/simd/SimdVoMathlibParityTest.java:1
@@ -237,6 +260,361 @@ private static DoubleUnivariateLongFunction longScalarFor(SimdSupportedUnaryOp o
     return x -> dbl.process((double) x);
   }
 
+  @Test
+  public void binaryDoubleDoubleStabilityAndParity()
+  {
+    for (SimdSupportedBinaryOp op : SimdSupportedBinaryOp.values()) {
+      if (!op.isMathLib()) {
+        continue;
+      }
+      final DoubleBinaryOperator scalar = binaryDoubleScalarFor(op);
+      final double[] leftIn = inDomainLeftDoubles(op);
+      final double[] rightIn = inDomainRightDoubles(op);
+      final ExprVectorProcessor<double[]> processor = SimdProcessors.makeDoubleDouble(
+          fixedDoubleInput(leftIn),
+          fixedDoubleInput(rightIn),
+          op,
+          (l, r) -> scalar.applyAsDouble(l, r)
+      );
+
+      double[] warmed = null;
+      for (int i = 0; i < WARMUP_ITERATIONS; i++) {
+        warmed = processor.evalVector(STUB_BINDING).values().clone();
+      }
+
+      assertStable("op=" + op + " shape=double,double", warmed, () -> processor.evalVector(STUB_BINDING).values());
+      assertParityBinary(op, "double,double", leftIn, rightIn, warmed, scalar);
+    }
+  }
+
+  @Test
+  public void binaryDoubleLongStabilityAndParity()
+  {
+    for (SimdSupportedBinaryOp op : SimdSupportedBinaryOp.values()) {
+      if (!op.isMathLib()) {
+        continue;
+      }
+      final DoubleBinaryOperator scalar = binaryDoubleScalarFor(op);
+      final double[] leftIn = inDomainLeftDoubles(op);
+      final long[] rightIn = inDomainRightLongs(op);
+      final ExprVectorProcessor<double[]> processor = SimdProcessors.makeDoubleLong(
+          fixedDoubleInput(leftIn),
+          fixedLongInput(rightIn),
+          op,
+          (l, r) -> scalar.applyAsDouble(l, (double) r)
+      );
+
+      double[] warmed = null;
+      for (int i = 0; i < WARMUP_ITERATIONS; i++) {
+        warmed = processor.evalVector(STUB_BINDING).values().clone();
+      }
+
+      assertStable("op=" + op + " shape=double,long", warmed, () -> processor.evalVector(STUB_BINDING).values());
+      assertParityBinary(op, "double,long", leftIn, longsToDoubles(rightIn), warmed, scalar);
+    }
+  }
+
+  @Test
+  public void binaryLongDoubleStabilityAndParity()
+  {
+    for (SimdSupportedBinaryOp op : SimdSupportedBinaryOp.values()) {
+      if (!op.isMathLib()) {
+        continue;
+      }
+      final DoubleBinaryOperator scalar = binaryDoubleScalarFor(op);
+      final long[] leftIn = inDomainLeftLongs(op);
+      final double[] rightIn = inDomainRightDoubles(op);
+      final ExprVectorProcessor<double[]> processor = SimdProcessors.makeLongDouble(
+          fixedLongInput(leftIn),
+          fixedDoubleInput(rightIn),
+          op,
+          (l, r) -> scalar.applyAsDouble((double) l, r)
+      );
+
+      double[] warmed = null;
+      for (int i = 0; i < WARMUP_ITERATIONS; i++) {
+        warmed = processor.evalVector(STUB_BINDING).values().clone();
+      }
+
+      assertStable("op=" + op + " shape=long,double", warmed, () -> processor.evalVector(STUB_BINDING).values());
+      assertParityBinary(op, "long,double", longsToDoubles(leftIn), rightIn, warmed, scalar);
+    }
+  }
+
+  @Test
+  public void binaryLongLongStabilityAndParity()
+  {
+    for (SimdSupportedBinaryOp op : SimdSupportedBinaryOp.values()) {
+      if (!op.isMathLib()) {
+        continue;
+      }
+      final DoubleBinaryOperator scalar = binaryDoubleScalarFor(op);
+      final long[] leftIn = inDomainLeftLongs(op);
+      final long[] rightIn = inDomainRightLongs(op);
+      final ExprVectorProcessor<double[]> processor = SimdProcessors.makeLongLongToDouble(
+          fixedLongInput(leftIn),
+          fixedLongInput(rightIn),
+          op,
+          (l, r) -> scalar.applyAsDouble((double) l, (double) r)
+      );
+
+      double[] warmed = null;
+      for (int i = 0; i < WARMUP_ITERATIONS; i++) {
+        warmed = processor.evalVector(STUB_BINDING).values().clone();
+      }
+
+      assertStable("op=" + op + " shape=long,long", warmed, () -> processor.evalVector(STUB_BINDING).values());
+      assertParityBinary(op, "long,long", longsToDoubles(leftIn), longsToDoubles(rightIn), warmed, scalar);
+    }
+  }
+
+  @Test
+  public void powDoubleDoubleSpecialValuesParity()
+  {
+    // VectorOperators.POW follows C99 and returns 1.0 for pow(1, NaN) and pow(±1, ±Infinity) where Math.pow returns
+    // NaN. SimdPowSpecialCases.withNanFixup patches those lanes, so the SIMD output should match Math.pow exactly on
+    // this special-value grid. ulp=0 because the Java spec pins these results deterministically.

The helper lives on SimdDoubleDoublePowProcessor, not SimdPowSpecialCases (reflowed to stay under the line limit):

    // NaN. SimdDoubleDoublePowProcessor.withNanFixup patches those lanes, so the SIMD output should match Math.pow
    // exactly on this special-value grid. ulp=0 because the Java spec pins these results deterministically.

amaechler amaechler commented on code on Nov 14, 2026 16:22 UTC
Code Review
processing/src/test/java/org/apache/druid/math/expr/vector/simd/SimdVoMathlibParityTest.java:1
@@ -237,6 +260,361 @@ private static DoubleUnivariateLongFunction longScalarFor(SimdSupportedUnaryOp o
     return x -> dbl.process((double) x);
   }
 
+  @Test
+  public void binaryDoubleDoubleStabilityAndParity()
+  {
+    for (SimdSupportedBinaryOp op : SimdSupportedBinaryOp.values()) {
+      if (!op.isMathLib()) {
+        continue;
+      }
+      final DoubleBinaryOperator scalar = binaryDoubleScalarFor(op);
+      final double[] leftIn = inDomainLeftDoubles(op);
+      final double[] rightIn = inDomainRightDoubles(op);
+      final ExprVectorProcessor<double[]> processor = SimdProcessors.makeDoubleDouble(
+          fixedDoubleInput(leftIn),
+          fixedDoubleInput(rightIn),
+          op,
+          (l, r) -> scalar.applyAsDouble(l, r)
+      );
+
+      double[] warmed = null;
+      for (int i = 0; i < WARMUP_ITERATIONS; i++) {
+        warmed = processor.evalVector(STUB_BINDING).values().clone();
+      }
+
+      assertStable("op=" + op + " shape=double,double", warmed, () -> processor.evalVector(STUB_BINDING).values());
+      assertParityBinary(op, "double,double", leftIn, rightIn, warmed, scalar);
+    }
+  }
+
+  @Test
+  public void binaryDoubleLongStabilityAndParity()
+  {
+    for (SimdSupportedBinaryOp op : SimdSupportedBinaryOp.values()) {
+      if (!op.isMathLib()) {
+        continue;
+      }
+      final DoubleBinaryOperator scalar = binaryDoubleScalarFor(op);
+      final double[] leftIn = inDomainLeftDoubles(op);
+      final long[] rightIn = inDomainRightLongs(op);
+      final ExprVectorProcessor<double[]> processor = SimdProcessors.makeDoubleLong(
+          fixedDoubleInput(leftIn),
+          fixedLongInput(rightIn),
+          op,
+          (l, r) -> scalar.applyAsDouble(l, (double) r)
+      );
+
+      double[] warmed = null;
+      for (int i = 0; i < WARMUP_ITERATIONS; i++) {
+        warmed = processor.evalVector(STUB_BINDING).values().clone();
+      }
+
+      assertStable("op=" + op + " shape=double,long", warmed, () -> processor.evalVector(STUB_BINDING).values());
+      assertParityBinary(op, "double,long", leftIn, longsToDoubles(rightIn), warmed, scalar);
+    }
+  }
+
+  @Test
+  public void binaryLongDoubleStabilityAndParity()
+  {
+    for (SimdSupportedBinaryOp op : SimdSupportedBinaryOp.values()) {
+      if (!op.isMathLib()) {
+        continue;
+      }
+      final DoubleBinaryOperator scalar = binaryDoubleScalarFor(op);
+      final long[] leftIn = inDomainLeftLongs(op);
+      final double[] rightIn = inDomainRightDoubles(op);
+      final ExprVectorProcessor<double[]> processor = SimdProcessors.makeLongDouble(
+          fixedLongInput(leftIn),
+          fixedDoubleInput(rightIn),
+          op,
+          (l, r) -> scalar.applyAsDouble((double) l, r)
+      );
+
+      double[] warmed = null;
+      for (int i = 0; i < WARMUP_ITERATIONS; i++) {
+        warmed = processor.evalVector(STUB_BINDING).values().clone();
+      }
+
+      assertStable("op=" + op + " shape=long,double", warmed, () -> processor.evalVector(STUB_BINDING).values());
+      assertParityBinary(op, "long,double", longsToDoubles(leftIn), rightIn, warmed, scalar);
+    }
+  }
+
+  @Test
+  public void binaryLongLongStabilityAndParity()
+  {
+    for (SimdSupportedBinaryOp op : SimdSupportedBinaryOp.values()) {
+      if (!op.isMathLib()) {
+        continue;
+      }
+      final DoubleBinaryOperator scalar = binaryDoubleScalarFor(op);
+      final long[] leftIn = inDomainLeftLongs(op);
+      final long[] rightIn = inDomainRightLongs(op);
+      final ExprVectorProcessor<double[]> processor = SimdProcessors.makeLongLongToDouble(
+          fixedLongInput(leftIn),
+          fixedLongInput(rightIn),
+          op,
+          (l, r) -> scalar.applyAsDouble((double) l, (double) r)
+      );
+
+      double[] warmed = null;
+      for (int i = 0; i < WARMUP_ITERATIONS; i++) {
+        warmed = processor.evalVector(STUB_BINDING).values().clone();
+      }
+
+      assertStable("op=" + op + " shape=long,long", warmed, () -> processor.evalVector(STUB_BINDING).values());
+      assertParityBinary(op, "long,long", longsToDoubles(leftIn), longsToDoubles(rightIn), warmed, scalar);
+    }
+  }
+
+  @Test
+  public void powDoubleDoubleSpecialValuesParity()
+  {
+    // VectorOperators.POW follows C99 and returns 1.0 for pow(1, NaN) and pow(±1, ±Infinity) where Math.pow returns
+    // NaN. SimdPowSpecialCases.withNanFixup patches those lanes, so the SIMD output should match Math.pow exactly on
+    // this special-value grid. ulp=0 because the Java spec pins these results deterministically.
+    final double inf = Double.POSITIVE_INFINITY;
+    final double nan = Double.NaN;
+    // Pairs covering: C99-vs-Math divergences (±1 base × NaN/±Inf exponent, pow(1, NaN)); unaffected NaN/Inf
+    // combinations that should already match (NaN base, pow(2, NaN), pow(2, Inf), pow(0.5, -Inf), pow(2, -Inf));
+    // and a plain finite baseline to confirm the fixup doesn't disturb ordinary values.
+    final double[] bases = {1.0, -1.0, 1.0, -1.0, 1.0, nan, 2.0, 2.0, 0.5, 2.0, inf, 3.0};
+    final double[] exps =  {nan, nan, inf, inf, -inf, 2.0, nan, inf, -inf, -inf, 2.0, 2.0};

Checkstyle SingleSpaceSeparator fails on the alignment spaces:

    final double[] exps = {nan, nan, inf, inf, -inf, 2.0, nan, inf, -inf, -inf, 2.0, 2.0};

amaechler amaechler commented on code on Nov 14, 2026 16:22 UTC
Code Review
processing/src/test/java/org/apache/druid/math/expr/vector/simd/SimdVoMathlibParityTest.java:1
@@ -237,6 +260,361 @@ private static DoubleUnivariateLongFunction longScalarFor(SimdSupportedUnaryOp o
     return x -> dbl.process((double) x);
   }
 
+  @Test
+  public void binaryDoubleDoubleStabilityAndParity()
+  {
+    for (SimdSupportedBinaryOp op : SimdSupportedBinaryOp.values()) {
+      if (!op.isMathLib()) {
+        continue;
+      }
+      final DoubleBinaryOperator scalar = binaryDoubleScalarFor(op);
+      final double[] leftIn = inDomainLeftDoubles(op);
+      final double[] rightIn = inDomainRightDoubles(op);
+      final ExprVectorProcessor<double[]> processor = SimdProcessors.makeDoubleDouble(
+          fixedDoubleInput(leftIn),
+          fixedDoubleInput(rightIn),
+          op,
+          (l, r) -> scalar.applyAsDouble(l, r)
+      );
+
+      double[] warmed = null;
+      for (int i = 0; i < WARMUP_ITERATIONS; i++) {
+        warmed = processor.evalVector(STUB_BINDING).values().clone();
+      }
+
+      assertStable("op=" + op + " shape=double,double", warmed, () -> processor.evalVector(STUB_BINDING).values());
+      assertParityBinary(op, "double,double", leftIn, rightIn, warmed, scalar);
+    }
+  }
+
+  @Test
+  public void binaryDoubleLongStabilityAndParity()
+  {
+    for (SimdSupportedBinaryOp op : SimdSupportedBinaryOp.values()) {
+      if (!op.isMathLib()) {
+        continue;
+      }
+      final DoubleBinaryOperator scalar = binaryDoubleScalarFor(op);
+      final double[] leftIn = inDomainLeftDoubles(op);
+      final long[] rightIn = inDomainRightLongs(op);
+      final ExprVectorProcessor<double[]> processor = SimdProcessors.makeDoubleLong(
+          fixedDoubleInput(leftIn),
+          fixedLongInput(rightIn),
+          op,
+          (l, r) -> scalar.applyAsDouble(l, (double) r)
+      );
+
+      double[] warmed = null;
+      for (int i = 0; i < WARMUP_ITERATIONS; i++) {
+        warmed = processor.evalVector(STUB_BINDING).values().clone();
+      }
+
+      assertStable("op=" + op + " shape=double,long", warmed, () -> processor.evalVector(STUB_BINDING).values());
+      assertParityBinary(op, "double,long", leftIn, longsToDoubles(rightIn), warmed, scalar);
+    }
+  }
+
+  @Test
+  public void binaryLongDoubleStabilityAndParity()
+  {
+    for (SimdSupportedBinaryOp op : SimdSupportedBinaryOp.values()) {
+      if (!op.isMathLib()) {
+        continue;
+      }
+      final DoubleBinaryOperator scalar = binaryDoubleScalarFor(op);
+      final long[] leftIn = inDomainLeftLongs(op);
+      final double[] rightIn = inDomainRightDoubles(op);
+      final ExprVectorProcessor<double[]> processor = SimdProcessors.makeLongDouble(
+          fixedLongInput(leftIn),
+          fixedDoubleInput(rightIn),
+          op,
+          (l, r) -> scalar.applyAsDouble((double) l, r)
+      );
+
+      double[] warmed = null;
+      for (int i = 0; i < WARMUP_ITERATIONS; i++) {
+        warmed = processor.evalVector(STUB_BINDING).values().clone();
+      }
+
+      assertStable("op=" + op + " shape=long,double", warmed, () -> processor.evalVector(STUB_BINDING).values());
+      assertParityBinary(op, "long,double", longsToDoubles(leftIn), rightIn, warmed, scalar);
+    }
+  }
+
+  @Test
+  public void binaryLongLongStabilityAndParity()
+  {
+    for (SimdSupportedBinaryOp op : SimdSupportedBinaryOp.values()) {
+      if (!op.isMathLib()) {
+        continue;
+      }
+      final DoubleBinaryOperator scalar = binaryDoubleScalarFor(op);
+      final long[] leftIn = inDomainLeftLongs(op);
+      final long[] rightIn = inDomainRightLongs(op);
+      final ExprVectorProcessor<double[]> processor = SimdProcessors.makeLongLongToDouble(
+          fixedLongInput(leftIn),
+          fixedLongInput(rightIn),
+          op,
+          (l, r) -> scalar.applyAsDouble((double) l, (double) r)
+      );
+
+      double[] warmed = null;
+      for (int i = 0; i < WARMUP_ITERATIONS; i++) {
+        warmed = processor.evalVector(STUB_BINDING).values().clone();
+      }
+
+      assertStable("op=" + op + " shape=long,long", warmed, () -> processor.evalVector(STUB_BINDING).values());
+      assertParityBinary(op, "long,long", longsToDoubles(leftIn), longsToDoubles(rightIn), warmed, scalar);
+    }
+  }
+
+  @Test
+  public void powDoubleDoubleSpecialValuesParity()
+  {
+    // VectorOperators.POW follows C99 and returns 1.0 for pow(1, NaN) and pow(±1, ±Infinity) where Math.pow returns
+    // NaN. SimdPowSpecialCases.withNanFixup patches those lanes, so the SIMD output should match Math.pow exactly on
+    // this special-value grid. ulp=0 because the Java spec pins these results deterministically.
+    final double inf = Double.POSITIVE_INFINITY;
+    final double nan = Double.NaN;
+    // Pairs covering: C99-vs-Math divergences (±1 base × NaN/±Inf exponent, pow(1, NaN)); unaffected NaN/Inf
+    // combinations that should already match (NaN base, pow(2, NaN), pow(2, Inf), pow(0.5, -Inf), pow(2, -Inf));
+    // and a plain finite baseline to confirm the fixup doesn't disturb ordinary values.
+    final double[] bases = {1.0, -1.0, 1.0, -1.0, 1.0, nan, 2.0, 2.0, 0.5, 2.0, inf, 3.0};
+    final double[] exps =  {nan, nan, inf, inf, -inf, 2.0, nan, inf, -inf, -inf, 2.0, 2.0};
+    runPowSpecialValuesParity("double,double", bases, exps);
+  }
+
+  @Test
+  public void powLongDoubleSpecialValuesParity()
+  {
+    // Long base widens to a finite double, so we can still hit the C99-vs-Math fixup via the double-side exponent
+    // being NaN or ±Infinity paired with base=±1 (and the base=2 pairings exercise the unaffected lanes).
+    final double inf = Double.POSITIVE_INFINITY;
+    final double nan = Double.NaN;
+    final long[] bases   = {1L, -1L, 1L, -1L, 1L, -1L, 2L,  2L,  2L,  3L};
+    final double[] exps =  {nan, nan, inf, inf, -inf, -inf, nan, inf, -inf, 2.0};

Same checkstyle failure here:

    final long[] bases = {1L, -1L, 1L, -1L, 1L, -1L, 2L, 2L, 2L, 3L};
    final double[] exps = {nan, nan, inf, inf, -inf, -inf, nan, inf, -inf, 2.0};

amaechler amaechler approved these changes on Nov 14, 2026 16:23 UTC
Approved Reviewer

Beauty 🐻. Some final small nits.

Reviewers
Assignees
clintropolis clintropolis
Labels
risk: low confidence: 91.5% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 71.5% (HIGH) · Higher = larger blast radius
71.5%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 91.5% · Lower = rushed or shallow review
91.5%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 6.1% · Safe to merge
6.1%
Repository