Skip to content

Commit

Permalink
Use __builtin_saddll_overflow() for adding integers
Browse files Browse the repository at this point in the history
  • Loading branch information
JanWielemaker committed Sep 11, 2023
1 parent 814fe4c commit 098e336
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions src/pl-arith.c
Original file line number Diff line number Diff line change
Expand Up @@ -1461,19 +1461,15 @@ pl_ar_add(Number n1, Number n2, Number r)

switch(n1->type)
{ case V_INTEGER:
{ if ( SAME_SIGN(n1->value.i, n2->value.i) )
{ if ( n2->value.i < 0 ) /* both negative */
{ if ( n1->value.i < PLMININT - n2->value.i )
goto overflow;
} else /* both positive */
{ if ( PLMAXINT - n1->value.i < n2->value.i )
goto overflow;
}
{ long long v;

static_assert(sizeof(long long) == sizeof(int64_t));
if ( !__builtin_saddll_overflow(n1->value.i, n2->value.i, &v) )
{ r->value.i = v;
r->type = V_INTEGER;
return TRUE;
}
r->value.i = n1->value.i + n2->value.i;
r->type = V_INTEGER;
succeed;
overflow:

if ( !promoteIntNumber(n1) ||
!promoteIntNumber(n2) )
fail;
Expand Down

0 comments on commit 098e336

Please sign in to comment.