-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbruun_kernel.hpp
More file actions
4907 lines (4344 loc) · 190 KB
/
Copy pathbruun_kernel.hpp
File metadata and controls
4907 lines (4344 loc) · 190 KB
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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
// MIT joshuah.rainstar@gmail.com 2026
// Internal normalized-basis Bruun RFFT kernel.
//
// BFFT builds this kernel as part of the library. Public users select transform
// size and output layout through include/bfft/bfft.h or include/bfft/bfft.hpp;
// the Makefile chooses safe host compiler switches automatically. Heap-optimized
// native ordering and fused scatter are the default hot path. The library keeps a
// sequential two-phase standard-output pack available internally and uses it only
// when the standard FFT-order output is large enough to win on the active SIMD
// backend.
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <new>
// ---------------------------------------------------------------------------
// SIMD backend resolution.
// BRUUN_LEVEL 0 = scalar, 1 = 128-bit (SSE2/NEON), 2 = AVX2+FMA, 3 = AVX-512
// Wider levels reuse the narrower loops as tails where needed.
// ---------------------------------------------------------------------------
#if defined(__AVX512F__)
# define BRUUN_LEVEL 3
#elif defined(__AVX2__) && defined(__FMA__)
# define BRUUN_LEVEL 2
#elif defined(__SSE2__) || defined(_M_X64) || (defined(__aarch64__) && defined(__ARM_NEON))
# define BRUUN_LEVEL 1
#else
# define BRUUN_LEVEL 0
#endif
#if BRUUN_LEVEL >= 2 || (BRUUN_LEVEL >= 1 && (defined(__SSE2__) || defined(_M_X64)))
# include <immintrin.h>
# define BRUUN_X86_128 1
#endif
#if BRUUN_LEVEL >= 1 && defined(__aarch64__) && defined(__ARM_NEON)
# include <arm_neon.h>
# define BRUUN_NEON_128 1
#endif
#if BRUUN_LEVEL == 1 && !defined(BRUUN_X86_128) && !defined(BRUUN_NEON_128)
# undef BRUUN_LEVEL
# define BRUUN_LEVEL 0
#endif
// 2-lane double vector primitive shared by the SSE2 and NEON paths.
#if defined(BRUUN_X86_128)
typedef __m128d bruun_v2;
# define V2_LD(p) _mm_loadu_pd(p)
# define V2_ST(p, a) _mm_storeu_pd((p), (a))
# define V2_ADD(a, b) _mm_add_pd((a), (b))
# define V2_SUB(a, b) _mm_sub_pd((a), (b))
# define V2_MUL(a, b) _mm_mul_pd((a), (b))
# define V2_DIV(a, b) _mm_div_pd((a), (b))
# define V2_MADD(a, b, c) V2_ADD((a), V2_MUL((b), (c)))
# define V2_MSUB(a, b, c) V2_SUB((a), V2_MUL((b), (c)))
# define V2_SET1(x) _mm_set1_pd(x)
# define V2_SETLH(l, h) _mm_set_pd((h), (l))
# define V2_UNPLO(a, b) _mm_unpacklo_pd((a), (b))
# define V2_UNPHI(a, b) _mm_unpackhi_pd((a), (b))
# define V2_DUP0(a) _mm_unpacklo_pd((a), (a))
# define V2_DUP1(a) _mm_unpackhi_pd((a), (a))
# define V2_NEGHI(a) _mm_xor_pd((a), _mm_set_pd(-0.0, 0.0))
# define V2_CMPGT(a, b) _mm_cmpgt_pd((a), (b))
# define V2_SELECT(m, t, f) _mm_or_pd(_mm_and_pd((m), (t)), _mm_andnot_pd((m), (f)))
#elif defined(BRUUN_NEON_128)
typedef float64x2_t bruun_v2;
# define V2_LD(p) vld1q_f64(p)
# define V2_ST(p, a) vst1q_f64((p), (a))
# define V2_ADD(a, b) vaddq_f64((a), (b))
# define V2_SUB(a, b) vsubq_f64((a), (b))
# define V2_MUL(a, b) vmulq_f64((a), (b))
# define V2_DIV(a, b) vdivq_f64((a), (b))
# define V2_MADD(a, b, c) vfmaq_f64((a), (b), (c))
# define V2_MSUB(a, b, c) vfmsq_f64((a), (b), (c))
# define V2_SET1(x) vdupq_n_f64(x)
# define V2_SETLH(l, h) vcombine_f64(vdup_n_f64(l), vdup_n_f64(h))
# define V2_UNPLO(a, b) vtrn1q_f64((a), (b))
# define V2_UNPHI(a, b) vtrn2q_f64((a), (b))
# define V2_DUP0(a) vdupq_laneq_f64((a), 0)
# define V2_DUP1(a) vdupq_laneq_f64((a), 1)
static inline float64x2_t bruun_neghi(float64x2_t a) {
const uint64x2_t m = { 0ULL, 0x8000000000000000ULL };
return vreinterpretq_f64_u64(veorq_u64(vreinterpretq_u64_f64(a), m));
}
# define V2_NEGHI(a) bruun_neghi(a)
static inline float64x2_t bruun_v2_cmpgt(float64x2_t a, float64x2_t b) {
return vreinterpretq_f64_u64(vcgtq_f64(a, b));
}
static inline float64x2_t bruun_v2_select(float64x2_t m, float64x2_t t, float64x2_t f) {
return vbslq_f64(vreinterpretq_u64_f64(m), t, f);
}
# define V2_CMPGT(a, b) bruun_v2_cmpgt((a), (b))
# define V2_SELECT(m, t, f) bruun_v2_select((m), (t), (f))
#endif
// 4-lane float vector primitive for internal float32 helper loops.
#if defined(BRUUN_X86_128)
typedef __m128 bruun_v4f;
# define V4F_LD(p) _mm_loadu_ps(p)
# define V4F_ST(p, a) _mm_storeu_ps((p), (a))
# define V4F_ADD(a, b) _mm_add_ps((a), (b))
# define V4F_SUB(a, b) _mm_sub_ps((a), (b))
# define V4F_MUL(a, b) _mm_mul_ps((a), (b))
# define V4F_MADD(a, b, c) V4F_ADD((a), V4F_MUL((b), (c)))
# define V4F_MSUB(a, b, c) V4F_SUB((a), V4F_MUL((b), (c)))
# define V4F_SET1(x) _mm_set1_ps(x)
# define V4F_SET4(a,b,c,d) _mm_setr_ps((a), (b), (c), (d))
# define V4F_ZERO() _mm_setzero_ps()
#elif defined(BRUUN_NEON_128)
typedef float32x4_t bruun_v4f;
# define V4F_LD(p) vld1q_f32(p)
# define V4F_ST(p, a) vst1q_f32((p), (a))
# define V4F_ADD(a, b) vaddq_f32((a), (b))
# define V4F_SUB(a, b) vsubq_f32((a), (b))
# define V4F_MUL(a, b) vmulq_f32((a), (b))
# define V4F_MADD(a, b, c) vfmaq_f32((a), (b), (c))
# define V4F_MSUB(a, b, c) vfmsq_f32((a), (b), (c))
# define V4F_SET1(x) vdupq_n_f32(x)
# define V4F_SET4(a,b,c,d) vsetq_lane_f32((d), vsetq_lane_f32((c), vsetq_lane_f32((b), vsetq_lane_f32((a), vdupq_n_f32(0.0f), 0), 1), 2), 3)
# define V4F_ZERO() vdupq_n_f32(0.0f)
#endif
#if defined(_MSC_VER) && !defined(__clang__)
#define RESTRICT __restrict
#elif defined(__GNUC__) || defined(__clang__)
#define RESTRICT __restrict__
#else
#define RESTRICT
#endif
#ifdef NDEBUG
#define BRUUN_ASSERT(cond) ((void)0)
#else
#define BRUUN_ASSERT(cond) assert(cond)
#endif
// BFFT keeps heap-optimized native spectrum ordering enabled internally.
// Applications select public layouts through the API instead of compile flags.
#ifndef BRUUN_HEAPOPT_SPECTRUM_ORDER
#define BRUUN_HEAPOPT_SPECTRUM_ORDER 1
#endif
#ifndef M_PI
#define M_PI 3.141592653589793238462643383279502884
#endif
namespace bruun {
#if defined(__GNUC__) || defined(__clang__)
#define BRUUN_ASSUME_ALIGNED(ptr, bytes) __builtin_assume_aligned((ptr), (bytes))
#else
#define BRUUN_ASSUME_ALIGNED(ptr, bytes) (ptr)
#endif
#if defined(__GNUC__) || defined(__clang__)
#define BRUUN_ALWAYS_INLINE inline __attribute__((always_inline))
#else
#define BRUUN_ALWAYS_INLINE inline
#endif
static constexpr std::size_t bruun_cache_alignment = 64;
template <typename T>
class heap_array {
public:
heap_array() noexcept : ptr_(nullptr), cap_(0), len_(0) {}
heap_array(const heap_array&) = delete;
heap_array& operator=(const heap_array&) = delete;
heap_array(heap_array&& other) noexcept
: ptr_(other.ptr_), cap_(other.cap_), len_(other.len_) {
other.ptr_ = nullptr;
other.cap_ = 0;
other.len_ = 0;
}
heap_array& operator=(heap_array&& other) noexcept {
if (this != &other) {
release();
ptr_ = other.ptr_;
cap_ = other.cap_;
len_ = other.len_;
other.ptr_ = nullptr;
other.cap_ = 0;
other.len_ = 0;
}
return *this;
}
~heap_array() { release(); }
bool resize(std::size_t size) {
release();
if (size == 0) return true;
if (!alloc_raw(size)) return false;
for (std::size_t i = 0; i < size; ++i) {
new (ptr_ + i) T();
}
len_ = size;
return true;
}
bool assign(std::size_t size, const T& value) {
if (!resize(size)) return false;
for (std::size_t i = 0; i < len_; ++i) {
ptr_[i] = value;
}
return true;
}
bool reserve(std::size_t cap) {
if (cap <= cap_) return true;
void* raw = aligned_alloc_raw(cap);
if (!raw) return false;
T* new_ptr = static_cast<T*>(raw);
for (std::size_t i = 0; i < len_; ++i) {
new (new_ptr + i) T(static_cast<T&&>(ptr_[i]));
ptr_[i].~T();
}
free_raw(ptr_);
ptr_ = new_ptr;
cap_ = cap;
return true;
}
bool push_back(const T& val) {
if (len_ >= cap_) {
if (!reserve(cap_ == 0 ? 16 : cap_ * 2)) return false;
}
new (ptr_ + len_) T(val);
++len_;
return true;
}
void pop_back() noexcept {
BRUUN_ASSERT(len_ > 0);
--len_;
ptr_[len_].~T();
}
T& back() noexcept { return ptr_[len_ - 1]; }
const T& back() const noexcept { return ptr_[len_ - 1]; }
void clear() noexcept {
for (std::size_t i = len_; i > 0; --i) {
ptr_[i - 1].~T();
}
len_ = 0;
}
T* begin() noexcept { return ptr_; }
T* end() noexcept { return ptr_ + len_; }
const T* begin() const noexcept { return ptr_; }
const T* end() const noexcept { return ptr_ + len_; }
std::size_t size() const noexcept { return len_; }
bool empty() const noexcept { return len_ == 0; }
T* data() noexcept { return ptr_; }
const T* data() const noexcept { return ptr_; }
T& operator[](std::size_t index) noexcept { return ptr_[index]; }
const T& operator[](std::size_t index) const noexcept { return ptr_[index]; }
private:
static void* aligned_alloc_raw(std::size_t count) {
const std::size_t bytes = sizeof(T) * count;
std::size_t padded = bytes;
const std::size_t remainder = padded % bruun_cache_alignment;
if (remainder != 0) padded += bruun_cache_alignment - remainder;
void* raw = nullptr;
#if defined(_MSC_VER)
raw = _aligned_malloc(padded, bruun_cache_alignment);
#else
if (posix_memalign(&raw, bruun_cache_alignment, padded) != 0) raw = nullptr;
#endif
return raw;
}
static void free_raw(void* p) noexcept {
if (!p) return;
#if defined(_MSC_VER)
_aligned_free(p);
#else
std::free(p);
#endif
}
bool alloc_raw(std::size_t count) {
void* raw = aligned_alloc_raw(count);
if (!raw) return false;
ptr_ = static_cast<T*>(raw);
cap_ = count;
return true;
}
void release() noexcept {
if (!ptr_) { cap_ = 0; len_ = 0; return; }
for (std::size_t i = len_; i > 0; --i) {
ptr_[i - 1].~T();
}
free_raw(ptr_);
ptr_ = nullptr;
cap_ = 0;
len_ = 0;
}
T* ptr_;
std::size_t cap_;
std::size_t len_;
};
struct MemHint {
uint32_t base;
uint32_t span;
uint16_t align;
uint16_t stream;
uint32_t next_base;
};
struct FwdOp {
uint32_t base;
uint32_t q;
uint32_t m;
uint16_t kind;
MemHint mem;
};
enum FwdOpKind : uint16_t {
FWD_OP_NORM2 = 1,
FWD_OP_CODELET_Q8 = 2,
FWD_OP_CODELET_D3 = 3,
FWD_OP_BINOMIAL = 4,
FWD_OP_SPINE_D3 = 5,
FWD_OP_SPINE_D2 = 6,
FWD_OP_SPINE_D1 = 7,
FWD_OP_SPINE_LEAF = 8,
FWD_OP_DC_NYQUIST = 9,
FWD_OP_SPINE_NORM2 = 10
};
// Small explicit traversal records keep depth-first segment ownership visible
// to the program instead of relying on recursive calls through inline member
// symbols. The stack bound covers every supported int-sized power-of-two plan.
struct DoubleSegment {
double* data;
int q;
int m;
};
struct int_pair {
int first;
int second;
};
struct complex_t {
double re;
double im;
};
struct complex_f32_t {
float re;
float im;
};
struct bruun_phase_node {
double c;
double s;
};
struct bruun_phase_leaf {
double center;
double c;
double s;
};
static constexpr double bruun_tau = 2.0 * M_PI;
static constexpr double bruun_pio2 = 0.5 * M_PI;
static constexpr bruun_phase_node bruun_phase_nodes[31] = {
{ 0.92387953251128674, 0.38268343236508978 },
{ 0.98078528040323043, 0.19509032201612825 },
{ 0.83146961230254524, 0.55557023301960218 },
{ 0.99518472667219693, 0.098017140329560604 },
{ 0.95694033573220882, 0.29028467725446233 },
{ 0.88192126434835505, 0.47139673682599764 },
{ 0.77301045336273699, 0.63439328416364549 },
{ 0.99879545620517241, 0.049067674327418015 },
{ 0.98917650996478101, 0.14673047445536175 },
{ 0.97003125319454397, 0.24298017990326387 },
{ 0.94154406518302081, 0.33688985339222005 },
{ 0.90398929312344334, 0.42755509343028208 },
{ 0.85772861000027212, 0.51410274419322166 },
{ 0.80320753148064494, 0.59569930449243336 },
{ 0.74095112535495911, 0.67155895484701833 },
{ 0.99969881869620425, 0.024541228522912288 },
{ 0.99729045667869021, 0.073564563599667426 },
{ 0.99247953459870997, 0.1224106751992162 },
{ 0.98527764238894122, 0.17096188876030122 },
{ 0.97570213003852857, 0.2191012401568698 },
{ 0.96377606579543984, 0.26671275747489837 },
{ 0.94952818059303667, 0.31368174039889152 },
{ 0.93299279883473896, 0.35989503653498811 },
{ 0.91420975570353069, 0.40524131400498986 },
{ 0.89322430119551532, 0.44961132965460654 },
{ 0.87008699110871146, 0.49289819222978404 },
{ 0.84485356524970712, 0.53499761988709715 },
{ 0.81758481315158371, 0.57580819141784534 },
{ 0.78834642762660634, 0.61523159058062682 },
{ 0.75720884650648457, 0.65317284295377676 },
{ 0.724247082951467, 0.68954054473706683 },
};
static constexpr bruun_phase_leaf bruun_phase_leaves[32] = {
{ 0.012271846303085129, 0.9999247018391445, 0.012271538285719925 },
{ 0.036815538909255388, 0.99932238458834954, 0.036807222941358832 },
{ 0.061359231515425647, 0.99811811290014918, 0.061320736302208578 },
{ 0.085902924121595906, 0.996312612182778, 0.085797312344439894 },
{ 0.11044661672776616, 0.99390697000235606, 0.11022220729388306 },
{ 0.13499030933393641, 0.99090263542778001, 0.13458070850712617 },
{ 0.1595340019401067, 0.98730141815785843, 0.15885814333386145 },
{ 0.18407769454627693, 0.98310548743121629, 0.18303988795514095 },
{ 0.20862138715244721, 0.97831737071962765, 0.20711137619221856 },
{ 0.23316507975861744, 0.97293995220556018, 0.23105810828067111 },
{ 0.25770877236478773, 0.96697647104485207, 0.25486565960451457 },
{ 0.28225246497095796, 0.96043051941556579, 0.27851968938505306 },
{ 0.30679615757712825, 0.95330604035419386, 0.30200594931922808 },
{ 0.33133985018329848, 0.94560732538052128, 0.32531029216226293 },
{ 0.35588354278946877, 0.93733901191257496, 0.34841868024943456 },
{ 0.380427235395639, 0.92850608047321559, 0.37131719395183754 },
{ 0.40497092800180928, 0.91911385169005777, 0.3939920400610481 },
{ 0.42951462060797951, 0.90916798309052238, 0.41642956009763715 },
{ 0.4540583132141498, 0.89867446569395382, 0.43861623853852766 },
{ 0.47860200582032003, 0.88763962040285393, 0.46053871095824001 },
{ 0.50314569842649026, 0.8760700941954066, 0.48218377207912272 },
{ 0.52768939103266055, 0.86397285612158681, 0.50353838372571758 },
{ 0.55223308363883084, 0.8513551931052652, 0.52458968267846895 },
{ 0.57677677624500112, 0.83822470555483808, 0.54532498842204646 },
{ 0.6013204688511713, 0.82458930278502529, 0.56573181078361312 },
{ 0.62586416145734158, 0.81045719825259477, 0.58579785745643886 },
{ 0.65040785406351187, 0.79583690460888357, 0.60551104140432555 },
{ 0.67495154666968216, 0.78073722857209449, 0.62485948814238634 },
{ 0.69949523927585233, 0.76516726562245896, 0.64383154288979139 },
{ 0.72403893188202262, 0.74913639452345937, 0.66241577759017178 },
{ 0.7485826244881929, 0.73265427167241282, 0.68060099779545302 },
{ 0.77312631709436319, 0.71573082528381859, 0.69837624940897292 },
};
static inline double bruun_fused_madd(double a, double b, double c) {
#if defined(__FMA__)
return __builtin_fma(a, b, c);
#else
return a * b + c;
#endif
}
static inline double bruun_fused_msub(double a, double b, double c, double d) {
#if defined(__FMA__)
return __builtin_fma(a, b, -(c * d));
#else
return a * b - c * d;
#endif
}
static inline double bruun_atan_leaf_odd(double h) {
const double s = h * h;
double p = bruun_fused_madd(-1.0 / 7.0, s, 1.0 / 5.0);
p = bruun_fused_madd(p, s, -1.0 / 3.0);
p = bruun_fused_madd(p, s, 1.0);
return h * p;
}
static BRUUN_ALWAYS_INLINE double bruun_atan_leaf_cubic(double h) {
const double s = h * h;
return h * (1.0 - s * (1.0 / 3.0));
}
static inline int bruun_phase_child_index(int idx, int base, double major, double minor) {
const bruun_phase_node node = bruun_phase_nodes[base + idx];
const double side = bruun_fused_msub(minor, node.c, major, node.s);
idx <<= 1;
if (side > 0.0) {
idx |= 1;
}
return idx;
}
static inline double bruun_phase_first_octant_tree32_degree7(double major, double minor, double mag) {
int idx = 0;
idx = bruun_phase_child_index(idx, 0, major, minor);
idx = bruun_phase_child_index(idx, 1, major, minor);
idx = bruun_phase_child_index(idx, 3, major, minor);
idx = bruun_phase_child_index(idx, 7, major, minor);
idx = bruun_phase_child_index(idx, 15, major, minor);
const bruun_phase_leaf leaf = bruun_phase_leaves[idx];
const double dot = bruun_fused_madd(major, leaf.c, minor * leaf.s);
const double cross = bruun_fused_msub(minor, leaf.c, major, leaf.s);
const double h = cross / (mag + dot);
const double delta = 2.0 * bruun_atan_leaf_odd(h);
return leaf.center + delta;
}
static BRUUN_ALWAYS_INLINE double bruun_phase_first_octant_vec5_cubic(double major, double minor, double mag) {
double theta = M_PI / 8.0;
double c = 0.92387953251128674;
double s = 0.38268343236508978;
const double side0 = bruun_fused_msub(minor, c, major, s);
const double negative0 = static_cast<double>(side0 <= 0.0);
const double sign0 = 1.0 - 2.0 * negative0;
theta = bruun_fused_madd(sign0, 0.19634954084936207, theta);
const double signed_sstep0 = sign0 * 0.19509032201612825;
const double nc0 = bruun_fused_madd(-signed_sstep0, s, c * 0.98078528040323043);
const double ns0 = bruun_fused_madd(signed_sstep0, c, s * 0.98078528040323043);
c = nc0;
s = ns0;
const double side1 = bruun_fused_msub(minor, c, major, s);
const double negative1 = static_cast<double>(side1 <= 0.0);
const double sign1 = 1.0 - 2.0 * negative1;
theta = bruun_fused_madd(sign1, 0.09817477042468103, theta);
const double signed_sstep1 = sign1 * 0.098017140329560604;
const double nc1 = bruun_fused_madd(-signed_sstep1, s, c * 0.99518472667219693);
const double ns1 = bruun_fused_madd(signed_sstep1, c, s * 0.99518472667219693);
c = nc1;
s = ns1;
const double side2 = bruun_fused_msub(minor, c, major, s);
const double negative2 = static_cast<double>(side2 <= 0.0);
const double sign2 = 1.0 - 2.0 * negative2;
theta = bruun_fused_madd(sign2, 0.04908738521234052, theta);
const double signed_sstep2 = sign2 * 0.049067674327418015;
const double nc2 = bruun_fused_madd(-signed_sstep2, s, c * 0.99879545620517241);
const double ns2 = bruun_fused_madd(signed_sstep2, c, s * 0.99879545620517241);
c = nc2;
s = ns2;
const double side3 = bruun_fused_msub(minor, c, major, s);
const double negative3 = static_cast<double>(side3 <= 0.0);
const double sign3 = 1.0 - 2.0 * negative3;
theta = bruun_fused_madd(sign3, 0.02454369260617026, theta);
const double signed_sstep3 = sign3 * 0.024541228522912288;
const double nc3 = bruun_fused_madd(-signed_sstep3, s, c * 0.99969881869620425);
const double ns3 = bruun_fused_madd(signed_sstep3, c, s * 0.99969881869620425);
c = nc3;
s = ns3;
const double side4 = bruun_fused_msub(minor, c, major, s);
const double negative4 = static_cast<double>(side4 <= 0.0);
const double sign4 = 1.0 - 2.0 * negative4;
theta = bruun_fused_madd(sign4, 0.01227184630308513, theta);
const double signed_sstep4 = sign4 * 0.012271538285719925;
const double nc4 = bruun_fused_madd(-signed_sstep4, s, c * 0.9999247018391445);
const double ns4 = bruun_fused_madd(signed_sstep4, c, s * 0.9999247018391445);
c = nc4;
s = ns4;
const double dot = bruun_fused_madd(major, c, minor * s);
const double cross = bruun_fused_msub(minor, c, major, s);
const double h = cross / (mag + dot);
const double delta = 2.0 * bruun_atan_leaf_cubic(h);
return theta + delta;
}
static BRUUN_ALWAYS_INLINE double bruun_phase_first_octant(double major, double minor, double mag) {
return bruun_phase_first_octant_vec5_cubic(major, minor, mag);
}
template <class FirstOctant>
static inline double bruun_phase_atan2_core(double y, double x, double mag, FirstOctant first_octant) {
const double ax = std::fabs(x);
const double ay = std::fabs(y);
if (ay == 0.0) {
if (ax == 0.0) {
return 0.0;
}
if (x < 0.0) {
return M_PI;
}
return 0.0;
}
if (ax == 0.0) {
if (y < 0.0) {
return bruun_tau - bruun_pio2;
}
return bruun_pio2;
}
double alpha = 0.0;
if (ax >= ay) {
alpha = first_octant(ax, ay, mag);
} else {
alpha = bruun_pio2 - first_octant(ay, ax, mag);
}
if (x < 0.0) {
alpha = M_PI - alpha;
}
if (y < 0.0) {
alpha = -alpha;
}
if (alpha < 0.0) {
alpha += bruun_tau;
}
return alpha;
}
#if BRUUN_LEVEL >= 1
static BRUUN_ALWAYS_INLINE bruun_v2 bruun_phase_first_octant_vec5_cubic_v2(bruun_v2 major,
bruun_v2 minor,
bruun_v2 mag) {
bruun_v2 theta = V2_SET1(M_PI / 8.0);
bruun_v2 c = V2_SET1(0.92387953251128674);
bruun_v2 s = V2_SET1(0.38268343236508978);
const bruun_v2 one = V2_SET1(1.0);
const bruun_v2 neg_one = V2_SET1(-1.0);
#define BRUUN_VEC5_V2_STEP(STEP, CSTEP, SSTEP) \
do { \
const bruun_v2 side = V2_MSUB(V2_MUL(minor, c), major, s); \
const bruun_v2 sign = V2_SELECT(V2_CMPGT(side, V2_SET1(0.0)), one, neg_one); \
theta = V2_ADD(theta, V2_MUL(sign, V2_SET1(STEP))); \
const bruun_v2 signed_sstep = V2_MUL(sign, V2_SET1(SSTEP)); \
const bruun_v2 nc = V2_SUB(V2_MUL(c, V2_SET1(CSTEP)), V2_MUL(s, signed_sstep)); \
const bruun_v2 ns = V2_ADD(V2_MUL(c, signed_sstep), V2_MUL(s, V2_SET1(CSTEP))); \
c = nc; \
s = ns; \
} while (false)
BRUUN_VEC5_V2_STEP(0.19634954084936207, 0.98078528040323043, 0.19509032201612825);
BRUUN_VEC5_V2_STEP(0.09817477042468103, 0.99518472667219693, 0.098017140329560604);
BRUUN_VEC5_V2_STEP(0.04908738521234052, 0.99879545620517241, 0.049067674327418015);
BRUUN_VEC5_V2_STEP(0.02454369260617026, 0.99969881869620425, 0.024541228522912288);
BRUUN_VEC5_V2_STEP(0.01227184630308513, 0.9999247018391445, 0.012271538285719925);
#undef BRUUN_VEC5_V2_STEP
const bruun_v2 dot = V2_ADD(V2_MUL(major, c), V2_MUL(minor, s));
const bruun_v2 cross = V2_SUB(V2_MUL(minor, c), V2_MUL(major, s));
const bruun_v2 h = V2_DIV(cross, V2_ADD(mag, dot));
const bruun_v2 h2 = V2_MUL(h, h);
const bruun_v2 cubic = V2_MUL(h, V2_SUB(one, V2_MUL(h2, V2_SET1(1.0 / 3.0))));
return V2_ADD(theta, V2_MUL(V2_SET1(2.0), cubic));
}
#endif
#if BRUUN_LEVEL >= 1
static BRUUN_ALWAYS_INLINE void bruun_phase_atan2_mag_pair(double y0, double x0, double mag0,
double y1, double x1, double mag1,
double* out0, double* out1) {
const double ax0 = std::fabs(x0);
const double ay0 = std::fabs(y0);
const double ax1 = std::fabs(x1);
const double ay1 = std::fabs(y1);
if (ax0 == 0.0 || ay0 == 0.0 || ax1 == 0.0 || ay1 == 0.0) {
*out0 = bruun_phase_atan2_core(y0, x0, mag0, bruun_phase_first_octant);
*out1 = bruun_phase_atan2_core(y1, x1, mag1, bruun_phase_first_octant);
return;
}
double major0 = ax0;
double minor0 = ay0;
bool swap0 = false;
if (ay0 > ax0) {
major0 = ay0;
minor0 = ax0;
swap0 = true;
}
double major1 = ax1;
double minor1 = ay1;
bool swap1 = false;
if (ay1 > ax1) {
major1 = ay1;
minor1 = ax1;
swap1 = true;
}
const bruun_v2 major = V2_SETLH(major0, major1);
const bruun_v2 minor = V2_SETLH(minor0, minor1);
const bruun_v2 mag = V2_SETLH(mag0, mag1);
double phase_pair[2];
V2_ST(phase_pair, bruun_phase_first_octant_vec5_cubic_v2(major, minor, mag));
double alpha0 = phase_pair[0];
if (swap0) {
alpha0 = bruun_pio2 - alpha0;
}
if (x0 < 0.0) {
alpha0 = M_PI - alpha0;
}
if (y0 < 0.0) {
alpha0 = -alpha0;
}
if (alpha0 < 0.0) {
alpha0 += bruun_tau;
}
double alpha1 = phase_pair[1];
if (swap1) {
alpha1 = bruun_pio2 - alpha1;
}
if (x1 < 0.0) {
alpha1 = M_PI - alpha1;
}
if (y1 < 0.0) {
alpha1 = -alpha1;
}
if (alpha1 < 0.0) {
alpha1 += bruun_tau;
}
*out0 = alpha0;
*out1 = alpha1;
}
#endif
static BRUUN_ALWAYS_INLINE double bruun_phase_atan2_mag(double y, double x, double mag) {
return bruun_phase_atan2_core(y, x, mag, bruun_phase_first_octant);
}
static inline float bruun_phase_atan2_mag_f32(float y, float x, float mag) {
const double phase = bruun_phase_atan2_mag(static_cast<double>(y),
static_cast<double>(x),
static_cast<double>(mag));
return static_cast<float>(phase);
}
static inline double bruun_phase_atan2(double y, double x) {
const double mag = std::sqrt(x * x + y * y);
return bruun_phase_atan2_mag(y, x, mag);
}
static inline float bruun_phase_atan2_f32(float y, float x) {
const float mag = std::sqrt(x * x + y * y);
return bruun_phase_atan2_mag_f32(y, x, mag);
}
struct bruun_sincos_sample {
double s;
double c;
};
template<int K>
struct bruun_sincos_table {
static_assert((K & (K - 1)) == 0, "K must be a power of two");
std::array<bruun_sincos_sample, K> value{};
bruun_sincos_table() {
for (int i = 0; i < K; ++i) {
const double phase = bruun_tau * static_cast<double>(i) / static_cast<double>(K);
value[static_cast<std::size_t>(i)].s = std::sin(phase);
value[static_cast<std::size_t>(i)].c = std::cos(phase);
}
}
static const bruun_sincos_table& get() {
static const bruun_sincos_table table;
return table;
}
};
// Fast phase -> sin/cos for the mag-phase inverse hot loop. The inverse of
// BFFT's own forward_mag_phase stores phases in [0, 2*pi), so the common round
// trip avoids libm sin/cos calls. Out-of-range public inputs keep the historical
// libm behavior instead of silently applying a range-reduction approximation.
static inline void bruun_table256_poly3_sincos(double phase, double* s_out, double* c_out) {
constexpr int table_size = 256;
constexpr double inv_step = static_cast<double>(table_size) / bruun_tau;
constexpr double step = bruun_tau / static_cast<double>(table_size);
if (!(phase >= 0.0 && phase < bruun_tau)) {
*s_out = std::sin(phase);
*c_out = std::cos(phase);
return;
}
const double qd = phase * inv_step + 0.5;
const auto qi = static_cast<std::int64_t>(qd);
const auto idx = static_cast<std::size_t>(qi) & static_cast<std::size_t>(table_size - 1);
const double r = phase - static_cast<double>(qi) * step;
const double r2 = r * r;
const double sr = r * (1.0 - r2 * (1.0 / 6.0));
const double cr = 1.0 - r2 * 0.5;
const bruun_sincos_sample sample = bruun_sincos_table<table_size>::get().value[idx];
*c_out = sample.c * cr - sample.s * sr;
*s_out = sample.s * cr + sample.c * sr;
}
static inline void bruun_table256_poly3_sincos_f32(float phase, float* s_out, float* c_out) {
double s;
double c;
bruun_table256_poly3_sincos(static_cast<double>(phase), &s, &c);
*s_out = static_cast<float>(s);
*c_out = static_cast<float>(c);
}
static inline const char* simd_backend_name() {
#if BRUUN_LEVEL == 3
return "avx512-512";
#elif BRUUN_LEVEL == 2
return "avx2-fma-256";
#elif defined(BRUUN_X86_128)
return "sse2-128";
#elif defined(BRUUN_NEON_128)
return "neon-128";
#else
return "scalar";
#endif
}
static inline bool is_power2(int n) {
return n > 0 && ((n & (n - 1)) == 0);
}
static inline int ilog2_pow2(int n) {
int l = 0;
while (n > 1) {
n >>= 1;
++l;
}
return l;
}
static inline int graydecode_int(int g) {
for (int s = 1; s < 32; s <<= 1) g ^= g >> s;
return g;
}
static inline int bitrev_int(int r, int t) {
int out = 0;
for (int i = 0; i < t; ++i) {
out = (out << 1) | (r & 1);
r >>= 1;
}
return out;
}
static inline int bruun_idx_int(int m, int L) {
#if defined(__GNUC__) || defined(__clang__)
const int t = 31 - __builtin_clz((unsigned)m);
#else
int t = 0;
for (int x = m; x > 1; x >>= 1) ++t;
#endif
const int r = m ^ (1 << t);
return (2 * graydecode_int(bitrev_int(r, t)) + 1) << ((L - 2) - t);
}
// ---------------------------------------------------------------------------
// Streaming kernels. Each has an optional 512 block, an optional 256 block,
// a 2-lane block for the 128-bit backend, and an exact scalar tail.
// ---------------------------------------------------------------------------
static inline void binomial_fwd(double* RESTRICT v, int h) {
int i = 0;
#if BRUUN_LEVEL >= 3
for (; i + 7 < h; i += 8) {
const __m512d a = _mm512_loadu_pd(v + i);
const __m512d b = _mm512_loadu_pd(v + h + i);
_mm512_storeu_pd(v + i, _mm512_add_pd(a, b));
_mm512_storeu_pd(v + h + i, _mm512_sub_pd(a, b));
}
#endif
#if BRUUN_LEVEL >= 2
for (; i + 3 < h; i += 4) {
const __m256d a = _mm256_loadu_pd(v + i);
const __m256d b = _mm256_loadu_pd(v + h + i);
_mm256_storeu_pd(v + i, _mm256_add_pd(a, b));
_mm256_storeu_pd(v + h + i, _mm256_sub_pd(a, b));
}
#elif BRUUN_LEVEL == 1
for (; i + 1 < h; i += 2) {
const bruun_v2 a = V2_LD(v + i);
const bruun_v2 b = V2_LD(v + h + i);
V2_ST(v + i, V2_ADD(a, b));
V2_ST(v + h + i, V2_SUB(a, b));
}
#endif
for (; i < h; ++i) {
const double a = v[i];
const double b = v[h + i];
v[i] = a + b;
v[h + i] = a - b;
}
}
// Fused "copy input + first binomial split": v[i] = in[i] + in[h+i], v[h+i] = in[i] - in[h+i].
static inline void binomial_oop(const double* RESTRICT in, double* RESTRICT v, int h) {
int i = 0;
#if BRUUN_LEVEL >= 3
for (; i + 7 < h; i += 8) {
const __m512d a = _mm512_loadu_pd(in + i);
const __m512d b = _mm512_loadu_pd(in + h + i);
_mm512_storeu_pd(v + i, _mm512_add_pd(a, b));
_mm512_storeu_pd(v + h + i, _mm512_sub_pd(a, b));
}
#endif
#if BRUUN_LEVEL >= 2
for (; i + 3 < h; i += 4) {
const __m256d a = _mm256_loadu_pd(in + i);
const __m256d b = _mm256_loadu_pd(in + h + i);
_mm256_storeu_pd(v + i, _mm256_add_pd(a, b));
_mm256_storeu_pd(v + h + i, _mm256_sub_pd(a, b));
}
#elif BRUUN_LEVEL == 1
for (; i + 1 < h; i += 2) {
const bruun_v2 a = V2_LD(in + i);
const bruun_v2 b = V2_LD(in + h + i);
V2_ST(v + i, V2_ADD(a, b));
V2_ST(v + h + i, V2_SUB(a, b));
}
#endif
for (; i < h; ++i) {
const double a = in[i];
const double b = in[h + i];
v[i] = a + b;
v[h + i] = a - b;
}
}
static inline void binomial_inv(double* RESTRICT v, int h) {
int i = 0;
#if BRUUN_LEVEL >= 3
{
const __m512d half8 = _mm512_set1_pd(0.5);
for (; i + 7 < h; i += 8) {
const __m512d a = _mm512_loadu_pd(v + i);
const __m512d b = _mm512_loadu_pd(v + h + i);
_mm512_storeu_pd(v + i, _mm512_mul_pd(half8, _mm512_add_pd(a, b)));
_mm512_storeu_pd(v + h + i, _mm512_mul_pd(half8, _mm512_sub_pd(a, b)));
}
}
#endif
#if BRUUN_LEVEL >= 2
const __m256d half4 = _mm256_set1_pd(0.5);
for (; i + 3 < h; i += 4) {
const __m256d a = _mm256_loadu_pd(v + i);
const __m256d b = _mm256_loadu_pd(v + h + i);
_mm256_storeu_pd(v + i, _mm256_mul_pd(half4, _mm256_add_pd(a, b)));
_mm256_storeu_pd(v + h + i, _mm256_mul_pd(half4, _mm256_sub_pd(a, b)));
}
#elif BRUUN_LEVEL == 1
const bruun_v2 half2 = V2_SET1(0.5);
for (; i + 1 < h; i += 2) {
const bruun_v2 a = V2_LD(v + i);
const bruun_v2 b = V2_LD(v + h + i);
V2_ST(v + i, V2_MUL(half2, V2_ADD(a, b)));
V2_ST(v + h + i, V2_MUL(half2, V2_SUB(a, b)));
}
#endif
for (; i < h; ++i) {
const double a = v[i];
const double b = v[h + i];
v[i] = 0.5 * (a + b);
v[h + i] = 0.5 * (a - b);
}
}
// One normalized-quadratic split: block [A0|B0|A1|B1] of quarters q.
static inline void norm_q_fwd(double* RESTRICT p, int q, double c_scalar, double s_scalar) {
double* RESTRICT A0p = p;
double* RESTRICT B0p = p + q;
double* RESTRICT A1p = p + 2*q;
double* RESTRICT B1p = p + 3*q;
int n = 0;
#if BRUUN_LEVEL >= 3
{
const __m512d wc = _mm512_set1_pd(c_scalar);
const __m512d ws = _mm512_set1_pd(s_scalar);
for (; n + 7 < q; n += 8) {
const __m512d A0 = _mm512_loadu_pd(A0p + n);
const __m512d B0 = _mm512_loadu_pd(B0p + n);
const __m512d A1 = _mm512_loadu_pd(A1p + n);