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


void
ztrunc(z_t a, z_t b, size_t bits)
{
	zahl_char_t mask = 1;
	size_t chars, i;

	if (zzero(b)) {
		SET_SIGNUM(a, 0);
		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 != b) {
		ENSURE_SIZE(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);
}