Fix segfault in affine_transform with negative b#45
Merged
Conversation
Three bugs in affine_transform_core/tensors when offset vector b has negative entries or entries exceeding 2^R (fixes #44): 1. BoundsError (hidden as segfault by @inbounds): activebit=false path computed y freely via z & 1, producing y_index=2 on a length-1 array. Fix: skip carry paths where y != 0 when bits are inactive. 2. Infinite loop (caused ~50GB memory): arithmetic right shift on negative b never reaches 0 (-1 >> 1 = -1). Fix: work with abs(b) and track signs separately so shifting always terminates. 3. Sign of b lost: copysign(b_, abs(b_)) always returned abs(b_), so negative offsets were treated as positive. Fix: pass signed bit contributions (bsign .* (b .& 1)) to the core function. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…xtension The MPO(::TensorTrain; sites=...) and MPS(::TensorTrain; sites=...) constructors used in fouriertransform.jl were moved from the standalone TCIITensorConversion.jl package (dropped in 9cf8d28) to a weak-dep extension in TensorCrossInterpolation.jl v0.9.18. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #44 —
affine_transform_mposegfaults (or causes ~50GB memory usage) when offset vectorbhas negative entries or entries exceeding2^R.Three bugs were identified and fixed in
src/affine.jl:Segfault (Bug 1): In
affine_transform_core, whenactivebit=false, theisodd(s)branch computedyfreely viaz & 1, producingy_index=2on a length-1 array.@inboundshid the BoundsError as a segfault. Fix: skip carry paths wherey != 0when bits are inactive.Infinite loop / 50GB memory (Bug 2): Arithmetic right shift on negative
bnever reaches 0 (-1 >> 1 = -1), so the extension loop inaffine_transform_tensorsran forever. Fix: work withabs(b)and track signs separately so shifting always terminates.Sign of
blost (Bug 3):copysign(b_, abs(b_)) & 1always returnedabs(b_) & 1, so negative offsets were treated as positive — the MPO would be mathematically wrong. Fix: pass signed bit contributions(b .& 1) .* bsignto the core function.Additionally,
Project.tomlnow requiresTensorCrossInterpolation >= 0.9.18, which provides theTCIITensorConversionextension (theMPO(::TensorTrain)/MPS(::TensorTrain)constructors needed after the standaloneTCIITensorConversion.jlwas dropped in 9cf8d28).Test plan
b=[-32, 32],R=6) — no crash, correct MPO[-1,1],[-5,5],[100,32]) — verified against reference matrix[32,32],[0,0]) — still pass🤖 Generated with Claude Code