]> pd.if.org Git - zpackage/blob - tomsfastmath/src/sqr/fp_sqr_comba_generic.c
commit files needed for zpm-fetchurl
[zpackage] / tomsfastmath / src / sqr / fp_sqr_comba_generic.c
1 /* TomsFastMath, a fast ISO C bignum library.
2  * 
3  * This project is meant to fill in where LibTomMath
4  * falls short.  That is speed ;-)
5  *
6  * This project is public domain and free for all purposes.
7  * 
8  * Tom St Denis, tomstdenis@gmail.com
9  */
10
11 #define TFM_DEFINES
12 #include "fp_sqr_comba.c"
13
14 /* generic comba squarer */
15 void fp_sqr_comba(fp_int *A, fp_int *B)
16 {
17   int       pa, ix, iz;
18   fp_digit  c0, c1, c2;
19   fp_int    tmp, *dst;
20 #ifdef TFM_ISO
21   fp_word   tt;
22 #endif    
23
24   /* get size of output and trim */
25   pa = A->used + A->used;
26   if (pa >= FP_SIZE) {
27      pa = FP_SIZE-1;
28   }
29
30   /* number of output digits to produce */
31   COMBA_START;
32   CLEAR_CARRY;
33
34   if (A == B) {
35      fp_zero(&tmp);
36      dst = &tmp;
37   } else {
38      fp_zero(B);
39      dst = B;
40   }
41
42   for (ix = 0; ix < pa; ix++) { 
43       int      tx, ty, iy;
44       fp_digit *tmpy, *tmpx;
45
46       /* get offsets into the two bignums */
47       ty = MIN(A->used-1, ix);
48       tx = ix - ty;
49
50       /* setup temp aliases */
51       tmpx = A->dp + tx;
52       tmpy = A->dp + ty;
53
54       /* this is the number of times the loop will iterrate,
55          while (tx++ < a->used && ty-- >= 0) { ... }
56        */
57       iy = MIN(A->used-tx, ty+1);
58
59       /* now for squaring tx can never equal ty 
60        * we halve the distance since they approach 
61        * at a rate of 2x and we have to round because 
62        * odd cases need to be executed
63        */
64       iy = MIN(iy, (ty-tx+1)>>1);
65
66       /* forward carries */
67       CARRY_FORWARD;
68
69       /* execute loop */
70       for (iz = 0; iz < iy; iz++) {
71           fp_digit _tmpx = *tmpx++;
72           fp_digit _tmpy = *tmpy--;
73           SQRADD2(_tmpx, _tmpy);
74       }
75
76       /* even columns have the square term in them */
77       if ((ix&1) == 0) {
78           fp_digit _a_dp = A->dp[ix>>1];
79           SQRADD(_a_dp, A->dp[ix>>1]);
80       }
81
82       /* store it */
83       COMBA_STORE(dst->dp[ix]);
84   }
85
86   COMBA_FINI;
87
88   /* setup dest */
89   dst->used = pa;
90   fp_clamp (dst);
91   if (dst != B) {
92      fp_copy(dst, B);
93   }
94 }
95
96 /* $Source$ */
97 /* $Revision$ */
98 /* $Date$ */