gcc/libgcc/soft-fp/fixsfbitint.c
Jakub Jelinek 2ce182e258 libgcc _BitInt support [PR102989]
This patch adds the library helpers for multiplication, division + modulo
and casts from and to floating point (both binary and decimal).
As described in the intro, the first step is try to reduce further the
passed in precision by skipping over most significant limbs with just zeros
or sign bit copies.  For multiplication and division I've implemented
a simple algorithm, using something smarter like Karatsuba or Toom N-Way
might be faster for very large _BitInts (which we don't support right now
anyway), but could mean more code in libgcc, which maybe isn't what people
are willing to accept.
For the to/from floating point conversions the patch uses soft-fp, because
it already has tons of handy macros which can be used for that.  In theory
it could be implemented using {,unsigned} long long or {,unsigned} __int128
to/from floating point conversions with some frexp before/after, but at that
point we already need to force it into integer registers and analyze it
anyway.  Plus, for 32-bit arches there is no __int128 that could be used
for XF/TF mode stuff.
I know that soft-fp is owned by glibc and I think the op-common.h change
should be propagated there, but the bitint stuff is really GCC specific
and IMHO doesn't belong into the glibc copy.

2023-09-06  Jakub Jelinek  <jakub@redhat.com>

	PR c/102989
libgcc/
	* config/aarch64/t-softfp (softfp_extras): Use += rather than :=.
	* config/i386/64/t-softfp (softfp_extras): Likewise.
	* config/i386/libgcc-glibc.ver (GCC_14.0.0): Export _BitInt support
	routines.
	* config/i386/t-softfp (softfp_extras): Add fixxfbitint and
	bf, hf and xf mode floatbitint.
	(CFLAGS-floatbitintbf.c, CFLAGS-floatbitinthf.c): Add -msse2.
	* config/riscv/t-softfp32 (softfp_extras): Use += rather than :=.
	* config/rs6000/t-e500v1-fp (softfp_extras): Likewise.
	* config/rs6000/t-e500v2-fp (softfp_extras): Likewise.
	* config/t-softfp (softfp_floatbitint_funcs): New.
	(softfp_bid_list): New.
	(softfp_func_list): Add sf and df mode from and to _BitInt libcalls.
	(softfp_bid_file_list): New.
	(LIB2ADD_ST): Add $(softfp_bid_file_list).
	* config/t-softfp-sfdftf (softfp_extras): Add fixtfbitint and
	floatbitinttf.
	* config/t-softfp-tf (softfp_extras): Likewise.
	* libgcc2.c (bitint_reduce_prec): New inline function.
	(BITINT_INC, BITINT_END): Define.
	(bitint_mul_1, bitint_addmul_1): New helper functions.
	(__mulbitint3): New function.
	(bitint_negate, bitint_submul_1): New helper functions.
	(__divmodbitint4): New function.
	* libgcc2.h (LIBGCC2_UNITS_PER_WORD): When building _BitInt support
	libcalls, redefine depending on __LIBGCC_BITINT_LIMB_WIDTH__.
	(__mulbitint3, __divmodbitint4): Declare.
	* libgcc-std.ver.in (GCC_14.0.0): Export _BitInt support routines.
	* Makefile.in (lib2funcs): Add _mulbitint3.
	(LIB2_DIVMOD_FUNCS): Add _divmodbitint4.
	* soft-fp/bitint.h: New file.
	* soft-fp/fixdfbitint.c: New file.
	* soft-fp/fixsfbitint.c: New file.
	* soft-fp/fixtfbitint.c: New file.
	* soft-fp/fixxfbitint.c: New file.
	* soft-fp/floatbitintbf.c: New file.
	* soft-fp/floatbitintdf.c: New file.
	* soft-fp/floatbitinthf.c: New file.
	* soft-fp/floatbitintsf.c: New file.
	* soft-fp/floatbitinttf.c: New file.
	* soft-fp/floatbitintxf.c: New file.
	* soft-fp/op-common.h (_FP_FROM_INT): Add support for rsize up to
	4 * _FP_W_TYPE_SIZE rather than just 2 * _FP_W_TYPE_SIZE.
	* soft-fp/bitintpow10.c: New file.
	* soft-fp/fixsdbitint.c: New file.
	* soft-fp/fixddbitint.c: New file.
	* soft-fp/fixtdbitint.c: New file.
	* soft-fp/floatbitintsd.c: New file.
	* soft-fp/floatbitintdd.c: New file.
	* soft-fp/floatbitinttd.c: New file.
2023-09-06 17:33:05 +02:00

72 lines
2.1 KiB
C

/* Software floating-point emulation.
Convert IEEE single to signed or unsigned _BitInt.
Copyright (C) 2023 Free Software Foundation, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
<http://www.gnu.org/licenses/>. */
#include "soft-fp.h"
#include "single.h"
#include "bitint.h"
#ifdef __BITINT_MAXWIDTH__
void
__fixsfbitint (UBILtype *r, SItype rprec, SFtype a)
{
FP_DECL_EX;
FP_DECL_S (A);
USItype arprec = rprec < 0 ? -rprec : rprec;
USItype rn = ((USItype) arprec + BIL_TYPE_SIZE - 1) / BIL_TYPE_SIZE;
USItype rv;
USItype rsize = arprec > SI_BITS ? SI_BITS : arprec;
USItype rsigned = rprec < 0;
USItype ovf = 0;
USItype shift = 0;
FP_INIT_EXCEPTIONS;
FP_UNPACK_RAW_S (A, a);
if (arprec > SI_BITS)
{
if (A_e < _FP_EXPBIAS_S || (A_s && !rsigned))
ovf = 1;
else if (A_e >= (_FP_EXPMAX_S < _FP_EXPBIAS_S + arprec
? _FP_EXPMAX_S
: _FP_EXPBIAS_S + arprec - rsigned))
{
ovf = 1;
if (A_s
&& A_e == _FP_EXPBIAS_S + arprec - 1
&& A_e < _FP_EXPMAX_S)
A_e -= arprec - SI_BITS;
}
else if (A_e >= _FP_EXPBIAS_S + SI_BITS - rsigned)
{
shift = A_e - (_FP_EXPBIAS_S + SI_BITS - rsigned - 1);
A_e -= shift;
}
}
FP_TO_INT_S (rv, A, rsize, rsigned);
FP_HANDLE_EXCEPTIONS;
FP_TO_BITINT (r, rn, arprec, shift, rv, rsize, rsigned, ovf, SI);
}
#endif