]> pd.if.org Git - zpackage/blob - tomsfastmath/src/generators/comba_mont_gen.c
commit files needed for zpm-fetchurl
[zpackage] / tomsfastmath / src / generators / comba_mont_gen.c
1 #include <stdio.h>
2
3 int main(void)
4 {
5    int x, y, z;
6
7 printf(
8 #if 1
9 "#ifdef TFM_SMALL_SET\n"
10 "/* computes x/R == x (mod N) via Montgomery Reduction */\n"
11 "void fp_montgomery_reduce_small(fp_int *a, fp_int *m, fp_digit mp)\n"
12 "{\n"
13 "   fp_digit c[FP_SIZE], *_c, *tmpm, mu, cy;\n"
14 "   int      oldused, x, y, pa;\n"
15 "\n"
16 "#if defined(USE_MEMSET)\n"
17 "   /* now zero the buff */\n"
18 "   memset(c, 0, sizeof c);\n"
19 "#endif\n"
20 "   pa = m->used;\n"
21 "\n"
22 "   /* copy the input */\n"
23 "   oldused = a->used;\n"
24 "   for (x = 0; x < oldused; x++) {\n"
25 "       c[x] = a->dp[x];\n"
26 "   }\n"
27 "#if !defined(USE_MEMSET)\n"
28 "   for (; x < 2*pa+3; x++) {\n"
29 "       c[x] = 0;\n"
30 "   }\n"
31 "#endif\n"
32 "   MONT_START;\n"
33 #endif
34 "\n"
35 "   switch (pa) {\n");
36
37 for (x = 1; x <= 16; x++) {
38 if (x > 16 && (x != 32 && x != 48 && x != 64)) continue;
39 if (x > 16) printf("#ifdef TFM_HUGE\n");
40
41
42
43 printf("      case %d:\n", x);
44
45 for (y = 0; y < x; y++) {
46
47 printf("            x = %d; cy   = 0;\n"
48        "            LOOP_START;\n"
49        "            _c   = c + %d;\n"
50        "            tmpm = m->dp;\n", y, y);
51
52 printf("#ifdef INNERMUL8\n");
53 for (z = 0; z+8 <= x; z += 8) {
54 printf("            INNERMUL8; _c += 8; tmpm += 8;\n");
55 }
56 for (; z < x; z++) {
57 printf("            INNERMUL; ++_c;\n");
58 }
59 printf("#else\n");
60 for (z = 0; z < x; z++) {
61 printf("            INNERMUL; ++_c;\n");
62 }
63 printf("#endif\n");
64 printf("            LOOP_END;\n"
65        "            while (cy) {\n"
66        "               PROPCARRY;\n"
67        "               ++_c;\n"
68        "            }\n");
69 }
70 //printf("         }\n");
71 printf("         break;\n");
72
73
74
75 #define LOOP_MACRO(stride)                                 \
76    for (x = 0; x < stride; x++) {                          \
77        fp_digit cy = 0;                                    \
78        /* get Mu for this round */                         \
79        LOOP_START;                                         \
80        _c   = c + x;                                       \
81        tmpm = m->dp;                                       \
82        for (y = 0; y < stride; y++) {                      \
83           INNERMUL;                                        \
84           ++_c;                                            \
85        }                                                   \
86        LOOP_END;                                           \
87        while (cy) {                                        \
88            PROPCARRY;                                      \
89            ++_c;                                           \
90        }                                                   \
91   }         
92
93
94
95
96
97 if (x > 16) printf("#endif /* TFM_HUGE */\n");
98
99
100 }
101
102 #if 1
103
104 printf(
105 "  }\n"
106 "  /* now copy out */\n"
107 "  _c   = c + pa;\n"
108 "  tmpm = a->dp;\n"
109 "  for (x = 0; x < pa+1; x++) {\n"
110 "     *tmpm++ = *_c++;\n"
111 "  }\n"
112 "\n"
113 "  for (; x < oldused; x++)   {\n"
114 "     *tmpm++ = 0;\n"
115 "  }\n"
116 "\n"
117 "  MONT_FINI;\n"
118 "\n"
119 "  a->used = pa+1;\n"
120 "  fp_clamp(a);\n"
121 "\n"  
122 "  /* if A >= m then A = A - m */\n"
123 "  if (fp_cmp_mag (a, m) != FP_LT) {\n"
124 "    s_fp_sub (a, m, a);\n"
125 "  }\n"
126 "}\n\n#endif\n");
127
128 #endif
129
130
131 return 0;
132 }