aboutsummaryrefslogtreecommitdiffstats
path: root/src/zsqr.c
blob: 06c41a42a77ce0c30056cd557df96275e5ae92fa (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* See LICENSE file for copyright and license details. */
#include "internals.h"


static inline void
zsqr_ll_single_char(z_t a, z_t b)
{
	ENSURE_SIZE(a, 1);
	a->used = 1;
	a->chars[0] = b->chars[0] * b->chars[0];
	SET_SIGNUM(a, 1);
}

void
zsqr_ll(z_t a, z_t b)
{
	/*
	 * Karatsuba algorithm, optimised for equal factors.
	 */

#define z2 a
	z_t z0, z1, high, low;
	zahl_char_t auxchars[3 * ZAHL_FLUFF];
	size_t bits;

	bits = zbits(b);

	if (bits <= BITS_PER_CHAR / 2) {
		zsqr_ll_single_char(a, b);
		return;
	}

	bits >>= 1;

	/* Try to split only at a character level rather than a bit level.
	 * Such splits are faster, even if bit-level is required, and do
	 * not require auxiliary memory except for the bit-level split
	 * which require constant auxiliary memory. */
	if (bits < BITS_PER_CHAR) {
		low->chars = auxchars;
		high->chars = auxchars + ZAHL_FLUFF;
		zsplit_unsigned_fast_small_auto(high, low, b, bits);
	} else {
		bits = TRUNCATE_TO_CHAR(bits);
		zsplit_unsigned_fast_large_taint(high, low, b, bits);
	}


	if (unlikely(zzero(low))) {
		zsqr_ll(z2, high);
		zlsh(a, z2, bits << 1);
	} else {
		zinit_temp(z0);
		zinit_temp(z1);

		zsqr_ll(z0, low);

		zmul_ll(z1, low, high);
		zlsh(z1, z1, bits + 1);

		zsqr_ll(z2, high);
		zlsh(a, z2, bits << 1);

		zadd_unsigned_assign(a, z1);
		zadd_unsigned_assign(a, z0);

		zfree_temp(z1);
		zfree_temp(z0);
	}
}