tomsfastmath/src/generators/comba_mont_gen.c

133 lines
3.0 KiB
C
Raw Normal View History

2005-07-23 12:43:03 +02:00
#include <stdio.h>
int main(void)
{
2005-08-01 18:37:35 +02:00
int x, y, z;
2005-07-23 12:43:03 +02:00
printf(
2006-11-01 08:42:37 +01:00
#if 1
2005-08-01 18:37:35 +02:00
"#ifdef TFM_SMALL_SET\n"
"/* computes x/R == x (mod N) via Montgomery Reduction */\n"
"void fp_montgomery_reduce_small(fp_int *a, fp_int *m, fp_digit mp)\n"
2005-07-23 12:43:03 +02:00
"{\n"
2005-08-01 18:37:35 +02:00
" fp_digit c[FP_SIZE], *_c, *tmpm, mu, cy;\n"
" int oldused, x, y, pa;\n"
2005-07-23 12:43:03 +02:00
"\n"
2005-08-01 18:37:35 +02:00
"#if defined(USE_MEMSET)\n"
2005-07-23 12:43:03 +02:00
" /* now zero the buff */\n"
2005-08-01 18:37:35 +02:00
" memset(c, 0, sizeof c);\n"
"#endif\n"
" pa = m->used;\n"
2005-07-23 12:43:03 +02:00
"\n"
" /* copy the input */\n"
" oldused = a->used;\n"
" for (x = 0; x < oldused; x++) {\n"
" c[x] = a->dp[x];\n"
" }\n"
2005-08-01 18:37:35 +02:00
"#if !defined(USE_MEMSET)\n"
" for (; x < 2*pa+3; x++) {\n"
" c[x] = 0;\n"
" }\n"
"#endif\n"
2005-07-23 12:43:03 +02:00
" MONT_START;\n"
2005-08-01 18:37:35 +02:00
#endif
2005-07-23 12:43:03 +02:00
"\n"
2005-08-01 18:37:35 +02:00
" switch (pa) {\n");
2006-11-01 08:42:37 +01:00
for (x = 1; x <= 16; x++) {
2005-08-01 18:37:35 +02:00
if (x > 16 && (x != 32 && x != 48 && x != 64)) continue;
if (x > 16) printf("#ifdef TFM_HUGE\n");
printf(" case %d:\n", x);
for (y = 0; y < x; y++) {
printf(" x = %d; cy = 0;\n"
" LOOP_START;\n"
" _c = c + %d;\n"
" tmpm = m->dp;\n", y, y);
printf("#ifdef INNERMUL8\n");
for (z = 0; z+8 <= x; z += 8) {
printf(" INNERMUL8; _c += 8; tmpm += 8;\n");
}
for (; z < x; z++) {
printf(" INNERMUL; ++_c;\n");
}
printf("#else\n");
for (z = 0; z < x; z++) {
printf(" INNERMUL; ++_c;\n");
}
printf("#endif\n");
printf(" LOOP_END;\n"
" while (cy) {\n"
" PROPCARRY;\n"
" ++_c;\n"
" }\n");
}
//printf(" }\n");
printf(" break;\n");
#define LOOP_MACRO(stride) \
for (x = 0; x < stride; x++) { \
fp_digit cy = 0; \
/* get Mu for this round */ \
LOOP_START; \
_c = c + x; \
tmpm = m->dp; \
for (y = 0; y < stride; y++) { \
INNERMUL; \
++_c; \
} \
LOOP_END; \
while (cy) { \
PROPCARRY; \
++_c; \
} \
}
if (x > 16) printf("#endif /* TFM_HUGE */\n");
}
2006-11-01 08:42:37 +01:00
#if 1
2005-08-01 18:37:35 +02:00
printf(
2005-07-23 12:43:03 +02:00
" }\n"
" /* now copy out */\n"
2005-08-01 18:37:35 +02:00
" _c = c + pa;\n"
2005-07-23 12:43:03 +02:00
" tmpm = a->dp;\n"
2005-08-01 18:37:35 +02:00
" for (x = 0; x < pa+1; x++) {\n"
2005-07-23 12:43:03 +02:00
" *tmpm++ = *_c++;\n"
" }\n"
"\n"
" for (; x < oldused; x++) {\n"
" *tmpm++ = 0;\n"
" }\n"
"\n"
" MONT_FINI;\n"
"\n"
2005-08-01 18:37:35 +02:00
" a->used = pa+1;\n"
2005-07-23 12:43:03 +02:00
" fp_clamp(a);\n"
"\n"
" /* if A >= m then A = A - m */\n"
" if (fp_cmp_mag (a, m) != FP_LT) {\n"
" s_fp_sub (a, m, a);\n"
" }\n"
2005-08-01 18:37:35 +02:00
"}\n\n#endif\n");
2005-07-23 12:43:03 +02:00
2005-08-01 18:37:35 +02:00
#endif
2005-07-23 12:43:03 +02:00
2005-08-01 18:37:35 +02:00
return 0;
}