[PATCH] Overflow-trapping integer arithmetic routines7code
Stefan Kanthak
stefan.kanthak@nexgo.de
Mon Dec 7 16:45:01 GMT 2020
Jeff Law wrote Wednesday, November 25, 2020 7:11 PM:
> On 11/25/20 6:18 AM, Stefan Kanthak wrote:
>> Jeff Law <law@redhat.com> wrote:
[...]
>>> My inclination is to leave the overflow checking double-word multiplier
>>> as-is.
>> See but <https://gcc.gnu.org/pipermail/gcc/2020-October/234048.html> ff.
> Already read and considered it.
>>
>>> Though I guess you could keep the same structure as the existing
>>> implementation which tries to avoid unnecessary multiplies and still use
>>> the __builtin_{add,mul}_overflow to simplify the code a bit less
>>> aggressively.
Only relying on GCC to generate efficient code for __builtin_mul_overflow,
this should be either
DWtype
__mulvDI3 (DWtype u, DWtype v)
{
DWtype w;
if (__builtin_mul_overflow (u, v, &w))
abort ();
return w;
}
or
DWtype
__mulvDI3 (DWtype u, DWtype v)
{
UDWtype w, s = 0 - (u < 0), t = 0 - (v < 0);
if (__builtin_mul_overflow ((u ^ s) - s, (v ^ t) - t, &w))
abort ();
s ^= t;
w = (w ^ s) - s;
if ((DWtype) (w ^ s) < 0)
abort ();
return w;
}
if the latter produces better machine code (which it does at least for
i386 and AMD64).
> Instead keep the tests that detect the special cases that don't need as
> many multiplies and use the overflow builtins within that implementation
> framework. In cases where we can use the operands directly, that's
> helpful as going through the struct/union likely leads to unnecessary
> register shuffling.
regards
Stefan
More information about the Gcc-patches
mailing list