diff mbox series

[1/4] libstdc++: Factor out uses of __int128 into a type alias

Message ID 20210311171653.2228370-1-ppalka@redhat.com
State New
Headers show
Series [1/4] libstdc++: Factor out uses of __int128 into a type alias | expand

Commit Message

Patrick Palka March 11, 2021, 5:16 p.m. UTC
Since Ryu has an alias uint128_t for this same purpose, it seems best
for us to use this name as well, so as to minimize the amount of local
modifications we'd need to make to our copy of Ryu.  (In a subsequent
patch, we're going to remove Ryu's aliases so that it uses the one
defined in floating_to_chars.cc.)

libstdc++-v3/ChangeLog:

	* src/c++17/floating_to_chars.cc (uint128_t): New conditionally
	defined alias of unsigned __int128.
	(floating_type_traits_binary128::mantissa_t): Use uint128_t
	instead of unsigned __int128.
	[LONG_DOUBLE_KIND == LDK_IBM128]
	(floating_type_traits<long double>::mantissa_t): Likewise.
	(get_ieee_repr): Likewise.  Make casts from uint_t to mantissa_t
	and uint32_t explicit.  Simplify the extraction of mantissa,
	exponent and sign bit.
---
 libstdc++-v3/src/c++17/floating_to_chars.cc | 25 +++++++++++++--------
 1 file changed, 16 insertions(+), 9 deletions(-)
diff mbox series

Patch

diff --git a/libstdc++-v3/src/c++17/floating_to_chars.cc b/libstdc++-v3/src/c++17/floating_to_chars.cc
index 611747bb99e..da3fbaa1ed1 100644
--- a/libstdc++-v3/src/c++17/floating_to_chars.cc
+++ b/libstdc++-v3/src/c++17/floating_to_chars.cc
@@ -98,6 +98,10 @@  using F128_type = void;
 
 namespace
 {
+#if defined __SIZEOF_INT128__
+  using uint128_t = unsigned __int128;
+#endif
+
   namespace ryu
   {
 #include "ryu/common.h"
@@ -171,7 +175,7 @@  namespace
     static constexpr int mantissa_bits = 112;
     static constexpr int exponent_bits = 15;
     static constexpr bool has_implicit_leading_bit = true;
-    using mantissa_t = unsigned __int128;
+    using mantissa_t = uint128_t;
     using shortest_scientific_t = ryu::floating_decimal_128;
 
     static constexpr uint64_t pow10_adjustment_tab[]
@@ -367,7 +371,7 @@  namespace
       static constexpr int mantissa_bits = 105;
       static constexpr int exponent_bits = 11;
       static constexpr bool has_implicit_leading_bit = true;
-      using mantissa_t = unsigned __int128;
+      using mantissa_t = uint128_t;
       using shortest_scientific_t = ryu::floating_decimal_128;
 
       static constexpr uint64_t pow10_adjustment_tab[]
@@ -393,6 +397,7 @@  namespace
     ieee_t<T>
     get_ieee_repr(const T value)
     {
+      using mantissa_t = typename floating_type_traits<T>::mantissa_t;
       constexpr int mantissa_bits = floating_type_traits<T>::mantissa_bits;
       constexpr int exponent_bits = floating_type_traits<T>::exponent_bits;
       constexpr int total_bits = mantissa_bits + exponent_bits + 1;
@@ -404,7 +409,7 @@  namespace
 	  return uint64_t{};
 #ifdef __SIZEOF_INT128__
 	else if constexpr (total_bits <= 128)
-	  return (unsigned __int128){};
+	  return uint128_t{};
 #endif
       };
       using uint_t = decltype(get_uint_t());
@@ -412,10 +417,13 @@  namespace
       memcpy(&value_bits, &value, sizeof(value));
 
       ieee_t<T> ieee_repr;
-      ieee_repr.mantissa = value_bits & ((uint_t{1} << mantissa_bits) - 1u);
+      ieee_repr.mantissa
+	= static_cast<mantissa_t>(value_bits & ((uint_t{1} << mantissa_bits) - 1u));
+      value_bits >>= mantissa_bits;
       ieee_repr.biased_exponent
-	= (value_bits >> mantissa_bits) & ((uint_t{1} << exponent_bits) - 1u);
-      ieee_repr.sign = (value_bits >> (mantissa_bits + exponent_bits)) & 1;
+	= static_cast<uint32_t>(value_bits & ((uint_t{1} << exponent_bits) - 1u));
+      value_bits >>= exponent_bits;
+      ieee_repr.sign = (value_bits & 1) != 0;
       return ieee_repr;
     }
 
@@ -430,7 +438,6 @@  namespace
       // mantissa (plus an implicit leading bit).  We use the exponent and sign
       // of the high part, and we merge the mantissa of the high part with the
       // mantissa (and the implicit leading bit) of the low part.
-      using uint_t = unsigned __int128;
       uint64_t value_bits[2] = {};
       memcpy(value_bits, &value, sizeof(value_bits));
 
@@ -478,8 +485,8 @@  namespace
 	}
 
       ieee_t<long double> ieee_repr;
-      ieee_repr.mantissa = ((uint_t{mantissa_hi} << 64)
-			    | (uint_t{mantissa_lo} << 4)) >> 11;
+      ieee_repr.mantissa = ((uint128_t{mantissa_hi} << 64)
+			    | (uint128_t{mantissa_lo} << 4)) >> 11;
       ieee_repr.biased_exponent = exponent_hi;
       ieee_repr.sign = sign_hi;
       return ieee_repr;