Skip to content

Add zeroWhere! fused masked zeroing primitive with ComparisonOp#91

Merged
Quafadas merged 4 commits intomainfrom
copilot/add-fused-zerowhere-operator
Apr 14, 2026
Merged

Add zeroWhere! fused masked zeroing primitive with ComparisonOp#91
Quafadas merged 4 commits intomainfrom
copilot/add-fused-zerowhere-operator

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 13, 2026

The two-pass pattern dz1 *:*= (z1 > 0) allocates a temporary Array[Boolean] and makes two SIMD passes. zeroWhere! fuses compare + blend + store into a single pass with zero allocations.

API

// In-place: zeros vec(i) wherever other(i) satisfies op vs threshold
vec.`zeroWhere!`(other, 0.0f, ComparisonOp.LE)

// Non-mutating: returns new array
val result = vec.zeroWhere(other, 0.0f, ComparisonOp.LE)

// relu backward — one pass, no boolean allocation
dz1.raw.`zeroWhere!`(z1.raw, 0.0f, ComparisonOp.LE)

Changes

  • vecxt/src/ComparisonOp.scala — new cross-platform enum ComparisonOp (LT, LE, GT, GE, EQ, NE); exported from all.scala
  • JVM (src-jvm/floatarrays.scala, doublearrays.scala) — SIMD implementation using FloatVector/DoubleVector.blend(zero, mask) for the main loop; scalar tail handles non-aligned lengths
  • JS/Native (src-js/, src-native/) — while-loop implementations for Array[Float] and Array[Double]
  • vecxt/test/src/zeroWhere.test.scala — cross-platform correctness suite: basic correctness, all 6 ops, non-aligned lengths, empty arrays, NaN/infinity edge cases, self-referential arrays; both Float and Double

The op parameter is inline, so the match is eliminated at each call site and the comparison is inlined directly into the loop.

Copilot AI linked an issue Apr 13, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Add fused ZeroWhere operator for masked zeroing Add zeroWhere! fused masked zeroing primitive with ComparisonOp Apr 13, 2026
Copilot AI requested a review from Quafadas April 13, 2026 18:28
@Quafadas
Copy link
Copy Markdown
Owner

@copilot Don’t test the large array in CI , concentrate on correctness only…

@Quafadas Quafadas marked this pull request as ready for review April 13, 2026 19:02
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 13, 2026

@copilot Don’t test the large array in CI , concentrate on correctness only…

Done — removed the large-array JVM test file in 98f99fa. The remaining tests in vecxt/test/src/zeroWhere.test.scala focus purely on correctness (basic ops, all 6 comparison operators, edge cases, NaN/infinity, etc.).

@Quafadas Quafadas merged commit b6acb42 into main Apr 14, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fused ZeroWhere operator

2 participants