libzahl

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

commit 64157e1d0e47a8b4e34817d77325fa09e1250b5f
parent 5860237d2f05f6ae45a98569b0d567c2227904c6
Author: Mattias Andrée <maandree@kth.se>
Date:   Sat,  5 Mar 2016 20:42:34 +0100

Improve zsub, only copy to temp when necessary

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

Diffstat:
Msrc/internals.h | 1+
Msrc/zsub.c | 29++++++++++++++---------------
2 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/src/internals.h b/src/internals.h @@ -21,6 +21,7 @@ X(libzahl_tmp_str_rem)\ X(libzahl_tmp_gcd_u)\ X(libzahl_tmp_gcd_v)\ + X(libzahl_tmp_sub)\ X(libzahl_tmp_modmul)\ X(libzahl_tmp_div)\ X(libzahl_tmp_mod)\ diff --git a/src/zsub.c b/src/zsub.c @@ -17,17 +17,6 @@ zsub_unsigned(z_t a, z_t b, z_t c) } else if (zzero(c)) { zabs(a, b); return; - } else if (a == b || a == c) { - /* TODO This should not be necessary. */ - z_t tb, tc; - zinit(tb); - zinit(tc); - zset(tb, b); - zset(tc, c); - zsub_unsigned(a, tb, tc); - zfree(tb); - zfree(tc); - return; } magcmp = zcmpmag(b, c); @@ -36,13 +25,23 @@ zsub_unsigned(z_t a, z_t b, z_t c) SET_SIGNUM(a, 0); return; } + n = MIN(b->used, c->used); + if (a == b) { + zset(libzahl_tmp_sub, b); + s = libzahl_tmp_sub->chars; + } else { + s = b->chars; + } SET(a, c); - n = MIN(a->used, b->used); - s = b->chars; } else { + n = MIN(b->used, c->used); + if (a == c) { + zset(libzahl_tmp_sub, c); + s = libzahl_tmp_sub->chars; + } else { + s = c->chars; + } SET(a, b); - n = MIN(a->used, c->used); - s = c->chars; } for (i = 0; i < n; i++) {