libzahl

big integer library
git clone git://git.suckless.org/libzahl
Log | Files | Refs | README | LICENSE

commit 5e29d29416b568f380a2ab753e3e77e96af4b094
parent e746f06265981fd17bc656ca59e0ed82f6cea84d
Author: Mattias Andrée <maandree@kth.se>
Date:   Wed, 27 Apr 2016 17:26:41 +0200

Error-check implies unlikely branching

Signed-off-by: Mattias Andrée <maandree@kth.se>

Diffstat:
Msrc/allocator.c | 2+-
Msrc/internals.h | 4++--
Msrc/zdivmod.c | 4++--
Msrc/zmodpow.c | 2+-
Msrc/zmodpowu.c | 2+-
Msrc/zrand.c | 6+++---
Msrc/zsets.c | 4++--
Msrc/zsetup.c | 2+-
8 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/src/allocator.c b/src/allocator.c @@ -22,7 +22,7 @@ libzahl_realloc(z_t a, size_t need) a->chars = new; } else { a->chars = realloc(a->chars, new_size * sizeof(zahl_char_t)); - if (check(unlikely(!a->chars))) + if (check(!a->chars)) libzahl_memfailure(); } a->alloced = new_size; diff --git a/src/internals.h b/src/internals.h @@ -100,7 +100,7 @@ extern void *libzahl_temp_allocation; #if defined(UNSAFE) # define check(expr) 0 #else -# define check(expr) (expr) +# define check(expr) unlikely(expr) #endif #define SET_SIGNUM(a, signum) ZAHL_SET_SIGNUM(a, signum) @@ -337,7 +337,7 @@ zinit_temp(z_t a) size_t n = (size_t)(libzahl_temp_stack_end - libzahl_temp_stack); void* old = libzahl_temp_stack; libzahl_temp_stack = realloc(old, 2 * n * sizeof(*libzahl_temp_stack)); - if (check(unlikely(!libzahl_temp_stack))) { + if (check(!libzahl_temp_stack)) { libzahl_temp_stack = old; libzahl_memfailure(); } diff --git a/src/zdivmod.c b/src/zdivmod.c @@ -72,9 +72,9 @@ zdivmod(z_t a, z_t b, z_t c, z_t d) sign = zsignum(c) * zsignum(d); if (unlikely(!sign)) { - if (check(unlikely(!zzero(c)))) { + if (check(!zzero(c))) { libzahl_failure(-ZERROR_DIV_0); - } else if (check(unlikely(zzero(d)))) { + } else if (check(zzero(d))) { libzahl_failure(-ZERROR_0_DIV_0); } else { SET_SIGNUM(a, 0); diff --git a/src/zmodpow.c b/src/zmodpow.c @@ -25,7 +25,7 @@ zmodpow(z_t a, z_t b, z_t c, z_t d) SET_SIGNUM(a, 0); } return; - } else if (check(unlikely(zzero(d)))) { + } else if (check(zzero(d))) { libzahl_failure(-ZERROR_DIV_0); } else if (unlikely(zzero(b))) { SET_SIGNUM(a, 0); diff --git a/src/zmodpowu.c b/src/zmodpowu.c @@ -16,7 +16,7 @@ zmodpowu(z_t a, z_t b, unsigned long long int c, z_t d) else zsetu(a, 1); return; - } else if (check(unlikely(zzero(d)))) { + } else if (check(zzero(d))) { libzahl_failure(-ZERROR_DIV_0); } else if (unlikely(zzero(b))) { SET_SIGNUM(a, 0); diff --git a/src/zrand.c b/src/zrand.c @@ -104,7 +104,7 @@ zrand_fd(void *out, size_t n, void *statep) while (n) { read_just = read(fd, buf + read_total, n); - if (check(unlikely(read_just < 0))) + if (check(read_just < 0)) libzahl_failure(errno); read_total += (size_t)read_just; n -= (size_t)read_just; @@ -141,7 +141,7 @@ zrand(z_t r, enum zranddev dev, enum zranddist dist, z_t n) { #define RANDOM_UNIFORM(RETRY)\ do {\ - if (check(unlikely(znegative(n))))\ + if (check(znegative(n)))\ libzahl_failure(-ZERROR_NEGATIVE);\ bits = zbits(n);\ do\ @@ -185,7 +185,7 @@ zrand(z_t r, enum zranddev dev, enum zranddist dist, z_t n) if (pathname) { fd = open(pathname, O_RDONLY); - if (check(unlikely(fd < 0))) + if (check(fd < 0)) libzahl_failure(errno); statep = &fd; } diff --git a/src/zsets.c b/src/zsets.c @@ -17,12 +17,12 @@ zsets(z_t a, const char *str) str += neg || (*str == '+'); - if (check(unlikely(!*str))) { + if (check(!*str)) { errno = EINVAL; return -1; } for (str_end = str; *str_end; str_end++) { - if (check(unlikely(!isdigit(*str_end)))) { + if (check(!isdigit(*str_end))) { errno = EINVAL; return -1; } diff --git a/src/zsetup.c b/src/zsetup.c @@ -46,7 +46,7 @@ zsetup(jmp_buf env) zinit(libzahl_tmp_divmod_ds[i]); libzahl_temp_stack = malloc(256 * sizeof(*libzahl_temp_stack)); - if (check(unlikely(!libzahl_temp_stack))) + if (check(!libzahl_temp_stack)) libzahl_memfailure(); libzahl_temp_stack_head = libzahl_temp_stack; libzahl_temp_stack_end = libzahl_temp_stack + 256;