Commit d8a88d7
fix(cfloat): frexp returns a [0.5,1) fraction (std::frexp semantics) (#1029)
cfloat's frexp returned a fraction in [1,2) with *exp = scale() (floor(log2|x|)),
which does not match std::frexp (fraction in [0.5,1), *exp = floor(log2|x|)+1) --
issue #1027. Generic code written to the std contract (and every other Universal
type's frexp: dd, qd, ereal, bfloat16 all use [0.5,1)) saw the wrong exponent.
Now: place the fraction at scale -1 (*exp = scale()+1). Extreme low-range configs
(es <= 2, minimum normal exponent >= 0) cannot represent any value below 1.0 as a
normal, so [0.5,1) is unachievable there; those fall back to the [1,2) fraction.
ldexp is UNCHANGED -- it rebuilds the exponent from scale(), so the round-trip
ldexp(frexp(x,&e),e) == x holds in every case. Also added the std special cases:
+-0/inf/nan return unchanged with *exp = 0 (the old code corrupted them).
Dependency sweep (the reason this is safe): the only callers of cfloat's frexp
are round-trip tests, which are convention-agnostic; no generic/templated code
instantiates cfloat's frexp expecting [1,2); manipulators/attributes/conversions
do not use it. So the change is confined to frexp + its test.
Test (fractional.cpp): assert the [0.5,1) fraction range for normal inputs where
representable, plus frexp(0)/inf/nan special cases; added half and cfloat<16,8>
coverage. (Note: a separate cfloat quirk -- isnormal() reports true for +-0 --
required excluding zero from the range assertion explicitly.)
Verified gcc + clang: cfloat_fractional passes; the #1022 elreal oracle (which
reads cfloat bits directly) is regression-clean.
Resolves #1027
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent c04efce commit d8a88d7
3 files changed
Lines changed: 59 additions & 18 deletions
File tree
- elastic/elreal/arithmetic
- include/sw/universal/number/cfloat
- static/float/cfloat/math
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
86 | 86 | | |
87 | 87 | | |
88 | 88 | | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
97 | 98 | | |
98 | 99 | | |
99 | 100 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4352 | 4352 | | |
4353 | 4353 | | |
4354 | 4354 | | |
| 4355 | + | |
| 4356 | + | |
| 4357 | + | |
| 4358 | + | |
4355 | 4359 | | |
4356 | 4360 | | |
4357 | | - | |
4358 | | - | |
4359 | | - | |
| 4361 | + | |
| 4362 | + | |
| 4363 | + | |
| 4364 | + | |
| 4365 | + | |
| 4366 | + | |
| 4367 | + | |
| 4368 | + | |
| 4369 | + | |
| 4370 | + | |
| 4371 | + | |
| 4372 | + | |
| 4373 | + | |
4360 | 4374 | | |
4361 | 4375 | | |
4362 | 4376 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
22 | 27 | | |
23 | 28 | | |
| 29 | + | |
24 | 30 | | |
25 | 31 | | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
| 32 | + | |
31 | 33 | | |
32 | 34 | | |
33 | 35 | | |
34 | | - | |
35 | | - | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
36 | 43 | | |
37 | 44 | | |
38 | 45 | | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
39 | 59 | | |
40 | 60 | | |
41 | 61 | | |
| |||
470 | 490 | | |
471 | 491 | | |
472 | 492 | | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
473 | 499 | | |
474 | 500 | | |
475 | 501 | | |
| |||
0 commit comments