[libc-commits] [libc] b663993 - [libc] Add explicit constructor calls to fix compilation when using UInt<T>

Mikhail R. Gadelha via libc-commits libc-commits at lists.llvm.org
Tue May 16 08:59:55 PDT 2023


Author: Mikhail R. Gadelha
Date: 2023-05-16T12:59:32-03:00
New Revision: b663993067ffb5800632ad41ea7f2f92caab1093

URL: https://github.com/llvm/llvm-project/commit/b663993067ffb5800632ad41ea7f2f92caab1093
DIFF: https://github.com/llvm/llvm-project/commit/b663993067ffb5800632ad41ea7f2f92caab1093.diff

LOG: [libc] Add explicit constructor calls to fix compilation when using UInt<T>

This patch is similar to 86fe88c8d9 and adds several explicit
constructor calls (bool(...), uint64_t(...), uint8_t(...)) that are
needed when we use UInt<T> (in my case UInt<128> in riscv32).

This patch also adds two operators to UInt<T>:
* operator/= required by printf_core/float_hex_converter.h:148
* operator-- required by FPUtil/ManipulationFunctions.h:166

Reviewed By: sivachandra, lntue

Differential Revision: https://reviews.llvm.org/D149594

Added: 
    

Modified: 
    libc/src/__support/FPUtil/generic/sqrt.h
    libc/src/__support/UInt.h
    libc/src/__support/float_to_string.h
    libc/src/__support/str_to_float.h
    libc/src/stdio/printf_core/char_converter.h
    libc/src/stdio/printf_core/float_dec_converter.h
    libc/src/stdio/printf_core/float_hex_converter.h
    libc/src/stdio/printf_core/float_inf_nan_converter.h
    libc/src/stdio/printf_core/int_converter.h

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/FPUtil/generic/sqrt.h b/libc/src/__support/FPUtil/generic/sqrt.h
index 1464056417d06..529f5283f89a3 100644
--- a/libc/src/__support/FPUtil/generic/sqrt.h
+++ b/libc/src/__support/FPUtil/generic/sqrt.h
@@ -136,7 +136,7 @@ LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, T> sqrt(T x) {
       }
 
       // We compute one more iteration in order to round correctly.
-      bool lsb = y & 1; // Least significant bit
+      bool lsb = static_cast<bool>(y & 1); // Least significant bit
       bool rb = false;  // Round bit
       r <<= 2;
       UIntType tmp = (y << 2) + 1;

diff  --git a/libc/src/__support/UInt.h b/libc/src/__support/UInt.h
index a702aaad827b2..1ac7534e1be68 100644
--- a/libc/src/__support/UInt.h
+++ b/libc/src/__support/UInt.h
@@ -468,6 +468,11 @@ template <size_t Bits> struct UInt {
     return result;
   }
 
