Skip to content

Commit

Permalink
Modified __safe_add and __safe_sub function in src/count_min_sketch.c (
Browse files Browse the repository at this point in the history
…#11)

* modified __safe_add and __safe_sub these two function and test it under Ubuntu14.04 LTS with gcc 4.8.4.
  • Loading branch information
meixsh authored and barrust committed Mar 7, 2019
1 parent 81bdf63 commit 7192c72
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/count_min_sketch.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,16 +310,16 @@ int __compare (const void * a, const void * b) {
int32_t __safe_add(int a, int b) {
/* use the gcc macro if compiling with GCC, otherwise, simple overflow check */
int32_t c = 0;
#ifdef __GNUC__
#if (defined(__GNU__) && __GNUC__ >= 5)
int bl = __builtin_add_overflow(a, b, &c);
if (bl != 0) {
c = INT_MAX;
}
#else
if (b < INT_MIN + a) {
c = INT_MIN;
if (b > INT_MAX - a) {
c = INT_MAX;
} else {
c = a - b;
c = a + b;
}
#endif

Expand All @@ -329,16 +329,16 @@ int32_t __safe_add(int a, int b) {
int32_t __safe_sub(int32_t a, int32_t b) {
/* use the gcc macro if compiling with GCC, otherwise, simple overflow check */
int32_t c = 0;
#ifdef __GNUC__
#if (defined(__GNU__) && __GNUC__ >= 5)
int32_t bl = __builtin_sub_overflow(a, b, &c);
if (bl != 0) {
c = INT_MAX;
}
#else
if (b > INT_MAX - a) {
if (b < a - INT_MAX) {
c = INT_MAX;
} else {
c = a + b;
c = a - b;
}
#endif

Expand Down

0 comments on commit 7192c72

Please sign in to comment.