[compiler-rt] [libc] [llvm] [clang-tools-extra] [clang] [libc] `FPRep` builders return `FPRep` instead of raw `StorageType` (PR #78588)

2024-01-22 Thread Guillaume Chatelet via cfe-commits

https://github.com/gchatelet closed 
https://github.com/llvm/llvm-project/pull/78588
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[compiler-rt] [libc] [llvm] [clang-tools-extra] [clang] [libc] `FPRep` builders return `FPRep` instead of raw `StorageType` (PR #78588)

2024-01-22 Thread Clement Courbet via cfe-commits

https://github.com/legrosbuffle approved this pull request.


https://github.com/llvm/llvm-project/pull/78588
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[compiler-rt] [libc] [llvm] [clang-tools-extra] [clang] [libc] `FPRep` builders return `FPRep` instead of raw `StorageType` (PR #78588)

2024-01-22 Thread Clement Courbet via cfe-commits


@@ -535,92 +472,178 @@ struct FPRep : public 
FPRepBase {
 // - Quiet Not a Number
 // - Unnormal
 // This can be reduced to the following logic:
-if (exp_bits() == encode(BiasedExponent::BITS_ALL_ONES()))
+if (exp_bits() == encode(BiasedExp::BITS_ALL_ONES()))
   return !is_inf();
-if (exp_bits() != encode(BiasedExponent::BITS_ALL_ZEROES()))
-  return (sig_bits() & encode(Significand::MSB())) == 0;
+if (exp_bits() != encode(BiasedExp::BITS_ALL_ZEROES()))
+  return (sig_bits() & encode(Sig::MSB())) == 0;
 return false;
   }
   LIBC_INLINE constexpr bool is_quiet_nan() const {
 return exp_sig_bits() >=
-   encode(BiasedExponent::BITS_ALL_ONES(),
-  Significand::MSB() | (Significand::MSB() >> 1));
+   encode(BiasedExp::BITS_ALL_ONES(), Sig::MSB() | (Sig::MSB() >> 1));
   }
   LIBC_INLINE constexpr bool is_signaling_nan() const {
 return is_nan() && !is_quiet_nan();
   }
   LIBC_INLINE constexpr bool is_inf() const {
-return exp_sig_bits() ==
-   encode(BiasedExponent::BITS_ALL_ONES(), Significand::MSB());
-  }
-  LIBC_INLINE constexpr bool is_zero() const {
-return exp_sig_bits() ==
-   encode(BiasedExponent::BITS_ALL_ZEROES(), Significand::ZERO());
+return exp_sig_bits() == encode(BiasedExp::BITS_ALL_ONES(), Sig::MSB());
   }
   LIBC_INLINE constexpr bool is_finite() const {
 return !is_inf() && !is_nan();
   }
   LIBC_INLINE
   constexpr bool is_subnormal() const {
-return exp_sig_bits() >
-   encode(BiasedExponent::BITS_ALL_ZEROES(), Significand::ZERO());
+return exp_bits() == encode(BiasedExp::BITS_ALL_ZEROES());
   }
   LIBC_INLINE constexpr bool is_normal() const {
 const auto exp = exp_bits();
-if (exp == encode(BiasedExponent::BITS_ALL_ZEROES()) ||
-exp == encode(BiasedExponent::BITS_ALL_ONES()))
+if (exp == encode(BiasedExp::BITS_ALL_ZEROES()) ||
+exp == encode(BiasedExp::BITS_ALL_ONES()))
   return false;
 return get_implicit_bit();
   }
+  LIBC_INLINE constexpr StorageType get_explicit_mantissa() const {
+return sig_bits();
+  }
 
-  LIBC_INLINE static constexpr FPRep zero(Sign sign = Sign::POS) {
-return encode(sign, BiasedExponent::BITS_ALL_ZEROES(), 
Significand::ZERO());
+  // This functions is specific to FPRepSem.
+  // TODO: Remove if possible.
+  LIBC_INLINE constexpr bool get_implicit_bit() const {
+return static_cast(bits & EXPLICIT_BIT_MASK);
   }
-  LIBC_INLINE static constexpr FPRep one(Sign sign = Sign::POS) {
-return encode(sign, Exponent::ZERO(), Significand::MSB());
+
+  // This functions is specific to FPRepSem.
+  // TODO: Remove if possible.
+  LIBC_INLINE constexpr void set_implicit_bit(bool implicitVal) {
+if (get_implicit_bit() != implicitVal)
+  bits ^= EXPLICIT_BIT_MASK;
   }
-  LIBC_INLINE static constexpr FPRep min_subnormal(Sign sign = Sign::POS) {
-return encode(sign, BiasedExponent::BITS_ALL_ZEROES(), Significand::LSB());
+};
+
+// 'FPRep' is the bottom of the class hierarchy that only deals with 'FPType'.
+// The operations dealing with specific float semantics are implemented by
+// 'FPRepSem' above and specialized when needed.
+//
+// 'RetT' is the return type used by the builders. If not specified it defaults
+// to the 'StorageType' but 'FPBits' class below defaults it to itself so

legrosbuffle wrote:

Yes, much clearer.

https://github.com/llvm/llvm-project/pull/78588
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[compiler-rt] [libc] [llvm] [clang-tools-extra] [clang] [libc] `FPRep` builders return `FPRep` instead of raw `StorageType` (PR #78588)

2024-01-22 Thread Guillaume Chatelet via cfe-commits

https://github.com/gchatelet updated 
https://github.com/llvm/llvm-project/pull/78588

>From 49ba96c8aa51fb56a5bf96a1e97fef48bcc42f09 Mon Sep 17 00:00:00 2001
From: Guillaume Chatelet 
Date: Thu, 18 Jan 2024 14:42:54 +
Subject: [PATCH 1/3] [libc] `FPRep` builders return `FPRep` instead of raw
 `StorageType`

---
 libc/src/__support/FPUtil/FPBits.h|  68 
 .../test/src/__support/FPUtil/fpbits_test.cpp | 163 +-
 2 files changed, 120 insertions(+), 111 deletions(-)

diff --git a/libc/src/__support/FPUtil/FPBits.h 
b/libc/src/__support/FPUtil/FPBits.h
index 3ee6289b749648..5df49350e3744e 100644
--- a/libc/src/__support/FPUtil/FPBits.h
+++ b/libc/src/__support/FPUtil/FPBits.h
@@ -332,7 +332,10 @@ struct FPRepBase : public internal::FPLayout {
   mask_trailing_ones();
 
   // The floating point number representation as an unsigned integer.
-  StorageType bits = 0;
+  StorageType bits;
+
+  LIBC_INLINE constexpr FPRepBase() : bits(0) {}
+  LIBC_INLINE constexpr FPRepBase(StorageType value) : bits(value) {}
 
 public:
   LIBC_INLINE constexpr Sign sign() const {
@@ -418,6 +421,7 @@ template  struct FPRep : public 
FPRepBase {
   using UP::exp_bits;
   using UP::exp_sig_bits;
   using UP::sig_bits;
+  using UP::UP;
 
 public:
   LIBC_INLINE constexpr bool is_nan() const {
@@ -450,37 +454,35 @@ template  struct FPRep : public 
FPRepBase {
 return is_finite() && !is_subnormal();
   }
 
-  LIBC_INLINE static constexpr StorageType zero(Sign sign = Sign::POS) {
+  LIBC_INLINE static constexpr FPRep zero(Sign sign = Sign::POS) {
 return encode(sign, BiasedExponent::BITS_ALL_ZEROES(), 
Significand::ZERO());
   }
-  LIBC_INLINE static constexpr StorageType one(Sign sign = Sign::POS) {
+  LIBC_INLINE static constexpr FPRep one(Sign sign = Sign::POS) {
 return encode(sign, Exponent::ZERO(), Significand::ZERO());
   }
-  LIBC_INLINE static constexpr StorageType
-  min_subnormal(Sign sign = Sign::POS) {
+  LIBC_INLINE static constexpr FPRep min_subnormal(Sign sign = Sign::POS) {
 return encode(sign, BiasedExponent::BITS_ALL_ZEROES(), Significand::LSB());
   }
-  LIBC_INLINE static constexpr StorageType
-  max_subnormal(Sign sign = Sign::POS) {
+  LIBC_INLINE static constexpr FPRep max_subnormal(Sign sign = Sign::POS) {
 return encode(sign, BiasedExponent::BITS_ALL_ZEROES(),
   Significand::BITS_ALL_ONES());
   }
-  LIBC_INLINE static constexpr StorageType min_normal(Sign sign = Sign::POS) {
+  LIBC_INLINE static constexpr FPRep min_normal(Sign sign = Sign::POS) {
 return encode(sign, Exponent::MIN(), Significand::ZERO());
   }
-  LIBC_INLINE static constexpr StorageType max_normal(Sign sign = Sign::POS) {
+  LIBC_INLINE static constexpr FPRep max_normal(Sign sign = Sign::POS) {
 return encode(sign, Exponent::MAX(), Significand::BITS_ALL_ONES());
   }
-  LIBC_INLINE static constexpr StorageType inf(Sign sign = Sign::POS) {
+  LIBC_INLINE static constexpr FPRep inf(Sign sign = Sign::POS) {
 return encode(sign, BiasedExponent::BITS_ALL_ONES(), Significand::ZERO());
   }
-  LIBC_INLINE static constexpr StorageType build_nan(Sign sign = Sign::POS,
- StorageType v = 0) {
+  LIBC_INLINE static constexpr FPRep build_nan(Sign sign = Sign::POS,
+   StorageType v = 0) {
 return encode(sign, BiasedExponent::BITS_ALL_ONES(),
   (v ? Significand(v) : (Significand::MSB() >> 1)));
   }
-  LIBC_INLINE static constexpr StorageType
-  build_quiet_nan(Sign sign = Sign::POS, StorageType v = 0) {
+  LIBC_INLINE static constexpr FPRep build_quiet_nan(Sign sign = Sign::POS,
+ StorageType v = 0) {
 return encode(sign, BiasedExponent::BITS_ALL_ONES(),
   Significand::MSB() | Significand(v));
   }
@@ -507,6 +509,7 @@ struct FPRep : public 
FPRepBase {
   using typename UP::BiasedExponent;
   using typename UP::Significand;
   using UP::encode;
+  using UP::UP;
 
 public:
   // The x86 80 bit float represents the leading digit of the mantissa
@@ -570,38 +573,36 @@ struct FPRep : public 
FPRepBase {
 return get_implicit_bit();
   }
 
-  LIBC_INLINE static constexpr StorageType zero(Sign sign = Sign::POS) {
+  LIBC_INLINE static constexpr FPRep zero(Sign sign = Sign::POS) {
 return encode(sign, BiasedExponent::BITS_ALL_ZEROES(), 
Significand::ZERO());
   }
-  LIBC_INLINE static constexpr StorageType one(Sign sign = Sign::POS) {
+  LIBC_INLINE static constexpr FPRep one(Sign sign = Sign::POS) {
 return encode(sign, Exponent::ZERO(), Significand::MSB());
   }
-  LIBC_INLINE static constexpr StorageType
-  min_subnormal(Sign sign = Sign::POS) {
+  LIBC_INLINE static constexpr FPRep min_subnormal(Sign sign = Sign::POS) {
 return encode(sign, BiasedExponent::BITS_ALL_ZEROES(), Significand::LSB());
   }
-  LIBC_INLINE static constexpr StorageType
-  max_subnormal(Sign