Skip to content

Commit 2d4a8c6

Browse files
committed
[APFloat] Add APFloat support for E8M0 type
This patch adds an APFloat type for unsigned E8M0 format. This format is used for representing the "scale-format" in the MX specification: https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf This format does not support {Inf, denorms, zeroes}. Like FP32, this format's exponents are 8-bits (all bits here) and the bias value is 127. However, it differs from IEEE-FP32 in that the minExponent is -127 (instead of -126). There are updates done in the APFloat utility functions to handle these constraints for this format. * The bias calculation is different and convertIEEE* APIs are updated to handle this. * Since there are no significand bits, the isSignificandAll{Zeroes/Ones} methods are updated accordingly. * Although the format does not have any precision, the precision bit in the fltSemantics is set to 1 for consistency with APFloat's internal representation. * Many utility functions are updated to handle the fact that this format does not support Zero. * Provide a separate initFromAPInt() implementation to handle the quirks of the format. * Add specific tests to verify the range of values for this format. Signed-off-by: Durgadoss R <[email protected]>
1 parent f1ff3a2 commit 2d4a8c6

File tree

3 files changed

+473
-24
lines changed

3 files changed

+473
-24
lines changed

llvm/include/llvm/ADT/APFloat.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,13 @@ struct APFloatBase {
195195
// improved range compared to half (16-bit) formats, at (potentially)
196196
// greater throughput than single precision (32-bit) formats.
197197
S_FloatTF32,
198+
// 8-bit floating point number with (all the) 8 bits for the exponent
199+
// like in FP32. There are no zeroes, no infinities, and no denormal values.
200+
// This format has unsigned representation only. (U -> Unsigned only).
201+
// NaN is represented with all bits set to 1. Bias is 127.
202+
// This format represents the scale data type in the MX specification from:
203+
// https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf
204+
S_Float8E8M0FNU,
198205
// 6-bit floating point number with bit layout S1E3M2. Unlike IEEE-754
199206
// types, there are no infinity or NaN values. The format is detailed in
200207
// https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf
@@ -229,6 +236,7 @@ struct APFloatBase {
229236
static const fltSemantics &Float8E4M3B11FNUZ() LLVM_READNONE;
230237
static const fltSemantics &Float8E3M4() LLVM_READNONE;
231238
static const fltSemantics &FloatTF32() LLVM_READNONE;
239+
static const fltSemantics &Float8E8M0FNU() LLVM_READNONE;
232240
static const fltSemantics &Float6E3M2FN() LLVM_READNONE;
233241
static const fltSemantics &Float6E2M3FN() LLVM_READNONE;
234242
static const fltSemantics &Float4E2M1FN() LLVM_READNONE;
@@ -591,6 +599,7 @@ class IEEEFloat final : public APFloatBase {
591599
unsigned int significandLSB() const;
592600
unsigned int significandMSB() const;
593601
void zeroSignificand();
602+
unsigned int getNumHighBits() const;
594603
/// Return true if the significand excluding the integral bit is all ones.
595604
bool isSignificandAllOnes() const;
596605
bool isSignificandAllOnesExceptLSB() const;
@@ -652,6 +661,7 @@ class IEEEFloat final : public APFloatBase {
652661
APInt convertFloat8E4M3B11FNUZAPFloatToAPInt() const;
653662
APInt convertFloat8E3M4APFloatToAPInt() const;
654663
APInt convertFloatTF32APFloatToAPInt() const;
664+
APInt convertFloat8E8M0FNUAPFloatToAPInt() const;
655665
APInt convertFloat6E3M2FNAPFloatToAPInt() const;
656666
APInt convertFloat6E2M3FNAPFloatToAPInt() const;
657667
APInt convertFloat4E2M1FNAPFloatToAPInt() const;
@@ -672,6 +682,7 @@ class IEEEFloat final : public APFloatBase {
672682
void initFromFloat8E4M3B11FNUZAPInt(const APInt &api);
673683
void initFromFloat8E3M4APInt(const APInt &api);
674684
void initFromFloatTF32APInt(const APInt &api);
685+
void initFromFloat8E8M0FNUAPInt(const APInt &api);
675686
void initFromFloat6E3M2FNAPInt(const APInt &api);
676687
void initFromFloat6E2M3FNAPInt(const APInt &api);
677688
void initFromFloat4E2M1FNAPInt(const APInt &api);
@@ -1079,6 +1090,9 @@ class APFloat : public APFloatBase {
10791090
/// \param Semantics - type float semantics
10801091
static APFloat getAllOnesValue(const fltSemantics &Semantics);
10811092

1093+
/// Returns true if the given semantics supports either NaN or Infinity.
1094+
///
1095+
/// \param Sem - type float semantics
10821096
static bool hasNanOrInf(const fltSemantics &Sem) {
10831097
switch (SemanticsToEnum(Sem)) {
10841098
default:
@@ -1091,6 +1105,13 @@ class APFloat : public APFloatBase {
10911105
}
10921106
}
10931107

1108+
/// Returns true if the given semantics has actual significand.
1109+
///
1110+
/// \param Sem - type float semantics
1111+
static bool hasSignificand(const fltSemantics &Sem) {
1112+
return &Sem != &Float8E8M0FNU();
1113+
}
1114+
10941115
/// Used to insert APFloat objects, or objects that contain APFloat objects,
10951116
/// into FoldingSets.
10961117
void Profile(FoldingSetNodeID &NID) const;

0 commit comments

Comments
 (0)