Make powl() thread-safe (#222)#355
Merged
Merged
Conversation
powl() in ld80/e_powl.c kept its working scratch values (z, w, W, Wa, Wb, ya, yb, u) in file-scope `static` variables. Two threads calling powl() concurrently clobbered each other's scratch, so a call could return arbitrary garbage -- e.g. the reproducer from the issue, powl(0x8.779021e7c2f81b2p+14982L, -0x8.0021b03e1f821c9p-15097L), which is exactly 1, would instead come back as huge values or inf. Move that scratch to local automatic variables inside powl(). The `volatile` on z is preserved (as a local volatile) because it is load-bearing for the rounding/underflow behaviour, exactly like the twom10000 constant. The math is unchanged. reducl() and powil() already used local automatics and were unaffected. Add test/regression/test-222.c: it spawns 8 threads, each repeatedly computing a different power with a known-exact result, and fails if any call ever deviates. Before the fix this fails essentially every run; after it, it is solid. It skips (77) when long double is not the 80-bit type or when threads cannot be started. The regression Makefile rule gains -pthread (harmless for the single-threaded tests). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #355 +/- ##
=======================================
Coverage 72.12% 72.12%
=======================================
Files 233 233
Lines 6135 6135
Branches 1607 1607
=======================================
Hits 4425 4425
Misses 1417 1417
Partials 293 293 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
good work! I'll check that with the next release |
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.
Fixes #222.
powlreturned garbage under concurrent calls — e.g. two threads computingpowl(0x8.779021e7c2f81b2p+14982L, -0x8.0021b03e1f821c9p-15097L)(exactly 1) got different huge/Inf values.Root cause
ld80/e_powl.ckeptpowl()'s working scratch in file-scopestaticmutable variables:Concurrent calls race on this shared state. (
reducl()/powil()already used locals; the read-only coefficient tables are fine.)Fix
Move that scratch to local automatic variables inside
powl()(preserving the load-bearingvolatileonzas a local). No math changes.Verification
make testpasses.test/regression/test-222.c(pthreads; ld80-only via#if LDBL_MANT_DIG == 64, so it builds/runs natively and compile-skips elsewhere). Proven to fail before / pass after.Note: touches
ld80/e_powl.cin a different region than #351 (#334's overflow guard); no conflict.