diff options
author | Christian Göttsche <cgzones@googlemail.com> | 2020-11-04 17:46:24 +0100 |
---|---|---|
committer | BenBE <BenBE@geshi.org> | 2020-11-15 18:25:21 +0100 |
commit | 397b5c4bd09115d0df0846fee1b06797b68ae11c (patch) | |
tree | c0680d6e463d6325f87e67ef12d88dc2bd805812 /Macros.h | |
parent | d785b1bbc3e30e41bacd283712455090eb2fe99c (diff) |
Introduce spaceship comparison for Processes
If currently two unsigned values are compared via `a - b`, in the case b
is actually bigger than a, the result will not be an negative number (as
-1 is expected) but a huge positive number as the subtraction is an
unsigned subtraction.
Avoid over-/underflow affected operations; use comparisons.
Modern compilers will generate sane code, like:
xor eax, eax
cmp rdi, rsi
seta al
sbb eax, 0
ret
Diffstat (limited to 'Macros.h')
-rw-r--r-- | Macros.h | 16 |
1 files changed, 12 insertions, 4 deletions
@@ -4,19 +4,27 @@ #include <assert.h> // IWYU pragma: keep #ifndef MINIMUM -#define MINIMUM(a, b) ((a) < (b) ? (a) : (b)) +#define MINIMUM(a, b) ((a) < (b) ? (a) : (b)) #endif #ifndef MAXIMUM -#define MAXIMUM(a, b) ((a) > (b) ? (a) : (b)) +#define MAXIMUM(a, b) ((a) > (b) ? (a) : (b)) #endif #ifndef CLAMP -#define CLAMP(x, low, high) (assert((low) <= (high)), ((x) > (high)) ? (high) : MAXIMUM(x, low)) +#define CLAMP(x, low, high) (assert((low) <= (high)), ((x) > (high)) ? (high) : MAXIMUM(x, low)) #endif #ifndef ARRAYSIZE -#define ARRAYSIZE(x) (sizeof(x) / sizeof((x)[0])) +#define ARRAYSIZE(x) (sizeof(x) / sizeof((x)[0])) +#endif + +#ifndef SPACESHIP_NUMBER +#define SPACESHIP_NUMBER(a, b) (((a) > (b)) - ((a) < (b))) +#endif + +#ifndef SPACESHIP_NULLSTR +#define SPACESHIP_NULLSTR(a, b) strcmp((a) ? (a) : "", (b) ? (b) : "") #endif #ifdef __GNUC__ // defined by GCC and Clang |