https://gcc.gnu.org/g:5e570c3303ae36757aa5340ac60d562562052fc9

commit r15-9848-g5e570c3303ae36757aa5340ac60d562562052fc9
Author: Jakub Jelinek <ja...@redhat.com>
Date:   Thu Jun 19 08:57:27 2025 +0200

    dfp: Further decimal_real_to_integer fixes [PR120631]
    
    Unfortunately, the following further testcase shows that there aren't
    problems only with very large precisions and large exponents, but pretty
    much anything larger than 64-bits.  After all, before _BitInt support dfp
    didn't even have {,unsigned }__int128 <-> _Decimal{32,64,128,64x} support,
    and the testcase again shows some of the conversions yielding zeros.
    While the pr120631.c test worked even without the earlier patch.
    
    So, this patch assumes 64-bit precision at most is ok and for anything
    larger it just uses exponent 0 and multiplies afterwards.
    
    2025-06-19  Jakub Jelinek  <ja...@redhat.com>
    
            PR middle-end/120631
            * dfp.cc (decimal_real_to_integer): Use result multiplication not 
just
            when precision > 128 and dn.exponent > 19, but when precision > 64
            and dn.exponent > 0.
    
            * gcc.dg/dfp/bitint-10.c: New test.
            * gcc.dg/dfp/pr120631.c: New test.
    
    (cherry picked from commit e2eb9da5546b5e2fccb86586cda3beee8f69f5c9)

Diff:
---
 gcc/dfp.cc                           | 46 +++++++++++++++++++++++++++------
 gcc/testsuite/gcc.dg/dfp/bitint-10.c | 49 ++++++++++++++++++++++++++++++++++++
 gcc/testsuite/gcc.dg/dfp/pr120631.c  | 25 ++++++++++++++++++
 3 files changed, 112 insertions(+), 8 deletions(-)

diff --git a/gcc/dfp.cc b/gcc/dfp.cc
index 4011a95db4af..74e964214b5c 100644
--- a/gcc/dfp.cc
+++ b/gcc/dfp.cc
@@ -625,14 +625,14 @@ decimal_real_to_integer (const REAL_VALUE_TYPE *r, bool 
*fail, int precision)
   set.traps = 0;
   set.round = DEC_ROUND_DOWN;
   decimal128ToNumber ((const decimal128 *) r->sig, &dn);
-  if (precision > 128 && decNumberIsFinite (&dn) && dn.exponent > 19)
+  if (precision > 64 && decNumberIsFinite (&dn) && dn.exponent > 0)
     {
       /* libdecNumber doesn't really handle too large integers.
         So when precision is large and exponent as well, trim the
         exponent and adjust the resulting wide_int by multiplying
-        it multiple times with 10^19.  */
-      scale = dn.exponent / 19;
-      dn.exponent %= 19;
+        it multiple times with powers of ten.  */
+      scale = dn.exponent;
+      dn.exponent = 0;
     }
 
   decNumberToIntegralValue (&dn2, &dn, &set);
