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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
/***********************************************************************/
/* */
/* OCaml */
/* */
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
/* */
/* Copyright 2003 Institut National de Recherche en Informatique et */
/* en Automatique. All rights reserved. This file is distributed */
/* under the terms of the GNU Library General Public License, with */
/* the special exception on linking described in file ../../LICENSE. */
/* */
/***********************************************************************/
/* $Id$ */
/* Code specific to the Intel IA32 (x86) architecture. */
#define BngAdd2(res,carryout,arg1,arg2) \
asm("xorl %1, %1 \n\t" \
"addl %3, %0 \n\t" \
"setc %b1" \
: "=r" (res), "=&q" (carryout) \
: "0" (arg1), "rm" (arg2))
#define BngSub2(res,carryout,arg1,arg2) \
asm("xorl %1, %1 \n\t" \
"subl %3, %0 \n\t" \
"setc %b1" \
: "=r" (res), "=&q" (carryout) \
: "0" (arg1), "rm" (arg2))
#define BngMult(resh,resl,arg1,arg2) \
asm("mull %3" \
: "=a" (resl), "=d" (resh) \
: "a" (arg1), "r" (arg2))
#define BngDiv(quo,rem,nh,nl,d) \
asm("divl %4" \
: "=a" (quo), "=d" (rem) \
: "a" (nl), "d" (nh), "r" (d))
/* Reimplementation in asm of some of the bng operations. */
static bngcarry bng_ia32_add
(bng a/*[alen]*/, bngsize alen,
bng b/*[blen]*/, bngsize blen,
bngcarry carry)
{
bngdigit tmp;
alen -= blen;
if (blen > 0) {
asm("negb %b3 \n\t"
"1: \n\t"
"movl (%0), %4 \n\t"
"adcl (%1), %4 \n\t"
"movl %4, (%0) \n\t"
"leal 4(%0), %0 \n\t"
"leal 4(%1), %1 \n\t"
"decl %2 \n\t"
"jnz 1b \n\t"
"setc %b3"
: "+&r" (a), "+&r" (b), "+&r" (blen), "+&q" (carry), "=&r" (tmp));
}
if (carry == 0 || alen == 0) return carry;
do {
if (++(*a) != 0) return 0;
a++;
} while (--alen);
return 1;
}
static bngcarry bng_ia32_sub
(bng a/*[alen]*/, bngsize alen,
bng b/*[blen]*/, bngsize blen,
bngcarry carry)
{
bngdigit tmp;
alen -= blen;
if (blen > 0) {
asm("negb %b3 \n\t"
"1: \n\t"
"movl (%0), %4 \n\t"
"sbbl (%1), %4 \n\t"
"movl %4, (%0) \n\t"
"leal 4(%0), %0 \n\t"
"leal 4(%1), %1 \n\t"
"decl %2 \n\t"
"jnz 1b \n\t"
"setc %b3"
: "+&r" (a), "+&r" (b), "+&r" (blen), "+&q" (carry), "=&r" (tmp));
}
if (carry == 0 || alen == 0) return carry;
do {
if ((*a)-- != 0) return 0;
a++;
} while (--alen);
return 1;
}
static bngdigit bng_ia32_mult_add_digit
(bng a/*[alen]*/, bngsize alen,
bng b/*[blen]*/, bngsize blen,
bngdigit d)
{
bngdigit out;
bngcarry carry;
alen -= blen;
out = 0;
if (blen > 0) {
asm("1: \n\t"
"movl (%1), %%eax \n\t"
"mull %4\n\t" /* edx:eax = d * next digit of b */
"addl (%0), %%eax \n\t" /* add next digit of a to eax */
"adcl $0, %%edx \n\t" /* accumulate carry in edx */
"addl %3, %%eax \n\t" /* add out to eax */
"adcl $0, %%edx \n\t" /* accumulate carry in edx */
"movl %%eax, (%0) \n\t" /* eax is next digit of result */
"movl %%edx, %3 \n\t" /* edx is next out */
"leal 4(%0), %0 \n\t"
"leal 4(%1), %1 \n\t"
"decl %2 \n\t"
"jnz 1b"
: "+&r" (a), "+&r" (b), "+&r" (blen), "=m" (out)
: "m" (d)
: "eax", "edx");
}
if (alen == 0) return out;
/* current digit of a += out */
BngAdd2(*a, carry, *a, out);
a++;
alen--;
/* Propagate carry */
if (carry == 0 || alen == 0) return carry;
do {
if (++(*a) != 0) return 0;
a++;
} while (--alen);
return 1;
}
static bngdigit bng_ia32_mult_sub_digit
(bng a/*[alen]*/, bngsize alen,
bng b/*[blen]*/, bngsize blen,
bngdigit d)
{
bngdigit out, tmp;
bngcarry carry;
alen -= blen;
out = 0;
if (blen > 0) {
asm("1: \n\t"
"movl (%1), %%eax \n\t"
"movl (%0), %4 \n\t"
"mull %5\n\t" /* edx:eax = d * next digit of b */
"subl %%eax, %4 \n\t" /* subtract eax from next digit of a */
"adcl $0, %%edx \n\t" /* accumulate carry in edx */
"subl %3, %4 \n\t" /* subtract out */
"adcl $0, %%edx \n\t" /* accumulate carry in edx */
"movl %4, (%0) \n\t" /* store next digit of result */
"movl %%edx, %3 \n\t" /* edx is next out */
"leal 4(%0), %0 \n\t"
"leal 4(%1), %1 \n\t"
"decl %2 \n\t"
"jnz 1b"
: "+&r" (a), "+&r" (b), "=m" (blen), "=m" (out), "=&r" (tmp)
: "m" (d)
: "eax", "edx");
}
if (alen == 0) return out;
/* current digit of a -= out */
BngSub2(*a, carry, *a, out);
a++;
alen--;
/* Propagate carry */
if (carry == 0 || alen == 0) return carry;
do {
if ((*a)-- != 0) return 0;
a++;
} while (--alen);
return 1;
}
/* This is another asm implementation of some of the bng operations,
using SSE2 operations to provide 64-bit arithmetic.
This is faster than the plain IA32 code above on the Pentium 4.
(Arithmetic operations with carry are slow on the Pentium 4). */
#if BNG_ASM_LEVEL >= 2
static bngcarry bng_ia32sse2_add
(bng a/*[alen]*/, bngsize alen,
bng b/*[blen]*/, bngsize blen,
bngcarry carry)
{
alen -= blen;
if (blen > 0) {
asm("movd %3, %%mm0 \n\t" /* MM0 is carry */
"1: \n\t"
"movd (%0), %%mm1 \n\t" /* MM1 is next digit of a */
"movd (%1), %%mm2 \n\t" /* MM2 is next digit of b */
"paddq %%mm1, %%mm0 \n\t" /* Add carry (64 bits) */
"paddq %%mm2, %%mm0 \n\t" /* Add digits (64 bits) */
"movd %%mm0, (%0) \n\t" /* Store low 32 bits of result */
"psrlq $32, %%mm0 \n\t" /* Next carry is top 32 bits of results */
"addl $4, %0\n\t"
"addl $4, %1\n\t"
"subl $1, %2\n\t"
"jne 1b \n\t"
"movd %%mm0, %3 \n\t"
"emms"
: "+&r" (a), "+&r" (b), "+&r" (blen), "+&rm" (carry));
}
if (carry == 0 || alen == 0) return carry;
do {
if (++(*a) != 0) return 0;
a++;
} while (--alen);
return 1;
}
static bngcarry bng_ia32sse2_sub
(bng a/*[alen]*/, bngsize alen,
bng b/*[blen]*/, bngsize blen,
bngcarry carry)
{
alen -= blen;
if (blen > 0) {
asm("movd %3, %%mm0 \n\t" /* MM0 is carry */
"1: \n\t"
"movd (%0), %%mm1 \n\t" /* MM1 is next digit of a */
"movd (%1), %%mm2 \n\t" /* MM2 is next digit of b */
"psubq %%mm0, %%mm1 \n\t" /* Subtract carry (64 bits) */
"psubq %%mm2, %%mm1 \n\t" /* Subtract digits (64 bits) */
"movd %%mm1, (%0) \n\t" /* Store low 32 bits of result */
"psrlq $63, %%mm1 \n\t" /* Next carry is sign bit of result */
"movq %%mm1, %%mm0 \n\t"
"addl $4, %0\n\t"
"addl $4, %1\n\t"
"subl $1, %2\n\t"
"jne 1b \n\t"
"movd %%mm0, %3 \n\t"
"emms"
: "+&r" (a), "+&r" (b), "+&r" (blen), "+&rm" (carry));
}
if (carry == 0 || alen == 0) return carry;
do {
if ((*a)-- != 0) return 0;
a++;
} while (--alen);
return 1;
}
static bngdigit bng_ia32sse2_mult_add_digit
(bng a/*[alen]*/, bngsize alen,
bng b/*[blen]*/, bngsize blen,
bngdigit d)
{
bngdigit out;
bngcarry carry;
alen -= blen;
out = 0;
if (blen > 0) {
asm("pxor %%mm0, %%mm0 \n\t" /* MM0 is carry */
"movd %4, %%mm7 \n\t" /* MM7 is digit d */
"1: \n\t"
"movd (%0), %%mm1 \n\t" /* MM1 is next digit of a */
"movd (%1), %%mm2 \n\t" /* MM2 is next digit of b */
"pmuludq %%mm7, %%mm2 \n\t" /* MM2 = d * digit of b */
"paddq %%mm1, %%mm0 \n\t" /* Add product and carry ... */
"paddq %%mm2, %%mm0 \n\t" /* ... and digit of a */
"movd %%mm0, (%0) \n\t" /* Store low 32 bits of result */
"psrlq $32, %%mm0 \n\t" /* Next carry is high 32 bits result */
"addl $4, %0\n\t"
"addl $4, %1\n\t"
"subl $1, %2\n\t"
"jne 1b \n\t"
"movd %%mm0, %3 \n\t"
"emms"
: "+&r" (a), "+&r" (b), "+&r" (blen), "=&rm" (out)
: "m" (d));
}
if (alen == 0) return out;
/* current digit of a += out */
BngAdd2(*a, carry, *a, out);
a++;
alen--;
/* Propagate carry */
if (carry == 0 || alen == 0) return carry;
do {
if (++(*a) != 0) return 0;
a++;
} while (--alen);
return 1;
}
static bngdigit bng_ia32sse2_mult_sub_digit
(bng a/*[alen]*/, bngsize alen,
bng b/*[blen]*/, bngsize blen,
bngdigit d)
{
static unsigned long long bias1 = 0xFFFFFFFF00000000ULL - 0xFFFFFFFFULL;
static unsigned long bias2 = 0xFFFFFFFFUL;
bngdigit out;
bngcarry carry;
alen -= blen;
out = 0;
if (blen > 0) {
/* Carry C is represented by ENC(C) = 0xFFFFFFFF - C (one's complement) */
asm("movd %6, %%mm0 \n\t" /* MM0 is carry (initially 0xFFFFFFFF) */
"movq %5, %%mm6 \n\t" /* MM6 is magic constant bias1 */
"movd %4, %%mm7 \n\t" /* MM7 is digit d */
"1: \n\t"
"movd (%0), %%mm1 \n\t" /* MM1 is next digit of a */
"movd (%1), %%mm2 \n\t" /* MM2 is next digit of b */
"paddq %%mm6, %%mm1 \n\t" /* bias digit of a */
"pmuludq %%mm7, %%mm2 \n\t" /* MM2 = d * digit of b */
/* Compute
digit of a + ENC(carry) + 0xFFFFFFFF00000000 - 0xFFFFFFFF - product
= digit of a - carry + 0xFFFFFFFF00000000 - product
= digit of a - carry - productlow + (ENC(nextcarry) << 32) */
"psubq %%mm2, %%mm1 \n\t"
"paddq %%mm1, %%mm0 \n\t"
"movd %%mm0, (%0) \n\t" /* Store low 32 bits of result */
"psrlq $32, %%mm0 \n\t" /* Next carry is 32 high bits of result */
"addl $4, %0\n\t"
"addl $4, %1\n\t"
"subl $1, %2\n\t"
"jne 1b \n\t"
"movd %%mm0, %3 \n\t"
"emms"
: "+&r" (a), "+&r" (b), "+&r" (blen), "=&rm" (out)
: "m" (d), "m" (bias1), "m" (bias2));
out = ~out; /* Undo encoding on out digit */
}
if (alen == 0) return out;
/* current digit of a -= out */
BngSub2(*a, carry, *a, out);
a++;
alen--;
/* Propagate carry */
if (carry == 0 || alen == 0) return carry;
do {
if ((*a)-- != 0) return 0;
a++;
} while (--alen);
return 1;
}
/* Detect whether SSE2 instructions are supported */
static int bng_ia32_sse2_supported(void)
{
unsigned int flags, newflags, max_id, capabilities;
#define EFLAG_CPUID 0x00200000
#define CPUID_IDENTIFY 0
#define CPUID_CAPABILITIES 1
#define SSE2_CAPABILITY 26
/* Check if processor has CPUID instruction */
asm("pushfl \n\t"
"popl %0"
: "=r" (flags) : );
newflags = flags ^ EFLAG_CPUID; /* CPUID detection flag */
asm("pushfl \n\t"
"pushl %1 \n\t"
"popfl \n\t"
"pushfl \n\t"
"popl %0 \n\t"
"popfl"
: "=r" (flags) : "r" (newflags));
/* If CPUID detection flag cannot be changed, CPUID instruction is not
available */
if ((flags & EFLAG_CPUID) != (newflags & EFLAG_CPUID)) return 0;
/* See if SSE2 extensions are supported */
asm("pushl %%ebx \n\t" /* need to preserve %ebx for PIC */
"cpuid \n\t"
"popl %%ebx"
: "=a" (max_id) : "a" (CPUID_IDENTIFY): "ecx", "edx");
if (max_id < 1) return 0;
asm("pushl %%ebx \n\t"
"cpuid \n\t"
"popl %%ebx"
: "=d" (capabilities) : "a" (CPUID_CAPABILITIES) : "ecx");
return capabilities & (1 << SSE2_CAPABILITY);
}
#endif
static void bng_ia32_setup_ops(void)
{
#if BNG_ASM_LEVEL >= 2
if (bng_ia32_sse2_supported()) {
bng_ops.add = bng_ia32sse2_add;
bng_ops.sub = bng_ia32sse2_sub;
bng_ops.mult_add_digit = bng_ia32sse2_mult_add_digit;
bng_ops.mult_sub_digit = bng_ia32sse2_mult_sub_digit;
return;
}
#endif
bng_ops.add = bng_ia32_add;
bng_ops.sub = bng_ia32_sub;
bng_ops.mult_add_digit = bng_ia32_mult_add_digit;
bng_ops.mult_sub_digit = bng_ia32_mult_sub_digit;
}
#define BNG_SETUP_OPS bng_ia32_setup_ops()
|