https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128159

            Bug ID: 128159
           Summary: wrong code: conversion of a _BitInt(256) constant to
                    _Decimal128 rounds an exact halfway value down instead
                    of to even
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: yuki.tanaka at defaultcase dot de
  Target Milestone: ---

typedef _BitInt(256) B;
#define E10 ((B) 10000000000)

__attribute__((noipa)) _Decimal128 a(void)
{
  return (E10 * E10 * E10 * 10000 + 15) * E10 * E10 * E10 * E10;
}

int main(void)
{
  if (a () != 1.000000000000000000000000000000002E74DL)
    __builtin_abort ();
  return 0;
}

$ gcc -O2 dfp_bitint.c -o dfp_bitint
$ ./dfp_bitint; echo $?
Aborted                    ./dfp_bitint
134

The value (10^34 + 15) * 10^40 has 35 significant digits and is exactly halfway
between two _Decimal128 values.  Round-half-even gives
1.000000000000000000000000000000002E74DL. GCC folds the conversion in a to
1.000000000000000000000000000000001E74DL, so main aborts.

Compiler Explorer:
https://godbolt.org/z/9n11oro47

Reply via email to