aboutsummaryrefslogtreecommitdiffstats
path: root/src/zmodpowu.c
blob: 7bb4f2b71d8954c93822b47d907d880f97e25e4b (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"

#define tb  libzahl_tmp_pow_b
#define td  libzahl_tmp_pow_d


void
zmodpowu(z_t a, z_t b, unsigned long long int c, z_t d)
{
	if (unlikely(!c)) {
		if (check(zzero(b)))
			libzahl_failure(-ZERROR_0_POW_0);
		else if (check(zzero(d)))
			libzahl_failure(-ZERROR_DIV_0);
		else
			zsetu(a, 1);
		return;
	} else if (check(unlikely(zzero(d)))) {
		libzahl_failure(-ZERROR_DIV_0);
	} else if (unlikely(zzero(b))) {
		SET_SIGNUM(a, 0);
		return;
	}

	zmod(tb, b, d);
	zset(td, d);
	zsetu(a, 1);

	if (c & 1)
		zmodmul(a, a, tb, td);
	while (c >>= 1) {
		zmodsqr(tb, tb, td);
		if (c & 1)
			zmodmul(a, a, tb, td);
	}
}