aboutsummaryrefslogtreecommitdiffstats
path: root/src/zrsh.c
blob: a7b886bd31dace5dfc464f56d16f85ed51950826 (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
39
40
41
42
43
44
45
/* See LICENSE file for copyright and license details. */
#include "internals.h"


void
zrsh(z_t a, z_t b, size_t bits)
{
	size_t i, chars, cbits;

	if (!bits) {
		SET(a, b);
		return;
	}

	chars = FLOOR_BITS_TO_CHARS(bits);

	if (zzero(b) || chars >= b->used || zbits(b) <= bits) {
		SET_SIGNUM(a, 0);
		return;
	}

	bits = BITS_IN_LAST_CHAR(bits);
	cbits = BITS_PER_CHAR - bits;

	if (chars && a == b) {
		a->used -= chars;
		zmemmove(a->chars, a->chars + chars, a->used);
	} else if (a != b) {
		a->used = b->used - chars;
		ENSURE_SIZE(a, a->used);
		zmemcpy(a->chars, b->chars + chars, a->used);
	}

	if (bits) { /* This if statement is very important in C. */
		a->chars[0] >>= bits;
		for (i = 1; i < a->used; i++) {
			a->chars[i - 1] |= a->chars[i] << cbits;
			a->chars[i] >>= bits;
		}
		while (!a->chars[a->used - 1])
			a->used--;
	}

	SET_SIGNUM(a, zsignum(b));
}