Commit | Line | Data |
---|---|---|
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 | |
8 | static char sccsid[] = "@(#)msqrt.c 5.1 (Berkeley) %G%"; | |
9 | #endif not lint | |
a0dba064 SL |
10 | |
11 | #include <mp.h> | |
12 | msqrt(a,b,r) MINT *a,*b,*r; | |
13 | { MINT x,junk,y; | |
14 | int j; | |
15 | x.len=junk.len=y.len=0; | |
16 | if(a->len<0) fatal("msqrt: neg arg"); | |
17 | if(a->len==0) | |
18 | { b->len=0; | |
19 | r->len=0; | |
20 | return(0); | |
21 | } | |
22 | if(a->len%2==1) x.len=(1+a->len)/2; | |
23 | else x.len=1+a->len/2; | |
24 | x.val=xalloc(x.len,"msqrt"); | |
25 | for(j=0;j<x.len;x.val[j++]=0); | |
26 | if(a->len%2==1) x.val[x.len-1]=0400; | |
27 | else x.val[x.len-1]=1; | |
28 | xfree(b); | |
29 | xfree(r); | |
30 | loop: | |
31 | mdiv(a,&x,&y,&junk); | |
32 | xfree(&junk); | |
33 | madd(&x,&y,&y); | |
34 | sdiv(&y,2,&y,(short *)&j); | |
35 | if(mcmp(&x,&y)>0) | |
36 | { xfree(&x); | |
37 | move(&y,&x); | |
38 | xfree(&y); | |
39 | goto loop; | |
40 | } | |
41 | xfree(&y); | |
42 | move(&x,b); | |
43 | mult(&x,&x,&x); | |
44 | msub(a,&x,r); | |
45 | xfree(&x); | |
46 | return(r->len); | |
47 | } |