> Am 18.06.2025 um 21:37 schrieb Jakub Jelinek <ja...@redhat.com>:
> 
> Hi!
> 
> 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.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for
> trunk/15.2/14.4?

Ok

Richard 

> 2025-06-18  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.
> 
> --- gcc/dfp.cc.jj    2025-06-18 08:07:15.397023476 +0200
> +++ gcc/dfp.cc    2025-06-18 09:11:09.555934992 +0200
> @@ -625,14 +625,14 @@ decimal_real_to_integer (const REAL_VALU
>   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_VALU
>     *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_VALU
>      if (!scale)
>        break;
>      wm = wi::umul (wm, wm, &ovf);
> +      if (ovf)
> +        break;
>    }
> -      while (!ovf);
>       if (ovf)
>    {
>      *fail = true;
> --- gcc/testsuite/gcc.dg/dfp/bitint-10.c.jj    2025-06-18 08:21:39.610714324 
> +0200
> +++ gcc/testsuite/gcc.dg/dfp/bitint-10.c    2025-06-18 08:38:02.954411784 
> +0200
> @@ -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
> +}
> --- gcc/testsuite/gcc.dg/dfp/pr120631.c.jj    2025-06-18 09:00:28.322767316 
> +0200
> +++ gcc/testsuite/gcc.dg/dfp/pr120631.c    2025-06-18 09:06:22.655964098 +0200
> @@ -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 ();
> +}
> 
>    Jakub
> 

Reply via email to