]> pd.if.org Git - zpackage/blob - tomsfastmath/src/generators/comba_sqr_smallgen.c
commit files needed for zpm-fetchurl
[zpackage] / tomsfastmath / src / generators / comba_sqr_smallgen.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 /* Generates squaring comba code... it learns it knows our secrets! */
12 #include <stdio.h>
13
14 int main(int argc, char **argv)
15 {
16    int x, y, z, N, f;
17
18 printf(
19 "#define TFM_DEFINES\n"
20 "#include \"fp_sqr_comba.c\"\n"
21 "\n"
22 "#if defined(TFM_SMALL_SET)\n"
23 "void fp_sqr_comba_small(fp_int *A, fp_int *B)\n"
24 "{\n"
25 "   fp_digit *a, b[32], c0, c1, c2, sc0, sc1, sc2;\n"
26 "#ifdef TFM_ISO\n"
27 "   fp_word tt;\n"
28 "#endif\n"
29 );
30
31 printf("   switch (A->used) { \n");
32
33 for (N = 1; N <= 16; N++) {
34 printf(
35 "   case %d:\n"
36 "      a = A->dp;\n"
37 "      COMBA_START; \n"
38 "\n"
39 "      /* clear carries */\n"
40 "      CLEAR_CARRY;\n"
41 "\n"
42 "      /* output 0 */\n"
43 "      SQRADD(a[0],a[0]);\n"
44 "      COMBA_STORE(b[0]);\n", N);
45
46    for (x = 1; x < N+N-1; x++) {
47 printf(
48 "\n      /* output %d */\n"
49 "      CARRY_FORWARD;\n   ", x);
50
51        for (f = y = 0; y < N; y++) {
52            for (z = 0; z < N; z++) {
53                if (z != y && z + y == x && y <= z) {
54                   ++f;
55                }
56            }
57        }
58
59    if (f <= 2) {
60        for (y = 0; y < N; y++) {
61            for (z = 0; z < N; z++) {
62                if (y<=z && (y+z)==x) {
63                   if (y == z) {
64                      printf("   SQRADD(a[%d], a[%d]); ", y, y);
65                   } else {
66                      printf("   SQRADD2(a[%d], a[%d]); ", y, z);
67                   }
68                }
69            }
70        }
71    } else {
72       // new method
73       /* do evens first */
74        f = 0;
75        for (y = 0; y < N; y++) {
76            for (z = 0; z < N; z++) {
77                if (z != y && z + y == x && y <= z) {
78                   if (f == 0) {
79                      // first double
80                      printf("SQRADDSC(a[%d], a[%d]); ", y, z);
81                      f = 1;
82                   } else {
83                      printf("SQRADDAC(a[%d], a[%d]); ", y, z);
84                   }
85                }
86            }
87        }
88        // forward the carry
89        printf("SQRADDDB; ");
90        if ((x&1) == 0) {
91           // add the square
92           printf("SQRADD(a[%d], a[%d]); ", x/2, x/2);
93        }
94     }
95 printf("\n      COMBA_STORE(b[%d]);\n", x);
96    }
97 printf("      COMBA_STORE2(b[%d]);\n", N+N-1);
98
99 printf(
100 "      COMBA_FINI;\n"
101 "\n"
102 "      B->used = %d;\n"
103 "      B->sign = FP_ZPOS;\n"
104 "      memcpy(B->dp, b, %d * sizeof(fp_digit));\n"
105 "      fp_clamp(B);\n"
106 "      break;\n\n", N+N, N+N);
107 }
108
109 printf("}\n}\n\n#endif /* TFM_SMALL_SET */\n\n"
110 "/* $Source$ */\n"
111 "/* $Revision$ */\n"
112 "/* $Date$ */\n"
113 );
114
115   return 0;
116 }
117
118 /* $Source$ */
119 /* $Revision$ */
120 /* $Date$ */