Commit 8495e57
ld80: fix spurious tgammal overflow for large negative arguments (#223)
tgammal(-0xd.b6e8f5c28f5c29p+7L) (approx -1755.455) returned +Inf, but
the true value is the tiny subnormal 0x0.01dbd551da54538p-16385L. The
ld80 implementation made two mistakes near the gamma-overflow threshold
MAXGAML:
1. The `q > MAXGAML` early-out was applied to *both* signs of x.
MAXGAML is the threshold at which the *positive* gamma overflows;
for large negative x, |Gamma(x)| is instead tiny, so this wrongly
forced negative arguments down the +Inf overflow path.
2. Just below MAXGAML the reflection code computed
`fabsl(z) * stirf(q)` first. For q near 1755, tgamma(q) is enormous
and the product overflowed to Inf, after which `PIL/Inf` collapsed
to 0 instead of the correct tiny subnormal.
Fix: restrict the MAXGAML overflow check to positive x (the only side
that overflows), and evaluate the reflection formula as
`(PIL / stirf(q)) / z` so the huge tgamma(q) divides pi down to a tiny
value before the multiply, avoiding the intermediate overflow. When
stirf(q) itself overflows to Inf the quotient correctly underflows
toward zero.
With this change tgammal(-0xd.b6e8f5c28f5c29p+7L) returns
0x0.01dbd551da54538p-16385L, matching glibc bit-for-bit, and nearby
large negative non-integers that previously flushed to 0 now yield the
correct subnormals. Positive overflow behavior is unchanged.
Adds test/regression/test-223.c (skipped when LDBL_MANT_DIG < 64).
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>1 parent aeef4da commit 8495e57
2 files changed
Lines changed: 72 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
233 | 233 | | |
234 | 234 | | |
235 | 235 | | |
236 | | - | |
237 | | - | |
238 | 236 | | |
239 | 237 | | |
240 | 238 | | |
| |||
250 | 248 | | |
251 | 249 | | |
252 | 250 | | |
253 | | - | |
254 | | - | |
255 | | - | |
256 | | - | |
257 | | - | |
258 | | - | |
259 | | - | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
260 | 262 | | |
261 | 263 | | |
262 | 264 | | |
| 265 | + | |
| 266 | + | |
263 | 267 | | |
264 | 268 | | |
265 | 269 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
0 commit comments