libzahl

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

commit 6cc727a4a17c6b037f665433cc47b37aa926b92b
parent 3728c12ecbe308092b213f1b664303a48858a2b8
Author: Mattias Andrée <maandree@kth.se>
Date:   Thu,  3 Mar 2016 12:52:25 +0100

Optimise zdivmod

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

Diffstat:
Msrc/internals.h | 3+--
Msrc/zdivmod.c | 18++++++++----------
2 files changed, 9 insertions(+), 12 deletions(-)

diff --git a/src/internals.h b/src/internals.h @@ -26,8 +26,7 @@ X(libzahl_tmp_modsqr)\ X(libzahl_tmp_divmod_a)\ X(libzahl_tmp_divmod_b)\ - X(libzahl_tmp_divmod_d)\ - X(libzahl_tmp_divmod_e) + X(libzahl_tmp_divmod_d) #define LIST_CONSTS\ X(libzahl_const_1e19, zsetu, 10000000000000000000ULL) /* The largest power of 10 < 2⁶⁴. */\ diff --git a/src/zdivmod.c b/src/zdivmod.c @@ -4,13 +4,12 @@ #define ta libzahl_tmp_divmod_a #define tb libzahl_tmp_divmod_b #define td libzahl_tmp_divmod_d -#define te libzahl_tmp_divmod_e void zdivmod(z_t a, z_t b, z_t c, z_t d) { - size_t c_bits, d_bits, shift; + size_t c_bits, d_bits, bit; int sign, cmpmag; sign = zsignum(c) * zsignum(d); @@ -47,25 +46,24 @@ zdivmod(z_t a, z_t b, z_t c, z_t d) c_bits = zbits(c); d_bits = zbits(d); - shift = c_bits - d_bits; - zlsh(td, d, shift); + bit = c_bits - d_bits; + zlsh(td, d, bit); SET_SIGNUM(td, 1); if (zcmpmag(td, c) > 0) { zrsh(td, td, 1); - shift -= 1; + bit -= 1; } - zsetu(te, 1); - zlsh(te, te, shift); SET_SIGNUM(ta, 0); zabs(tb, c); - while (!zzero(te)) { + for (;;) { if (zcmpmag(td, tb) <= 0) { zsub(tb, tb, td); - zor(ta, ta, te); + zbset(ta, ta, bit, 1); } - zrsh(te, te, 1); + if (!bit--) + break; zrsh(td, td, 1); }