gcc/libgcc/soft-fp
Jakub Jelinek 2ca373b7e8 libgcc: Fix UB in FP_FROM_BITINT
As I wrote earlier, I was seeing
FAIL: gcc.dg/torture/bitint-24.c   -O0  execution test
FAIL: gcc.dg/torture/bitint-24.c   -O2  execution test
with the ia32 _BitInt enablement patch on i686-linux.  I thought
floatbitintxf.c was miscompiled with -O2 -march=i686 -mtune=generic, but it
turned out to be UB in it.

If a signed _BitInt to be converted to binary floating point has
(after sign extension from possible partial limb to full limb) one or
more most significant limbs equal to all ones and then in the limb below
(the most significant non-~(UBILtype)0 limb) has the most significant limb
cleared, like for 32-bit limbs
0x81582c05U, 0x0a8b01e4U, 0xc1b8b18fU, 0x2aac2a08U, -1U, -1U
then bitint_reduce_prec can't reduce it to that 0x2aac2a08U limb, so
msb is all ones and precision is negative (so it reduced precision from
161 to 192 bits down to 160 bits, in theory could go as low as 129 bits
but that wouldn't change anything on the following behavior).
But still iprec is negative, -160 here.
For that case (i.e. where we are dealing with an negative input), the
code was using 65 - __builtin_clzll (~msb) to compute how many relevant
bits we have from the msb.  Unfortunately that invokes UB for msb all ones.
The right number of relevant bits in that case is 1 though (like for
-2 it is 2 and -4 or -3 3 as already computed) - all we care about from that
is that the most significant bit is set (i.e. the number is negative) and
the bits below that should be supplied from the limbs below.

So, the following patch fixes it by special casing it not to invoke UB.

For msb 0 we already have a special case from before (but that is also
different because msb 0 implies the whole number is 0 given the way
bitint_reduce_prec works - even if we have limbs like ..., 0x80000000U, 0U
the reduction can skip the most significant limb and msb then would be
the one below it), so if iprec > 0, we already don't call __builtin_clzll
on 0.

2024-02-13  Jakub Jelinek  <jakub@redhat.com>

	* soft-fp/bitint.h (FP_FROM_BITINT): If iprec < 0 and msb is all ones,
	just set n to 1 instead of using __builtin_clzll (~msb).
2024-02-13 10:33:08 +01:00
..
adddf3.c
addsf3.c
addtf3.c
bitint.h libgcc: Fix UB in FP_FROM_BITINT 2024-02-13 10:33:08 +01:00
bitintpow10.c
bitintpow10.h
brain.h
divdf3.c
divsf3.c
divtf3.c
double.h
eqdf2.c
eqhf2.c
eqsf2.c
eqtf2.c
extendbfsf2.c
extenddftf2.c
extended.h
extendhfdf2.c
extendhfsf2.c
extendhftf2.c
extendhfxf2.c
extendsfdf2.c
extendsftf2.c
extendxftf2.c
fixddbitint.c libgcc: Fix BIL_TYPE_SIZE == 32 support in _BitInt <-> dfp support 2024-02-10 12:50:52 +01:00
fixddti.c
fixdfbitint.c
fixdfdi.c
fixdfsi.c
fixdfti.c
fixhfdi.c
fixhfsi.c
fixhfti.c
fixsdbitint.c libgcc: Fix BIL_TYPE_SIZE == 32 support in _BitInt <-> dfp support 2024-02-10 12:50:52 +01:00
fixsdti.c
fixsfbitint.c
fixsfdi.c
fixsfsi.c
fixsfti.c
fixtdbitint.c libgcc: Fix BIL_TYPE_SIZE == 32 support in _BitInt <-> dfp support 2024-02-10 12:50:52 +01:00
fixtdti.c
fixtfbitint.c
fixtfdi.c
fixtfsi.c
fixtfti.c
fixunsddti.c
fixunsdfdi.c
fixunsdfsi.c
fixunsdfti.c
fixunshfdi.c
fixunshfsi.c
fixunshfti.c
fixunssdti.c
fixunssfdi.c
fixunssfsi.c
fixunssfti.c
fixunstdti.c
fixunstfdi.c
fixunstfsi.c
fixunstfti.c
fixxfbitint.c
floatbitintbf.c
floatbitintdd.c libgcc: Fix a bug in _BitInt -> dfp conversions 2024-02-10 12:51:39 +01:00
floatbitintdf.c
floatbitinthf.c
floatbitintsd.c libgcc: Fix a bug in _BitInt -> dfp conversions 2024-02-10 12:51:39 +01:00
floatbitintsf.c
floatbitinttd.c libgcc: Fix a bug in _BitInt -> dfp conversions 2024-02-10 12:51:39 +01:00
floatbitinttf.c
floatbitintxf.c
floatdibf.c
floatdidf.c
floatdihf.c
floatdisf.c
floatditf.c
floatsidf.c
floatsihf.c
floatsisf.c
floatsitf.c
floattibf.c
floattidd.c
floattidf.c
floattihf.c
floattisd.c
floattisf.c
floattitd.c
floattitf.c
floatundibf.c
floatundidf.c
floatundihf.c
floatundisf.c
floatunditf.c
floatunsidf.c
floatunsihf.c
floatunsisf.c
floatunsitf.c
floatuntibf.c
floatuntidd.c
floatuntidf.c
floatuntihf.c
floatuntisd.c
floatuntisf.c
floatuntitd.c
floatuntitf.c
gedf2.c
gesf2.c
getf2.c
half.h
ledf2.c
lesf2.c
letf2.c
muldf3.c
mulsf3.c
multf3.c
negdf2.c
negsf2.c
negtf2.c
op-1.h
op-2.h
op-4.h
op-8.h
op-common.h
quad.h
README
single.h
soft-fp.h
subdf3.c
subsf3.c
subtf3.c
truncbfhf2.c
truncdfbf2.c
truncdfhf2.c
truncdfsf2.c
trunchfbf2.c
truncsfbf2.c
truncsfhf2.c
trunctfbf2.c
trunctfdf2.c
trunctfhf2.c
trunctfsf2.c
trunctfxf2.c
truncxfbf2.c
truncxfhf2.c
unorddf2.c
unordsf2.c
unordtf2.c

The files in this directory are part of the GNU C Library, not part of
GCC.  As described at <http://gcc.gnu.org/codingconventions.html>,
changes should be made to the GNU C Library and the changed files then
imported into GCC.