aboutsummaryrefslogtreecommitdiff
path: root/libgcc/soft-fp/floatbitinttd.c
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2025-05-27 23:10:08 +0200
committerJakub Jelinek <jakub@gcc.gnu.org>2025-05-27 23:10:08 +0200
commita57ea0a189b95228d524841497684b029cc51c00 (patch)
treea07a57f7618435d36443b7b949e01d4dc133fa73 /libgcc/soft-fp/floatbitinttd.c
parentee636671898c6bce63d9b2a698007b609aabbfe8 (diff)
downloadgcc-a57ea0a189b95228d524841497684b029cc51c00.zip
gcc-a57ea0a189b95228d524841497684b029cc51c00.tar.gz
gcc-a57ea0a189b95228d524841497684b029cc51c00.tar.bz2
libgcc: Add DPD support + fix big-endian support of _BitInt <-> dfp conversions
The following patch fixes FAIL: gcc.dg/dfp/bitint-1.c (test for excess errors) FAIL: gcc.dg/dfp/bitint-2.c (test for excess errors) FAIL: gcc.dg/dfp/bitint-3.c (test for excess errors) FAIL: gcc.dg/dfp/bitint-4.c (test for excess errors) FAIL: gcc.dg/dfp/bitint-5.c (test for excess errors) FAIL: gcc.dg/dfp/bitint-6.c (test for excess errors) FAIL: gcc.dg/dfp/bitint-8.c (test for excess errors) FAIL: gcc.dg/dfp/int128-1.c (test for excess errors) FAIL: gcc.dg/dfp/int128-2.c (test for excess errors) FAIL: gcc.dg/dfp/int128-4.c (test for excess errors) on s390x-linux (with the 3 not yet posted patches). The patch does multiple things: 1) the routines were written for the DFP BID (binary integer decimal) format which is used on all arches but powerpc*/s390* (those use DPD - densely packed decimal format); as most of the code is actually the same for both BID and DPD formats, I haven't copied the sources + slightly modified them, but added the DPD support directly, + renaming of the exported symbols from __bid_* prefixed to __dpd_* prefixed that GCC expects on the DPD targets 2) while testing that I've found some big-endian issues in the existing support 3) testing also revealed that in some cases __builtin_clzll (~msb) was called with msb set to all ones, so invoking UB; apparently on aarch64 and x86 we were lucky and got some value that happened to work well, but that wasn't the case on s390x For 1), the patch uses two ~ 2KB tables to speed up the decoding/encoding. I haven't found such tables in what is added into libgcc.a, though they are in libdecnumber/bid/bid2dpd_dpd2bid.h, but there they are just huge and next to other huge tables - there is d2b which is like __dpd_d2bbitint in the patch but it uses 64-bit entries rather than 16-bit, then there is d2b2 with 64-bit entries like in d2b all multiplied by 1000, then d2b3 similarly multiplied by 1000000, then d2b4 similarly multiplied by 1000000000, then d2b5 similarly multiplied by 1000000000000ULL and d2b6 similarly multipled by 1000000000000000ULL. Arguably it can save some of the multiplications, but on the other side accesses memory which is unlikely in the caches, and the 2048 bytes in the patch vs. 24 times more for d2b is IMHO significant. For b2d, libdecnumber/bid/bid2dpd_dpd2bid.h has again b2d table like __dpd_b2dbitint in the patch, except that it has 64-bit entries rather than 16-bit (this time 1000 entries), but then has b2d2 which has the same entries shifted left by 10, then b2d3 shifted left by 20, b2d4 shifted left by 30 and b2d5 shifted left by 40. I can understand for d2b paying memory cost to speed up multiplications, but don't understand paying extra 4 * 8 * 1000 bytes (+ 6 * 1000 bytes for b2d not using ushort) just to avoid shifts. 2025-05-27 Jakub Jelinek <jakub@redhat.com> * config/t-softfp (softfp_bid_list): Don't guard with $(enable_decimal_float) == bid. * soft-fp/bitint.h (__bid_pow10bitint): For !defined(ENABLE_DECIMAL_BID_FORMAT) redefine to __dpd_pow10bitint. (__dpd_d2bbitint, __dpd_b2dbitint): Declare. * soft-fp/bitintpow10.c (__dpd_d2bbitint, __dpd_b2dbitint): New variables. * soft-fp/fixsdbitint.c (__bid_fixsdbitint): For !defined(ENABLE_DECIMAL_BID_FORMAT) redefine to __dpd_fixsdbitint. Add DPD support. Fix big-endian support. * soft-fp/fixddbitint.c (__bid_fixddbitint): For !defined(ENABLE_DECIMAL_BID_FORMAT) redefine to __dpd_fixddbitint. Add DPD support. Fix big-endian support. * soft-fp/fixtdbitint.c (__bid_fixtdbitint): For !defined(ENABLE_DECIMAL_BID_FORMAT) redefine to __dpd_fixtdbitint. Add DPD support. Fix big-endian support. * soft-fp/fixsdti.c (__bid_fixsdbitint): For !defined(ENABLE_DECIMAL_BID_FORMAT) redefine to __dpd_fixsdbitint. (__bid_fixsdti): For !defined(ENABLE_DECIMAL_BID_FORMAT) redefine to __dpd_fixsdti. * soft-fp/fixddti.c (__bid_fixddbitint): For !defined(ENABLE_DECIMAL_BID_FORMAT) redefine to __dpd_fixddbitint. (__bid_fixddti): For !defined(ENABLE_DECIMAL_BID_FORMAT) redefine to __dpd_fixddti. * soft-fp/fixtdti.c (__bid_fixtdbitint): For !defined(ENABLE_DECIMAL_BID_FORMAT) redefine to __dpd_fixtdbitint. (__bid_fixtdti): For !defined(ENABLE_DECIMAL_BID_FORMAT) redefine to __dpd_fixtdti. * soft-fp/fixunssdti.c (__bid_fixsdbitint): For !defined(ENABLE_DECIMAL_BID_FORMAT) redefine to __dpd_fixsdbitint. (__bid_fixunssdti): For !defined(ENABLE_DECIMAL_BID_FORMAT) redefine to __dpd_fixunssdti. * soft-fp/fixunsddti.c (__bid_fixddbitint): For !defined(ENABLE_DECIMAL_BID_FORMAT) redefine to __dpd_fixddbitint. (__bid_fixunsddti): For !defined(ENABLE_DECIMAL_BID_FORMAT) redefine to __dpd_fixunsddti. * soft-fp/fixunstdti.c (__bid_fixtdbitint): For !defined(ENABLE_DECIMAL_BID_FORMAT) redefine to __dpd_fixtdbitint. (__bid_fixunstdti): For !defined(ENABLE_DECIMAL_BID_FORMAT) redefine to __dpd_fixunstdti. * soft-fp/floatbitintsd.c (__bid_floatbitintsd): For !defined(ENABLE_DECIMAL_BID_FORMAT) redefine to __dpd_floatbitintsd. Add DPD support. Avoid calling __builtin_clzll with 0 argument. Fix big-endian support. * soft-fp/floatbitintdd.c (__bid_floatbitintdd): For !defined(ENABLE_DECIMAL_BID_FORMAT) redefine to __dpd_floatbitintdd. Add DPD support. Avoid calling __builtin_clzll with 0 argument. Fix big-endian support. * soft-fp/floatbitinttd.c (__bid_floatbitinttd): For !defined(ENABLE_DECIMAL_BID_FORMAT) redefine to __dpd_floatbitinttd. Add DPD support. Avoid calling __builtin_clzll with 0 argument. Fix big-endian support. * soft-fp/floattisd.c (__bid_floatbitintsd): For !defined(ENABLE_DECIMAL_BID_FORMAT) redefine to __dpd_floatbitintsd. (__bid_floattisd): For !defined(ENABLE_DECIMAL_BID_FORMAT) redefine to __dpd_floattisd. * soft-fp/floattidd.c (__bid_floatbitintdd): For !defined(ENABLE_DECIMAL_BID_FORMAT) redefine to __dpd_floatbitintdd. (__bid_floattidd): For !defined(ENABLE_DECIMAL_BID_FORMAT) redefine to __dpd_floattidd. * soft-fp/floattitd.c (__bid_floatbitinttd): For !defined(ENABLE_DECIMAL_BID_FORMAT) redefine to __dpd_floatbitinttd. (__bid_floattitd): For !defined(ENABLE_DECIMAL_BID_FORMAT) redefine to __dpd_floattitd. * soft-fp/floatuntisd.c (__bid_floatbitintsd): For !defined(ENABLE_DECIMAL_BID_FORMAT) redefine to __dpd_floatbitintsd. (__bid_floatuntisd): For !defined(ENABLE_DECIMAL_BID_FORMAT) redefine to __dpd_floatuntisd. * soft-fp/floatuntidd.c (__bid_floatbitintdd): For !defined(ENABLE_DECIMAL_BID_FORMAT) redefine to __dpd_floatbitintdd. (__bid_floatuntidd): For !defined(ENABLE_DECIMAL_BID_FORMAT) redefine to __dpd_floatuntidd. * soft-fp/floatuntitd.c (__bid_floatbitinttd): For !defined(ENABLE_DECIMAL_BID_FORMAT) redefine to __dpd_floatbitinttd. (__bid_floatuntitd): For !defined(ENABLE_DECIMAL_BID_FORMAT) redefine to __dpd_floatuntitd.
Diffstat (limited to 'libgcc/soft-fp/floatbitinttd.c')
-rw-r--r--libgcc/soft-fp/floatbitinttd.c68
1 files changed, 65 insertions, 3 deletions
diff --git a/libgcc/soft-fp/floatbitinttd.c b/libgcc/soft-fp/floatbitinttd.c
index 5fff339..50b9e77 100644
--- a/libgcc/soft-fp/floatbitinttd.c
+++ b/libgcc/soft-fp/floatbitinttd.c
@@ -1,7 +1,7 @@
/* Software floating-point emulation.
Convert a _BitInt to _Decimal128.
- Copyright (C) 2023 Free Software Foundation, Inc.
+ Copyright (C) 2023-2025 Free Software Foundation, Inc.
This file is part of GCC.
@@ -28,6 +28,9 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
#include "bitint.h"
#ifdef __BITINT_MAXWIDTH__
+#ifndef ENABLE_DECIMAL_BID_FORMAT
+#define __bid_floatbitinttd __dpd_floatbitinttd
+#endif
extern _Decimal128 __bid_floatbitinttd (const UBILtype *, SItype);
_Decimal128
@@ -51,7 +54,11 @@ __bid_floatbitinttd (const UBILtype *i, SItype iprec)
}
if (iprec < 0)
{
- SItype n = sizeof (0ULL) * __CHAR_BIT__ + 1 - __builtin_clzll (~msb);
+ SItype n;
+ if (msb == ~(UBILtype) 0)
+ n = 1;
+ else
+ n = sizeof (0ULL) * __CHAR_BIT__ + 1 - __builtin_clzll (~msb);
aiprec = (in - 1) * BIL_TYPE_SIZE + n;
}
else if (msb == 0)
@@ -117,7 +124,8 @@ __bid_floatbitinttd (const UBILtype *i, SItype iprec)
}
else
{
- __builtin_memcpy (buf + BITINT_END (q_limbs - in + 1, 0), i,
+ __builtin_memcpy (buf + BITINT_END (q_limbs - in + 1, 0),
+ i + BITINT_END (1, 0),
(in - 1) * sizeof (UBILtype));
buf[BITINT_END (q_limbs - in, in - 1)] = msb;
if (iprec < 0)
@@ -253,15 +261,69 @@ __bid_floatbitinttd (const UBILtype *i, SItype iprec)
}
exponent += 6176;
+#ifdef ENABLE_DECIMAL_BID_FORMAT
u.u[__FLOAT_WORD_ORDER__ != __ORDER_BIG_ENDIAN__]
= ((((UDItype) (iprec < 0)) << 63)
| (((UDItype) exponent) << 49)
| mantissahi);
+#else
+#ifdef __SIZEOF_INT128__
+ unsigned __int128 m = mantissahi;
+#else
+ unsigned _BitInt(128) m = mantissahi;
+#endif
+ m = (m << 64) | mantissalo;
+ u.u[0] = m / 1000000000000000ULL;
+ u.u[1] = m % 1000000000000000ULL;
+ mantissalo = __dpd_b2dbitint[u.u[1] % 1000];
+ u.u[1] /= 1000;
+ mantissalo |= ((UDItype) __dpd_b2dbitint[u.u[1] % 1000]) << 10;
+ u.u[1] /= 1000;
+ mantissalo |= ((UDItype) __dpd_b2dbitint[u.u[1] % 1000]) << 20;
+ u.u[1] /= 1000;
+ mantissalo |= ((UDItype) __dpd_b2dbitint[u.u[1] % 1000]) << 30;
+ u.u[1] /= 1000;
+ mantissalo |= ((UDItype) __dpd_b2dbitint[u.u[1] % 1000]) << 40;
+ mantissalo |= ((UDItype) __dpd_b2dbitint[u.u[0] % 1000]) << 50;
+ u.u[0] /= 1000;
+ mantissahi = __dpd_b2dbitint[u.u[0] % 1000];
+ u.u[0] /= 1000;
+ mantissalo |= mantissahi << 60;
+ mantissahi >>= 4;
+ mantissahi |= ((UDItype) __dpd_b2dbitint[u.u[0] % 1000]) << 6;
+ u.u[0] /= 1000;
+ mantissahi |= ((UDItype) __dpd_b2dbitint[u.u[0] % 1000]) << 16;
+ u.u[0] /= 1000;
+ mantissahi |= ((UDItype) __dpd_b2dbitint[u.u[0] % 1000]) << 26;
+ u.u[0] /= 1000;
+ mantissahi |= ((UDItype) __dpd_b2dbitint[u.u[0] % 1000]) << 36;
+ u.u[0] /= 1000;
+ if (u.u[0] >= 8)
+ u.u[__FLOAT_WORD_ORDER__ != __ORDER_BIG_ENDIAN__]
+ = (((((iprec < 0) << 2) | (UDItype) 3) << 61)
+ | (((UDItype) exponent & (3 << 12)) << 47)
+ | ((u.u[0] & 1) << 58)
+ | (((UDItype) exponent & 4095) << 46)
+ | mantissahi);
+ else
+ u.u[__FLOAT_WORD_ORDER__ != __ORDER_BIG_ENDIAN__]
+ = ((((UDItype) (iprec < 0)) << 63)
+ | (((UDItype) exponent & (3 << 12)) << 49)
+ | (u.u[0] << 58)
+ | (((UDItype) exponent & 4095) << 46)
+ | mantissahi);
+#endif
u.u[__FLOAT_WORD_ORDER__ == __ORDER_BIG_ENDIAN__] = mantissalo;
if (inexact)
{
ui.u[__FLOAT_WORD_ORDER__ != __ORDER_BIG_ENDIAN__]
+#ifdef ENABLE_DECIMAL_BID_FORMAT
= (((UDItype) (iprec < 0)) << 63) | (((UDItype) exponent - 1) << 49);
+#else
+ = ((((UDItype) (iprec < 0)) << 63)
+ | ((((UDItype) exponent - 1) & (3 << 12)) << 49)
+ | ((((UDItype) exponent - 1) & 4095) << 46));
+#endif
ui.u[__FLOAT_WORD_ORDER__ == __ORDER_BIG_ENDIAN__] = inexact + 3;
__asm ("" : "+g" (u.d));
__asm ("" : "+g" (ui.d));