Repository navigation
Expand file tree
/
Copy pathkernel_float.h
More file actions
6409 lines (5420 loc) · 216 KB
/
Copy pathkernel_float.h
File metadata and controls
6409 lines (5420 loc) · 216 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
/*
* Kernel Float: Header-only library for vector types and reduced precision floating-point math.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file 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.
*/
//================================================================================
// this file has been auto-generated, do not modify its contents!
// date: 2026-09-25 19:45:19.408615
// git hash: 5226c9232b45c090be46c41e17034c089ffbf8d0
//================================================================================
#ifndef KERNEL_FLOAT_MACROS_H
#define KERNEL_FLOAT_MACROS_H
#ifdef __HIPCC__
#include "hip/hip_runtime.h"
#endif
// clang-format off
#ifdef __CUDACC__
#define KERNEL_FLOAT_IS_CUDA (1)
#define KERNEL_FLOAT_DEVICE __forceinline__ __device__
// NVRTC cannot deal with __host__ annotations
#ifdef __CUDACC_RTC__
#define KERNEL_FLOAT_INLINE __forceinline__ __device__
#else
#define KERNEL_FLOAT_INLINE __forceinline__ __host__ __device__
#endif
#ifdef __CUDA_ARCH__
#define KERNEL_FLOAT_IS_DEVICE (1)
#else // __CUDA_ARCH__
#define KERNEL_FLOAT_IS_HOST (1)
#endif // __CUDA_ARCH__
#elif defined(__HIPCC__)
#define KERNEL_FLOAT_IS_HIP (1)
#define KERNEL_FLOAT_DEVICE __attribute__((always_inline)) inline __device__
#define KERNEL_FLOAT_INLINE __attribute__((always_inline)) inline __host__ __device__
#ifdef __HIP_DEVICE_COMPILE__
#define KERNEL_FLOAT_IS_DEVICE (1)
#else
#define KERNEL_FLOAT_IS_HOST (1)
#endif
#else
#define KERNEL_FLOAT_INLINE inline
#define KERNEL_FLOAT_IS_HOST (1)
#endif
#ifndef KERNEL_FLOAT_FP16_AVAILABLE
#define KERNEL_FLOAT_FP16_AVAILABLE (1)
#endif // KERNEL_FLOAT_FP16_AVAILABLE
#ifndef KERNEL_FLOAT_FP16_OPS_AVAILABLE
#define KERNEL_FLOAT_FP16_OPS_AVAILABLE ((KERNEL_FLOAT_IS_CUDA && __CUDA_ARCH__ >= 530) || KERNEL_FLOAT_IS_HIP)
#endif
#ifndef KERNEL_FLOAT_BF16_AVAILABLE
#define KERNEL_FLOAT_BF16_AVAILABLE (1)
#endif // KERNEL_FLOAT_BF16_AVAILABLE
#ifndef KERNEL_FLOAT_BF16_OPS_AVAILABLE
#define KERNEL_FLOAT_BF16_OPS_AVAILABLE (KERNEL_FLOAT_IS_CUDA && __CUDA_ARCH__ >= 800)
#endif
#ifndef KERNEL_FLOAT_FP8_AVAILABLE
#ifdef __CUDACC_VER_MAJOR__
#define KERNEL_FLOAT_FP8_AVAILABLE (__CUDACC_VER_MAJOR__ >= 12)
#else // __CUDACC_VER_MAJOR__
#define KERNEL_FLOAT_FP8_AVAILABLE (0)
#endif // __CUDACC_VER_MAJOR__
#endif // KERNEL_FLOAT_FP8_AVAILABLE
#define KERNEL_FLOAT_ASSERT(expr) \
do { \
} while (0)
#define KERNEL_FLOAT_UNREACHABLE __builtin_unreachable()
// Somet utility macros
#define KERNEL_FLOAT_CONCAT_IMPL(A, B) A##B
#define KERNEL_FLOAT_CONCAT(A, B) KERNEL_FLOAT_CONCAT_IMPL(A, B)
#define KERNEL_FLOAT_CALL(F, ...) F(__VA_ARGS__)
// TOOD: check if this way is support across all compilers
#if defined(__has_builtin) && 0 // Seems that `__builtin_assume_aligned` leads to segfaults
#if __has_builtin(__builtin_assume_aligned)
#define KERNEL_FLOAT_ASSUME_ALIGNED(TYPE, PTR, ALIGNMENT) static_cast <TYPE*>(
__builtin_assume_aligned(static_cast <TYPE*>(PTR), (ALIGNMENT)))
#else
#define KERNEL_FLOAT_ASSUME_ALIGNED(TYPE, PTR, ALIGNMENT) (PTR)
#endif
#else
#define KERNEL_FLOAT_ASSUME_ALIGNED(TYPE, PTR, ALIGNMENT) (PTR)
#endif
#define KERNEL_FLOAT_MAX_ALIGNMENT (32)
#if KERNEL_FLOAT_FAST_MATH
#define KERNEL_FLOAT_POLICY ::kernel_float::fast_policy;
#endif
// clang-format on
#endif //KERNEL_FLOAT_MACROS_H
#ifndef KERNEL_FLOAT_CORE_H
#define KERNEL_FLOAT_CORE_H
namespace kernel_float {
using size_t = decltype(sizeof(int));
template<size_t... Is>
struct index_sequence {
static constexpr size_t size = sizeof...(Is);
};
namespace detail {
template<size_t N>
struct make_index_sequence_impl {};
// Benchmarks show that it is much faster to predefine all possible index sequences instead of doing something
// recursive with variadic templates.
#define KERNEL_FLOAT_INDEX_SEQ(N, ...) \
template<> \
struct make_index_sequence_impl<N> { \
using type = index_sequence<__VA_ARGS__>; \
};
KERNEL_FLOAT_INDEX_SEQ(0)
KERNEL_FLOAT_INDEX_SEQ(1, 0)
KERNEL_FLOAT_INDEX_SEQ(2, 0, 1)
KERNEL_FLOAT_INDEX_SEQ(3, 0, 1, 2)
KERNEL_FLOAT_INDEX_SEQ(4, 0, 1, 2, 3)
KERNEL_FLOAT_INDEX_SEQ(5, 0, 1, 2, 3, 4)
KERNEL_FLOAT_INDEX_SEQ(6, 0, 1, 2, 3, 4, 5)
KERNEL_FLOAT_INDEX_SEQ(7, 0, 1, 2, 3, 4, 5, 6)
KERNEL_FLOAT_INDEX_SEQ(8, 0, 1, 2, 3, 4, 5, 6, 7)
KERNEL_FLOAT_INDEX_SEQ(9, 0, 1, 2, 3, 4, 5, 6, 7, 8)
KERNEL_FLOAT_INDEX_SEQ(10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
KERNEL_FLOAT_INDEX_SEQ(11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
KERNEL_FLOAT_INDEX_SEQ(12, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
KERNEL_FLOAT_INDEX_SEQ(13, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
KERNEL_FLOAT_INDEX_SEQ(14, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
KERNEL_FLOAT_INDEX_SEQ(15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
KERNEL_FLOAT_INDEX_SEQ(16, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
KERNEL_FLOAT_INDEX_SEQ(17, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
} // namespace detail
template<size_t N>
using make_index_sequence = typename detail::make_index_sequence_impl<N>::type;
namespace detail {
template<typename T>
struct decay_impl {
using type = T;
};
template<typename T>
struct decay_impl<const T> {
using type = T;
};
template<typename T>
struct decay_impl<const T&> {
using type = T;
};
template<typename T>
struct decay_impl<T&> {
using type = T;
};
template<typename T>
struct decay_impl<T&&> {
using type = T;
};
} // namespace detail
template<typename T>
using decay_t = typename detail::decay_impl<T>::type;
template<typename A, typename B>
struct promote_type {};
template<typename T>
struct promote_type<T, T> {
using type = T;
};
template<typename T>
struct promote_type<void, T> {
using type = T;
};
template<typename T>
struct promote_type<T, void> {
using type = T;
};
template<>
struct promote_type<void, void> {
using type = void;
};
#define KERNEL_FLOAT_DEFINE_PROMOTED_TYPE(T, U) \
template<> \
struct promote_type<T, U> { \
using type = T; \
}; \
template<> \
struct promote_type<U, T> { \
using type = T; \
};
// T + bool becomes T
KERNEL_FLOAT_DEFINE_PROMOTED_TYPE(char, bool)
KERNEL_FLOAT_DEFINE_PROMOTED_TYPE(signed char, bool)
KERNEL_FLOAT_DEFINE_PROMOTED_TYPE(signed short, bool)
KERNEL_FLOAT_DEFINE_PROMOTED_TYPE(signed int, bool)
KERNEL_FLOAT_DEFINE_PROMOTED_TYPE(signed long, bool)
KERNEL_FLOAT_DEFINE_PROMOTED_TYPE(signed long long, bool)
KERNEL_FLOAT_DEFINE_PROMOTED_TYPE(unsigned char, bool)
KERNEL_FLOAT_DEFINE_PROMOTED_TYPE(unsigned short, bool)
KERNEL_FLOAT_DEFINE_PROMOTED_TYPE(unsigned int, bool)
KERNEL_FLOAT_DEFINE_PROMOTED_TYPE(unsigned long, bool)
KERNEL_FLOAT_DEFINE_PROMOTED_TYPE(unsigned long long, bool)
KERNEL_FLOAT_DEFINE_PROMOTED_TYPE(double, float)
KERNEL_FLOAT_DEFINE_PROMOTED_TYPE(long double, float)
KERNEL_FLOAT_DEFINE_PROMOTED_TYPE(long double, double)
#define KERNEL_FLOAT_DEFINE_PROMOTED_FLOAT(T) \
KERNEL_FLOAT_DEFINE_PROMOTED_TYPE(T, char) \
KERNEL_FLOAT_DEFINE_PROMOTED_TYPE(T, signed char) \
KERNEL_FLOAT_DEFINE_PROMOTED_TYPE(T, signed short) \
KERNEL_FLOAT_DEFINE_PROMOTED_TYPE(T, signed int) \
KERNEL_FLOAT_DEFINE_PROMOTED_TYPE(T, signed long) \
KERNEL_FLOAT_DEFINE_PROMOTED_TYPE(T, signed long long) \
KERNEL_FLOAT_DEFINE_PROMOTED_TYPE(T, unsigned char) \
KERNEL_FLOAT_DEFINE_PROMOTED_TYPE(T, unsigned short) \
KERNEL_FLOAT_DEFINE_PROMOTED_TYPE(T, unsigned int) \
KERNEL_FLOAT_DEFINE_PROMOTED_TYPE(T, unsigned long) \
KERNEL_FLOAT_DEFINE_PROMOTED_TYPE(T, unsigned long long) \
KERNEL_FLOAT_DEFINE_PROMOTED_TYPE(T, bool)
KERNEL_FLOAT_DEFINE_PROMOTED_FLOAT(float)
KERNEL_FLOAT_DEFINE_PROMOTED_FLOAT(double)
KERNEL_FLOAT_DEFINE_PROMOTED_FLOAT(long double)
#define KERNEL_FLOAT_DEFINE_PROMOTED_INTEGRAL(T, U) \
KERNEL_FLOAT_DEFINE_PROMOTED_TYPE(signed T, signed U) \
KERNEL_FLOAT_DEFINE_PROMOTED_TYPE(unsigned T, unsigned U)
KERNEL_FLOAT_DEFINE_PROMOTED_INTEGRAL(short, char)
KERNEL_FLOAT_DEFINE_PROMOTED_INTEGRAL(int, char)
KERNEL_FLOAT_DEFINE_PROMOTED_INTEGRAL(int, short)
KERNEL_FLOAT_DEFINE_PROMOTED_INTEGRAL(long, char)
KERNEL_FLOAT_DEFINE_PROMOTED_INTEGRAL(long, short)
KERNEL_FLOAT_DEFINE_PROMOTED_INTEGRAL(long, int)
KERNEL_FLOAT_DEFINE_PROMOTED_INTEGRAL(long long, char)
KERNEL_FLOAT_DEFINE_PROMOTED_INTEGRAL(long long, short)
KERNEL_FLOAT_DEFINE_PROMOTED_INTEGRAL(long long, int)
KERNEL_FLOAT_DEFINE_PROMOTED_INTEGRAL(long long, long)
template<typename T>
struct promote_type<T*, T*> {
using type = T*;
};
#define KERNEL_FLOAT_DEFINE_PROMOTED_POINTER(I) \
template<typename T> \
struct promote_type<T*, I> { \
using type = T*; \
}; \
template<typename T> \
struct promote_type<I, T*> { \
using type = T*; \
};
KERNEL_FLOAT_DEFINE_PROMOTED_POINTER(char)
KERNEL_FLOAT_DEFINE_PROMOTED_POINTER(signed char)
KERNEL_FLOAT_DEFINE_PROMOTED_POINTER(signed short)
KERNEL_FLOAT_DEFINE_PROMOTED_POINTER(signed int)
KERNEL_FLOAT_DEFINE_PROMOTED_POINTER(signed long)
KERNEL_FLOAT_DEFINE_PROMOTED_POINTER(signed long long)
KERNEL_FLOAT_DEFINE_PROMOTED_POINTER(unsigned char)
KERNEL_FLOAT_DEFINE_PROMOTED_POINTER(unsigned short)
KERNEL_FLOAT_DEFINE_PROMOTED_POINTER(unsigned int)
KERNEL_FLOAT_DEFINE_PROMOTED_POINTER(unsigned long)
KERNEL_FLOAT_DEFINE_PROMOTED_POINTER(unsigned long long)
// half precision
// KERNEL_FLOAT_DEFINE_PROMOTED_FLOAT(half)
// KERNEL_FLOAT_DEFINE_PROMOTED_TYPE(half, bool)
// KERNEL_FLOAT_DEFINE_PROMOTED_TYPE(float, half)
// KERNEL_FLOAT_DEFINE_PROMOTED_TYPE(double, half)
// KERNEL_FLOAT_DEFINE_PROMOTED_TYPE(long double, half)
namespace detail {
template<typename... Ts>
struct multi_promote_type;
template<typename T>
struct multi_promote_type<T> {
using type = T;
};
template<typename A, typename B>
struct multi_promote_type<A, B>: promote_type<A, B> {};
template<typename A, typename B, typename C, typename... Rest>
struct multi_promote_type<A, B, C, Rest...>:
multi_promote_type<typename promote_type<A, B>::type, C, Rest...> {};
} // namespace detail
template<typename... Ts>
using promote_t = typename detail::multi_promote_type<decay_t<Ts>...>::type;
namespace detail {
template<typename A, typename B>
struct is_same_type_impl {
static constexpr bool value = false;
};
template<typename A>
struct is_same_type_impl<A, A> {
static constexpr bool value = true;
};
} // namespace detail
template<typename A, typename B>
static constexpr bool is_same_type = detail::is_same_type_impl<A, B>::value;
namespace detail {
template<typename From, typename To, typename Common = To>
struct is_implicit_convertible_impl {
static constexpr bool value = false;
};
// This has to be done using SFINAE since `promote_type<From, To>` might actually be undefined if `From` is not
// implicitly convertible to `To`.
template<typename From, typename To>
struct is_implicit_convertible_impl<From, To, typename promote_type<From, To>::type> {
static constexpr bool value = true;
};
} // namespace detail
template<typename From, typename To>
static constexpr bool is_implicit_convertible =
detail::is_implicit_convertible_impl<decay_t<From>, decay_t<To>>::value;
namespace detail {
template<typename T>
KERNEL_FLOAT_INLINE T& declval() {
while (1)
;
}
} // namespace detail
namespace detail {
template<bool, typename T>
struct enable_if_impl {};
template<typename T>
struct enable_if_impl<true, T> {
using type = T;
};
} // namespace detail
template<bool C, typename T = void>
using enable_if_t = typename detail::enable_if_impl<C, T>::type;
KERNEL_FLOAT_INLINE
constexpr size_t round_up_to_power_of_two(size_t n) {
size_t result = 1;
while (result < n) {
result *= 2;
}
return result;
}
} // namespace kernel_float
#endif
#ifndef KERNEL_FLOAT_BASE_H
#define KERNEL_FLOAT_BASE_H
#if KERNEL_FLOAT_IS_HIP
#include <hip/hip_vector_types.h>
#endif
namespace kernel_float {
template<typename T, size_t N, size_t Alignment = alignof(T)>
struct alignas(Alignment) aligned_array {
KERNEL_FLOAT_INLINE
T* data() {
return items_;
}
KERNEL_FLOAT_INLINE
const T* data() const {
return items_;
}
T items_[N];
};
template<typename T, size_t Alignment>
struct alignas(Alignment) aligned_array<T, 1, Alignment> {
aligned_array() = default;
KERNEL_FLOAT_INLINE
aligned_array(T item) : item_(item) {}
KERNEL_FLOAT_INLINE
operator T() const {
return item_;
}
KERNEL_FLOAT_INLINE
T* data() {
return &item_;
}
KERNEL_FLOAT_INLINE
const T* data() const {
return &item_;
}
T item_;
};
template<typename T, size_t Alignment>
struct aligned_array<T, 0, Alignment> {
KERNEL_FLOAT_INLINE
T* data() {
while (true)
;
}
KERNEL_FLOAT_INLINE
const T* data() const {
while (true)
;
}
};
KERNEL_FLOAT_INLINE
static constexpr size_t compute_max_alignment(size_t total_size, size_t min_align) {
if (total_size % 32 == 0 || min_align >= 32) {
return 32;
} else if (total_size % 16 == 0 || min_align == 16) {
return 16;
} else if (total_size % 8 == 0 || min_align == 8) {
return 8;
} else if (total_size % 4 == 0 || min_align == 4) {
return 4;
} else if (total_size % 2 == 0 || min_align == 2) {
return 2;
} else {
return 1;
}
}
template<typename T, size_t N>
using vector_storage = aligned_array<T, N, compute_max_alignment(sizeof(T) * N, alignof(T))>;
template<size_t... Ns>
struct extent;
template<size_t N>
struct extent<N> {
static constexpr size_t number_of_elements = N;
};
template<typename E>
static constexpr size_t extent_size = E::number_of_elements;
namespace detail {
// Indicates that elements of type `T` offer less precision than floats, thus operations
// on elements of type `T` can be performed by upcasting them to ` float`.
template<typename T>
struct allow_float_fallback {
static constexpr bool value = false;
};
template<>
struct allow_float_fallback<float> {
static constexpr bool value = true;
};
} // namespace detail
template<typename T>
struct into_vector_impl {
using value_type = T;
using extent_type = extent<1>;
KERNEL_FLOAT_INLINE
static vector_storage<T, 1> call(const T& input) {
return vector_storage<T, 1> {input};
}
};
template<typename T, size_t N>
struct into_vector_impl<T[N]> {
using value_type = T;
using extent_type = extent<N>;
KERNEL_FLOAT_INLINE
static vector_storage<T, N> call(const T (&input)[N]) {
return call(input, make_index_sequence<N>());
}
private:
template<size_t... Is>
KERNEL_FLOAT_INLINE static vector_storage<T, N>
call(const T (&input)[N], index_sequence<Is...>) {
return {input[Is]...};
}
};
template<typename V>
struct into_vector_impl<const V>: into_vector_impl<V> {};
template<typename V>
struct into_vector_impl<V&>: into_vector_impl<V> {};
template<typename V>
struct into_vector_impl<const V&>: into_vector_impl<V> {};
template<typename V>
struct into_vector_impl<V&&>: into_vector_impl<V> {};
template<typename T, size_t N, size_t A>
struct into_vector_impl<aligned_array<T, N, A>> {
using value_type = T;
using extent_type = extent<N>;
KERNEL_FLOAT_INLINE
static vector_storage<T, N> call(const aligned_array<T, N, A>& input) {
return input;
}
};
#define KERNEL_FLOAT_DEFINE_VECTOR_TYPE(T, T1, T2, T3, T4) \
template<> \
struct into_vector_impl<::T1> { \
using value_type = T; \
using extent_type = extent<1>; \
\
KERNEL_FLOAT_INLINE \
static vector_storage<T, 1> call(::T1 v) { \
return {v.x}; \
} \
}; \
\
template<> \
struct into_vector_impl<::T2> { \
using value_type = T; \
using extent_type = extent<2>; \
\
KERNEL_FLOAT_INLINE \
static vector_storage<T, 2> call(::T2 v) { \
return {v.x, v.y}; \
} \
}; \
\
template<> \
struct into_vector_impl<::T3> { \
using value_type = T; \
using extent_type = extent<3>; \
\
KERNEL_FLOAT_INLINE \
static vector_storage<T, 3> call(::T3 v) { \
return {v.x, v.y, v.z}; \
} \
}; \
\
template<> \
struct into_vector_impl<::T4> { \
using value_type = T; \
using extent_type = extent<4>; \
\
KERNEL_FLOAT_INLINE \
static vector_storage<T, 4> call(::T4 v) { \
return {v.x, v.y, v.z, v.w}; \
} \
};
KERNEL_FLOAT_DEFINE_VECTOR_TYPE(char, char1, char2, char3, char4)
KERNEL_FLOAT_DEFINE_VECTOR_TYPE(short, short1, short2, short3, short4)
KERNEL_FLOAT_DEFINE_VECTOR_TYPE(int, int1, int2, int3, int4)
KERNEL_FLOAT_DEFINE_VECTOR_TYPE(long, long1, long2, long3, long4)
KERNEL_FLOAT_DEFINE_VECTOR_TYPE(long long, longlong1, longlong2, longlong3, longlong4)
KERNEL_FLOAT_DEFINE_VECTOR_TYPE(unsigned char, uchar1, uchar2, uchar3, uchar4)
KERNEL_FLOAT_DEFINE_VECTOR_TYPE(unsigned short, ushort1, ushort2, ushort3, ushort4)
KERNEL_FLOAT_DEFINE_VECTOR_TYPE(unsigned int, uint1, uint2, uint3, uint4)
KERNEL_FLOAT_DEFINE_VECTOR_TYPE(unsigned long, ulong1, ulong2, ulong3, ulong4)
KERNEL_FLOAT_DEFINE_VECTOR_TYPE(unsigned long long, ulonglong1, ulonglong2, ulonglong3, ulonglong4)
KERNEL_FLOAT_DEFINE_VECTOR_TYPE(float, float1, float2, float3, float4)
KERNEL_FLOAT_DEFINE_VECTOR_TYPE(double, double1, double2, double3, double4)
template<typename T, typename E, typename S = vector_storage<T, E::number_of_elements>>
struct vector;
template<typename T, typename E, typename S>
struct into_vector_impl<vector<T, E, S>> {
using value_type = T;
using extent_type = E;
KERNEL_FLOAT_INLINE
static vector_storage<T, extent_size<E>> call(const vector<T, E, S>& input) {
return input.storage();
}
};
template<typename T>
struct preferred_vector_size {
static constexpr size_t value = 1;
};
template<typename V>
struct is_vector_impl {
static constexpr bool value = false;
};
template<typename V>
struct is_vector_impl<V&>: is_vector_impl<V> {};
template<typename T, typename E, typename S>
struct is_vector_impl<vector<T, E, S>> {
static constexpr bool value = true;
};
template<typename V>
using vector_value_type = typename into_vector_impl<V>::value_type;
template<typename V>
using vector_extent_type = typename into_vector_impl<V>::extent_type;
template<typename V>
static constexpr size_t vector_size = extent_size<vector_extent_type<V>>;
template<typename V>
static constexpr bool is_vector = is_vector_impl<V>::value;
template<typename V>
using into_vector_type = vector<vector_value_type<V>, vector_extent_type<V>>;
template<typename V>
using vector_storage_type = vector_storage<vector_value_type<V>, vector_size<V>>;
template<typename... Vs>
using promoted_vector_value_type = promote_t<vector_value_type<Vs>...>;
template<typename V>
KERNEL_FLOAT_INLINE vector_storage_type<V> into_vector_storage(V&& input) {
return into_vector_impl<V>::call(static_cast<V&&>(input));
}
} // namespace kernel_float
#endif
#ifndef KERNEL_FLOAT_APPLY_H
#define KERNEL_FLOAT_APPLY_H
namespace kernel_float {
namespace detail {
template<typename... Es>
struct broadcast_extent_impl;
template<typename E>
struct broadcast_extent_impl<E> {
using type = E;
};
template<size_t N>
struct broadcast_extent_impl<extent<N>, extent<N>> {
using type = extent<N>;
};
template<size_t N>
struct broadcast_extent_impl<extent<1>, extent<N>> {
using type = extent<N>;
};
template<size_t N>
struct broadcast_extent_impl<extent<N>, extent<1>> {
using type = extent<N>;
};
template<>
struct broadcast_extent_impl<extent<1>, extent<1>> {
using type = extent<1>;
};
template<typename A, typename B, typename C, typename... Rest>
struct broadcast_extent_impl<A, B, C, Rest...>:
broadcast_extent_impl<typename broadcast_extent_impl<A, B>::type, C, Rest...> {};
} // namespace detail
template<typename... Es>
using broadcast_extent = typename detail::broadcast_extent_impl<Es...>::type;
template<typename... Vs>
using broadcast_vector_extent_type = broadcast_extent<vector_extent_type<Vs>...>;
template<typename From, typename To>
static constexpr bool is_broadcastable = is_same_type<broadcast_extent<From, To>, To>;
template<typename V, typename To>
static constexpr bool is_vector_broadcastable = is_broadcastable<vector_extent_type<V>, To>;
namespace detail {
template<typename T, typename From, typename To>
struct broadcast_impl;
template<typename T, size_t N>
struct broadcast_impl<T, extent<1>, extent<N>> {
KERNEL_FLOAT_INLINE static vector_storage<T, N> call(const vector_storage<T, 1>& input) {
vector_storage<T, N> output;
for (size_t i = 0; i < N; i++) {
output.data()[i] = input.data()[0];
}
return output;
}
};
template<typename T, size_t N>
struct broadcast_impl<T, extent<N>, extent<N>> {
KERNEL_FLOAT_INLINE static vector_storage<T, N> call(vector_storage<T, N> input) {
return input;
}
};
template<typename T>
struct broadcast_impl<T, extent<1>, extent<1>> {
KERNEL_FLOAT_INLINE static vector_storage<T, 1> call(vector_storage<T, 1> input) {
return input;
}
};
} // namespace detail
/**
* Takes the given vector `input` and extends its size to a length of `N`. This is only valid if the size of `input`
* is 1 or `N`.
*
* Example
* =======
* ```
* vec<float, 1> a = {1.0f};
* vec<float, 5> x = broadcast<5>(a); // Returns [1.0f, 1.0f, 1.0f, 1.0f, 1.0f]
*
* vec<float, 5> b = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f};
* vec<float, 5> y = broadcast<5>(b); // Returns [1.0f, 2.0f, 3.0f, 4.0f, 5.0f]
* ```
*/
template<size_t N, typename V>
KERNEL_FLOAT_INLINE vector<vector_value_type<V>, extent<N>>
broadcast(const V& input, extent<N> new_size = {}) {
using T = vector_value_type<V>;
return detail::broadcast_impl<T, vector_extent_type<V>, extent<N>>::call(
into_vector_storage(input));
}
/**
* Takes the given vector `input` and extends its size to the same length as vector `other`. This is only valid if the
* size of `input` is 1 or the same as `other`.
*/
template<typename V, typename R>
KERNEL_FLOAT_INLINE vector<vector_value_type<V>, vector_extent_type<R>>
broadcast_like(const V& input, const R& other) {
return broadcast(input, vector_extent_type<R> {});
}
namespace detail {
template<typename F, typename... Args>
struct invoke_impl {
KERNEL_FLOAT_INLINE static decltype(auto) call(F fun, Args... args) {
return static_cast<F&&>(fun)(static_cast<Args&&>(args)...);
}
};
} // namespace detail
template<typename F, typename... Args>
using result_t = decltype(detail::invoke_impl<decay_t<F>, decay_t<Args>...>::call(
detail::declval<F>(),
detail::declval<Args>()...));
/**
* Invoke the given function `fun` with the arguments `args...`.
*
* The main difference between directly calling `fun(args...)`, is that the behavior can be overridden by
* specializing on `detail::invoke_impl<F, Args...>`.
*
* @return The result of `fun(args...)`.
*/
template<typename F, typename... Args>
KERNEL_FLOAT_INLINE result_t<F, Args...> invoke(F fun, const Args&... args) {
return detail::invoke_impl<decay_t<F>, decay_t<Args>...>::call(
static_cast<F&&>(fun),
static_cast<Args&&>(args)...);
}
/**
* The accurate_policy is designed for computations where maximum accuracy is essential. This policy ensures that all
* operations are performed without any approximations or optimizations that could potentially alter the precise
* outcome of the computations
*/
struct accurate_policy {};
/**
* The fast_policy is intended for scenarios where performance and execution speed are more critical than achieving
* the utmost accuracy. This policy leverages optimizations to accelerate computations, which may involve
* approximations that slightly compromise precision.
*/
struct fast_policy {
using fallback_policy = accurate_policy;
};
/**
* This template policy allows developers to specify a custom degree of approximation for their computations. By
* adjusting the `Level` parameter, you can fine-tune the balance between accuracy and performance to meet the
* specific needs of your application. Higher values mean more precision.
*/
template<int Level = -1>
struct approx_level_policy {
using fallback_policy = approx_level_policy<>;
};
template<>
struct approx_level_policy<> {
using fallback_policy = fast_policy;
};
/**
* The approximate_policy serves as the default approximation policy, providing a standard level of approximation
* without requiring explicit configuration. It balances accuracy and performance, making it suitable for
* general-purpose use cases where neither extreme precision nor maximum speed is necessary.
*/
using approx_policy = approx_level_policy<>;
/**
* The `default_policy` acts as the standard computation policy. It can be configured externally using the
* `KERNEL_FLOAT_GLOBAL_POLICY` macro. If `KERNEL_FLOAT_GLOBAL_POLICY` is not defined, default to `accurate_policy`.
*/
#if defined(KERNEL_FLOAT_GLOBAL_POLICY)
using default_policy = KERNEL_FLOAT_GLOBAL_POLICY;
#elif defined(KERNEL_FLOAT_POLICY)
using default_policy = KERNEL_FLOAT_POLICY;
#else
using default_policy = accurate_policy;
#endif
namespace detail {
template<typename Policy, typename F, size_t N, typename Output, typename... Args>
struct apply_impl;
template<typename Policy, typename F, size_t N, typename Output, typename... Args>
struct apply_base_impl: apply_impl<typename Policy::fallback_policy, F, N, Output, Args...> {};
template<typename Policy, typename F, size_t N, typename Output, typename... Args>
struct apply_impl: apply_base_impl<Policy, F, N, Output, Args...> {};
// Only for `accurate_policy` do we implement `apply_impl`, the others will fall back to `apply_base_impl`.
template<typename F, size_t N, typename Output, typename... Args>
struct apply_impl<accurate_policy, F, N, Output, Args...> {
KERNEL_FLOAT_INLINE static void call(F fun, Output* output, const Args*... args) {
#pragma unroll
for (size_t i = 0; i < N; i++) {
output[i] = detail::invoke_impl<F, Args...>::call(fun, args[i]...);
}
}
};
template<typename Policy, typename F, size_t N, typename Output, typename... Args>
struct map_impl {
static constexpr size_t packet_size = preferred_vector_size<Output>::value;
static constexpr size_t remainder = N % packet_size;
KERNEL_FLOAT_INLINE static void call(F fun, Output* output, const Args*... args) {
if constexpr (N / packet_size > 0) {
#pragma unroll
for (size_t i = 0; i < N - remainder; i += packet_size) {
apply_impl<Policy, F, packet_size, Output, Args...>::call(
fun,
output + i,
(args + i)...);
}
}
if constexpr (remainder > 0) {
#pragma unroll
for (size_t i = N - remainder; i < N; i++) {
apply_impl<Policy, F, 1, Output, Args...>::call(fun, output + i, (args + i)...);
}
}
}
};
template<typename F, size_t N, typename Output, typename... Args>
using default_map_impl = map_impl<default_policy, F, N, Output, Args...>;
} // namespace detail
template<typename F, typename... Args>
using map_type =
vector<result_t<F, vector_value_type<Args>...>, broadcast_vector_extent_type<Args...>>;
/**
* Apply the function `F` to each element from the vector `input` and return the results as a new vector.
*
* Examples
* ========
* ```
* vec<float, 4> input = {1.0f, 2.0f, 3.0f, 4.0f};
* vec<float, 4> squared = map([](auto x) { return x * x; }, input); // [1.0f, 4.0f, 9.0f, 16.0f]
* ```
*/
template<typename Accuracy = default_policy, typename F, typename... Args>
KERNEL_FLOAT_INLINE map_type<F, Args...> map(F fun, const Args&... args) {
using Output = result_t<F, vector_value_type<Args>...>;
using E = broadcast_vector_extent_type<Args...>;
vector_storage<Output, extent_size<E>> result;
detail::map_impl<Accuracy, F, extent_size<E>, Output, vector_value_type<Args>...>::call(
fun,
result.data(),
(detail::broadcast_impl<vector_value_type<Args>, vector_extent_type<Args>, E>::call(
into_vector_storage(args))
.data())...);
return result;
}
} // namespace kernel_float
#endif // KERNEL_FLOAT_APPLY_H
#ifndef KERNEL_FLOAT_COMPLEX_TYPE_H
#define KERNEL_FLOAT_COMPLEX_TYPE_H
namespace kernel_float {
template<typename T>
struct alignas(2 * alignof(T)) complex_type_storage {
T re;
T im;
};
template<typename T>
struct complex_type: complex_type_storage<T> {
using base_type = complex_type_storage<T>;
template<typename T2>
KERNEL_FLOAT_INLINE complex_type(complex_type<T2> that) : base_type(that.real(), that.imag()) {}
KERNEL_FLOAT_INLINE
complex_type(T real = {}, T imag = {}) : base_type(real, imag) {}
KERNEL_FLOAT_INLINE
T real() const {
return this->re;
}