From 76d0af5599554d11f104d582cdac8fbaa8569fcc Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Fri, 4 Mar 2016 23:50:00 +0100 Subject: Clean up, add zerror and zperror, fix bugs and add more tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/zlsh.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) (limited to 'src/zlsh.c') diff --git a/src/zlsh.c b/src/zlsh.c index b631c1a..2a76fbc 100644 --- a/src/zlsh.c +++ b/src/zlsh.c @@ -19,25 +19,27 @@ zlsh(z_t a, z_t b, size_t bits) chars = FLOOR_BITS_TO_CHARS(bits); bits = BITS_IN_LAST_CHAR(bits); - cbits = BITS_PER_CHAR - 1 - bits; + cbits = BITS_PER_CHAR - bits; - a->used = b->used + chars; - ENSURE_SIZE(a, a->used); + ENSURE_SIZE(a, b->used + chars); if (a == b) - zmemmove(a->chars + chars, b->chars, a->used); + zmemmove(a->chars + chars, b->chars, b->used); else - zmemcpy(a->chars + chars, b->chars, a->used); + zmemcpy(a->chars + chars, b->chars, b->used); zmemset(a->chars, 0, chars); + a->used = b->used + chars; - for (i = chars; i < a->used; i++) { - carry[~i & 1] = a->chars[i] >> cbits; - a->chars[i] <<= bits; - a->chars[i] |= carry[i & 1]; - } - if (carry[i & 1]) { - ENSURE_SIZE(a, a->alloced << 1); - a->chars[i] = carry[i & 1]; - a->used++; + if (bits) { /* This if statement is very important in C. */ + for (i = chars; i < a->used; i++) { + carry[~i & 1] = a->chars[i] >> cbits; + a->chars[i] <<= bits; + a->chars[i] |= carry[i & 1]; + } + if (carry[i & 1]) { + ENSURE_SIZE(a, a->used + 1); + a->chars[i] = carry[i & 1]; + a->used++; + } } SET_SIGNUM(a, zsignum(b)); -- cgit v1.2.3-70-g09d2