aboutsummaryrefslogtreecommitdiffstats
path: root/src/zand.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2016-03-02 21:07:08 +0100
committerMattias Andrée <maandree@kth.se>2016-03-02 21:07:08 +0100
commit3ae8c069900514c1785fe4a84b6cdb0157dff59e (patch)
treeaec0c146010be40239c1582a8b8ae6e7b28aa045 /src/zand.c
parentAdd zgcd (diff)
downloadlibzahl-3ae8c069900514c1785fe4a84b6cdb0157dff59e.tar.gz
libzahl-3ae8c069900514c1785fe4a84b6cdb0157dff59e.tar.bz2
libzahl-3ae8c069900514c1785fe4a84b6cdb0157dff59e.tar.xz
Add zand, zor, zxor, znot, zbtest, zsplit, and the newly introduced ztrunc
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'src/zand.c')
-rw-r--r--src/zand.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/zand.c b/src/zand.c
new file mode 100644
index 0000000..3156857
--- /dev/null
+++ b/src/zand.c
@@ -0,0 +1,42 @@
+/* See LICENSE file for copyright and license details. */
+#include "internals"
+
+#include <string.h>
+
+
+void
+zand(z_t a, z_t b, z_t c)
+{
+ size_t n;
+
+ if (zzero(b) || zzero(c)) {
+ SET_SIGNUM(a, 0);
+ return;
+ }
+
+ n = b->used < c->used ? b->used : c->used;
+ while (n--)
+ if (b->chars[n] & c->chars[n])
+ goto found_highest;
+ SET_SIGNUM(a, 0);
+ return;
+
+found_highest:
+ a->used = ++n;
+ if (a == b) {
+ while (n--)
+ a->chars[n] &= c->chars[n];
+ } else if (a == c) {
+ while (n--)
+ a->chars[n] &= b->chars[n];
+ } else {
+ if (a->alloced < a->used) {
+ a->alloced = a->used;
+ a->chars = realloc(a->chars, a->used * sizeof(*(a->chars)));
+ }
+ memcpy(a->chars, c->chars, a->used * sizeof(*(a->chars)));
+ while (n--)
+ a->chars[n] &= b->chars[n];
+ }
+ SET_SIGNUM(a, (zsignum(b) > 0 || zsignum(c) > 0) * 2 - 1);
+}