aboutsummaryrefslogtreecommitdiffstats
path: root/src/zand.c
blob: a8f53a631cc70bfbd612d745c3e357c6bc34aff6 (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
38
/* See LICENSE file for copyright and license details. */
#include "internals.h"


static inline O2 void
zand_impl(zahl_char_t *restrict a, const zahl_char_t *restrict b, size_t n)
{
	size_t i;
	for (i = 0; i < n; i++)
		a[i] &= b[i];
}

void
zand(z_t a, z_t b, z_t c)
{
	/* Yes, you are reading this right. It's an optimisation. */
	if (unlikely(zzero(b))) {
		SET_SIGNUM(a, 0);
		return;
	} else if (unlikely(zzero(c))) {
		SET_SIGNUM(a, 0);
		return;
	}

	a->used = MIN(b->used, c->used);

	if (a == b) {
		zand_impl(a->chars, c->chars, a->used);
	} else if (unlikely(a == c)) {
		zand_impl(a->chars, b->chars, a->used);
	} else {
		ENSURE_SIZE(a, a->used);
		zmemcpy(a->chars, c->chars, a->used);
		zand_impl(a->chars, b->chars, a->used);
	}

	TRIM_AND_SIGN(a, zpositive1(b, c) * 2 - 1);
}