@@ -649,13 +649,42 @@ decimal_real_to_integer (const REAL_VALUE_TYPE *r, bool 
*fail, int precision)
     *fail = true;
   if (scale && !failp)
     {
-      wide_int wm = wi::uhwi (HOST_WIDE_INT_UC (10000000000000000000),
-                             w.get_precision ());
       bool isneg = wi::neg_p (w);
       if (isneg)
        w = -w;
       enum wi::overflow_type ovf = wi::OVF_NONE;
-      do
+      unsigned HOST_WIDE_INT pow10s[] = {
+       HOST_WIDE_INT_UC (10),
+       HOST_WIDE_INT_UC (100),
+       HOST_WIDE_INT_UC (1000),
+       HOST_WIDE_INT_UC (10000),
+       HOST_WIDE_INT_UC (100000),
+       HOST_WIDE_INT_UC (1000000),
+       HOST_WIDE_INT_UC (10000000),
+       HOST_WIDE_INT_UC (100000000),
+       HOST_WIDE_INT_UC (1000000000),
+       HOST_WIDE_INT_UC (10000000000),
+       HOST_WIDE_INT_UC (100000000000),
+       HOST_WIDE_INT_UC (1000000000000),
+       HOST_WIDE_INT_UC (10000000000000),
+       HOST_WIDE_INT_UC (100000000000000),
+       HOST_WIDE_INT_UC (1000000000000000),
+       HOST_WIDE_INT_UC (10000000000000000),
+       HOST_WIDE_INT_UC (100000000000000000),
+       HOST_WIDE_INT_UC (1000000000000000000),
+       HOST_WIDE_INT_UC (10000000000000000000),
+      };
+      int s = scale % 19;
+      if (s)
+       {
+         wide_int wm = wi::uhwi (pow10s[s - 1], w.get_precision ());
+         w = wi::umul (w, wm, &ovf);
+         if (ovf)
+           scale = 0;
+       }
+      scale /= 19;
+      wide_int wm = wi::uhwi (pow10s[18], w.get_precision ());
+      while (scale)
        {
          if (scale & 1)
            {
@@ -667,8 +696,9 @@ decimal_real_to_integer (const REAL_VALUE_TYPE *r, bool 
*fail, int precision)
          if (!scale)
            break;
          wm = wi::umul (wm, wm, &ovf);
+         if (ovf)
+           break;
        }
-      while (!ovf);
       if (ovf)
        {
          *fail = true;
diff --git a/gcc/testsuite/gcc.dg/dfp/bitint-10.c 
b/gcc/testsuite/gcc.dg/dfp/bitint-10.c
new file mode 100644
index 000000000000..b48f0ea6c277
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/dfp/bitint-10.c
@@ -0,0 +1,49 @@
+/* PR middle-end/120631 */
+/* { dg-do run { target bitint } } */
+/* { dg-options "-O2" } */
+
+#if __BITINT_MAXWIDTH__ >= 128
+_Decimal128 a = 123456789135792468012345678900000000000.0dl;
+_BitInt(128) b = 123456789135792468012345678900000000000wb;
+_Decimal64 c = 12345678913579000000000000000000000000.0dd;
+_BitInt(127) d = 12345678913579000000000000000000000000wb;
+#endif
+#if __BITINT_MAXWIDTH__ >= 256
+_Decimal128 m = 
1234567891357924680123456789000000000000000000000000000000000000000000000000.0dl;
+_BitInt(256) n = 
1234567891357924680123456789000000000000000000000000000000000000000000000000wb;
+_Decimal64 o = 
1234567891357900000000000000000000000000000000000000000000000000000000000000.0dd;
+_BitInt(255) p = 
1234567891357900000000000000000000000000000000000000000000000000000000000000wb;
+#endif
+
+int
+main ()
+{
+#if __BITINT_MAXWIDTH__ >= 128
+  if (a != b || (_BitInt(128)) a != b || c != d || (_BitInt(127)) c != d)
+    __builtin_abort ();
+  _Decimal128 e = 123456789135792468012345678900000000000.0dl;
+  _BitInt(128) f = 123456789135792468012345678900000000000wb;
+  _Decimal128 g = 123456789135792468012345678900000000000wb;
+  _BitInt(128) h = 123456789135792468012345678900000000000.0dl;
+  _Decimal64 i = 12345678913579000000000000000000000000.0dd;
+  _BitInt(128) j = 12345678913579000000000000000000000000wb;
+  _Decimal64 k = 12345678913579000000000000000000000000wb;
+  _BitInt(128) l = 12345678913579000000000000000000000000.0dd;
+  if (e != g || f != h || i != k || j != l)
+    __builtin_abort ();
+#endif
+#if __BITINT_MAXWIDTH__ >= 256
+  if (m != n || (_BitInt(256)) m != n || o != p || (_BitInt(255)) o != p)
+    __builtin_abort ();
+  _Decimal128 q = 
1234567891357924680123456789000000000000000000000000000000000000000000000000.0dl;
+  _BitInt(256) r = 
1234567891357924680123456789000000000000000000000000000000000000000000000000wb;
+  _Decimal128 s = 
1234567891357924680123456789000000000000000000000000000000000000000000000000wb;
+  _BitInt(256) t = 
1234567891357924680123456789000000000000000000000000000000000000000000000000.0dl;
+  _Decimal64 u = 
1234567891357900000000000000000000000000000000000000000000000000000000000000.0dd;
+  _BitInt(255) v = 
1234567891357900000000000000000000000000000000000000000000000000000000000000wb;
+  _Decimal64 w = 
1234567891357900000000000000000000000000000000000000000000000000000000000000wb;
+  _BitInt(255) x = 
1234567891357900000000000000000000000000000000000000000000000000000000000000.0dd;
+  if (q != s || r != t || u != w || v != x)
+    __builtin_abort ();
+#endif
+}
diff --git a/gcc/testsuite/gcc.dg/dfp/pr120631.c 
b/gcc/testsuite/gcc.dg/dfp/pr120631.c
new file mode 100644
index 000000000000..2085ff7ba5a7
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/dfp/pr120631.c
@@ -0,0 +1,25 @@
+/* PR middle-end/120631 */
+/* { dg-do run } */
+/* { dg-options "-O2" } */
+
+_Decimal64 a = 1234567891357900000.0dd;
+long long b = 1234567891357900000LL;
+_Decimal32 c = 1234567000000000000.0df;
+long long d = 1234567000000000000LL;
+
+int
+main ()
+{
+  if (a != b || (long long) a != b || c != d || (long long) c != d)
+    __builtin_abort ();
+  _Decimal64 e = 1234567891357900000.0dd;
+  long long f = 1234567891357900000LL;
+  _Decimal64 g = 1234567891357900000LL;
+  long long h = 1234567891357900000.0dd;
+  _Decimal32 i = 1234567000000000000.0df;
+  long long j = 1234567000000000000LL;
+  _Decimal32 k = 1234567000000000000LL;
+  long long l = 1234567000000000000.0df;
+  if (e != g || f != h || i != k || j != l)
+    __builtin_abort ();
+}

Reply via email to