[llvm] [APFloat] Extend fltSemantics and drop special case E8M0 bit conversionJan/apfloat e8m0 (PR #204200)

Matthias Springer via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 17 00:53:02 PDT 2026


================
@@ -3737,55 +3736,40 @@ void IEEEFloat::initFromPPCDoubleDoubleLegacyAPInt(const APInt &api) {
 // NaN is represented by all 1's.
 // Bias is 127.
 void IEEEFloat::initFromFloat8E8M0FNUAPInt(const APInt &api) {
-  const uint64_t exponent_mask = 0xff;
-  uint64_t val = api.getRawData()[0];
-  uint64_t myexponent = val & exponent_mask;
-
-  initialize(&APFloatBase::semFloat8E8M0FNU);
-  assert(partCount() == 1);
-
-  // This format has unsigned representation only
-  sign = 0;
-
-  // Set the significand
-  // This format does not have any significand but the 'Pth' precision bit is
-  // always set to 1 for consistency in APFloat's internal representation.
-  uint64_t mysignificand = 1;
-  significandParts()[0] = mysignificand;
-
-  // This format can either have a NaN or fcNormal
-  // All 1's i.e. 255 is a NaN
-  if (val == exponent_mask) {
-    category = fcNaN;
-    exponent = exponentNaN();
-    return;
-  }
-  // Handle fcNormal...
-  category = fcNormal;
-  exponent = myexponent - 127; // 127 is bias
+  initFromIEEEAPInt<APFloatBase::semFloat8E8M0FNU>(api);
 }
 
 template <const fltSemantics &S>
 void IEEEFloat::initFromIEEEAPInt(const APInt &api) {
   assert(api.getBitWidth() == S.sizeInBits);
-  constexpr integerPart integer_bit = integerPart{1}
-                                      << ((S.precision - 1) % integerPartWidth);
-  constexpr uint64_t significand_mask = integer_bit - 1;
+
   constexpr unsigned int trailing_significand_bits = S.precision - 1;
+  constexpr integerPart integer_bit =
+      integerPart{1} << (trailing_significand_bits % integerPartWidth);
+  constexpr uint64_t significand_mask = integer_bit - 1;
+  constexpr unsigned int exponent_bits =
+      S.sizeInBits - (S.hasZero ? 1 : 0) - trailing_significand_bits;
----------------
matthias-springer wrote:

Why do you check for `S.hasZero` here? I expected something like:
```c++
  constexpr unsigned int exponent_bits =
      S.sizeInBits - (S.hasSignedRepr ? 1 : 0) - trailing_significand_bits;
```
Intuitively: exponent_bits = total_bits - sign_bit - significand_bits


https://github.com/llvm/llvm-project/pull/204200


More information about the llvm-commits mailing list