I think the “clean” models available for the semantics of unnormalized hexfloat values in Clang/LLVM are:
Unnormalized values exist (at run time) but APFloat never holds such a value: the folding of a bitcast that would result in such a value should fail, leaving the evaluation to happen at run time. Conformance-wise, this treats the unnormalized values as non-value representations for C++ constant evaluation but does not manifest in undefined behaviour at run time.
Unnormalized values exist at both compile and run time: APFloat can represent such values, the number of leading zeros in the significand matters in terms of identity (because they matter for unnormalized operations in assembly, etc.), and APFloat arithmetic/comparison normalizes operands and results.
We may be closer to (2) than (1) right now (and I think 2 is better anyway).
A further model, which I think is too aggressive, is to treat unnormalized values as non-value representations with the run-time undefined behaviour implications: LLVM bitcast succeeds (generating poison), Clang bit_cast should fail.
As for 128-bit HFP, I think the “unused” bits should be considered padding bits. We are not required to preserve round-tripping and C++ bit_cast does not have to honour the values of the corresponding bits.
Thank you everyone for the productive discussion! @anb will provide a summary here of the decisions reached at the meeting to ensure that everyone is on the same page, but it sounds like all of the outstanding concerns were addressed. Thank you to everyone who was able to come to the meeting!
KnownFPClass (the floating-point class analysis used by ValueTracking, InstCombine, and the Attributor) would have some difficulty representing HexFloat, as it assumes that:
all types support subnormals, infinity, qNaN, and sNaN.
+0.0 and -0.0 are distinct values.
So there is the potential for incorrect deductions for HexFloat to be made based on these assumptions.
Additionally, KnownFPClass also has no way to represent non-canonical values. This is currently an issue for PPCDoubleDouble and x86_fp80 which both have non-canonical values. The same issue would also apply to unnormalized HexFloat values if they can flow through IR.
It seems that subnormals used as inputs are not flushed to zero. Additionally, the caveat that results are not always normalized, established at the meeting as a requirement of the IR semantics to allow for certain transformations, means that subnormal results are not always flushed to zero (but could be). I think the IR update should be worded to make that consequence explicit.
#include <assert.h>
int main(void) {
union {
unsigned u;
float f;
} u;
u.u = 0x00000001;
float a = u.f;
u.u = 0x00100000;
float b = u.f;
float res = a + b;
assert(res != b && "not expecting flush-to-zero of operand");
assert(res - b == 0.f && "expecting flush-to-zero of result");
}
The above assertions pass using plain -O0 code generation.
It seems that, more generally, the hardware add operation (even if it normalizes the result) does not pre-normalize its inputs; the IR semantics should indicate the conditions under which operand truncation can occur:
#include <assert.h>
int main(void) {
union {
unsigned u;
float f;
} u;
u.u = 0x7d000001;
float a = u.f;
u.u = 0x7f001000;
float b = u.f;
float res = b + a;
assert(res == b && "expecting truncation of lower-exponent-value operand");
}
The above (corrected) assertion passes using plain -O0 code generation.
@anb, can we confirm whether __builtin_fpclassify will be bug-compatible with the C library’s fpclassify? In particular, the latter misclassifies unnormalized representations of normal values as “subnormal”.
Also, I think the C standard allows for flush-to-zero underflow even where the exact result is representable as a subnormal value. In other words, just because hexfloat uses flush-to-zero underflow for normalized-result operations does not support a claim in the C semantics that the subnormal values do not exist. That also means that the value of FLT_TRUE_MIN should be clarified.
Additionally, will LLVM properly handle FLT_RADIX == 16 for ilogb/logb/scalbn? I think C23’s ldexp and frexp always use a radix of 2 unless the input type is a decimal floating point type (_Decimal32/_Decimal64/_Decimal128 and notfloat/double/long double), so rounding may occur for HexFloat.
For addition the hardware does not “pre-normalize” the operands. What is does is alignment, ie.e., shift the fraction of the operand with the smaller exponent right, increasing its exponent until the exponent is the same as that of the operamd with the larger exponent. In your first example, where the operands are 0x00000001 and 0x00100000, the exponents are the same so there’s no shifting. The addition is straightforward, and the results is 0x00100001. This is a normal, so there’s no further adjustment.
In the subtraction “res - b”, again the exponents are the same, and the subtraction can proceed. The result is 0x00000001, which will be made a positive 0.
With respect to the second example, the exponents of a and b are different. As a has the smaller exponent, it will be shifted right two hexit places to make its exponent agree with that of b. Note that the values are not normalized prior to the addition; they are just aligned so that their exponents agree. In this case, shifting a right two places makes its fraction 0. Then the addition is performed. The value of the result is just the value of b. res is then normalized to give 0x7d100000. For the assertion to pass, the equality must be done by value rather than (bit) representation.
It seems that there is an extra hexit during this process? For example, if we make the difference in the exponent only one, then the smaller-exponent operand’s fraction does not become zero from alignment:
#include <assert.h>
int main(void) {
union {
unsigned u;
float f;
} u;
u.u = 0x7e000001;
float a = u.f;
u.u = 0x7f001000;
float b = u.f;
float res = b + a;
u.u = 0x7d100010;
float c = u.f;
assert(res == c && "expecting no truncation of lower-exponent-value operand");
}
The above assertion passes using plain -O0 code generation.
During the addition there is an extra guard hexit on the right. This is initially 0, but if during alignment the value is shifted right, the last hexit shifted out fills that guard place. The guard place participates in the addition, and can become part of the value if it is shifted in during normalization. That is what your example shows. a is aligned with b, and then added to give 0x7f001000’1, which then gets normalized to 0x7f100010 and the guard shifted in. See the description of ADD NORMALIZED in the Principles of Operation. The PR implements this guard place.
When you say the “assertion passed using plain -O0 code generation”, which compiler are you using?
This is the summary of the community meeting, together with the results of further investigation that the discussion provoked/
For the evaluation of maths library function/builtin calls with an LLVM libc dependency, we will bail out instead of evaluating in a different format.
As LLVM libc support using integer arithmetic becomes available, calls will be made to LLVM libc.
LLVM IR language reference to be updated to say that the result of an expression might not be normalized, and:
[addition since the discussion hosted by the Clang Area Team] Except that subnormal results might be not flushed to zero, the result will otherwise be equal to that of a normalized operation.
[addition since] When non-normal operands apart from canonical zeros are used, loss of precision may occur.
[addition since] Clang user manual to be updated to note that otherwise value-preserving optimizations can affect the result value when non-normal operands (apart from canonical zeros) are involved.
APFloat for HexFloat will preserve different representations that have the same values. However, values mathematically computed at compile time will be canonical.
The current PR to add HexFloat to APFloat can go in.
With respect to HFP128, the sense of the meeting was that the low sign/exponent fields would be formally considered padding and not participate in round-trip bitcast operations. However, see the following point …
[addition since] Further investigation reveals that the implementation currently supports round-tripping and, thus, the formal treatment as padding is unnecessary and user-unfriendly for the purposes of C++ std::bit_cast. We will treat these bits as part of the value representation (as defined by C++) although they do not contribute to the determination of the represented value.
[addition since][under evaluation] With respect to whether two HexFloat values are template-argument-equivalent, because representations of the same value using different exponents have distinct behaviour, we will define values as identical based on their parameters under the C floating-point model. In effect, HFP128’s low sign/exponent are ignored for the determination but we otherwise distinguish between HexFloat template arguments based on the representation instead of the value.
The sense of the meeting leads to the same conclusions except that its formal expression is different: HexFloat values are identical if their value representations are the same, with the HFP128 low sign/exponent being not part of the value representation.
[addition since][under evaluation] A number of issues were discovered with respect to the C library treatment of various non-normal values. A subsequent Clang C/C++ Language Working Group meeting suggested that the compiler implementation should not aim for bug-compatibility.
Open questions:
[addition since][under evaluation] Lack of NaN and infinite values raises the question of how error cases for APFloat functions should be handled. One possibility is to trigger fallback to runtime evaluation.