https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88451
Bug ID: 88451
Summary: No rounding in fixed-point arithmetic (Decimal to
fixed-point conversion, multiplication)
Product: gcc
Version: 6.3.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: mantas.mikaitis at manchester dot ac.uk
Target Milestone: ---
Hello,
This bug report is about that there is no rounding in various parts of the
fixed-point arithmetic. Here are some cases run on ARM968:
1)
accum x = 0.04k; // This ends up as 0.03997802734375.
2)
double x = 0.04;
accum y = x; // This ends up as 0.03997802734375.
As you can notice, 0.04 is rounded down to 0.03997802734375, however, the
nearest accum to the 0.04 is 0.040008544921875.
3)
Multiplication of two accums results in a 63-bit value which needs to be
shifted right to put the result back to the accum type. Upon a shift, there is
no rounding and 15 bottom bits are truncated.
It seems to be doing binary truncation instead of rounding, which is also
mathematically legal operation, but introduces a larger error. Looking into the
ISO draft standard for fixed-point arithmetic, it specifies that
FX_FULL_PRECISION pragma can be used to obtain full precision:
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n968.pdf, but it seems to have
no effect when I turn it on.
I am working with gcc version 6.3.1 20170620 (release) [ARM/embedded-6-branch
revision 249437] (GNU MCU Eclipse ARM Embedded GCC, 64-bits).
Many thanks,
Mantas M.