-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathkernels.cpp
583 lines (487 loc) · 14.9 KB
/
kernels.cpp
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/*
RealLib, a library for efficient exact real computation
Copyright (C) 2006 Branimir Lambov
This library is licensed under the Apache License, Version 2.0 (the "License");
you may not use this library except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "kernels.h"
#include "convolution.h"
#include <assert.h>
#include <math.h>
namespace RealLib {
#ifndef max
static inline int max(int x, int y) { return x > y ? x : y; }
#endif
i32 g_KernelsPrecision = 0;
#ifdef MULTIPLY_BY_CONVOLUTION
#define PI2 (2*3.14159265358979323846264338327950288419)
// buffers for convolution
double *g_pConvBufA = 0;
double *g_pConvBufB = 0;
// and the object itself
Convolution<double> *g_pConv = 0;
#endif
void InitializeKernels(u32 precision)
{
g_KernelsPrecision = precision;
#ifdef MULTIPLY_BY_CONVOLUTION
if (precision >= CONVOLUTION_THRESHOLD) {
u32 prec2pow;
// convolution will take 16 bits for a double
// size should be at least twice the number of 16-bit words
for (prec2pow = 16; prec2pow < precision*4; prec2pow <<= 1) ;
g_pConvBufA = new double[prec2pow];
g_pConvBufB = new double[prec2pow];
g_pConv = new Convolution<double>(prec2pow, PI2);
}
#endif
}
void FinalizeKernels()
{
#ifdef MULTIPLY_BY_CONVOLUTION
if (g_KernelsPrecision >= CONVOLUTION_THRESHOLD) {
delete g_pConvBufA;
g_pConvBufA = 0;
delete g_pConvBufB;
g_pConvBufB = 0;
delete g_pConv;
g_pConv = 0;
}
#endif
g_KernelsPrecision = 0;
}
// NormalizeMantissa: substraction can introduce zeroes in most
// significant positions of the mantissa. This function corrects
// such mantissas and returns the value that has to be substracted
// from the exponent. If this value is equal to working precision,
// the substraction function must recognize the value as Zero.
u32 NormalizeMantissa(u32 *man)
{
i32 kprec = g_KernelsPrecision;
i32 prec = kprec - 1;
if (man[prec] != 0) return 0;
// find the first non-zero word
while (--prec >= 0 && man[prec] == 0) ;
// calculate needed offset
prec = kprec - (prec + 1);
// do we have something to save?
if (prec != kprec) {
i32 u;
for (u=kprec - 1; u >= prec; --u)
man[u] = man[u - prec];
for (;u >= 0; --u)
man[u] = 0;
}
return prec;
}
// perform the actual addition
// returns true if there is carry
bool AddMantissa(u32 *man, // destination, pre-initialized by a call to the default constructor
const u32 *full, // the greater value
const u32 *part, // the partial value,
i32 start) // which is shifted by this many words
{
int carry = 0;
i32 kprec = g_KernelsPrecision;
// start with carry if highest bit in what's left out is 1
if (start != 0 && start <= kprec)
carry = part[start-1] >= (1u << 31);
u64 v; i32 u;
// add words
for (u=0; u<kprec - start; ++u) {
v = u64(full[u]) + u64(part[u + start]) + u64(carry);
man[u] = u32(v & 0xFFFFFFFF);
carry = u32(v >> 32);
}
// update for carry
for (; carry && u<kprec; ++u) {
man[u] = full[u] + carry;
carry = man[u] == 0;
}
// just copy
for (; u<kprec; ++u) {
man[u] = full[u];
}
return !!carry;
}
// adjust for calculations that don't fit the preallocated space.
// an extra pass might be needed if the leftover word introduces more carry.
// returns number of shifts done
u32 AdjustForCarry(u32 *man,
u32 msw) // most significant word, the one that doesn't fit in
{
i32 kprec = g_KernelsPrecision;
// round what's left over
u32 carry = man[0] >= (1u<<31);
i32 u;
// shift
for (u = 1; u<kprec && carry; ++u) {
man[u-1] = man[u] + 1;
carry = man[u-1] == 0;
}
for (; u<kprec; ++u)
man[u-1] = man[u];
// put new value
man[u-1] = msw + carry;
// reiterate if necessary
if (man[u-1] == 0) return 1 + AdjustForCarry(man, 1);
else return 1;
}
// perform the actual substraction
// returns true if part was greater and the result must be negated
bool SubMantissa(u32 *man, // destination, pre-initialized by a call to the default constructor
const u32 *full, // the greater value
const u32 *part, // the partial value,
i32 start) // which is shifted by this many words
{
i32 kprec = g_KernelsPrecision;
int carry = 0;
// start with carry if highest bit in what's left out is 1
if (start != 0 && start <= kprec)
carry = part[start-1] >= (1u << 31);
u64 v; i32 u;
// sub words
for (u=0; u<kprec - start; ++u) {
v = u64(full[u]) - u64(part[u + start]) - u64(carry);
man[u] = u32(v);
carry = (v >> 32) != 0;
}
// update for carry
for (; carry && u<kprec; ++u) {
man[u] = full[u] - carry;
carry = man[u] == u32(-1);
}
// just copy
for (; u<kprec; ++u) {
man[u] = full[u];
}
return !!carry;
}
// negate a mantissa. needed if SubMantissa returned true.
void NegMantissa(u32 *man)
{
i32 prec = g_KernelsPrecision;
i32 u;
for (u = 0; u < prec && man[u] == 0; ++u) {}
assert(u < prec);
man[u] = -man[u];
for (++u; u < prec; ++u)
man[u] = ~man[u];
}
// MulMantissa
// perform actual multiplication
// the most significant word of the result is not put in man.
// instead it is returned, so no precision will be lost if
// it is zero
#ifndef MULTIPLY_BY_CONVOLUTION
u32 MulMantissa(u32 *man,
const u32 *a,
const u32 *b,
i32 inputstart,
i32 inputlen)
#else
u32 MulMantissaDirect(u32 *man,
const u32 *a,
const u32 *b,
i32 inputstart,
i32 inputlen)
#endif
{
i32 kprec = g_KernelsPrecision;
u32 carry = 0;
u64 u, w = 0;
i32 i = kprec - inputlen * 2 + 1;
i32 j;
i32 k = 0;
// start by only calculating carry
for (; i < 0 && k < inputlen; ++i, ++k) {
w >>= 32;
w += u64(carry)<<32;
carry = 0;
for (j=0; j<=k; ++j) {
u = u64(a[j+inputstart]) * u64(b[k-j+inputstart]);
w += u;
if (w < u) ++carry; // this is a trick to check for carry in u64s
// assembler would've made this a lot easier
}
}
// alternatively
for (j=0; j<i; ++j) man[j] = 0;
assert(i>=0);
// we didn't write till now.
// besides carry, we should add 1 if the previous value had 1 in MS bit
if (w & 0x80000000) w += u64(1)<<32;
// start writing
for (; k < inputlen; ++i, ++k) {
w >>= 32;
w += u64(carry)<<32;
carry = 0;
for (j=0; j<=k; ++j) {
u = u64(a[j+inputstart]) * u64(b[k-j+inputstart]);
w += u;
if (w < u) ++carry;
}
man[i] = u32(w & 0xFFFFFFFF);
}
for (; i < kprec; ++i, ++k) {
w >>= 32;
w += u64(carry)<<32;
carry = 0;
for (j=k - inputlen + 1; j<inputlen; ++j) {
u = u64(a[j+inputstart]) * u64(b[k-j+inputstart]);
w += u;
if (w < u) ++carry;
}
man[i] = u32(w & 0xFFFFFFFF);
}
w >>= 32;
assert(!carry);
// leave the last word as return value
return u32(w & 0xFFFFFFFF);
}
#ifdef MULTIPLY_BY_CONVOLUTION
#ifndef __RESTRICT
#define restrict
#endif
u32 MulMantissa(u32 *restrict man,
const u32 *restrict a,
const u32 *restrict b,
i32 inputstart,
i32 inputlen)
{
double * restrict bufa = g_pConvBufA;
double * restrict bufb = g_pConvBufB;
// do it directly if it would be faster
if (inputlen < CONVOLUTION_THRESHOLD)
return MulMantissaDirect(man, a, b, inputstart, inputlen);
int i;
int prec = inputlen;
int prec2pow = 16;
if (inputlen == g_KernelsPrecision)
prec2pow = g_pConv->GetSize();
else while (prec2pow < prec*4) prec2pow *= 2;
// initialize buffers to input
for (i=0; i<inputlen; ++i) {
bufa[i*2] = a[i+inputstart] & 0xFFFF;
bufa[i*2+1] = (a[i+inputstart] >> 16) & 0xFFFF;
}
i=i*2-1;
while (++i<prec2pow) bufa[i] = 0;
for (i=0; i<inputlen; ++i) {
bufb[i*2] = b[i+inputstart] & 0xFFFF;
bufb[i*2+1] = (b[i+inputstart] >> 16) & 0xFFFF;
}
i=i*2-1;
while (++i<prec2pow) bufb[i] = 0;
// convolve
g_pConv->Convolve(bufa, bufb, prec2pow);
// make each value 16-bit
double carry = 0, t;
for (i=0; i<inputlen - 1-inputstart; ++i) {
t = floor(bufa[i] + carry + 0.5); // round it too
carry = floor(ldexp(t, -16));
bufa[i] = t - ldexp(carry, 16);
}
// from here on we start writing, one in MSB of previous word is carry
if (bufa[i-1] > (1<<15)) carry += 1;
for (; i<(prec + inputlen) * 2; ++i) {
t = floor(bufa[i] + carry + 0.5); // round it too
carry = floor(ldexp(t, -16));
bufa[i] = t - ldexp(carry, 16);
}
for (i=0;i<=inputstart-inputlen;++i) man[i] = 0;
// write the result
for (i=max(0, inputlen-1-inputstart); i<prec + inputlen - 1; ++i) {
man[i-inputlen+1+inputstart] = u32(bufa[i*2]) + (u32(bufa[i*2+1]) << 16);
}
// leave the last word out
return u32(bufa[i*2]) + (u32(bufa[i*2+1]) << 16);
}
#endif
bool MultipliedByConvolution(i32 inputlen)
{
return inputlen >= CONVOLUTION_THRESHOLD;
}
template<class T>
static inline void swap(T &a, T &b) { T c=a;a=b;b=c; }
// auxilliary function to help division
// amsw is the most significant word of a
// aofs is how many words a is shifted, with the msw's
// taken as 0, the first substituted by the amsw
// the result is shifted aofs words
// bscale is assumed positive, < 32
// returns false is a was < b, possibly breaking with an
// incomplete res.
bool SubManBScaled(u32 *res,
const u32 *a,
const u32 *b,
u32 &amsw,
i32 bscale,
i32 inputlen,
i32 inputstart,
i32 aofs)
{
#define combinewords(a, b, bscale) (bscale==0 ? b : ((a >> (32 - bscale)) + (b << bscale)))
int carry = 0;
u64 v;
i32 u;
u32 s = combinewords(0, (b[inputstart]), bscale);
for (u=inputstart; u<inputstart+aofs; ++u) {
v = u64(0) - u64(s) - u64(carry);
res[u] = u32(v);
carry = (v >> 32) != 0;
s = combinewords(b[u], b[u+1], bscale);
}
// sub words
for (u=0; u<inputlen-1; ++u) {
v = u64(a[u-aofs+inputstart]) - u64(s) - u64(carry);
res[u+inputstart] = u32(v);
carry = (v >> 32) != 0;
s = combinewords(b[inputstart+u], b[inputstart+u+1], bscale);
}
{
v = u64(a[u-aofs+inputstart]) - u64(s) - u64(carry);
res[u+inputstart] = u32(v);
carry = (v >> 32) != 0;
s = combinewords(b[inputstart+u], 0, bscale);
}
v = u64(amsw) - u64(s) - u64(carry);
carry = (v >> 32) != 0;
if (carry) return false;
else {
amsw = u32(v);
return true;
}
}
i32 DivMantissa(u32 *man,
const u32 *a,
const u32 *b,
i32 inputstart,
i32 inputlen,
u32 *temp1,
u32 *temp2)
{
i32 kprec = g_KernelsPrecision;
u32 amsw = 0;
i32 sc;
u32 r = 0;
i32 e = 1;
i32 j = inputstart + inputlen - 1;
i32 i = j;
i32 ofs = 0;
for (int k=0;k<inputstart;++k) man[k] = 0;
for (sc=31; sc>=0; --sc)
if (SubManBScaled(temp1, a, b, amsw, sc, inputlen, inputstart, 0)) break;
if (sc<0) {
e = 0; --i;
amsw = a[inputlen-1 + inputstart];
for (sc = 31; sc>=0; --sc)
if (SubManBScaled(temp1, a, b, amsw, sc, inputlen, inputstart, 1)) break;
assert(sc >= 0);
}
r |= 1<<sc;
while (j>=inputstart) {
while (--sc >= 0) {
if (SubManBScaled(temp2, temp1, b, amsw, sc, inputlen, inputstart, 0)) {
r |= 1<<sc;
swap(temp1, temp2);
}
}
ofs = 0;
while (sc < 0 && j >= inputstart) {
++ofs;
sc = 32; --i;
man[j--] = r;
if (j<inputstart) break;
amsw = temp1[inputlen - ofs + inputstart];
r = 0;
for (sc = 31; sc>=0; --sc)
if (SubManBScaled(temp2, temp1, b, amsw, sc, inputlen, inputstart, ofs)) {
r |= 1<<sc;
swap(temp1, temp2);
break;
}
}
}
// check if we need to round up
if (SubManBScaled(temp2, temp1, b, amsw, 31, inputlen, inputstart, ofs))
{
while (++j < kprec && ++man[j] == 0) ;
if (j==kprec) { // carry on msw means we have 1(0)
++e;
man[j-1] = 1;
}
}
return e;
}
// scale mantissa: multiplication by u32 multiplier
// implemented for performance
u32 ScaleMantissa(u32 *man,
const u32 *src,
u32 multiplier)
{
i32 kprec = g_KernelsPrecision;
u64 v = 0;
for (i32 i=0; i<kprec; ++i)
{
v += u64(src[i]) * u64(multiplier);
man[i] = u32(v & 0xFFFFFFFF);
v >>= 32;
}
return u32(v);
}
i32 InvScaleMantissa(u32 *man,
const u32 *src,
u32 divisor)
{
i32 kprec = g_KernelsPrecision;
i32 i = kprec - 1;
i32 j = i;
i32 e = 0;
u64 v = src[i];
if (v < divisor) {
v = (v << 32) + src[--i];
e = -1;
}
while (i > 0)
{
man[j--] = u32(v / divisor);
v = ((v % divisor) << 32) + src[--i];
}
man[j--] = u32(v / divisor);
if (j == 0) { // this would happen if msw in src was < divisor
v = (v % divisor) << 32;
man[j--] = u32(v / divisor);
}
// round the result; j is -1
if ((v % divisor) > divisor/2) {
while (++j < kprec && ++man[j] == 0) ;
if (j==kprec) { // carry on msw means we have 1(0)
++e;
man[j-1] = 1;
}
}
return e;
}
// binary scale mantissa, i.e. multiply by 1<<scale, where scale < 32
u32 BScaleMantissa(u32 *man,
const u32 *src,
u32 scale)
{
i32 kprec = g_KernelsPrecision;
u32 v = 0;
for (i32 i=0; i<kprec; ++i)
{
man[i] = (src[i] << scale) | v;
v = src[i] >> (32 - scale);
}
return u32(v);
}
}