aboutsummaryrefslogtreecommitdiffstats
path: root/src/ztrunc.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2016-03-03 23:02:59 +0100
committerMattias Andrée <maandree@kth.se>2016-03-03 23:02:59 +0100
commitd6987458f21cf1890045f2606d0f8ec4d2225b44 (patch)
tree90afabbea01b01c4dedcb41748eb534ce04fbf77 /src/ztrunc.c
parentzsets: minor optimisation (diff)
downloadlibzahl-d6987458f21cf1890045f2606d0f8ec4d2225b44.tar.gz
libzahl-d6987458f21cf1890045f2606d0f8ec4d2225b44.tar.bz2
libzahl-d6987458f21cf1890045f2606d0f8ec4d2225b44.tar.xz
Cleanup and fix bug in ztrunc
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'src/ztrunc.c')
-rw-r--r--src/ztrunc.c44
1 files changed, 23 insertions, 21 deletions
diff --git a/src/ztrunc.c b/src/ztrunc.c
index 3d05c6e..074edc3 100644
--- a/src/ztrunc.c
+++ b/src/ztrunc.c
@@ -1,33 +1,35 @@
/* See LICENSE file for copyright and license details. */
#include "internals"
-#include <stdlib.h>
-#include <string.h>
-
void
ztrunc(z_t a, z_t b, size_t bits)
{
zahl_char_t mask = 1;
- size_t chars;
+ size_t chars, i;
+
if (zzero(b)) {
SET_SIGNUM(a, 0);
- } else {
- chars = CEILING_BITS_TO_CHARS(bits);
- a->sign = b->sign;
- a->used = chars < b->used ? chars : b->used;
- if (a->used < chars)
- bits = 0;
- if (a->alloced < b->alloced) {
- a->alloced = b->alloced;
- a->chars = realloc(a->chars, b->alloced * sizeof(*(a->chars)));
- }
- memcpy(a->chars, b->chars, a->used * sizeof(*(a->chars)));
- bits = BITS_IN_LAST_CHAR(bits);
- if (bits) {
- mask <<= bits;
- mask -= 1;
- a->chars[a->used - 1] &= mask;
- }
+ return;
}
+
+ chars = CEILING_BITS_TO_CHARS(bits);
+ a->sign = b->sign;
+ a->used = MIN(chars, b->used);
+ if (a->used < chars)
+ bits = 0;
+ if (a->alloced < b->alloced)
+ zahl_realloc(a, b->alloced);
+ zmemcpy(a->chars, b->chars, a->used);
+ bits = BITS_IN_LAST_CHAR(bits);
+ if (bits) {
+ mask <<= bits;
+ mask -= 1;
+ a->chars[a->used - 1] &= mask;
+ }
+
+ for (i = a->used; i--;)
+ if (a->chars[i])
+ return;
+ SET_SIGNUM(a, 0);
}