manual page distributed with 4.1BSD
[unix-history] / usr / src / lib / libmp / pow.c
CommitLineData
051b1e55
DF
1/*
2 * Copyright (c) 1980 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
7#ifndef lint
8static char sccsid[] = "@(#)pow.c 5.1 (Berkeley) %G%";
9#endif not lint
3be90705
SL
10
11#include <mp.h>
12pow(a,b,c,d) MINT *a,*b,*c,*d;
13{ int i,j,n;
14 MINT x,y;
15 x.len=y.len=0;
16 xfree(d);
17 d->len=1;
18 d->val=xalloc(1,"pow");
19 *d->val=1;
20 for(j=0;j<b->len;j++)
21 { n=b->val[b->len-j-1];
22 for(i=0;i<15;i++)
23 { mult(d,d,&x);
24 mdiv(&x,c,&y,d);
25 if((n=n<<1)&0100000)
26 { mult(a,d,&x);
27 mdiv(&x,c,&y,d);
28 }
29 }
30 }
31 xfree(&x);
32 xfree(&y);
33 return;
34}
35rpow(a,n,b) MINT *a,*b;
36{ MINT x,y;
37 int i;
38 x.len=1;
39 x.val=xalloc(1,"rpow");
40 *x.val=n;
41 y.len=n*a->len+4;
42 y.val=xalloc(y.len,"rpow2");
43 for(i=0;i<y.len;i++) y.val[i]=0;
44 y.val[y.len-1]=010000;
45 xfree(b);
46 pow(a,&x,&y,b);
47 xfree(&x);
48 xfree(&y);
49 return;
50}