Skip to content

Fix reciprocal of infinity in turbo loops#568

Open
AshtonSBradley wants to merge 1 commit intoJuliaSIMD:mainfrom
AshtonSBradley:fix-inf-division-539
Open

Fix reciprocal of infinity in turbo loops#568
AshtonSBradley wants to merge 1 commit intoJuliaSIMD:mainfrom
AshtonSBradley:fix-inf-division-539

Conversation

@AshtonSBradley
Copy link
Copy Markdown

@AshtonSBradley AshtonSBradley commented May 5, 2026

Summary

Fixes #539 by lowering the literal reciprocal pattern 1 / x to the existing inv compute path instead of the fast division path.

The reported failure happens on AVX-512 when @turbo compiles 1 / Inf through div_fast. That can enter VectorizationBase's pipelined fast division strategy, which mixes division with reciprocal approximation and Newton refinement; the refinement path can turn the mathematically expected zero into NaN for infinite denominators.

This keeps the change intentionally narrow:

  • no public API changes
  • no new dependencies
  • no unrelated refactoring
  • only the literal numerator 1 special case is rerouted
  • adds a regression test for @turbo y[i] = 1 / x[i] with x .= Inf

Type stability and allocations

For the regression case, the replacement is still type-stable: inv(::Float64) returns Float64, matching the result type of 1 / x[i]. The change happens while constructing the LoopSet, so it should not introduce runtime allocations in the generated loop body. It also reuses the existing inv lowering path rather than adding a new runtime wrapper.

Validation

  • formatted touched files with JuliaFormatter using the repository configs
  • include("test/testsetup.jl"); include("test/simplemisc.jl") passes from a temporary test environment based on test/Project.toml
  • Aqua.test_all(LoopVectorization, ambiguities=false, piracies=false) passes from the same temporary test environment
  • focused smoke test for @turbo y[i] = 1 / x[i] with x .= Inf passes
  • macro expansion check confirms this pattern now contains :inv and no longer contains :div_fast
  • git diff --check passes

I also ran LOOPVECTORIZATION_TEST=part2 julia --project=/private/tmp/lv_test_env test/runtests.jl. It runs with Aqua available and the new issue 539 regression passes, but the shard exits nonzero due to existing ifelsemasks.jl unexpected-pass @test_broken entries unrelated to this change.

@AshtonSBradley AshtonSBradley marked this pull request as ready for review May 5, 2026 23:53
@AshtonSBradley AshtonSBradley force-pushed the fix-inf-division-539 branch from 559d78b to 7faaa7c Compare May 6, 2026 01:57
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.

Division by Inf is sometimes producing NaN's

1 participant