-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdlmath_funcs.h
652 lines (524 loc) · 11.9 KB
/
dlmath_funcs.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
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
/**
* @file dlmath_funcs.h
* @brief Mathematical functions
* @author Dominique LaSalle <[email protected]>
* Copyright (c) 2013-2015, Dominique LaSalle
* @version 1
* @date 2013-10-06
*/
/* prefixing ugliness */
#define DLMATH_PRE2(prefix,suffix) prefix ## _ ## suffix
#define DLMATH_PRE1(prefix,suffix) DLMATH_PRE2(prefix,suffix)
#define DLMATH_PUB(name) DLMATH_PRE1(DLMATH_PREFIX,name)
#define DLMATH_PRI(name) DLMATH_PRE1(_,DLMATH_PRE1(DLMATH_PREFIX,name))
/******************************************************************************
* PRIVATE CONSTANTS ***********************************************************
******************************************************************************/
#define R2(n) n, n + 2*64, n + 1*64, n + 3*64
#define R4(n) R2(n), R2(n + 2*16), R2(n + 1*16), R2(n + 3*16)
#define R6(n) R4(n), R4(n + 2*4 ), R4(n + 1*4 ), R4(n + 3*4 )
static const unsigned char DLMATH_PRI(rbyte)[256] = {
R6(0), R6(2), R6(1), R6(3)
};
#undef R2
#undef R4
#undef R6
/******************************************************************************
* PUBLIC FUNCTIONS ************************************************************
******************************************************************************/
#ifndef DLMATH_VISIBILITY
#define DLMATH_DEFVIS
#define DLMATH_VISIBILITY
#endif
DLMATH_VISIBILITY DLMATH_TYPE_T DLMATH_PUB(abs_diff)(
DLMATH_TYPE_T const a,
DLMATH_TYPE_T const b)
{
if (a > b) {
return a - b;
} else {
return b - a;
}
}
DLMATH_VISIBILITY DLMATH_TYPE_T DLMATH_PUB(sum)(
DLMATH_TYPE_T const * const ptr,
size_t const n)
{
size_t i;
DLMATH_TYPE_T sum = 0;
#if defined(DLMATH_DLTYPE) && DLMATH_DLTYPE == DLTYPE_FLOAT
size_t pend;
DLMATH_TYPE_T pagesum;
size_t const psize = MEM_BLOCK_SIZE / sizeof(DLMATH_TYPE_T);
for (i=0;i<n;) {
pagesum = 0;
for (pend=dl_min(i+psize,n);i<pend;++i) {
pagesum += ptr[i];
}
sum += pagesum;
}
#else
for (i=0;i<n;++i) {
sum += ptr[i];
}
#endif
return sum;
}
DLMATH_VISIBILITY DLMATH_TYPE_T DLMATH_PUB(product)(
DLMATH_TYPE_T const * const ptr,
size_t const n)
{
size_t i;
DLMATH_TYPE_T product = 1;
for (i=0;i<n;++i) {
product *= ptr[i];
}
return product;
}
DLMATH_VISIBILITY void DLMATH_PUB(differentiate)(
DLMATH_TYPE_T * const ptr,
size_t const n)
{
size_t i;
if (n > 0) {
for (i=0;i<n-1;++i) {
ptr[i] = ptr[i+1] - ptr[i];
}
ptr[i] = 0;
}
}
DLMATH_VISIBILITY DLMATH_TYPE_T DLMATH_PUB(prefixsum_exc)(
DLMATH_TYPE_T * const ptr,
size_t const n)
{
size_t i;
DLMATH_TYPE_T tmp1,tmp2;
if (n > 0) {
tmp1 = ptr[0];
ptr[0] = 0;
for (i=1;i<n;++i) {
tmp2 = ptr[i];
ptr[i] = tmp1 + ptr[i-1];
tmp1 = tmp2;
}
return tmp1 + ptr[n-1];
} else {
return 0;
}
}
DLMATH_VISIBILITY void DLMATH_PUB(prefixsum_inc)(
DLMATH_TYPE_T * const ptr,
size_t const n)
{
size_t i;
if (n > 0) {
for (i=1;i<n;++i) {
ptr[i] += ptr[i-1];
}
}
}
DLMATH_VISIBILITY DLMATH_TYPE_T DLMATH_PUB(prefixshift)(
DLMATH_TYPE_T * const ptr,
size_t const n)
{
size_t i;
DLMATH_TYPE_T end;
if (n > 0) {
end = ptr[n-1];
for (i=n;i>1;) {
--i;
ptr[i] = ptr[i-1];
}
ptr[0] = 0;
return end;
} else {
return 0;
}
}
DLMATH_VISIBILITY void DLMATH_PUB(add)(
DLMATH_TYPE_T * const ptr,
DLMATH_TYPE_T const a,
size_t const n)
{
size_t i;
for (i=0;i<n;++i) {
ptr[i] += a;
}
}
DLMATH_VISIBILITY void DLMATH_PUB(scale)(
DLMATH_TYPE_T * const ptr,
DLMATH_TYPE_T const a,
size_t const n)
{
size_t i;
for (i=0;i<n;++i) {
ptr[i] *= a;
}
}
DLMATH_VISIBILITY size_t DLMATH_PUB(max_index)(
DLMATH_TYPE_T const * const ptr,
size_t const n)
{
size_t i,maxidx;
for (maxidx=0,i=1; i<n;++i) {
if (ptr[i] > ptr[maxidx] ||
(ptr[i] == ptr[maxidx] && i < maxidx)) {
maxidx = i;
}
}
return maxidx;
}
DLMATH_VISIBILITY DLMATH_TYPE_T DLMATH_PUB(max_value)(
DLMATH_TYPE_T const * const ptr,
size_t const n)
{
DL_ASSERT(n > 0, "Passed in an empty array");
return ptr[DLMATH_PUB(max_index)(ptr,n)];
}
DLMATH_VISIBILITY DLMATH_TYPE_T DLMATH_PUB(set_max)(
DLMATH_TYPE_T * const ptr,
DLMATH_TYPE_T const max,
size_t const n)
{
size_t i, maxidx;
DLMATH_TYPE_T diff, oldmax;
maxidx = DLMATH_PUB(max_index)(ptr,n);
oldmax = ptr[maxidx];
/* Have to do this for unsigned types */
if (oldmax < max) {
diff = max - oldmax;
for (i=0;i<n;++i) {
ptr[i] += diff;
}
} else {
diff = oldmax - max;
for (i=0;i<n;++i) {
ptr[i] -= diff;
}
}
return oldmax;
}
DLMATH_VISIBILITY size_t DLMATH_PUB(min_index)(
DLMATH_TYPE_T const * const ptr,
size_t const n)
{
size_t i,minidx;
for (minidx=0,i=1; i<n;++i) {
if (ptr[i] < ptr[minidx] ||
(ptr[i] == ptr[minidx] && i < minidx)) {
minidx = i;
}
}
return minidx;
}
DLMATH_VISIBILITY DLMATH_TYPE_T DLMATH_PUB(min_value)(
DLMATH_TYPE_T const * const ptr,
size_t const n)
{
DL_ASSERT(n > 0, "Passed in an empty array");
return ptr[DLMATH_PUB(min_index)(ptr,n)];
}
DLMATH_VISIBILITY DLMATH_TYPE_T DLMATH_PUB(set_min)(
DLMATH_TYPE_T * const ptr,
DLMATH_TYPE_T const min,
size_t const n)
{
size_t i, minidx;
DLMATH_TYPE_T diff, oldmin;
minidx = DLMATH_PUB(min_index)(ptr,n);
oldmin = ptr[minidx];
/* Have to do this for unsigned types */
if (oldmin < min) {
diff = min - oldmin;
for (i=0;i<n;++i) {
ptr[i] += diff;
}
} else {
diff = oldmin - min;
for (i=0;i<n;++i) {
ptr[i] -= diff;
}
}
return oldmin;
}
DLMATH_VISIBILITY void DLMATH_PUB(incset)(
DLMATH_TYPE_T * const ptr,
DLMATH_TYPE_T const start,
DLMATH_TYPE_T const inc,
size_t const n)
{
size_t i;
for (i=0;i<n;++i) {
ptr[i] = start + (inc*i);
}
}
DLMATH_VISIBILITY void DLMATH_PUB(cyclicperm)(
DLMATH_TYPE_T * const ptr,
size_t const cyclesize,
size_t const n)
{
size_t i,j,k;
size_t * counts;
counts = size_calloc(cyclesize);
for (i=0;i<n;++i) {
j = i % cyclesize;
k = size_chunkstart(j,cyclesize,n);
ptr[k + counts[j]++] = i;
}
dl_free(counts);
}
DLMATH_VISIBILITY void DLMATH_PUB(blockcyclicperm)(
DLMATH_TYPE_T * const ptr,
size_t const cyclesize,
size_t const blocksize,
size_t const n)
{
size_t i,j,k;
size_t * counts;
if (n > 0) {
counts = size_calloc(cyclesize);
for (i=0;i<n;++i) {
j = (i/blocksize) % cyclesize;
k = size_chunkstart(j*blocksize,cyclesize*blocksize,n);
ptr[k + counts[j]++] = i;
}
dl_free(counts);
}
}
DLMATH_VISIBILITY void DLMATH_PUB(max_merge)(
DLMATH_TYPE_T * const dst,
DLMATH_TYPE_T const * const src,
size_t const n,
DLMATH_TYPE_T const empty_value)
{
size_t i;
for (i=0;i<n;++i) {
if (src[i] != empty_value) {
if (dst[i] != empty_value) {
dst[i] = dl_max(dst[i],src[i]);
} else {
dst[i] = src[i];
}
}
}
}
DLMATH_VISIBILITY void DLMATH_PUB(min_merge)(
DLMATH_TYPE_T * const dst,
DLMATH_TYPE_T const * const src,
size_t const n,
DLMATH_TYPE_T const empty_value)
{
size_t i;
for (i=0;i<n;++i) {
if (src[i] != empty_value) {
if (dst[i] != empty_value) {
dst[i] = dl_min(dst[i],src[i]);
} else {
dst[i] = src[i];
}
}
}
}
DLMATH_VISIBILITY void DLMATH_PUB(avg_merge)(
DLMATH_TYPE_T * const dst,
DLMATH_TYPE_T const * const src,
size_t const n,
DLMATH_TYPE_T const empty_value)
{
size_t i;
for (i=0;i<n;++i) {
if (src[i] != empty_value) {
if (dst[i] != empty_value) {
dst[i] = (DLMATH_TYPE_T)((dst[i]+src[i])/2.0);
} else {
dst[i] = src[i];
}
}
}
}
DLMATH_VISIBILITY size_t DLMATH_PUB(intersection_size)(
DLMATH_TYPE_T const * const a,
size_t const n,
DLMATH_TYPE_T const * const b,
size_t const m)
{
size_t i,j,matches;
matches = i = j = 0;
while (i<n && j < m) {
if (a[i] > b[j]) {
++j;
} else if (a[i] < b[j]) {
++i;
} else {
++i;
++j;
++matches;
}
}
return matches;
}
#if defined(DLMATH_DLTYPE) && DLMATH_DLTYPE == DLTYPE_FLOAT
DLMATH_VISIBILITY DLMATH_TYPE_T DLMATH_PUB(stable_sum)(
DLMATH_TYPE_T const * const ptr,
size_t const n)
{
/* kahan algorithm */
size_t i;
DLMATH_TYPE_T y, t;
DLMATH_TYPE_T sum, c;
sum = 0;
c = 0;
for (i=0;i<n;++i) {
y = ptr[i] - c;
t = sum + y;
c = (t - sum) - y;
sum = t;
}
return sum;
}
DLMATH_VISIBILITY long double DLMATH_PUB(fa_sum)(
DLMATH_TYPE_T const * const ptr,
size_t const n)
{
size_t i,pend;
long double sum;
double pagesum;
size_t const psize = MEM_BLOCK_SIZE / sizeof(DLMATH_TYPE_T);
sum = 0;
for (i=0;i<n;) {
pagesum = 0;
for (pend=dl_min(i+psize,n);i<pend;++i) {
pagesum += ptr[i];
}
sum += (long double)pagesum;
}
return sum;
}
#endif
#if defined(DLMATH_DLTYPE) && DLMATH_DLTYPE == DLTYPE_INTEGRAL
DLMATH_VISIBILITY int64_t DLMATH_PUB(lsum)(
DLMATH_TYPE_T const * const a,
size_t const n)
{
int64_t sum;
size_t i;
sum = 0;
for (i=0;i<n;++i) {
sum+=a[i];
}
return sum;
}
DLMATH_VISIBILITY DLMATH_TYPE_T DLMATH_PUB(updiv)(
DLMATH_TYPE_T const a,
DLMATH_TYPE_T const b)
{
return (a/b) + (a%b > 0 ? 1 : 0);
}
DLMATH_VISIBILITY DLMATH_TYPE_T DLMATH_PUB(chunksize)(
DLMATH_TYPE_T const i,
DLMATH_TYPE_T const n,
DLMATH_TYPE_T const m)
{
return (m/n) + (i < (m%n) ? 1 : 0);
}
DLMATH_VISIBILITY DLMATH_TYPE_T DLMATH_PUB(chunkstart)(
DLMATH_TYPE_T const i,
DLMATH_TYPE_T const n,
DLMATH_TYPE_T const m)
{
return ((m/n)*i) + dl_min(i,m%n);
}
DLMATH_VISIBILITY DLMATH_TYPE_T DLMATH_PUB(chunkid)(
DLMATH_TYPE_T const g,
DLMATH_TYPE_T const n,
DLMATH_TYPE_T const m)
{
DLMATH_TYPE_T a, b, c, d;
c = m/n;
d = m%n;
a = g / (c + 1);
if (g < d) {
return a;
} else {
b = (g - d) / c;
return dl_max(a,b);
}
}
DLMATH_VISIBILITY DLMATH_TYPE_T DLMATH_PUB(uplog2)(
DLMATH_TYPE_T const n)
{
return dl_bitsize(DLMATH_TYPE_T)-dl_clz((n-1) | 1);
}
DLMATH_VISIBILITY DLMATH_TYPE_T DLMATH_PUB(downlog2)(
DLMATH_TYPE_T const n)
{
return (dl_bitsize(DLMATH_TYPE_T)-1)-dl_clz(n | 1);
}
DLMATH_VISIBILITY DLMATH_TYPE_T DLMATH_PUB(uppow2)(
DLMATH_TYPE_T n)
{
if (n <= 1) {
return 1;
}
n = n -1;
n = n | (n >> 1);
n = n | (n >> 2);
n = n | (n >> 4);
if (sizeof(DLMATH_TYPE_T) >= 2) {
n = n | (n >> dl_maxshift(DLMATH_TYPE_T,8));
if (sizeof(DLMATH_TYPE_T) >= 4) {
n = n | (n >> dl_maxshift(DLMATH_TYPE_T,16));
if (sizeof(DLMATH_TYPE_T) >= 8) {
n = n | (n >> dl_maxshift(DLMATH_TYPE_T,32));
if (sizeof(DLMATH_TYPE_T) >= 16) {
n = n | (n >> dl_maxshift(DLMATH_TYPE_T,64));
}
}
}
}
return n + 1;
}
DLMATH_VISIBILITY DLMATH_TYPE_T DLMATH_PUB(downpow2)(
DLMATH_TYPE_T n)
{
if (n <= 1) {
return 1;
}
n = n | (n >> 1);
n = n | (n >> 2);
n = n | (n >> 4);
if (sizeof(DLMATH_TYPE_T) >= 2) {
n = n | (n >> dl_maxshift(DLMATH_TYPE_T,8));
if (sizeof(DLMATH_TYPE_T) >= 4) {
n = n | (n >> dl_maxshift(DLMATH_TYPE_T,16));
if (sizeof(DLMATH_TYPE_T) >= 8) {
n = n | (n >> dl_maxshift(DLMATH_TYPE_T,32));
if (sizeof(DLMATH_TYPE_T) >= 16) {
n = n | (n >> dl_maxshift(DLMATH_TYPE_T,64));
}
}
}
}
return n - (n >> 1);
}
DLMATH_VISIBILITY DLMATH_TYPE_T DLMATH_PUB(reversebits)(
DLMATH_TYPE_T const n)
{
DLMATH_TYPE_T r;
size_t i;
r = 0;
for (i=0;i<sizeof(DLMATH_TYPE_T);++i) {
r |= DLMATH_PRI(rbyte)[(n >> (8*i)) & 0xFF] <<
((sizeof(DLMATH_TYPE_T)-i-1)*8);
}
return r;
}
#endif
#ifdef DLMATH_DEFVIS
#undef DLMATH_DEFVIS
#undef DLMATH_VISIBILITY
#endif
#undef DLMATH_PRE1
#undef DLMATH_PRE2
#undef DLMATH_PUB
#undef DLMATH_PRI