+  constexpr UInt<Bits> operator/=(const UInt<Bits> &other) {
+    *this = *this / other;
+    return *this;
+  }
+
   constexpr UInt<Bits> operator%(const UInt<Bits> &other) const {
     UInt<Bits> result(*this);
     return *result.div(other);
@@ -714,8 +719,12 @@ template <size_t Bits> struct UInt {
   }
 
   constexpr UInt<Bits> &operator++() {
-    UInt<Bits> one(1);
-    add(one);
+    *this = *this + 1;
+    return *this;
+  }
+
+  constexpr UInt<Bits> &operator--() {
+    *this = *this - 1;
     return *this;
   }
 

diff  --git a/libc/src/__support/float_to_string.h b/libc/src/__support/float_to_string.h
index 09370148d803a..43f49036156a0 100644
--- a/libc/src/__support/float_to_string.h
+++ b/libc/src/__support/float_to_string.h
@@ -202,8 +202,8 @@ LIBC_INLINE uint32_t mul_shift_mod_1e9(const MantissaInt mantissa,
   cpp::UInt<MID_INT_SIZE + MANT_INT_SIZE> val(large);
   // TODO: Find a better way to force __uint128_t to be UInt<128>
   cpp::UInt<MANT_INT_SIZE> wide_mant(0);
-  wide_mant[0] = mantissa & (uint64_t(-1));
-  wide_mant[1] = mantissa >> 64;
+  wide_mant[0] = static_cast<uint64_t>(mantissa & (uint64_t(-1)));
+  wide_mant[1] = static_cast<uint64_t>(mantissa >> 64);
   val = (val * wide_mant) >> shift_amount;
   return fast_uint_mod_1e9(val);
 }

diff  --git a/libc/src/__support/str_to_float.h b/libc/src/__support/str_to_float.h
index 0697cf0a68c32..33c394e389a8a 100644
--- a/libc/src/__support/str_to_float.h
+++ b/libc/src/__support/str_to_float.h
@@ -298,9 +298,11 @@ eisel_lemire<long double>(ExpandedFloat<long double> init_num,
   BitsType msb = final_approx_upper >> (BITS_IN_MANTISSA - 1);
   BitsType final_mantissa =
       final_approx_upper >>
-      (msb + BITS_IN_MANTISSA -
-       (fputil::FloatProperties<long double>::MANTISSA_WIDTH + 3));
-  exp2 -= static_cast<uint32_t>(1 ^ msb); // same as !msb
+      static_cast<uint64_t>(
+          msb + BITS_IN_MANTISSA -
+          (fputil::FloatProperties<long double>::MANTISSA_WIDTH + 3));
+  exp2 -=
+      static_cast<uint32_t>(1ULL ^ static_cast<uint64_t>(msb)); // same as !msb
 
   if (round == RoundDirection::Nearest) {
     // Half-way ambiguity
@@ -565,7 +567,7 @@ clinger_fast_path(ExpandedFloat<T> init_num,
   }
 
   fputil::FPBits<T> result;
-  T float_mantissa = static_cast<T>(mantissa);
+  T float_mantissa = static_cast<T>(static_cast<uint64_t>(mantissa));
 
   if (exp10 == 0) {
     result = fputil::FPBits<T>(float_mantissa);
@@ -800,7 +802,7 @@ LIBC_INLINE FloatConvertReturn<T> binary_exp_to_float(ExpandedFloat<T> init_num,
 
   BitsType round_bit_mask = BitsType(1) << (amount_to_shift_right - 1);
   BitsType sticky_mask = round_bit_mask - 1;
-  bool round_bit = mantissa & round_bit_mask;
+  bool round_bit = static_cast<bool>(mantissa & round_bit_mask);
   bool sticky_bit = static_cast<bool>(mantissa & sticky_mask) || truncated;
 
   if (amount_to_shift_right < NUMBITS) {
@@ -810,7 +812,7 @@ LIBC_INLINE FloatConvertReturn<T> binary_exp_to_float(ExpandedFloat<T> init_num,
   } else {
     mantissa = 0;
   }
-  bool least_significant_bit = mantissa & BitsType(1);
+  bool least_significant_bit = static_cast<bool>(mantissa & BitsType(1));
 
   // TODO: check that this rounding behavior is correct.
 

diff  --git a/libc/src/stdio/printf_core/char_converter.h b/libc/src/stdio/printf_core/char_converter.h
index 23a85eb572883..93d2111548fa0 100644
--- a/libc/src/stdio/printf_core/char_converter.h
+++ b/libc/src/stdio/printf_core/char_converter.h
@@ -19,7 +19,7 @@ namespace __llvm_libc {
 namespace printf_core {
 
 LIBC_INLINE int convert_char(Writer *writer, const FormatSection &to_conv) {
-  char c = to_conv.conv_val_raw;
+  char c = static_cast<uint8_t>(to_conv.conv_val_raw);
 
   constexpr int string_len = 1;
 

diff  --git a/libc/src/stdio/printf_core/float_dec_converter.h b/libc/src/stdio/printf_core/float_dec_converter.h
index 8d220ceeebb02..dabeb9173dc85 100644
--- a/libc/src/stdio/printf_core/float_dec_converter.h
+++ b/libc/src/stdio/printf_core/float_dec_converter.h
@@ -598,8 +598,9 @@ LIBC_INLINE int convert_float_decimal_typed(Writer *writer,
         const bool trailingZeros =
             requiredTwos <= 0 ||
             (requiredTwos < 60 &&
-             multiple_of_power_of_2(float_bits.get_explicit_mantissa(),
-                                    static_cast<uint32_t>(requiredTwos)));
+             multiple_of_power_of_2(
+                 static_cast<uint64_t>(float_bits.get_explicit_mantissa()),
+                 static_cast<uint32_t>(requiredTwos)));
         switch (fputil::get_round()) {
         case FE_TONEAREST:
           // Round to nearest, if it's exactly halfway then round to even.
@@ -772,8 +773,9 @@ LIBC_INLINE int convert_float_dec_exp_typed(Writer *writer,
   const bool trailingZeros =
       requiredTwos <= 0 ||
       (requiredTwos < 60 &&
-       multiple_of_power_of_2(float_bits.get_explicit_mantissa(),
-                              static_cast<uint32_t>(requiredTwos)));
+       multiple_of_power_of_2(
+           static_cast<uint64_t>(float_bits.get_explicit_mantissa()),
+           static_cast<uint32_t>(requiredTwos)));
   switch (fputil::get_round()) {
   case FE_TONEAREST:
     // Round to nearest, if it's exactly halfway then round to even.
@@ -1020,8 +1022,9 @@ LIBC_INLINE int convert_float_dec_auto_typed(Writer *writer,
   const bool trailingZeros =
       requiredTwos <= 0 ||
       (requiredTwos < 60 &&
-       multiple_of_power_of_2(float_bits.get_explicit_mantissa(),
-                              static_cast<uint32_t>(requiredTwos)));
+       multiple_of_power_of_2(
+           static_cast<uint64_t>(float_bits.get_explicit_mantissa()),
+           static_cast<uint32_t>(requiredTwos)));
   switch (fputil::get_round()) {
   case FE_TONEAREST:
     // Round to nearest, if it's exactly halfway then round to even.
@@ -1147,7 +1150,8 @@ LIBC_INLINE int convert_float_decimal(Writer *writer,
                                                       float_bits);
     }
   } else {
-    fputil::FPBits<double>::UIntType float_raw = to_conv.conv_val_raw;
+    fputil::FPBits<double>::UIntType float_raw =
+        static_cast<uint64_t>(to_conv.conv_val_raw);
     fputil::FPBits<double> float_bits(float_raw);
     if (!float_bits.is_inf_or_nan()) {
       return convert_float_decimal_typed<double>(writer, to_conv, float_bits);
@@ -1167,7 +1171,8 @@ LIBC_INLINE int convert_float_dec_exp(Writer *writer,
                                                       float_bits);
     }
   } else {
-    fputil::FPBits<double>::UIntType float_raw = to_conv.conv_val_raw;
+    fputil::FPBits<double>::UIntType float_raw =
+        static_cast<uint64_t>(to_conv.conv_val_raw);
     fputil::FPBits<double> float_bits(float_raw);
     if (!float_bits.is_inf_or_nan()) {
       return convert_float_dec_exp_typed<double>(writer, to_conv, float_bits);
@@ -1187,7 +1192,8 @@ LIBC_INLINE int convert_float_dec_auto(Writer *writer,
                                                        float_bits);
     }
   } else {
-    fputil::FPBits<double>::UIntType float_raw = to_conv.conv_val_raw;
+    fputil::FPBits<double>::UIntType float_raw =
+        static_cast<uint64_t>(to_conv.conv_val_raw);
     fputil::FPBits<double> float_bits(float_raw);
     if (!float_bits.is_inf_or_nan()) {
       return convert_float_dec_auto_typed<double>(writer, to_conv, float_bits);

diff  --git a/libc/src/stdio/printf_core/float_hex_converter.h b/libc/src/stdio/printf_core/float_hex_converter.h
index a26a3e796e4df..b3d7b22aace77 100644
--- a/libc/src/stdio/printf_core/float_hex_converter.h
+++ b/libc/src/stdio/printf_core/float_hex_converter.h
@@ -51,7 +51,8 @@ LIBC_INLINE int convert_float_hex_exp(Writer *writer,
   } else {
     mantissa_width = fputil::MantissaWidth<double>::VALUE;
     exponent_bias = fputil::FPBits<double>::EXPONENT_BIAS;
-    fputil::FPBits<double>::UIntType float_raw = to_conv.conv_val_raw;
+    fputil::FPBits<double>::UIntType float_raw =
+        static_cast<uint64_t>(to_conv.conv_val_raw);
     fputil::FPBits<double> float_bits(float_raw);
     is_negative = float_bits.get_sign();
     exponent = float_bits.get_exponent();
@@ -146,8 +147,9 @@ LIBC_INLINE int convert_float_hex_exp(Writer *writer,
   size_t mant_cur = mant_len;
   size_t first_non_zero = 1;
   for (; mant_cur > 0; --mant_cur, mantissa /= 16) {
-    char new_digit = ((mantissa % 16) > 9) ? ((mantissa % 16) - 10 + a)
-                                           : ((mantissa % 16) + '0');
+    char new_digit =
+        static_cast<uint8_t>(((mantissa % 16) > 9) ? ((mantissa % 16) - 10 + a)
+                                                   : ((mantissa % 16) + '0'));
     mant_buffer[mant_cur - 1] = new_digit;
     if (new_digit != '0' && first_non_zero < mant_cur)
       first_non_zero = mant_cur;

diff  --git a/libc/src/stdio/printf_core/float_inf_nan_converter.h b/libc/src/stdio/printf_core/float_inf_nan_converter.h
index b7dcf8692e975..1883b47ed99b0 100644
--- a/libc/src/stdio/printf_core/float_inf_nan_converter.h
+++ b/libc/src/stdio/printf_core/float_inf_nan_converter.h
@@ -36,7 +36,8 @@ LIBC_INLINE int convert_inf_nan(Writer *writer, const FormatSection &to_conv) {
     is_negative = float_bits.get_sign();
     mantissa = float_bits.get_explicit_mantissa();
   } else {
-    fputil::FPBits<double>::UIntType float_raw = to_conv.conv_val_raw;
+    fputil::FPBits<double>::UIntType float_raw =
+        static_cast<uint64_t>(to_conv.conv_val_raw);
     fputil::FPBits<double> float_bits(float_raw);
     is_negative = float_bits.get_sign();
     mantissa = float_bits.get_explicit_mantissa();

diff  --git a/libc/src/stdio/printf_core/int_converter.h b/libc/src/stdio/printf_core/int_converter.h
index f5c849c4ef929..b4775a6861a44 100644
--- a/libc/src/stdio/printf_core/int_converter.h
+++ b/libc/src/stdio/printf_core/int_converter.h
@@ -43,7 +43,7 @@ LIBC_INLINE int convert_int(Writer *writer, const FormatSection &to_conv) {
   static constexpr size_t BITS_IN_BYTE = 8;
   static constexpr size_t BITS_IN_NUM = sizeof(uintmax_t) * BITS_IN_BYTE;
 
-  uintmax_t num = to_conv.conv_val_raw;
+  uintmax_t num = static_cast<uint64_t>(to_conv.conv_val_raw);
   bool is_negative = false;
   FormatFlags flags = to_conv.flags;
 


        


More information about the libc-commits mailing list