libzahl

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

commit b8f7e263d28edb721f18f64d3553947710ff9fce
parent 1f8023e18a6dc7b950826810b392fdd46bcb0d45
Author: Mattias Andrée <maandree@kth.se>
Date:   Wed,  4 May 2016 17:04:27 +0200

Optimise znot

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

Diffstat:
Msrc/internals.h | 29++++++++++++++++++++++++++++-
Msrc/zand.c | 6+++---
Msrc/znot.c | 5++---
Msrc/zor.c | 8++++----
Msrc/zxor.c | 8++++----
5 files changed, 41 insertions(+), 15 deletions(-)

diff --git a/src/internals.h b/src/internals.h @@ -344,7 +344,7 @@ zfree_temp(z_t a) /* } */ -#define ZMEM_OP(a, b, c, n, OP) \ +#define ZMEM_2OP(a, b, c, n, OP) \ do { \ zahl_char_t *a__ = (a); \ const zahl_char_t *b__ = (b); \ @@ -371,3 +371,30 @@ zfree_temp(z_t a) a__[i__] = b__[i__] OP c__[i__]; \ } \ } while (0) + +#define ZMEM_1OP(a, b, n, OP) \ + do { \ + zahl_char_t *a__ = (a); \ + const zahl_char_t *b__ = (b); \ + size_t i__, n__ = (n); \ + if (n__ <= 4) { \ + if (n__ >= 1) \ + a__[0] = OP(b__[0]); \ + if (n__ >= 2) \ + a__[1] = OP(b__[1]); \ + if (n__ >= 3) \ + a__[2] = OP(b__[2]); \ + if (n__ >= 4) \ + a__[3] = OP(b__[3]); \ + } else { \ + for (i__ = 0; (i__ += 4) < n__;) { \ + a__[i__ - 1] = OP(b__[i__ - 1]); \ + a__[i__ - 2] = OP(b__[i__ - 2]); \ + a__[i__ - 3] = OP(b__[i__ - 3]); \ + a__[i__ - 4] = OP(b__[i__ - 4]); \ + } \ + if (i__ > n__) \ + for (i__ -= 4; i__ < n__; i__++) \ + a__[i__] = OP(b__[i__]); \ + } \ + } while (0) diff --git a/src/zand.c b/src/zand.c @@ -17,12 +17,12 @@ zand(z_t a, z_t b, z_t c) a->used = MIN(b->used, c->used); if (a == b) { - ZMEM_OP(a->chars, a->chars, c->chars, a->used, &); + ZMEM_2OP(a->chars, a->chars, c->chars, a->used, &); } else if (unlikely(a == c)) { - ZMEM_OP(a->chars, a->chars, b->chars, a->used, &); + ZMEM_2OP(a->chars, a->chars, b->chars, a->used, &); } else { ENSURE_SIZE(a, a->used); - ZMEM_OP(a->chars, b->chars, c->chars, a->used, &); + ZMEM_2OP(a->chars, b->chars, c->chars, a->used, &); } TRIM_AND_SIGN(a, zpositive1(b, c) * 2 - 1); diff --git a/src/znot.c b/src/znot.c @@ -5,7 +5,7 @@ void znot(z_t a, z_t b) { - size_t bits, i; + size_t bits; if (unlikely(zzero(b))) { SET_SIGNUM(a, 0); @@ -16,8 +16,7 @@ znot(z_t a, z_t b) a->used = b->used; SET_SIGNUM(a, -zsignum(b)); - for (i = 0; i < a->used; i++) - a->chars[i] = ~(b->chars[i]); + ZMEM_1OP(a->chars, b->chars, a->used, ~); bits = BITS_IN_LAST_CHAR(bits); if (bits) a->chars[a->used - 1] &= ((zahl_char_t)1 << bits) - 1; diff --git a/src/zor.c b/src/zor.c @@ -19,18 +19,18 @@ zor(z_t a, z_t b, z_t c) ENSURE_SIZE(a, m); if (a == b) { - ZMEM_OP(a->chars, a->chars, c->chars, n, |); + ZMEM_2OP(a->chars, a->chars, c->chars, n, |); if (a->used < c->used) zmemcpy_range(a->chars, c->chars, n, m); } else if (unlikely(a == c)) { - ZMEM_OP(a->chars, a->chars, b->chars, n, |); + ZMEM_2OP(a->chars, a->chars, b->chars, n, |); if (a->used < b->used) zmemcpy_range(a->chars, b->chars, n, m); } else if (m == b->used) { - ZMEM_OP(a->chars, c->chars, b->chars, n, |); + ZMEM_2OP(a->chars, c->chars, b->chars, n, |); zmemcpy_range(a->chars, b->chars, n, m); } else { - ZMEM_OP(a->chars, b->chars, c->chars, n, |); + ZMEM_2OP(a->chars, b->chars, c->chars, n, |); zmemcpy_range(a->chars, c->chars, n, m); } diff --git a/src/zxor.c b/src/zxor.c @@ -26,18 +26,18 @@ zxor(z_t a, z_t b, z_t c) ENSURE_SIZE(a, m); if (a == b) { - ZMEM_OP(a->chars, a->chars, cc, n, ^); + ZMEM_2OP(a->chars, a->chars, cc, n, ^); if (a->used < cn) zmemcpy_range(a->chars, cc, n, m); } else if (unlikely(a == c)) { - ZMEM_OP(a->chars, b->chars, cc, n, ^); + ZMEM_2OP(a->chars, b->chars, cc, n, ^); if (a->used < bn) zmemcpy_range(a->chars, bc, n, m); } else if (m == bn) { - ZMEM_OP(a->chars, c->chars, b->chars, n, ^); + ZMEM_2OP(a->chars, c->chars, b->chars, n, ^); zmemcpy_range(a->chars, b->chars, n, m); } else { - ZMEM_OP(a->chars, b->chars, c->chars, n, ^); + ZMEM_2OP(a->chars, b->chars, c->chars, n, ^); zmemcpy_range(a->chars, c->chars, n, m); }