File: gen_bb_mul_code.c

package info (click to toggle)
gf2x 1.3.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,000 kB
  • sloc: ansic: 14,162; sh: 7,809; cpp: 1,500; makefile: 980; perl: 176
file content (191 lines) | stat: -rw-r--r-- 6,903 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* This file is part of the gf2x library.

   Copyright 2007, 2008, 2009, 2010, 2013, 2015
   Richard Brent, Pierrick Gaudry, Emmanuel Thome', Paul Zimmermann

   This program is free software; you can redistribute it and/or modify it
   under the terms of either:
    - If the archive contains a file named toom-gpl.c (not a trivial
    placeholder), the GNU General Public License as published by the Free
    Software Foundation; either version 3 of the License, or (at your
    option) any later version.
    - If the archive contains a file named toom-gpl.c which is a trivial
    placeholder, the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 2.1 of the License, or
    (at your option) any later version.
   
   This program is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE.  See the license text for more details.
   
   You should have received a copy of the GNU General Public License as
   well as the GNU Lesser General Public License along with this program;
   see the files COPYING and COPYING.LIB.  If not, write to the Free
   Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   02110-1301, USA.
*/

/* Generates low-level multiplication routines over GF(2)[x]. */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

int main(int argc, char *argv[])
{
    unsigned long i;
    unsigned long w, K, CHOP, REM, fn;
    unsigned long MASK, mask2;

    if (argc != 3) {
	fprintf(stderr, "Usage: %s <wordsize> k\n", argv[0]);
	exit(1);
    }

    w = atoi(argv[1]);
    K = atoi(argv[2]);

    printf(
"/* This file is part of the gf2x library.\n"
"\n"
"   Copyright 2007, 2008, 2009, 2010, 2011, 2012, 2013\n"
"   Richard Brent, Pierrick Gaudry, Emmanuel Thome', Paul Zimmermann\n"
"\n"
"   This program is free software; you can redistribute it and/or modify it\n"
"   under the terms of either:\n"
"    - If the archive contains a file named toom-gpl.c (not a trivial\n"
"    placeholder), the GNU General Public License as published by the Free\n"
"    Software Foundation; either version 3 of the License, or (at your\n"
"    option) any later version.\n"
"    - If the archive contains a file named toom-gpl.c which is a trivial\n"
"    placeholder, the GNU Lesser General Public License as published by\n"
"    the Free Software Foundation; either version 2.1 of the License, or\n"
"    (at your option) any later version.\n"
"\n"
"   This program is distributed in the hope that it will be useful, but WITHOUT\n"
"   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\n"
"   FITNESS FOR A PARTICULAR PURPOSE.  See the license text for more details.\n"
"\n"
"   You should have received a copy of the GNU General Public License as\n"
"   well as the GNU Lesser General Public License along with this program;\n"
"   see the files COPYING and COPYING.LIB.  If not, write to the Free\n"
"   Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA\n"
"   02110-1301, USA.\n"
"*/\n"
"\n");
    printf("#ifndef GF2X_MUL1_H_\n");
    printf("#define GF2X_MUL1_H_\n");
    printf("\n");
    printf("/* This file was generated automatically with\n");
    printf("   %s %lu %lu. Don't edit it! */\n",
	   argv[0], w, K);
    printf("\n");
    printf(
        "#include \"gf2x.h\"\n"
"/* All gf2x source files for lowlevel functions must include gf2x-small.h\n"
" * This is mandatory for the tuning mechanism. */\n"
        "#include \"gf2x/gf2x-small.h\"\n"
        "\n");

    for (fn = 0; fn < 3; fn++) {
	if (fn == 0) {
	    printf("GF2X_STORAGE_CLASS_mul1 void\n");
	    printf("gf2x_mul1 (unsigned long *c, unsigned long a, unsigned long b)\n");
	} else if (fn == 1) {
	 /* continue;	/* mul_1_n is no longer used */
	    printf("GF2X_STORAGE_CLASS_mul_1_n unsigned long\n");
	    printf
		("gf2x_mul_1_n (unsigned long *cp, const unsigned long *bp, long sb, unsigned long a)\n");
	} else if (fn == 2) {
	    printf("GF2X_STORAGE_CLASS_addmul_1_n unsigned long\n");
	    printf
		("gf2x_addmul_1_n (unsigned long *dp, const unsigned long *cp, const unsigned long* bp, long sb,\n");
	    printf("        unsigned long a)\n");
	}
	printf("{\n");
	if (fn > 0) {		/* Mul1/AddMul1 */
	    printf("   long i;\n");
	    printf("   unsigned long carry = 0, b;\n");
	}
	printf("   unsigned long hi, lo, tmp, A[%lu];\n", 1UL << K);
	printf("\n");
	printf("   A[0] = 0;\t\t");
	/* do not truncate: fix non-considered bit products afterwards */
	printf("A[1] = a;\n");
	for (i = 2u; i < (1u << K); i++) {
	    if (i % 2 == 0)
		printf("   A[%lu] = A[%lu] << 1;\t", i, i / 2);
	    else
		printf("A[%lu] = A[%lu] ^ a;\n", i, i - 1);
	}
	printf("\n");

	if (fn > 0) {
	    printf("   for (i = 0; i < sb; i++)\n");
	    printf("     {\n");
	    printf("       b = bp[i];\n");
	}
	REM = w - 2 * K;
	MASK = (1UL << K) - 1UL;
	for (CHOP = 0; CHOP + 2 * K < w; CHOP += 2 * K);
	CHOP = w - CHOP;
	/* now 1 <= CHOP <= 2 * K, with w - CHOP multiple of 2K */
	if (CHOP <= K)		/* one slice is enough for the upper part */
	    printf("   lo = A[b >> %lu];\n", w - CHOP);
	else			/* two slices for the upper part */
	    printf("   lo = (A[b >> %lu] << %lu) ^ A[(b >> %lu) & %lu];\n",
		   w - (CHOP - K), K,
		   w - CHOP, MASK);
	printf("   hi = lo >> %lu;\n", w - 2 * K);
	for (i = w - CHOP - 2 * K; i >= 2 * K; i -= 2 * K) {
	    printf
		("   lo = (lo << %lu) ^ (A[(b >> %lu) & %lu] << %lu) ^ A[(b >> %lu) & %lu];\n",
		 2 * K, i + K, MASK, K, i, MASK);
	    printf("   hi = (hi << %lu) | (lo >> %lu);\n", 2 * K, REM);
	}
	/* special case for i=0 since a shift of 0 is undefined */
	printf
	    ("   lo = (lo << %lu) ^ (A[(b >> %lu) & %lu] << %lu) ^ A[b & %lu];\n",
	     2 * K, K, MASK, K, MASK);

	/* now perform the repair step for 2*K */
	MASK = 0;
	for (i = 0; i < w; i += 2 * K)
	    MASK |= 1UL << i;
	MASK = ~MASK;
	mask2 = MASK;
	printf("\n");
	for (i = 1; i < 2 * K; i++) {
	    /* bit w-i from a was not considered in blocks of
	       K bits from b for index j >= i */
	    /* Gaudry's no-branching optimization */
	    printf("   tmp = -((a >> %lu) & 1);\n", w - i);
            if (w == 32) {
                mask2 = (unsigned long) (uint32_t) mask2;
            }
	    printf("   tmp &= ((b & 0x%lx) >> %lu);\n", mask2, i);
	    printf("   hi = hi ^ tmp;\n");
	    mask2 = (mask2 << 1) & MASK;
	}
	printf("\n");
	if (fn == 0) {
	    printf("   c[0] = lo;\n");
	    printf("   c[1] = hi;\n");
	} else if (fn == 1) {
	    printf("   cp[i] = carry ^ lo;\n");
	    printf("   carry = hi;\n");
	} else if (fn == 2) {
	    printf("   dp[i] = cp[i] ^ (carry ^ lo);\n");
	    printf("   carry = hi;\n");
	}
	if (fn > 0) {
	    printf("    }\n");
	    printf("   return carry;\n");
	}
	printf("}\n\n");
    }

    printf("#endif\t/* GF2X_MUL1_H_ */\n");

    return 0;
}