comparison tomsfastmath/src/sqr/fp_sqr_comba_generic.c @ 643:a362b62d38b2 dropbear-tfm

Add tomsfastmath from git rev bfa4582842bc3bab42e4be4aed5703437049502a with Makefile.in renamed
author Matt Johnston <matt@ucc.asn.au>
date Wed, 23 Nov 2011 18:10:20 +0700
parents
children
comparison
equal deleted inserted replaced
642:33fd2f3499d2 643:a362b62d38b2
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, [email protected]
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 SQRADD2(*tmpx++, *tmpy--);
72 }
73
74 /* even columns have the square term in them */
75 if ((ix&1) == 0) {
76 SQRADD(A->dp[ix>>1], A->dp[ix>>1]);
77 }
78
79 /* store it */
80 COMBA_STORE(dst->dp[ix]);
81 }
82
83 COMBA_FINI;
84
85 /* setup dest */
86 dst->used = pa;
87 fp_clamp (dst);
88 if (dst != B) {
89 fp_copy(dst, B);
90 }
91 }
92
93 /* $Source$ */
94 /* $Revision$ */
95 /* $Date$ */