aboutsummaryrefslogtreecommitdiff
path: root/src/benchmarks/cfrac/ppowmod.c
blob: 4528db9b2427f3eea1f50d9fc31026a182e74b36 (plain)
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
#include "precision.h"

/*
 * Raise to precision power mod m
 */
precision ppowmod(u, v, m)
   precision 	u, v, m;
{
   precision j = pUndef, i = pUndef, n = pUndef;

   (void) pparm(m);
   pset(&i,   pparm(u));
   pset(&n,   pparm(v));
   pset(&j,   pone);

   do {
      if (podd(n)) {
	 pset(&j, pmod(pmul(i, j), m));
      }
      pset(&n, phalf(n));
      if (peqz(n)) break;
      pset(&i, pmod(pmul(i, i), m));
   } while (1);

   pdestroy(i); pdestroy(n);
   pdestroy(u); pdestroy(v); pdestroy(m);
   return presult(j);
}