mirror of
https://github.com/justinethier/cyclone.git
synced 2025-05-24 20:45:06 +02:00
Signed multiplication overflow check
This commit is contained in:
parent
ad51bc7e80
commit
9899189c60
1 changed files with 4 additions and 2 deletions
|
@ -2489,6 +2489,8 @@ object __halt(object obj)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Signed arithmetic overflow checks, based on code from CHICKEN:
|
||||||
|
|
||||||
static int Cyc_checked_add(int x, int y, int *result)
|
static int Cyc_checked_add(int x, int y, int *result)
|
||||||
{
|
{
|
||||||
*result = x + y;
|
*result = x + y;
|
||||||
|
@ -2501,11 +2503,11 @@ static int Cyc_checked_sub(int x, int y, int *result)
|
||||||
return ((((*result ^ x) & ~(*result ^ y)) >> 30) != 0);
|
return ((((*result ^ x) & ~(*result ^ y)) >> 30) != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: overflow checking
|
// Code from http://stackoverflow.com/q/1815367/101258
|
||||||
static int Cyc_checked_mul(int x, int y, int *result)
|
static int Cyc_checked_mul(int x, int y, int *result)
|
||||||
{
|
{
|
||||||
*result = x * y;
|
*result = x * y;
|
||||||
return 0;
|
return (*result != 0 && (*result)/x != y);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define declare_num_op(FUNC, FUNC_OP, FUNC_APPLY, OP, INT_OP, BN_OP, NO_ARG, ONE_ARG, DIV) \
|
#define declare_num_op(FUNC, FUNC_OP, FUNC_APPLY, OP, INT_OP, BN_OP, NO_ARG, ONE_ARG, DIV) \
|
||||||
|
|
Loading…
Add table
Reference in a new issue