-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspecialMath.h
385 lines (344 loc) · 8.22 KB
/
specialMath.h
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
#ifndef SPECIAL_MATH
#define SPECIAL_MATH
#include "constants.h"
#include "bigint.h"
#include "fixedpoint.h"
#include "kernels.h"
/* Forward declare the non-inline kernels in specialMath.c */
float fp2(FP* restrict real, FP* restrict imag);
float fp3(FP* restrict real, FP* restrict imag);
float fp4(FP* restrict real, FP* restrict imag);
float fp5(FP* restrict real, FP* restrict imag);
float fp6(FP* restrict real, FP* restrict imag);
float fp7(FP* restrict real, FP* restrict imag);
float fp8(FP* restrict real, FP* restrict imag);
float fp9(FP* restrict real, FP* restrict imag);
float fp10(FP* restrict real, FP* restrict imag);
float fp2s(FP* restrict real, FP* restrict imag);
float fp3s(FP* restrict real, FP* restrict imag);
float fp4s(FP* restrict real, FP* restrict imag);
float fp5s(FP* restrict real, FP* restrict imag);
float fp6s(FP* restrict real, FP* restrict imag);
float fp7s(FP* restrict real, FP* restrict imag);
float fp8s(FP* restrict real, FP* restrict imag);
float fp9s(FP* restrict real, FP* restrict imag);
float fp10s(FP* restrict real, FP* restrict imag);
/* Macro-based generic specializations */
//Required functions:
//printN, loadN, storeN, getValN, setValN, zeroN, incN, negN, addN, subN, mulN
#define IMPL_TYPE(n) \
typedef struct \
{ \
u64 w[n]; \
} FP##n;
//declare complete specialization
#define DECL_FUNCS(n) \
static inline void print##n(FP##n val); \
static inline FP##n load##n(FP* fp); \
static inline void store##n(FP* fp, FP##n v); \
static inline double getVal##n(FP##n v); \
static inline void setVal##n(FP##n* v, double val); \
static inline FP##n zero##n(); \
static inline FP##n shl##n(FP##n v, int bits); \
static inline FP##n inc##n(FP##n v); \
static inline FP##n neg##n(FP##n v); \
static inline FP##n add##n(FP##n a, FP##n b); \
static inline FP##n sub##n(FP##n a, FP##n b); \
static inline FP##n mul##n(FP##n a, FP##n b);
#define DECL_SPECIALIZATION(n) \
IMPL_TYPE(n) \
DECL_FUNCS(n)
//Declare all specialization types + inline functions
DECL_SPECIALIZATION(3)
DECL_SPECIALIZATION(4)
DECL_SPECIALIZATION(5)
DECL_SPECIALIZATION(6)
DECL_SPECIALIZATION(7)
DECL_SPECIALIZATION(8)
DECL_SPECIALIZATION(9)
DECL_SPECIALIZATION(10)
#define IMPL_PRINT(n) \
inline void print##n(FP##n val) \
{ \
for(int i = 0; i < n; i++) \
printf("%016llx", val.w[i]); \
}
#define IMPL_LOAD(n) \
inline FP##n load##n(FP* fp) \
{ \
FP##n v; \
memcpy(v.w, fp->value.val, n * 8); \
if(fp->sign) \
v = neg##n(v); \
return v; \
}
#define IMPL_STORE(n) \
inline void store##n(FP* fp, FP##n v) \
{ \
bool sign = v.w[0] & (1ULL << 63); \
if(sign) \
v = neg##n(v); \
memcpy(fp->value.val, v.w, n * 8); \
fp->sign = sign; \
}
#define IMPL_GETVAL(n) \
inline double getVal##n(FP##n v) \
{ \
MAKE_STACK_FP_PREC(fp, n); \
store##n(&fp, v); \
return getValue(&fp); \
}
#define IMPL_SETVAL(n) \
inline void setVal##n(FP##n* v, double val) \
{ \
MAKE_STACK_FP_PREC(fp, n); \
loadValue(&fp, val); \
*v = load##n(&fp); \
}
#define IMPL_ZERO(n) \
inline FP##n zero##n() \
{ \
FP##n v; \
memset(v.w, 0, n * 8); \
return v; \
}
#define IMPL_SHL(n) \
inline FP##n shl##n(FP##n v, int bits) \
{ \
u64 transfer; \
int transferShift = 64 - bits; \
u64 transferMask = ((1ULL << bits) - 1) << transferShift; \
for(int i = 0; i < n - 1; i++) \
{ \
transfer = (v.w[i + 1] & transferMask) >> transferShift; \
v.w[i] <<= bits; \
v.w[i] |= transfer; \
} \
v.w[n - 1] <<= bits; \
return v; \
}
#define IMPL_INC(n) \
inline FP##n inc##n(FP##n v) \
{ \
u128 wsum = 1; \
for(int i = n - 1; i >= 0; i--) \
{ \
wsum += v.w[i]; \
v.w[i] = (u64) wsum; \
wsum >>= 64; \
} \
return v; \
}
#define IMPL_NEG(n) \
inline FP##n neg##n(FP##n v) \
{ \
for(int i = 0; i < n; i++) \
v.w[i] = ~v.w[i]; \
return inc##n(v); \
}
#define IMPL_ADD(n) \
inline FP##n add##n(FP##n a, FP##n b) \
{ \
u128 wsum = 0; \
for(int i = n - 1; i >= 0; i--) \
{ \
wsum += a.w[i]; \
wsum += b.w[i]; \
a.w[i] = (u64) wsum; \
wsum >>= 64; \
} \
return a; \
}
#define IMPL_SUB(n) \
inline FP##n sub##n(FP##n a, FP##n b) \
{ \
return add##n(a, neg##n(b)); \
}
#define IMPL_MUL(n) \
inline FP##n mul##n(FP##n a, FP##n b) \
{ \
FP##n sab = (a.w[0] & (1ULL << 63)) ? neg##n(b) : zero##n(); \
FP##n sba = (b.w[0] & (1ULL << 63)) ? neg##n(a) : zero##n(); \
u128 partial[n]; \
memset(partial, 0, n * sizeof(u128)); \
for(int i = 0; i < n; i++) \
{ \
partial[i] += sab.w[i]; \
partial[i] += sba.w[i]; \
} \
for(int i = 0; i < n; i++) \
{ \
for(int j = 0; j < n - i; j++) \
{ \
u128 prod = (u128) a.w[i] * (u128) b.w[j]; \
partial[i + j] += (prod >> 64); \
if(i + j + 1 < n) \
partial[i + j + 1] += (u64) prod; \
} \
} \
FP##n result; \
u128 sum = 0; \
for(int i = n - 1; i >= 0; i--) \
{ \
sum += partial[i]; \
result.w[i] = (u64) sum; \
sum >>= 64; \
} \
return shl##n(result, maxExpo); \
}
/*
#define IMPL_MUL(n) \
inline FP##n mul##n(FP##n a, FP##n b) \
{ \
u64 f1[n * 2]; \
u64 f2[n * 2]; \
for(int i = 0; i < n; i++) \
{ \
f1[i + n] = a.w[i]; \
f2[i + n] = b.w[i]; \
} \
for(int i = 0; i < n; i++) \
{ \
f1[i] = (a.w[0] & (1ULL << 63)) ? ~(0ULL) : 0; \
f2[i] = (b.w[0] & (1ULL << 63)) ? ~(0ULL) : 0; \
} \
u128 partial[n * 4]; \
memset(partial, 0, n * 4 * sizeof(u128)); \
for(int i = 0; i < n * 2; i++) \
{ \
for(int j = 0; j < n * 2; j++) \
{ \
u128 prod = (u128) a.w[i] * (u128) b.w[j]; \
partial[i + j] += (prod >> 64); \
partial[i + j + 1] += (u64) prod; \
} \
} \
FP##n result; \
u128 sum = 0; \
for(int i = 3 * n - 1; i >= 2 * n; i--) \
{ \
sum += partial[i]; \
result.w[i - 2 * n] = (u64) sum; \
sum >>= 64; \
} \
return shl##n(result, maxExpo); \
}
*/
/* 128-bit specialization using u128 */
/* simpler & faster than macro */
#define LOW_8 ((u128) 0xFFFFFFFFFFFFFFFFULL)
/* each iteration does:
* 3x mul
* 3x add
* 1x sub
* 1x shl
*/
typedef u128 FP2;
/* 128-bit specializations */
//trivial wrappers for built-in add and sub
static inline FP2 add2(FP2 a, FP2 b)
{
return a + b;
}
static inline FP2 sub2(FP2 a, FP2 b)
{
return a - b;
}
static inline void print2(FP2 val);
static inline FP2 load2(FP* fp); //translate FP to FP2
static inline void store2(FP* fp, FP2 v); //translate FP2 to FP
static inline double getVal2(FP2 fp2); //translate FP2 to double
static inline void setVal2(FP2* fp2, double val); //translate double to FP2
static inline FP2 mul2(FP2 a, FP2 b);
static inline void print2(FP2 val)
{
printf("#%016llx%016llx", (u64) (val >> 64), (u64) val);
}
static inline FP2 load2(FP* fp)
{
//load words
s128 v = fp->value.val[0];
v <<= 64;
v |= fp->value.val[1];
//2s complement if fp negative
if(fp->sign)
{
v = -v;
}
return v;
}
static inline void store2(FP* fp, FP2 v)
{
s128 sv = v;
if(sv >= 0)
{
fp->sign = false;
}
else
{
fp->sign = true;
sv = -sv;
}
fp->value.val[1] = (u64) sv;
sv >>= 64;
fp->value.val[0] = (u64) sv;
}
static inline double getVal2(FP2 fp2)
{
bool sign = false;
s128 sv = fp2;
if(sv < 0)
{
sign = true;
sv = -sv;
}
double highword = sv >> 64;
double val = highword / (1ULL << (64 - maxExpo));
return sign ? -val : val;
}
static inline void setVal2(FP2* fp2, double val)
{
MAKE_STACK_FP_PREC(temp, 2);
loadValue(&temp, val);
*fp2 = load2(&temp);
}
static inline FP2 mul2(FP2 a, FP2 b)
{
u128 alo = (u64) a;
u128 ahi = a >> 64;
u128 blo = (u64) b;
u128 bhi = b >> 64;
//himask is a mask for sign bit of int128
u128 himask = 1;
himask <<= 127;
u128 sab = a & himask ? -b : 0;
u128 sba = b & himask ? -a : 0;
return (((ahi * blo) >> 64) +
((bhi * alo) >> 64) +
sab + sba +
(ahi * bhi)) << maxExpo;
}
//complete specialization for precision 64n bits
#define IMPL_SPECIALIZATION(n) \
IMPL_PRINT(n) \
IMPL_LOAD(n) \
IMPL_STORE(n) \
IMPL_GETVAL(n) \
IMPL_SETVAL(n) \
IMPL_ZERO(n) \
IMPL_SHL(n) \
IMPL_INC(n) \
IMPL_NEG(n) \
IMPL_ADD(n) \
IMPL_SUB(n) \
IMPL_MUL(n)
//Only need to impl
IMPL_SPECIALIZATION(3)
IMPL_SPECIALIZATION(4)
IMPL_SPECIALIZATION(5)
IMPL_SPECIALIZATION(6)
IMPL_SPECIALIZATION(7)
IMPL_SPECIALIZATION(8)
IMPL_SPECIALIZATION(9)
IMPL_SPECIALIZATION(10)
#endif