aboutsummaryrefslogtreecommitdiffstats
path: root/src/zand.c
blob: 119f66ca667ab483200081877e63321dc7a15e8b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/* See LICENSE file for copyright and license details. */
#include "internals.h"


void
zand(z_t a, z_t b, z_t c)
{
	size_t n;

	if (unlikely(zzero1(b, c))) {
		SET_SIGNUM(a, 0);
		return;
	}

	n = MIN(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 (unlikely(a == c)) {
		while (n--)
			a->chars[n] &= b->chars[n];
	} else {
		ENSURE_SIZE(a, a->used);
		zmemcpy(a->chars, c->chars, a->used);
		while (n--)
			a->chars[n] &= b->chars[n];
	}
	SET_SIGNUM(a, zpositive1(b, c) * 2 - 1);
}