aboutsummaryrefslogtreecommitdiffstats
path: root/src/zxor.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/zxor.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/zxor.c')
-rw-r--r--src/zxor.c56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/zxor.c b/src/zxor.c
new file mode 100644
index 0000000..4c68448
--- /dev/null
+++ b/src/zxor.c
@@ -0,0 +1,56 @@
+/* See LICENSE file for copyright and license details. */
+#include "internals"
+
+
+void
+zxor(z_t a, z_t b, z_t c)
+{
+ size_t n, m;
+
+ if (zzero(b)) {
+ if (zzero(c)) {
+ SET_SIGNUM(a, 0);
+ } else {
+ if (a != c)
+ zset(a, c);
+ }
+ return;
+ } else if (zzero(c)) {
+ if (a != b)
+ zset(a, b);
+ return;
+ }
+
+ m = b->used > c->used ? b->used : c->used;
+ n = b->used + c->used - m;
+
+ if (n == m && !memcmp(b->chars, c->chars, m * sizeof(*(b->chars)))) {
+ SET_SIGNUM(a, 0);
+ return;
+ }
+
+ if (a->alloced < m) {
+ a->alloced = m;
+ a->chars = realloc(a->chars, m * sizeof(*(a->chars)));
+ }
+
+ if (a == b) {
+ memcpy(a->chars + n, m == b->used ? b->chars : c->chars, (m - n) * sizeof(*(a->chars)));
+ while (n--)
+ a->chars[n] ^= c->chars[n];
+ } else if (a == c) {
+ memcpy(a->chars + n, m == b->used ? b->chars : c->chars, (m - n) * sizeof(*(a->chars)));
+ while (n--)
+ a->chars[n] ^= b->chars[n];
+ } else if (m == b->used) {
+ memcpy(a->chars, b->chars, m * sizeof(*(a->chars)));
+ while (n--)
+ a->chars[n] ^= c->chars[n];
+ } else {
+ memcpy(a->chars, c->chars, m * sizeof(*(a->chars)));
+ while (n--)
+ a->chars[n] ^= b->chars[n];
+ }
+
+ SET_SIGNUM(a, 1 - 2 * ((zsignum(b) ^ zsignum(c)) < 0);
+}