[builtins] Avoid undefined behavior when calculating absolute value in floatsidf.c and floatsisf.c

When compiling compiler-rt with -fsanitize=undefined and running testcases you
end up with the following warning:

UBSan: floatsidf.c:32:9: negation of -2147483648 cannot be represented in type 'si_int' (aka 'long'); cast to an unsigned type to negate this value to itself

The same kind of pattern exists in floatsisf.c

This was found in an out of tree target.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D146123
This commit is contained in:
Karl-Johan Karlsson 2023-08-23 08:25:51 +02:00
parent 34e3bc0b92
commit ff14585eb0
3 changed files with 11 additions and 9 deletions

View File

@ -27,20 +27,21 @@ COMPILER_RT_ABI fp_t __floatsidf(si_int a) {
// All other cases begin by extracting the sign and absolute value of a
rep_t sign = 0;
su_int aAbs = (su_int)a;
if (a < 0) {
sign = signBit;
a = -a;
aAbs = -aAbs;
}
// Exponent of (fp_t)a is the width of abs(a).
const int exponent = (aWidth - 1) - clzsi(a);
const int exponent = (aWidth - 1) - clzsi(aAbs);
rep_t result;
// Shift a into the significand field and clear the implicit bit. Extra
// cast to unsigned int is necessary to get the correct behavior for
// the input INT_MIN.
const int shift = significandBits - exponent;
result = (rep_t)(su_int)a << shift ^ implicitBit;
result = (rep_t)aAbs << shift ^ implicitBit;
// Insert the exponent
result += (rep_t)(exponent + exponentBias) << significandBits;

View File

@ -27,23 +27,24 @@ COMPILER_RT_ABI fp_t __floatsisf(si_int a) {
// All other cases begin by extracting the sign and absolute value of a
rep_t sign = 0;
su_int aAbs = (su_int)a;
if (a < 0) {
sign = signBit;
a = -a;
aAbs = -aAbs;
}
// Exponent of (fp_t)a is the width of abs(a).
const int exponent = (aWidth - 1) - clzsi(a);
const int exponent = (aWidth - 1) - clzsi(aAbs);
rep_t result;
// Shift a into the significand field, rounding if it is a right-shift
if (exponent <= significandBits) {
const int shift = significandBits - exponent;
result = (rep_t)a << shift ^ implicitBit;
result = (rep_t)aAbs << shift ^ implicitBit;
} else {
const int shift = exponent - significandBits;
result = (rep_t)a >> shift ^ implicitBit;
rep_t round = (rep_t)a << (typeWidth - shift);
result = (rep_t)aAbs >> shift ^ implicitBit;
rep_t round = (rep_t)aAbs << (typeWidth - shift);
if (round > signBit)
result++;
if (round == signBit)

View File

@ -29,7 +29,7 @@ COMPILER_RT_ABI fp_t __floatsitf(si_int a) {
su_int aAbs = (su_int)a;
if (a < 0) {
sign = signBit;
aAbs = ~(su_int)a + (su_int)1U;
aAbs = -aAbs;
}
// Exponent of (fp_t)a is the width of abs(a).