[clang] [llvm] [HexFloat] add HexFloat to APFloat (PR #179771)
@@ -991,6 +1181,9 @@ enum class fltNanEncoding {
};
/* Represents floating point arithmetic semantics. */
struct fltSemantics {
+ /* Human readable name that identifies what these semantics represent */
+ const char *name;
ZERICO2005 wrote:
an alternate implementation would be to have a dedicated function to return a
"pretty" name
```c++
if (&Sem == &getSemIEEEsingle()) { return "single"; }
return "unknown semantics";
```
https://github.com/llvm/llvm-project/pull/179771
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [HexFloat] add HexFloat to APFloat (PR #179771)
Ariel-Burton wrote: ping ... https://github.com/llvm/llvm-project/pull/179771 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [HexFloat] add HexFloat to APFloat (PR #179771)
@@ -54,46 +56,105 @@ static_assert(APFloatBase::integerPartWidth % 4 == 0,
"Part width must be divisi
namespace llvm {
-constexpr fltSemantics APFloatBase::semIEEEhalf = {15, -14, 11, 16};
-constexpr fltSemantics APFloatBase::semBFloat = {127, -126, 8, 16};
-constexpr fltSemantics APFloatBase::semIEEEsingle = {127, -126, 24, 32};
-constexpr fltSemantics APFloatBase::semIEEEdouble = {1023, -1022, 53, 64};
-constexpr fltSemantics APFloatBase::semIEEEquad = {16383, -16382, 113, 128};
-constexpr fltSemantics APFloatBase::semFloat8E5M2 = {15, -14, 3, 8};
+constexpr fltSemantics APFloatBase::semIEEEhalf = {"IEEEhalf", 15, -14, 11,
16};
+constexpr fltSemantics APFloatBase::semBFloat = {"BFloat", 127, -126, 8, 16};
+constexpr fltSemantics APFloatBase::semIEEEsingle = {"IEEESingle", 127, -126,
+ 24, 32};
+constexpr fltSemantics APFloatBase::semIEEEdouble = {"IEEEdouble", 1023, -1022,
+ 53, 64};
+constexpr fltSemantics APFloatBase::semIEEEquad = {"IEEEquad", 16383, -16382,
+ 113, 128};
+constexpr fltSemantics APFloatBase::semFloat8E5M2 = {"Float8E5M2", 15, -14, 3,
+ 8};
constexpr fltSemantics APFloatBase::semFloat8E5M2FNUZ = {
-15, -15, 3, 8, fltNonfiniteBehavior::NanOnly,
fltNanEncoding::NegativeZero};
-constexpr fltSemantics APFloatBase::semFloat8E4M3 = {7, -6, 4, 8};
+"Float8E5M2FNUZ",
+15,
+-15,
+3,
+8,
+2,
+fltNonfiniteBehavior::NanOnly,
+fltNanEncoding::NegativeZero};
+constexpr fltSemantics APFloatBase::semFloat8E4M3 = {"Float8E4M3", 7, -6, 4,
8};
constexpr fltSemantics APFloatBase::semFloat8E4M3FN = {
-8, -6, 4, 8, fltNonfiniteBehavior::NanOnly, fltNanEncoding::AllOnes};
+"Float8E4M3FN", 8, -6, 4, 8, 2, fltNonfiniteBehavior::NanOnly,
+fltNanEncoding::AllOnes};
constexpr fltSemantics APFloatBase::semFloat8E4M3FNUZ = {
-7, -7, 4, 8, fltNonfiniteBehavior::NanOnly, fltNanEncoding::NegativeZero};
+"Float8E4M3FNUZ",
+7,
+-7,
+4,
+8,
+2,
+fltNonfiniteBehavior::NanOnly,
+fltNanEncoding::NegativeZero};
constexpr fltSemantics APFloatBase::semFloat8E4M3B11FNUZ = {
-4, -10, 4, 8, fltNonfiniteBehavior::NanOnly, fltNanEncoding::NegativeZero};
-constexpr fltSemantics APFloatBase::semFloat8E3M4 = {3, -2, 5, 8};
-constexpr fltSemantics APFloatBase::semFloatTF32 = {127, -126, 11, 19};
+"Float8E4M3B11FNUZ", 4, -10, 4, 8, 2,
fltNonfiniteBehavior::NanOnly,
+fltNanEncoding::NegativeZero};
+constexpr fltSemantics APFloatBase::semFloat8E3M4 = {"Float8E3M4", 3, -2, 5,
8};
+constexpr fltSemantics APFloatBase::semFloatTF32 = {"FloatTF32", 127, -126, 11,
+19};
constexpr fltSemantics APFloatBase::semFloat8E8M0FNU = {
+"Float8E8M0FNU",
127,
-127,
1,
8,
+2,
fltNonfiniteBehavior::NanOnly,
fltNanEncoding::AllOnes,
false,
false,
false};
constexpr fltSemantics APFloatBase::semFloat6E3M2FN = {
-4, -2, 3, 6, fltNonfiniteBehavior::FiniteOnly};
+"Float6E3M2FN", 4, -2, 3, 6, 2, fltNonfiniteBehavior::FiniteOnly};
constexpr fltSemantics APFloatBase::semFloat6E2M3FN = {
-2, 0, 4, 6, fltNonfiniteBehavior::FiniteOnly};
+"Float6E2M3FN", 2, 0, 4, 6, 2, fltNonfiniteBehavior::FiniteOnly};
constexpr fltSemantics APFloatBase::semFloat4E2M1FN = {
-2, 0, 2, 4, fltNonfiniteBehavior::FiniteOnly};
-constexpr fltSemantics APFloatBase::semX87DoubleExtended = {16383, -16382, 64,
-80};
-constexpr fltSemantics APFloatBase::semBogus = {0, 0, 0, 0};
-constexpr fltSemantics APFloatBase::semPPCDoubleDouble = {-1, 0, 0, 128};
+"Float4E2M1FN", 2, 0, 2, 4, 2, fltNonfiniteBehavior::FiniteOnly};
+constexpr fltSemantics APFloatBase::semX87DoubleExtended = {
+"X87DoubleExtended", 16383, -16382, 64, 80};
+constexpr fltSemantics APFloatBase::semBogus = {"Bogus", 0, 0, 0, 0};
+constexpr fltSemantics APFloatBase::semPPCDoubleDouble = {"PPCDoubleDouble",
-1,
+ 0, 0, 128};
constexpr fltSemantics APFloatBase::semPPCDoubleDoubleLegacy = {
-1023, -1022 + 53, 53 + 53, 128};
+"PPCDoubleDoubleLegacy", 1023, -1022 + 53, 53 + 53, 128};
+
+/* Hex float
+ All three sizes share the same sign and exponent fields
+ 1 bit for sign
+ 7 bits for the unsigned biased exponent; this is called the "characteristic"
+ in the documentation. The characteristic is the exponent + a bias of 64.
+ Therefore the range of exponent is -64 .. +63.
+ The exponent is for base 16.
+ (Therefore, the minimum exponent for base 2 is 16^-64 == 2^-256
+ The maximum exponent for base 2 is 16^63 == 2^252)
+ For Hex_FP32, the next 24 bits are the "significand", representing
+ six hexits..
+
[clang] [llvm] [HexFloat] add HexFloat to APFloat (PR #179771)
@@ -991,6 +1181,9 @@ enum class fltNanEncoding {
};
/* Represents floating point arithmetic semantics. */
struct fltSemantics {
+ /* Human readable name that identifies what these semantics represent */
+ const char *name;
nikic wrote:
Adding a name to fltSemantics seems like an unrelated change, can it be
submitted separately?
https://github.com/llvm/llvm-project/pull/179771
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [HexFloat] add HexFloat to APFloat (PR #179771)
https://github.com/nikic requested changes to this pull request. If I'm counting right, IEEEFloat is 3 words, while HexFloat is 5. This needs to be implemented in a way that does not increase APFloat size. https://github.com/llvm/llvm-project/pull/179771 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [HexFloat] add HexFloat to APFloat (PR #179771)
Ariel-Burton wrote: Ping ...If there are no concerns I'd like to move ahead and get this is in https://github.com/llvm/llvm-project/pull/179771 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [HexFloat] add HexFloat to APFloat (PR #179771)
Ariel-Burton wrote: Ping https://github.com/llvm/llvm-project/pull/179771 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [HexFloat] add HexFloat to APFloat (PR #179771)
@@ -53,46 +55,102 @@ static_assert(APFloatBase::integerPartWidth % 4 == 0,
"Part width must be divisi
namespace llvm {
-constexpr fltSemantics APFloatBase::semIEEEhalf = {15, -14, 11, 16};
-constexpr fltSemantics APFloatBase::semBFloat = {127, -126, 8, 16};
-constexpr fltSemantics APFloatBase::semIEEEsingle = {127, -126, 24, 32};
-constexpr fltSemantics APFloatBase::semIEEEdouble = {1023, -1022, 53, 64};
-constexpr fltSemantics APFloatBase::semIEEEquad = {16383, -16382, 113, 128};
-constexpr fltSemantics APFloatBase::semFloat8E5M2 = {15, -14, 3, 8};
+constexpr fltSemantics APFloatBase::semIEEEhalf = {"IEEEhalf", 15, -14, 11,
16};
+constexpr fltSemantics APFloatBase::semBFloat = {"BFloat", 127, -126, 8, 16};
+constexpr fltSemantics APFloatBase::semIEEEsingle = {"IEEESingle", 127, -126,
+ 24, 32};
+constexpr fltSemantics APFloatBase::semIEEEdouble = {"IEEEdouble", 1023, -1022,
+ 53, 64};
+constexpr fltSemantics APFloatBase::semIEEEquad = {"IEEEquad", 16383, -16382,
+ 113, 128};
+constexpr fltSemantics APFloatBase::semFloat8E5M2 = {"Float8E5M2", 15, -14, 3,
+ 8};
constexpr fltSemantics APFloatBase::semFloat8E5M2FNUZ = {
-15, -15, 3, 8, fltNonfiniteBehavior::NanOnly,
fltNanEncoding::NegativeZero};
-constexpr fltSemantics APFloatBase::semFloat8E4M3 = {7, -6, 4, 8};
+"Float8E5M2FNUZ",
+15,
+-15,
+3,
+8,
+2,
+fltNonfiniteBehavior::NanOnly,
+fltNanEncoding::NegativeZero};
+constexpr fltSemantics APFloatBase::semFloat8E4M3 = {"Float8E4M3", 7, -6, 4,
8};
constexpr fltSemantics APFloatBase::semFloat8E4M3FN = {
-8, -6, 4, 8, fltNonfiniteBehavior::NanOnly, fltNanEncoding::AllOnes};
+"Float8E4M3FN", 8, -6, 4, 8, 2, fltNonfiniteBehavior::NanOnly,
+fltNanEncoding::AllOnes};
constexpr fltSemantics APFloatBase::semFloat8E4M3FNUZ = {
-7, -7, 4, 8, fltNonfiniteBehavior::NanOnly, fltNanEncoding::NegativeZero};
+"Float8E4M3FNUZ",
+7,
+-7,
+4,
+8,
+2,
+fltNonfiniteBehavior::NanOnly,
+fltNanEncoding::NegativeZero};
constexpr fltSemantics APFloatBase::semFloat8E4M3B11FNUZ = {
-4, -10, 4, 8, fltNonfiniteBehavior::NanOnly, fltNanEncoding::NegativeZero};
-constexpr fltSemantics APFloatBase::semFloat8E3M4 = {3, -2, 5, 8};
-constexpr fltSemantics APFloatBase::semFloatTF32 = {127, -126, 11, 19};
+"Float8E4M3B11FNUZ", 4, -10, 4, 8, 2,
fltNonfiniteBehavior::NanOnly,
+fltNanEncoding::NegativeZero};
+constexpr fltSemantics APFloatBase::semFloat8E3M4 = {"Float8E3M4", 3, -2, 5,
8};
+constexpr fltSemantics APFloatBase::semFloatTF32 = {"FloatTF32", 127, -126, 11,
+19};
constexpr fltSemantics APFloatBase::semFloat8E8M0FNU = {
+"Float8E8M0FNU",
127,
-127,
1,
8,
+2,
fltNonfiniteBehavior::NanOnly,
fltNanEncoding::AllOnes,
false,
false,
false};
constexpr fltSemantics APFloatBase::semFloat6E3M2FN = {
-4, -2, 3, 6, fltNonfiniteBehavior::FiniteOnly};
+"Float6E3M2FN", 4, -2, 3, 6, 2, fltNonfiniteBehavior::FiniteOnly};
constexpr fltSemantics APFloatBase::semFloat6E2M3FN = {
-2, 0, 4, 6, fltNonfiniteBehavior::FiniteOnly};
+"Float6E2M3FN", 2, 0, 4, 6, 2, fltNonfiniteBehavior::FiniteOnly};
constexpr fltSemantics APFloatBase::semFloat4E2M1FN = {
-2, 0, 2, 4, fltNonfiniteBehavior::FiniteOnly};
-constexpr fltSemantics APFloatBase::semX87DoubleExtended = {16383, -16382, 64,
-80};
-constexpr fltSemantics APFloatBase::semBogus = {0, 0, 0, 0};
-constexpr fltSemantics APFloatBase::semPPCDoubleDouble = {-1, 0, 0, 128};
+"Float4E2M1FN", 2, 0, 2, 4, 2, fltNonfiniteBehavior::FiniteOnly};
+constexpr fltSemantics APFloatBase::semX87DoubleExtended = {
+"X87DoubleExtended", 16383, -16382, 64, 80};
+constexpr fltSemantics APFloatBase::semBogus = {"Bogus", 0, 0, 0, 0};
+constexpr fltSemantics APFloatBase::semPPCDoubleDouble = {"PPCDoubleDouble",
-1,
+ 0, 0, 128};
constexpr fltSemantics APFloatBase::semPPCDoubleDoubleLegacy = {
-1023, -1022 + 53, 53 + 53, 128};
+"PPCDoubleDoubleLegacy", 1023, -1022 + 53, 53 + 53, 128};
+
+/* Hex float
+ All three sizes share the same sign and exponent fields
+ 1 bit for sign
+ 7 bits for unsigned CHARACTERISTIC, which is the exponent + a bias of 64.
+ Therefore the range of exponent is -64 .. +63.
+ The exponent is for base 16.
+ (Therefore, the minimum exponent for base 2 is 16^-64 == 2^-256
+ The maximum exponent for base 2 is 16^63 == 2^252)
+ For Hex_FP32, the next 24 bits are the SIGNIFICAND.
+ For Hex_FP64, the next 56 bits are the SIGNIFICAND.
+ For Hex_FP128, two 64-bit values are used. The first is
[clang] [llvm] [HexFloat] add HexFloat to APFloat (PR #179771)
@@ -1509,14 +1752,20 @@ class APFloat : public APFloatBase {
bool isInfinity() const { return getCategory() == fcInfinity; }
bool isNaN() const { return getCategory() == fcNaN; }
- bool isNegative() const { return getIEEE().isNegative(); }
+ bool isNegative() const {
+return isHexFloat() ? getHex().isNegative() : getIEEE().isNegative();
+ }
bool isDenormal() const { APFLOAT_DISPATCH_ON_SEMANTICS(isDenormal()); }
- bool isSignaling() const { return getIEEE().isSignaling(); }
+ bool isSignaling() const {
+return (!isHexFloat()) && getIEEE().isSignaling();
Ariel-Burton wrote:
Removed
https://github.com/llvm/llvm-project/pull/179771
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [HexFloat] add HexFloat to APFloat (PR #179771)
@@ -5865,257 +5933,2301 @@ DoubleAPFloat frexp(const DoubleAPFloat &Arg, int
&Exp,
std::move(Second));
}
-} // namespace detail
-
-APFloat::Storage::Storage(IEEEFloat F, const fltSemantics &Semantics) {
- if (usesLayout(Semantics)) {
-new (&IEEE) IEEEFloat(std::move(F));
-return;
- }
- if (usesLayout(Semantics)) {
-const fltSemantics& S = F.getSemantics();
-new (&Double) DoubleAPFloat(Semantics, APFloat(std::move(F), S),
-APFloat(APFloatBase::IEEEdouble()));
-return;
- }
- llvm_unreachable("Unexpected semantics");
-}
+// class HexFloatArith implements HFP arithmetic using the conventions
+// and approaches of the arith library used by the IBM XL compiler,
+// and matches the behaviour of the hardware.
+class HexFloatArith {
+public:
+ struct value_t {
+int sign; // -1 for negative, +1 for positive
+int exponent;
+APInt fraction;
+ };
+ static void fetch(const HexFloat &in, value_t &out);
+ static void align(value_t &, value_t &, bool sticky = false);
+ static void add(value_t &, const value_t &);
+ static void sub(value_t &, const value_t &);
+ static void mult(value_t &, const value_t &);
+ static void divide(value_t &, const value_t &);
+ static void norm(value_t &);
+ static int putres(const value_t &, HexFloat &);
+};
-Expected APFloat::convertFromString(StringRef Str,
- roundingMode RM) {
- APFLOAT_DISPATCH_ON_SEMANTICS(convertFromString(Str, RM));
+unsigned int HexFloat::getNumPrecisionBits(const fltSemantics *semantics) {
+ assert(APFloat::usesLayout(*semantics) && "not a HexFloat");
+ return 4 * semantics->precision;
}
-hash_code hash_value(const APFloat &Arg) {
- if (APFloat::usesLayout(Arg.getSemantics()))
-return hash_value(Arg.U.IEEE);
- if (APFloat::usesLayout(Arg.getSemantics()))
-return hash_value(Arg.U.Double);
- llvm_unreachable("Unexpected semantics");
+void HexFloat::initialize(const fltSemantics *ourSemantics) {
+ semantics = ourSemantics;
+ significand = APInt(getNumPrecisionBits(semantics), 0);
+ makeZero(/* IsNegative */ false);
}
-APFloat::APFloat(const fltSemantics &Semantics, StringRef S)
-: APFloat(Semantics) {
- auto StatusOrErr = convertFromString(S, rmNearestTiesToEven);
- assert(StatusOrErr && "Invalid floating point representation");
- consumeError(StatusOrErr.takeError());
+void HexFloat::assign(const HexFloat &rhs) {
+ assert(semantics == rhs.semantics);
+ sign = rhs.sign;
+ exponent = rhs.exponent;
+ significand = rhs.significand;
+ low_sign = rhs.low_sign;
+ low_exponent = rhs.low_exponent;
}
-FPClassTest APFloat::classify() const {
- if (isZero())
-return isNegative() ? fcNegZero : fcPosZero;
- if (isNormal())
-return isNegative() ? fcNegNormal : fcPosNormal;
- if (isDenormal())
-return isNegative() ? fcNegSubnormal : fcPosSubnormal;
- if (isInfinity())
-return isNegative() ? fcNegInf : fcPosInf;
- assert(isNaN() && "Other class of FP constant");
- return isSignaling() ? fcSNan : fcQNan;
+HexFloat::HexFloat(const fltSemantics &ourSemantics)
+: semantics(&ourSemantics) {
+ initialize(&ourSemantics);
}
-bool APFloat::getExactInverse(APFloat *Inv) const {
- // Only finite, non-zero numbers can have a useful, representable inverse.
- // This check filters out +/- zero, +/- infinity, and NaN.
- if (!isFiniteNonZero())
-return false;
-
- // Historically, this function rejects subnormal inputs. One reason why this
- // might be important is that subnormals may behave differently under FTZ/DAZ
- // runtime behavior.
- if (isDenormal())
-return false;
-
- // A number has an exact, representable inverse if and only if it is a power
- // of two.
- //
- // Mathematical Rationale:
- // 1. A binary floating-point number x is a dyadic rational, meaning it can
- //be written as x = M / 2^k for integers M (the significand) and k.
- // 2. The inverse is 1/x = 2^k / M.
- // 3. For 1/x to also be a dyadic rational (and thus exactly representable
- //in binary), its denominator M must also be a power of two.
- //Let's say M = 2^m.
- // 4. Substituting this back into the formula for x, we get
- //x = (2^m) / (2^k) = 2^(m-k).
- //
- // This proves that x must be a power of two.
-
- // getExactLog2Abs() returns the integer exponent if the number is a power of
- // two or INT_MIN if it is not.
- const int Exp = getExactLog2Abs();
- if (Exp == INT_MIN)
-return false;
-
- // The inverse of +/- 2^Exp is +/- 2^(-Exp). We can compute this by
- // scaling 1.0 by the negated exponent.
- APFloat Reciprocal =
- scalbn(APFloat::getOne(getSemantics(), /*Negative=*/isNegative()), -Exp,
- rmTowardZero);
+HexFloat::HexFloat(const fltSemantics &ourSemantics, integerPart intValue)
+: semantics(&ourSemantics) {
+ initialize(&ourSemantics);
+ static_assert(sizeof(intValue) <=
[clang] [llvm] [HexFloat] add HexFloat to APFloat (PR #179771)
@@ -5865,257 +5933,2301 @@ DoubleAPFloat frexp(const DoubleAPFloat &Arg, int
&Exp,
std::move(Second));
}
-} // namespace detail
-
-APFloat::Storage::Storage(IEEEFloat F, const fltSemantics &Semantics) {
- if (usesLayout(Semantics)) {
-new (&IEEE) IEEEFloat(std::move(F));
-return;
- }
- if (usesLayout(Semantics)) {
-const fltSemantics& S = F.getSemantics();
-new (&Double) DoubleAPFloat(Semantics, APFloat(std::move(F), S),
-APFloat(APFloatBase::IEEEdouble()));
-return;
- }
- llvm_unreachable("Unexpected semantics");
-}
+// class HexFloatArith implements HFP arithmetic using the conventions
+// and approaches of the arith library used by the IBM XL compiler,
+// and matches the behaviour of the hardware.
+class HexFloatArith {
+public:
+ struct value_t {
+int sign; // -1 for negative, +1 for positive
+int exponent;
+APInt fraction;
+ };
+ static void fetch(const HexFloat &in, value_t &out);
+ static void align(value_t &, value_t &, bool sticky = false);
+ static void add(value_t &, const value_t &);
+ static void sub(value_t &, const value_t &);
+ static void mult(value_t &, const value_t &);
+ static void divide(value_t &, const value_t &);
+ static void norm(value_t &);
+ static int putres(const value_t &, HexFloat &);
+};
-Expected APFloat::convertFromString(StringRef Str,
- roundingMode RM) {
- APFLOAT_DISPATCH_ON_SEMANTICS(convertFromString(Str, RM));
+unsigned int HexFloat::getNumPrecisionBits(const fltSemantics *semantics) {
+ assert(APFloat::usesLayout(*semantics) && "not a HexFloat");
+ return 4 * semantics->precision;
}
-hash_code hash_value(const APFloat &Arg) {
- if (APFloat::usesLayout(Arg.getSemantics()))
-return hash_value(Arg.U.IEEE);
- if (APFloat::usesLayout(Arg.getSemantics()))
-return hash_value(Arg.U.Double);
- llvm_unreachable("Unexpected semantics");
+void HexFloat::initialize(const fltSemantics *ourSemantics) {
+ semantics = ourSemantics;
+ significand = APInt(getNumPrecisionBits(semantics), 0);
+ makeZero(/* IsNegative */ false);
}
-APFloat::APFloat(const fltSemantics &Semantics, StringRef S)
-: APFloat(Semantics) {
- auto StatusOrErr = convertFromString(S, rmNearestTiesToEven);
- assert(StatusOrErr && "Invalid floating point representation");
- consumeError(StatusOrErr.takeError());
+void HexFloat::assign(const HexFloat &rhs) {
+ assert(semantics == rhs.semantics);
+ sign = rhs.sign;
+ exponent = rhs.exponent;
+ significand = rhs.significand;
+ low_sign = rhs.low_sign;
+ low_exponent = rhs.low_exponent;
}
-FPClassTest APFloat::classify() const {
- if (isZero())
-return isNegative() ? fcNegZero : fcPosZero;
- if (isNormal())
-return isNegative() ? fcNegNormal : fcPosNormal;
- if (isDenormal())
-return isNegative() ? fcNegSubnormal : fcPosSubnormal;
- if (isInfinity())
-return isNegative() ? fcNegInf : fcPosInf;
- assert(isNaN() && "Other class of FP constant");
- return isSignaling() ? fcSNan : fcQNan;
+HexFloat::HexFloat(const fltSemantics &ourSemantics)
+: semantics(&ourSemantics) {
+ initialize(&ourSemantics);
}
-bool APFloat::getExactInverse(APFloat *Inv) const {
- // Only finite, non-zero numbers can have a useful, representable inverse.
- // This check filters out +/- zero, +/- infinity, and NaN.
- if (!isFiniteNonZero())
-return false;
-
- // Historically, this function rejects subnormal inputs. One reason why this
- // might be important is that subnormals may behave differently under FTZ/DAZ
- // runtime behavior.
- if (isDenormal())
-return false;
-
- // A number has an exact, representable inverse if and only if it is a power
- // of two.
- //
- // Mathematical Rationale:
- // 1. A binary floating-point number x is a dyadic rational, meaning it can
- //be written as x = M / 2^k for integers M (the significand) and k.
- // 2. The inverse is 1/x = 2^k / M.
- // 3. For 1/x to also be a dyadic rational (and thus exactly representable
- //in binary), its denominator M must also be a power of two.
- //Let's say M = 2^m.
- // 4. Substituting this back into the formula for x, we get
- //x = (2^m) / (2^k) = 2^(m-k).
- //
- // This proves that x must be a power of two.
-
- // getExactLog2Abs() returns the integer exponent if the number is a power of
- // two or INT_MIN if it is not.
- const int Exp = getExactLog2Abs();
- if (Exp == INT_MIN)
-return false;
-
- // The inverse of +/- 2^Exp is +/- 2^(-Exp). We can compute this by
- // scaling 1.0 by the negated exponent.
- APFloat Reciprocal =
- scalbn(APFloat::getOne(getSemantics(), /*Negative=*/isNegative()), -Exp,
- rmTowardZero);
+HexFloat::HexFloat(const fltSemantics &ourSemantics, integerPart intValue)
+: semantics(&ourSemantics) {
+ initialize(&ourSemantics);
+ static_assert(sizeof(intValue) <=
[clang] [llvm] [HexFloat] add HexFloat to APFloat (PR #179771)
@@ -5865,257 +5933,2301 @@ DoubleAPFloat frexp(const DoubleAPFloat &Arg, int
&Exp,
std::move(Second));
}
-} // namespace detail
-
-APFloat::Storage::Storage(IEEEFloat F, const fltSemantics &Semantics) {
- if (usesLayout(Semantics)) {
-new (&IEEE) IEEEFloat(std::move(F));
-return;
- }
- if (usesLayout(Semantics)) {
-const fltSemantics& S = F.getSemantics();
-new (&Double) DoubleAPFloat(Semantics, APFloat(std::move(F), S),
-APFloat(APFloatBase::IEEEdouble()));
-return;
- }
- llvm_unreachable("Unexpected semantics");
-}
+// class HexFloatArith implements HFP arithmetic using the conventions
+// and approaches of the arith library used by the IBM XL compiler,
+// and matches the behaviour of the hardware.
+class HexFloatArith {
+public:
+ struct value_t {
+int sign; // -1 for negative, +1 for positive
+int exponent;
+APInt fraction;
+ };
+ static void fetch(const HexFloat &in, value_t &out);
+ static void align(value_t &, value_t &, bool sticky = false);
+ static void add(value_t &, const value_t &);
+ static void sub(value_t &, const value_t &);
+ static void mult(value_t &, const value_t &);
+ static void divide(value_t &, const value_t &);
+ static void norm(value_t &);
+ static int putres(const value_t &, HexFloat &);
+};
-Expected APFloat::convertFromString(StringRef Str,
- roundingMode RM) {
- APFLOAT_DISPATCH_ON_SEMANTICS(convertFromString(Str, RM));
+unsigned int HexFloat::getNumPrecisionBits(const fltSemantics *semantics) {
+ assert(APFloat::usesLayout(*semantics) && "not a HexFloat");
+ return 4 * semantics->precision;
}
-hash_code hash_value(const APFloat &Arg) {
- if (APFloat::usesLayout(Arg.getSemantics()))
-return hash_value(Arg.U.IEEE);
- if (APFloat::usesLayout(Arg.getSemantics()))
-return hash_value(Arg.U.Double);
- llvm_unreachable("Unexpected semantics");
+void HexFloat::initialize(const fltSemantics *ourSemantics) {
+ semantics = ourSemantics;
+ significand = APInt(getNumPrecisionBits(semantics), 0);
+ makeZero(/* IsNegative */ false);
Ariel-Burton wrote:
Done
https://github.com/llvm/llvm-project/pull/179771
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [HexFloat] add HexFloat to APFloat (PR #179771)
@@ -938,6 +960,171 @@ LLVM_ABI DoubleAPFloat scalbn(const DoubleAPFloat &Arg,
int Exp,
roundingMode RM);
LLVM_ABI DoubleAPFloat frexp(const DoubleAPFloat &X, int &Exp, roundingMode);
+class HexFloat final {
+ friend class HexFloatArith;
+
+ /// Note: this must be the first data member.
+ /// The semantics that this value obeys.
+ const fltSemantics *semantics;
+
+ /// A binary fraction with an explicit integer bit.
+ APInt significand;
+
+ /// The signed unbiased exponent of the value.
+ ExponentType exponent;
+
+ /// The exponent in the low-order part of a HexFloat 128.
+ /// This is ignored during computation, but must be preserved so that a
+ /// bitcast to APInt and back is an identity operation.
+ ExponentType low_exponent;
+
+ /// What kind of floating point number this is.
+ ///
+ /// Only 2 bits are required, but VisualStudio incorrectly sign extends it.
+ /// Using the extra bit keeps it from failing under VisualStudio.
+ fltCategory category : 3;
+
+ /// Sign bit of the number.
+ unsigned int sign : 1;
+
+ /// The sign bit in the low-order part of a HexFloat 128.
+ /// This is ignored; see comment for low_exponent.
+ unsigned int low_sign : 1;
+
+ void initialize(const fltSemantics *);
+ void assign(const HexFloat &);
+ void copySignificand(const HexFloat &);
+ void freeSignificand();
+
+ opStatus convertToSignExtendedInteger(MutableArrayRef Input,
+unsigned int Width, bool IsSigned,
+roundingMode RM, bool *IsExact) const;
+
+ static bool roundAwayFromZero(int sign, const APInt &fraction,
+roundingMode RM, lostFraction loast_fraction,
+int truncatedBits);
+
+public:
+ HexFloat(const fltSemantics &); // Default construct to +0.0
Ariel-Burton wrote:
The default constructor initializes to positive 0.
https://github.com/llvm/llvm-project/pull/179771
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [HexFloat] add HexFloat to APFloat (PR #179771)
@@ -5865,257 +5933,2301 @@ DoubleAPFloat frexp(const DoubleAPFloat &Arg, int
&Exp,
std::move(Second));
}
-} // namespace detail
-
-APFloat::Storage::Storage(IEEEFloat F, const fltSemantics &Semantics) {
- if (usesLayout(Semantics)) {
-new (&IEEE) IEEEFloat(std::move(F));
-return;
- }
- if (usesLayout(Semantics)) {
-const fltSemantics& S = F.getSemantics();
-new (&Double) DoubleAPFloat(Semantics, APFloat(std::move(F), S),
-APFloat(APFloatBase::IEEEdouble()));
-return;
- }
- llvm_unreachable("Unexpected semantics");
-}
+// class HexFloatArith implements HFP arithmetic using the conventions
+// and approaches of the arith library used by the IBM XL compiler,
+// and matches the behaviour of the hardware.
+class HexFloatArith {
+public:
+ struct value_t {
+int sign; // -1 for negative, +1 for positive
+int exponent;
+APInt fraction;
+ };
+ static void fetch(const HexFloat &in, value_t &out);
+ static void align(value_t &, value_t &, bool sticky = false);
+ static void add(value_t &, const value_t &);
+ static void sub(value_t &, const value_t &);
+ static void mult(value_t &, const value_t &);
+ static void divide(value_t &, const value_t &);
+ static void norm(value_t &);
+ static int putres(const value_t &, HexFloat &);
+};
-Expected APFloat::convertFromString(StringRef Str,
- roundingMode RM) {
- APFLOAT_DISPATCH_ON_SEMANTICS(convertFromString(Str, RM));
+unsigned int HexFloat::getNumPrecisionBits(const fltSemantics *semantics) {
+ assert(APFloat::usesLayout(*semantics) && "not a HexFloat");
+ return 4 * semantics->precision;
Ariel-Burton wrote:
I've added a comment. Basically, the precision in the fltSemantics is in terms
of the radix. For HexFloat the radix is 16, so the number of bits required for
a precision base-16 places is 4 times the number of places.
https://github.com/llvm/llvm-project/pull/179771
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [HexFloat] add HexFloat to APFloat (PR #179771)
@@ -53,46 +55,93 @@ static_assert(APFloatBase::integerPartWidth % 4 == 0, "Part
width must be divisi
namespace llvm {
-constexpr fltSemantics APFloatBase::semIEEEhalf = {15, -14, 11, 16};
-constexpr fltSemantics APFloatBase::semBFloat = {127, -126, 8, 16};
-constexpr fltSemantics APFloatBase::semIEEEsingle = {127, -126, 24, 32};
-constexpr fltSemantics APFloatBase::semIEEEdouble = {1023, -1022, 53, 64};
-constexpr fltSemantics APFloatBase::semIEEEquad = {16383, -16382, 113, 128};
-constexpr fltSemantics APFloatBase::semFloat8E5M2 = {15, -14, 3, 8};
+constexpr fltSemantics APFloatBase::semIEEEhalf = {"IEEEhalf", 15, -14, 11,
16};
+constexpr fltSemantics APFloatBase::semBFloat = {"BFloat", 127, -126, 8, 16};
+constexpr fltSemantics APFloatBase::semIEEEsingle = {"IEEESingle", 127, -126,
+ 24, 32};
+constexpr fltSemantics APFloatBase::semIEEEdouble = {"IEEEdouble", 1023, -1022,
+ 53, 64};
+constexpr fltSemantics APFloatBase::semIEEEquad = {"IEEEquad", 16383, -16382,
+ 113, 128};
+constexpr fltSemantics APFloatBase::semFloat8E5M2 = {"Float8E5M2", 15, -14, 3,
+ 8};
constexpr fltSemantics APFloatBase::semFloat8E5M2FNUZ = {
-15, -15, 3, 8, fltNonfiniteBehavior::NanOnly,
fltNanEncoding::NegativeZero};
-constexpr fltSemantics APFloatBase::semFloat8E4M3 = {7, -6, 4, 8};
+"Float8E5M2FNUZ",
+15,
+-15,
+3,
+8,
+2,
+fltNonfiniteBehavior::NanOnly,
+fltNanEncoding::NegativeZero};
+constexpr fltSemantics APFloatBase::semFloat8E4M3 = {"Float8E4M3", 7, -6, 4,
8};
constexpr fltSemantics APFloatBase::semFloat8E4M3FN = {
-8, -6, 4, 8, fltNonfiniteBehavior::NanOnly, fltNanEncoding::AllOnes};
+"Float8E4M3FN", 8, -6, 4, 8, 2, fltNonfiniteBehavior::NanOnly,
+fltNanEncoding::AllOnes};
constexpr fltSemantics APFloatBase::semFloat8E4M3FNUZ = {
-7, -7, 4, 8, fltNonfiniteBehavior::NanOnly, fltNanEncoding::NegativeZero};
+"Float8E4M3FNUZ",
+7,
+-7,
+4,
+8,
+2,
+fltNonfiniteBehavior::NanOnly,
+fltNanEncoding::NegativeZero};
constexpr fltSemantics APFloatBase::semFloat8E4M3B11FNUZ = {
-4, -10, 4, 8, fltNonfiniteBehavior::NanOnly, fltNanEncoding::NegativeZero};
-constexpr fltSemantics APFloatBase::semFloat8E3M4 = {3, -2, 5, 8};
-constexpr fltSemantics APFloatBase::semFloatTF32 = {127, -126, 11, 19};
+"Float8E4M3B11FNUZ", 4, -10, 4, 8, 2,
fltNonfiniteBehavior::NanOnly,
+fltNanEncoding::NegativeZero};
+constexpr fltSemantics APFloatBase::semFloat8E3M4 = {"Float8E3M4", 3, -2, 5,
8};
+constexpr fltSemantics APFloatBase::semFloatTF32 = {"FloatTF32", 127, -126, 11,
+19};
constexpr fltSemantics APFloatBase::semFloat8E8M0FNU = {
+"Float8E8M0FNU",
127,
-127,
1,
8,
+2,
fltNonfiniteBehavior::NanOnly,
fltNanEncoding::AllOnes,
false,
false,
false};
constexpr fltSemantics APFloatBase::semFloat6E3M2FN = {
-4, -2, 3, 6, fltNonfiniteBehavior::FiniteOnly};
+"Float6E3M2FN", 4, -2, 3, 6, 2, fltNonfiniteBehavior::FiniteOnly};
constexpr fltSemantics APFloatBase::semFloat6E2M3FN = {
-2, 0, 4, 6, fltNonfiniteBehavior::FiniteOnly};
+"Float6E2M3FN", 2, 0, 4, 6, 2, fltNonfiniteBehavior::FiniteOnly};
constexpr fltSemantics APFloatBase::semFloat4E2M1FN = {
-2, 0, 2, 4, fltNonfiniteBehavior::FiniteOnly};
-constexpr fltSemantics APFloatBase::semX87DoubleExtended = {16383, -16382, 64,
-80};
-constexpr fltSemantics APFloatBase::semBogus = {0, 0, 0, 0};
-constexpr fltSemantics APFloatBase::semPPCDoubleDouble = {-1, 0, 0, 128};
+"Float4E2M1FN", 2, 0, 2, 4, 2, fltNonfiniteBehavior::FiniteOnly};
+constexpr fltSemantics APFloatBase::semX87DoubleExtended = {
+"X87DoubleExtended", 16383, -16382, 64, 80};
+constexpr fltSemantics APFloatBase::semBogus = {"Bogus", 0, 0, 0, 0};
+constexpr fltSemantics APFloatBase::semPPCDoubleDouble = {"PPCDoubleDouble",
-1,
+ 0, 0, 128};
constexpr fltSemantics APFloatBase::semPPCDoubleDoubleLegacy = {
-1023, -1022 + 53, 53 + 53, 128};
+"PPCDoubleDoubleLegacy", 1023, -1022 + 53, 53 + 53, 128};
+
+/* Hex float
+ All three sizes share the same sign and exponent fields
+ 1 bit for sign
+ 7 bits for unsigned CHARACTERISTIC, which is the exponent + a bias of 64.
Ariel-Burton wrote:
Just for emphasis for the terms we defined here.
https://github.com/llvm/llvm-project/pull/179771
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [HexFloat] add HexFloat to APFloat (PR #179771)
@@ -53,46 +55,93 @@ static_assert(APFloatBase::integerPartWidth % 4 == 0, "Part
width must be divisi
namespace llvm {
-constexpr fltSemantics APFloatBase::semIEEEhalf = {15, -14, 11, 16};
-constexpr fltSemantics APFloatBase::semBFloat = {127, -126, 8, 16};
-constexpr fltSemantics APFloatBase::semIEEEsingle = {127, -126, 24, 32};
-constexpr fltSemantics APFloatBase::semIEEEdouble = {1023, -1022, 53, 64};
-constexpr fltSemantics APFloatBase::semIEEEquad = {16383, -16382, 113, 128};
-constexpr fltSemantics APFloatBase::semFloat8E5M2 = {15, -14, 3, 8};
+constexpr fltSemantics APFloatBase::semIEEEhalf = {"IEEEhalf", 15, -14, 11,
16};
Ariel-Burton wrote:
Sure. See the expanded comment just before the fltSemantics for HexFloat are
defined.
https://github.com/llvm/llvm-project/pull/179771
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [HexFloat] add HexFloat to APFloat (PR #179771)
@@ -5865,257 +5933,2301 @@ DoubleAPFloat frexp(const DoubleAPFloat &Arg, int
&Exp,
std::move(Second));
}
-} // namespace detail
-
-APFloat::Storage::Storage(IEEEFloat F, const fltSemantics &Semantics) {
- if (usesLayout(Semantics)) {
-new (&IEEE) IEEEFloat(std::move(F));
-return;
- }
- if (usesLayout(Semantics)) {
-const fltSemantics& S = F.getSemantics();
-new (&Double) DoubleAPFloat(Semantics, APFloat(std::move(F), S),
-APFloat(APFloatBase::IEEEdouble()));
-return;
- }
- llvm_unreachable("Unexpected semantics");
-}
+// class HexFloatArith implements HFP arithmetic using the conventions
+// and approaches of the arith library used by the IBM XL compiler,
+// and matches the behaviour of the hardware.
+class HexFloatArith {
+public:
+ struct value_t {
+int sign; // -1 for negative, +1 for positive
+int exponent;
+APInt fraction;
+ };
+ static void fetch(const HexFloat &in, value_t &out);
+ static void align(value_t &, value_t &, bool sticky = false);
+ static void add(value_t &, const value_t &);
+ static void sub(value_t &, const value_t &);
+ static void mult(value_t &, const value_t &);
+ static void divide(value_t &, const value_t &);
+ static void norm(value_t &);
+ static int putres(const value_t &, HexFloat &);
+};
-Expected APFloat::convertFromString(StringRef Str,
- roundingMode RM) {
- APFLOAT_DISPATCH_ON_SEMANTICS(convertFromString(Str, RM));
+unsigned int HexFloat::getNumPrecisionBits(const fltSemantics *semantics) {
+ assert(APFloat::usesLayout(*semantics) && "not a HexFloat");
+ return 4 * semantics->precision;
shafik wrote:
`4` is a magic number, can we give it a name and some explanation?
https://github.com/llvm/llvm-project/pull/179771
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [HexFloat] add HexFloat to APFloat (PR #179771)
@@ -5865,257 +5933,2301 @@ DoubleAPFloat frexp(const DoubleAPFloat &Arg, int
&Exp,
std::move(Second));
}
-} // namespace detail
-
-APFloat::Storage::Storage(IEEEFloat F, const fltSemantics &Semantics) {
- if (usesLayout(Semantics)) {
-new (&IEEE) IEEEFloat(std::move(F));
-return;
- }
- if (usesLayout(Semantics)) {
-const fltSemantics& S = F.getSemantics();
-new (&Double) DoubleAPFloat(Semantics, APFloat(std::move(F), S),
-APFloat(APFloatBase::IEEEdouble()));
-return;
- }
- llvm_unreachable("Unexpected semantics");
-}
+// class HexFloatArith implements HFP arithmetic using the conventions
+// and approaches of the arith library used by the IBM XL compiler,
+// and matches the behaviour of the hardware.
+class HexFloatArith {
+public:
+ struct value_t {
+int sign; // -1 for negative, +1 for positive
+int exponent;
+APInt fraction;
+ };
+ static void fetch(const HexFloat &in, value_t &out);
+ static void align(value_t &, value_t &, bool sticky = false);
+ static void add(value_t &, const value_t &);
+ static void sub(value_t &, const value_t &);
+ static void mult(value_t &, const value_t &);
+ static void divide(value_t &, const value_t &);
+ static void norm(value_t &);
+ static int putres(const value_t &, HexFloat &);
+};
-Expected APFloat::convertFromString(StringRef Str,
- roundingMode RM) {
- APFLOAT_DISPATCH_ON_SEMANTICS(convertFromString(Str, RM));
+unsigned int HexFloat::getNumPrecisionBits(const fltSemantics *semantics) {
+ assert(APFloat::usesLayout(*semantics) && "not a HexFloat");
+ return 4 * semantics->precision;
}
-hash_code hash_value(const APFloat &Arg) {
- if (APFloat::usesLayout(Arg.getSemantics()))
-return hash_value(Arg.U.IEEE);
- if (APFloat::usesLayout(Arg.getSemantics()))
-return hash_value(Arg.U.Double);
- llvm_unreachable("Unexpected semantics");
+void HexFloat::initialize(const fltSemantics *ourSemantics) {
+ semantics = ourSemantics;
+ significand = APInt(getNumPrecisionBits(semantics), 0);
+ makeZero(/* IsNegative */ false);
}
-APFloat::APFloat(const fltSemantics &Semantics, StringRef S)
-: APFloat(Semantics) {
- auto StatusOrErr = convertFromString(S, rmNearestTiesToEven);
- assert(StatusOrErr && "Invalid floating point representation");
- consumeError(StatusOrErr.takeError());
+void HexFloat::assign(const HexFloat &rhs) {
+ assert(semantics == rhs.semantics);
+ sign = rhs.sign;
+ exponent = rhs.exponent;
+ significand = rhs.significand;
+ low_sign = rhs.low_sign;
+ low_exponent = rhs.low_exponent;
}
-FPClassTest APFloat::classify() const {
- if (isZero())
-return isNegative() ? fcNegZero : fcPosZero;
- if (isNormal())
-return isNegative() ? fcNegNormal : fcPosNormal;
- if (isDenormal())
-return isNegative() ? fcNegSubnormal : fcPosSubnormal;
- if (isInfinity())
-return isNegative() ? fcNegInf : fcPosInf;
- assert(isNaN() && "Other class of FP constant");
- return isSignaling() ? fcSNan : fcQNan;
+HexFloat::HexFloat(const fltSemantics &ourSemantics)
+: semantics(&ourSemantics) {
+ initialize(&ourSemantics);
}
-bool APFloat::getExactInverse(APFloat *Inv) const {
- // Only finite, non-zero numbers can have a useful, representable inverse.
- // This check filters out +/- zero, +/- infinity, and NaN.
- if (!isFiniteNonZero())
-return false;
-
- // Historically, this function rejects subnormal inputs. One reason why this
- // might be important is that subnormals may behave differently under FTZ/DAZ
- // runtime behavior.
- if (isDenormal())
-return false;
-
- // A number has an exact, representable inverse if and only if it is a power
- // of two.
- //
- // Mathematical Rationale:
- // 1. A binary floating-point number x is a dyadic rational, meaning it can
- //be written as x = M / 2^k for integers M (the significand) and k.
- // 2. The inverse is 1/x = 2^k / M.
- // 3. For 1/x to also be a dyadic rational (and thus exactly representable
- //in binary), its denominator M must also be a power of two.
- //Let's say M = 2^m.
- // 4. Substituting this back into the formula for x, we get
- //x = (2^m) / (2^k) = 2^(m-k).
- //
- // This proves that x must be a power of two.
-
- // getExactLog2Abs() returns the integer exponent if the number is a power of
- // two or INT_MIN if it is not.
- const int Exp = getExactLog2Abs();
- if (Exp == INT_MIN)
-return false;
-
- // The inverse of +/- 2^Exp is +/- 2^(-Exp). We can compute this by
- // scaling 1.0 by the negated exponent.
- APFloat Reciprocal =
- scalbn(APFloat::getOne(getSemantics(), /*Negative=*/isNegative()), -Exp,
- rmTowardZero);
+HexFloat::HexFloat(const fltSemantics &ourSemantics, integerPart intValue)
+: semantics(&ourSemantics) {
+ initialize(&ourSemantics);
+ static_assert(sizeof(intValue) <=
[clang] [llvm] [HexFloat] add HexFloat to APFloat (PR #179771)
@@ -5865,257 +5933,2301 @@ DoubleAPFloat frexp(const DoubleAPFloat &Arg, int
&Exp,
std::move(Second));
}
-} // namespace detail
-
-APFloat::Storage::Storage(IEEEFloat F, const fltSemantics &Semantics) {
- if (usesLayout(Semantics)) {
-new (&IEEE) IEEEFloat(std::move(F));
-return;
- }
- if (usesLayout(Semantics)) {
-const fltSemantics& S = F.getSemantics();
-new (&Double) DoubleAPFloat(Semantics, APFloat(std::move(F), S),
-APFloat(APFloatBase::IEEEdouble()));
-return;
- }
- llvm_unreachable("Unexpected semantics");
-}
+// class HexFloatArith implements HFP arithmetic using the conventions
+// and approaches of the arith library used by the IBM XL compiler,
+// and matches the behaviour of the hardware.
+class HexFloatArith {
+public:
+ struct value_t {
+int sign; // -1 for negative, +1 for positive
+int exponent;
+APInt fraction;
+ };
+ static void fetch(const HexFloat &in, value_t &out);
+ static void align(value_t &, value_t &, bool sticky = false);
+ static void add(value_t &, const value_t &);
+ static void sub(value_t &, const value_t &);
+ static void mult(value_t &, const value_t &);
+ static void divide(value_t &, const value_t &);
+ static void norm(value_t &);
+ static int putres(const value_t &, HexFloat &);
+};
-Expected APFloat::convertFromString(StringRef Str,
- roundingMode RM) {
- APFLOAT_DISPATCH_ON_SEMANTICS(convertFromString(Str, RM));
+unsigned int HexFloat::getNumPrecisionBits(const fltSemantics *semantics) {
+ assert(APFloat::usesLayout(*semantics) && "not a HexFloat");
+ return 4 * semantics->precision;
}
-hash_code hash_value(const APFloat &Arg) {
- if (APFloat::usesLayout(Arg.getSemantics()))
-return hash_value(Arg.U.IEEE);
- if (APFloat::usesLayout(Arg.getSemantics()))
-return hash_value(Arg.U.Double);
- llvm_unreachable("Unexpected semantics");
+void HexFloat::initialize(const fltSemantics *ourSemantics) {
+ semantics = ourSemantics;
+ significand = APInt(getNumPrecisionBits(semantics), 0);
+ makeZero(/* IsNegative */ false);
shafik wrote:
```suggestion
makeZero(/*IsNegative=*/false);
```
https://github.com/llvm/llvm-project/pull/179771
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [HexFloat] add HexFloat to APFloat (PR #179771)
@@ -5865,257 +5933,2301 @@ DoubleAPFloat frexp(const DoubleAPFloat &Arg, int
&Exp,
std::move(Second));
}
-} // namespace detail
-
-APFloat::Storage::Storage(IEEEFloat F, const fltSemantics &Semantics) {
- if (usesLayout(Semantics)) {
-new (&IEEE) IEEEFloat(std::move(F));
-return;
- }
- if (usesLayout(Semantics)) {
-const fltSemantics& S = F.getSemantics();
-new (&Double) DoubleAPFloat(Semantics, APFloat(std::move(F), S),
-APFloat(APFloatBase::IEEEdouble()));
-return;
- }
- llvm_unreachable("Unexpected semantics");
-}
+// class HexFloatArith implements HFP arithmetic using the conventions
+// and approaches of the arith library used by the IBM XL compiler,
+// and matches the behaviour of the hardware.
+class HexFloatArith {
+public:
+ struct value_t {
+int sign; // -1 for negative, +1 for positive
+int exponent;
+APInt fraction;
+ };
+ static void fetch(const HexFloat &in, value_t &out);
+ static void align(value_t &, value_t &, bool sticky = false);
+ static void add(value_t &, const value_t &);
+ static void sub(value_t &, const value_t &);
+ static void mult(value_t &, const value_t &);
+ static void divide(value_t &, const value_t &);
+ static void norm(value_t &);
+ static int putres(const value_t &, HexFloat &);
+};
-Expected APFloat::convertFromString(StringRef Str,
- roundingMode RM) {
- APFLOAT_DISPATCH_ON_SEMANTICS(convertFromString(Str, RM));
+unsigned int HexFloat::getNumPrecisionBits(const fltSemantics *semantics) {
+ assert(APFloat::usesLayout(*semantics) && "not a HexFloat");
+ return 4 * semantics->precision;
}
-hash_code hash_value(const APFloat &Arg) {
- if (APFloat::usesLayout(Arg.getSemantics()))
-return hash_value(Arg.U.IEEE);
- if (APFloat::usesLayout(Arg.getSemantics()))
-return hash_value(Arg.U.Double);
- llvm_unreachable("Unexpected semantics");
+void HexFloat::initialize(const fltSemantics *ourSemantics) {
+ semantics = ourSemantics;
+ significand = APInt(getNumPrecisionBits(semantics), 0);
+ makeZero(/* IsNegative */ false);
}
-APFloat::APFloat(const fltSemantics &Semantics, StringRef S)
-: APFloat(Semantics) {
- auto StatusOrErr = convertFromString(S, rmNearestTiesToEven);
- assert(StatusOrErr && "Invalid floating point representation");
- consumeError(StatusOrErr.takeError());
+void HexFloat::assign(const HexFloat &rhs) {
+ assert(semantics == rhs.semantics);
+ sign = rhs.sign;
+ exponent = rhs.exponent;
+ significand = rhs.significand;
+ low_sign = rhs.low_sign;
+ low_exponent = rhs.low_exponent;
}
-FPClassTest APFloat::classify() const {
- if (isZero())
-return isNegative() ? fcNegZero : fcPosZero;
- if (isNormal())
-return isNegative() ? fcNegNormal : fcPosNormal;
- if (isDenormal())
-return isNegative() ? fcNegSubnormal : fcPosSubnormal;
- if (isInfinity())
-return isNegative() ? fcNegInf : fcPosInf;
- assert(isNaN() && "Other class of FP constant");
- return isSignaling() ? fcSNan : fcQNan;
+HexFloat::HexFloat(const fltSemantics &ourSemantics)
+: semantics(&ourSemantics) {
+ initialize(&ourSemantics);
}
-bool APFloat::getExactInverse(APFloat *Inv) const {
- // Only finite, non-zero numbers can have a useful, representable inverse.
- // This check filters out +/- zero, +/- infinity, and NaN.
- if (!isFiniteNonZero())
-return false;
-
- // Historically, this function rejects subnormal inputs. One reason why this
- // might be important is that subnormals may behave differently under FTZ/DAZ
- // runtime behavior.
- if (isDenormal())
-return false;
-
- // A number has an exact, representable inverse if and only if it is a power
- // of two.
- //
- // Mathematical Rationale:
- // 1. A binary floating-point number x is a dyadic rational, meaning it can
- //be written as x = M / 2^k for integers M (the significand) and k.
- // 2. The inverse is 1/x = 2^k / M.
- // 3. For 1/x to also be a dyadic rational (and thus exactly representable
- //in binary), its denominator M must also be a power of two.
- //Let's say M = 2^m.
- // 4. Substituting this back into the formula for x, we get
- //x = (2^m) / (2^k) = 2^(m-k).
- //
- // This proves that x must be a power of two.
-
- // getExactLog2Abs() returns the integer exponent if the number is a power of
- // two or INT_MIN if it is not.
- const int Exp = getExactLog2Abs();
- if (Exp == INT_MIN)
-return false;
-
- // The inverse of +/- 2^Exp is +/- 2^(-Exp). We can compute this by
- // scaling 1.0 by the negated exponent.
- APFloat Reciprocal =
- scalbn(APFloat::getOne(getSemantics(), /*Negative=*/isNegative()), -Exp,
- rmTowardZero);
+HexFloat::HexFloat(const fltSemantics &ourSemantics, integerPart intValue)
+: semantics(&ourSemantics) {
+ initialize(&ourSemantics);
+ static_assert(sizeof(intValue) <=
[clang] [llvm] [HexFloat] add HexFloat to APFloat (PR #179771)
@@ -53,46 +55,93 @@ static_assert(APFloatBase::integerPartWidth % 4 == 0, "Part
width must be divisi
namespace llvm {
-constexpr fltSemantics APFloatBase::semIEEEhalf = {15, -14, 11, 16};
-constexpr fltSemantics APFloatBase::semBFloat = {127, -126, 8, 16};
-constexpr fltSemantics APFloatBase::semIEEEsingle = {127, -126, 24, 32};
-constexpr fltSemantics APFloatBase::semIEEEdouble = {1023, -1022, 53, 64};
-constexpr fltSemantics APFloatBase::semIEEEquad = {16383, -16382, 113, 128};
-constexpr fltSemantics APFloatBase::semFloat8E5M2 = {15, -14, 3, 8};
+constexpr fltSemantics APFloatBase::semIEEEhalf = {"IEEEhalf", 15, -14, 11,
16};
+constexpr fltSemantics APFloatBase::semBFloat = {"BFloat", 127, -126, 8, 16};
+constexpr fltSemantics APFloatBase::semIEEEsingle = {"IEEESingle", 127, -126,
+ 24, 32};
+constexpr fltSemantics APFloatBase::semIEEEdouble = {"IEEEdouble", 1023, -1022,
+ 53, 64};
+constexpr fltSemantics APFloatBase::semIEEEquad = {"IEEEquad", 16383, -16382,
+ 113, 128};
+constexpr fltSemantics APFloatBase::semFloat8E5M2 = {"Float8E5M2", 15, -14, 3,
+ 8};
constexpr fltSemantics APFloatBase::semFloat8E5M2FNUZ = {
-15, -15, 3, 8, fltNonfiniteBehavior::NanOnly,
fltNanEncoding::NegativeZero};
-constexpr fltSemantics APFloatBase::semFloat8E4M3 = {7, -6, 4, 8};
+"Float8E5M2FNUZ",
+15,
+-15,
+3,
+8,
+2,
+fltNonfiniteBehavior::NanOnly,
+fltNanEncoding::NegativeZero};
+constexpr fltSemantics APFloatBase::semFloat8E4M3 = {"Float8E4M3", 7, -6, 4,
8};
constexpr fltSemantics APFloatBase::semFloat8E4M3FN = {
-8, -6, 4, 8, fltNonfiniteBehavior::NanOnly, fltNanEncoding::AllOnes};
+"Float8E4M3FN", 8, -6, 4, 8, 2, fltNonfiniteBehavior::NanOnly,
+fltNanEncoding::AllOnes};
constexpr fltSemantics APFloatBase::semFloat8E4M3FNUZ = {
-7, -7, 4, 8, fltNonfiniteBehavior::NanOnly, fltNanEncoding::NegativeZero};
+"Float8E4M3FNUZ",
+7,
+-7,
+4,
+8,
+2,
+fltNonfiniteBehavior::NanOnly,
+fltNanEncoding::NegativeZero};
constexpr fltSemantics APFloatBase::semFloat8E4M3B11FNUZ = {
-4, -10, 4, 8, fltNonfiniteBehavior::NanOnly, fltNanEncoding::NegativeZero};
-constexpr fltSemantics APFloatBase::semFloat8E3M4 = {3, -2, 5, 8};
-constexpr fltSemantics APFloatBase::semFloatTF32 = {127, -126, 11, 19};
+"Float8E4M3B11FNUZ", 4, -10, 4, 8, 2,
fltNonfiniteBehavior::NanOnly,
+fltNanEncoding::NegativeZero};
+constexpr fltSemantics APFloatBase::semFloat8E3M4 = {"Float8E3M4", 3, -2, 5,
8};
+constexpr fltSemantics APFloatBase::semFloatTF32 = {"FloatTF32", 127, -126, 11,
+19};
constexpr fltSemantics APFloatBase::semFloat8E8M0FNU = {
+"Float8E8M0FNU",
127,
-127,
1,
8,
+2,
fltNonfiniteBehavior::NanOnly,
fltNanEncoding::AllOnes,
false,
false,
false};
constexpr fltSemantics APFloatBase::semFloat6E3M2FN = {
-4, -2, 3, 6, fltNonfiniteBehavior::FiniteOnly};
+"Float6E3M2FN", 4, -2, 3, 6, 2, fltNonfiniteBehavior::FiniteOnly};
constexpr fltSemantics APFloatBase::semFloat6E2M3FN = {
-2, 0, 4, 6, fltNonfiniteBehavior::FiniteOnly};
+"Float6E2M3FN", 2, 0, 4, 6, 2, fltNonfiniteBehavior::FiniteOnly};
constexpr fltSemantics APFloatBase::semFloat4E2M1FN = {
-2, 0, 2, 4, fltNonfiniteBehavior::FiniteOnly};
-constexpr fltSemantics APFloatBase::semX87DoubleExtended = {16383, -16382, 64,
-80};
-constexpr fltSemantics APFloatBase::semBogus = {0, 0, 0, 0};
-constexpr fltSemantics APFloatBase::semPPCDoubleDouble = {-1, 0, 0, 128};
+"Float4E2M1FN", 2, 0, 2, 4, 2, fltNonfiniteBehavior::FiniteOnly};
+constexpr fltSemantics APFloatBase::semX87DoubleExtended = {
+"X87DoubleExtended", 16383, -16382, 64, 80};
+constexpr fltSemantics APFloatBase::semBogus = {"Bogus", 0, 0, 0, 0};
+constexpr fltSemantics APFloatBase::semPPCDoubleDouble = {"PPCDoubleDouble",
-1,
+ 0, 0, 128};
constexpr fltSemantics APFloatBase::semPPCDoubleDoubleLegacy = {
-1023, -1022 + 53, 53 + 53, 128};
+"PPCDoubleDoubleLegacy", 1023, -1022 + 53, 53 + 53, 128};
+
+/* Hex float
+ All three sizes share the same sign and exponent fields
+ 1 bit for sign
+ 7 bits for unsigned CHARACTERISTIC, which is the exponent + a bias of 64.
shafik wrote:
why is `CHARACTERISTIC` capitalized? Same question for `SIGNIFICAND`
https://github.com/llvm/llvm-project/pull/179771
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [HexFloat] add HexFloat to APFloat (PR #179771)
Ariel-Burton wrote: ... ping https://github.com/llvm/llvm-project/pull/179771 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [HexFloat] add HexFloat to APFloat (PR #179771)
Ariel-Burton wrote: Ping ... https://github.com/llvm/llvm-project/pull/179771 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [HexFloat] add HexFloat to APFloat (PR #179771)
@@ -5865,257 +5933,2297 @@ DoubleAPFloat frexp(const DoubleAPFloat &Arg, int
&Exp,
std::move(Second));
}
-} // namespace detail
-
-APFloat::Storage::Storage(IEEEFloat F, const fltSemantics &Semantics) {
- if (usesLayout(Semantics)) {
-new (&IEEE) IEEEFloat(std::move(F));
-return;
- }
- if (usesLayout(Semantics)) {
-const fltSemantics& S = F.getSemantics();
-new (&Double) DoubleAPFloat(Semantics, APFloat(std::move(F), S),
-APFloat(APFloatBase::IEEEdouble()));
-return;
- }
- llvm_unreachable("Unexpected semantics");
-}
+// class HexFloatArith implements HFP arithemtic using the conventions
+// and approaches of the arith library, and matches the behaviour
hubert-reinterpretcast wrote:
Can you expand the comment. For example, elaborate by saying:
> the "arith" library used by the IBM XL compiler
https://github.com/llvm/llvm-project/pull/179771
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [HexFloat] add HexFloat to APFloat (PR #179771)
Ariel-Burton wrote:
> My opinion might change as I actually look through the implementation of
> `HexFloat`, but given that we will have 4 subclasses (IEEEFloat, HexFloat,
> DecimalFloat, and DoubleAPFloat), we might to reorganize the hierarchy of the
> APFloat inner classes so that we can do some of the operations without having
> to do the full union dispatch--there is essentially a common core of
> {fltSemantics, sign, category, exponent, significand} that we can share among
> all of them, with the latter three having extra fields.
>
> But I'm not sure this patch is the appropriate place to do that
> reorganization.
I agree this is not the right place for a reorganization of the APFloat
hierarchy, and I agree that there's a conversation to be had.
I have wondered a bit about how this set of classes are arranged. To me it
looks like dispatching off the fltSemantics pointer is much like a virtual
method call, and I wonder why the decision to implement an ad hoc mechanism
like this was adopted. The arrangement requires the first field of any of
these related classes to be the pointer to the fltSemantics. This looks very
fragile.
https://github.com/llvm/llvm-project/pull/179771
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [HexFloat] add HexFloat to APFloat (PR #179771)
@@ -257,7 +259,13 @@ class APFloatBase {
S_Float4E2M1FN,
// TODO: Documentation is missing.
S_x87DoubleExtended,
-S_MaxSemantics = S_x87DoubleExtended,
+
+// HexFloat
+S_HexFP32,
+S_HexFP64,
+S_HexFP128,
Ariel-Burton wrote:
The original RFC gave an overview. See also:
- the wiki page for HexFloat
(https://en.wikipedia.org/wiki/IBM_hexadecimal_floating-point)
- chapters 9 and 18 of the z/Architecture Principles of Operation
- the 1999 paper by Schwartz et al
(http://eece.cu.edu.eg/~hfahmy/arith_class/hex_bin_FP.pdf)
https://github.com/llvm/llvm-project/pull/179771
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
