public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH 1/4] libstdc++: Factor out uses of __int128 into a type alias
@ 2021-03-11 17:16 Patrick Palka
  2021-03-11 17:16 ` [PATCH 2/4] libstdc++: Add LOCAL_PATCHES file to Ryu sources Patrick Palka
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Patrick Palka @ 2021-03-11 17:16 UTC (permalink / raw)
  To: gcc-patches; +Cc: libstdc++, Patrick Palka

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 --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;
-- 
2.31.0.rc2


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2021-03-11 18:15 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-11 17:16 [PATCH 1/4] libstdc++: Factor out uses of __int128 into a type alias Patrick Palka
2021-03-11 17:16 ` [PATCH 2/4] libstdc++: Add LOCAL_PATCHES file to Ryu sources Patrick Palka
2021-03-11 17:16 ` [PATCH 3/4] libstdc++: Remove Ryu's uint128_t aliases Patrick Palka
2021-03-11 17:16 ` [PATCH 4/4] libstdc++: Add fallback 128-bit integer class type and use it Patrick Palka
2021-03-11 18:09   ` Daniel Krügler
2021-03-11 18:15     ` Jonathan Wakely
2021-03-11 18:11   ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).