/home/runner/work/LabNeura/LabNeura/src/labneura/backends/cpu/avx2/avx2_internal.h
Line | Count | Source (jump to first uncovered line) |
1 | | #pragma once |
2 | | |
3 | | #if defined(__AVX2__) |
4 | | |
5 | | #include <immintrin.h> |
6 | | #include <algorithm> |
7 | | #include <cstdlib> |
8 | | |
9 | | #if defined(__GNUC__) || defined(__clang__) |
10 | | #include <cpuid.h> |
11 | | #endif |
12 | | |
13 | | namespace labneura { |
14 | | |
15 | | // ======================= |
16 | | // Register detection utility |
17 | | // ======================= |
18 | | |
19 | | // Detect number of available AVX2 (YMM) registers using CPUID |
20 | | // Returns 32 if AVX-512 is supported (extends YMM registers) |
21 | | // Returns 16 for standard x86_64 (default) |
22 | 0 | static inline int detect_avx2_registers() { |
23 | 0 | int num_registers = 16; // Default: standard x86_64 |
24 | 0 |
|
25 | 0 | try { |
26 | 0 | #if defined(__GNUC__) || defined(__clang__) |
27 | 0 | unsigned int eax, ebx, ecx, edx; |
28 | 0 | |
29 | 0 | // Check CPUID support |
30 | 0 | if (__get_cpuid_max(0, nullptr) >= 7) { |
31 | 0 | // CPUID leaf 7, subleaf 0: Extended Features |
32 | 0 | if (__get_cpuid_count(7, 0, &eax, &ebx, &ecx, &edx)) { |
33 | 0 | // Check EBX bit 16: AVX-512F (Foundation) |
34 | 0 | // AVX-512 extends YMM registers from 16 to 32 |
35 | 0 | if (ebx & (1 << 16)) { |
36 | 0 | num_registers = 32; |
37 | 0 | } |
38 | 0 | } |
39 | 0 | } |
40 | 0 | #elif defined(_MSC_VER) |
41 | 0 | int cpuInfo[4]; |
42 | 0 | __cpuidex(cpuInfo, 7, 0); |
43 | 0 | // Check EBX bit 16: AVX-512F |
44 | 0 | if (cpuInfo[1] & (1 << 16)) { |
45 | 0 | num_registers = 32; |
46 | 0 | } |
47 | 0 | #endif |
48 | 0 | } catch (...) { |
49 | 0 | // If any error occurs, use safe default |
50 | 0 | num_registers = 16; |
51 | 0 | } |
52 | 0 |
|
53 | 0 | return num_registers; |
54 | 0 | } Unexecuted instantiation: avx2_backend.cpp:_ZN8labneuraL21detect_avx2_registersEv Unexecuted instantiation: avx2_ops_fp32.cpp:_ZN8labneuraL21detect_avx2_registersEv Unexecuted instantiation: avx2_ops_int8.cpp:_ZN8labneuraL21detect_avx2_registersEv Unexecuted instantiation: avx2_ops_int16.cpp:_ZN8labneuraL21detect_avx2_registersEv |
55 | | |
56 | | // Get optimized unroll factor: half of available registers |
57 | 0 | static inline int get_avx2_unroll_factor() { |
58 | 0 | return detect_avx2_registers() / 2; // 8 registers per operand |
59 | 0 | } Unexecuted instantiation: avx2_backend.cpp:_ZN8labneuraL22get_avx2_unroll_factorEv Unexecuted instantiation: avx2_ops_fp32.cpp:_ZN8labneuraL22get_avx2_unroll_factorEv Unexecuted instantiation: avx2_ops_int8.cpp:_ZN8labneuraL22get_avx2_unroll_factorEv Unexecuted instantiation: avx2_ops_int16.cpp:_ZN8labneuraL22get_avx2_unroll_factorEv |
60 | | |
61 | | // FP32: 8 elements per YMM, unroll_factor=8 → 64 floats per iteration |
62 | 0 | static inline std::size_t get_avx2_fp32_chunk() { |
63 | 0 | return get_avx2_unroll_factor() * 8; |
64 | 0 | } Unexecuted instantiation: avx2_backend.cpp:_ZN8labneuraL19get_avx2_fp32_chunkEv Unexecuted instantiation: avx2_ops_fp32.cpp:_ZN8labneuraL19get_avx2_fp32_chunkEv Unexecuted instantiation: avx2_ops_int8.cpp:_ZN8labneuraL19get_avx2_fp32_chunkEv Unexecuted instantiation: avx2_ops_int16.cpp:_ZN8labneuraL19get_avx2_fp32_chunkEv |
65 | | |
66 | 894 | static inline __m256i divide_int32_avx2(__m256i numerator, __m256i denominator) { |
67 | 894 | const __m256i zero = _mm256_setzero_si256(); |
68 | 894 | const __m256i zero_mask = _mm256_cmpeq_epi32(denominator, zero); |
69 | 894 | const __m256i safe_denominator = _mm256_blendv_epi8( |
70 | 894 | denominator, _mm256_set1_epi32(1), zero_mask); |
71 | 894 | const __m256i abs_numerator = _mm256_abs_epi32(numerator); |
72 | 894 | const __m256i abs_denominator = _mm256_abs_epi32(safe_denominator); |
73 | 894 | __m256i quotient = _mm256_cvttps_epi32(_mm256_div_ps( |
74 | 894 | _mm256_cvtepi32_ps(abs_numerator), |
75 | 894 | _mm256_cvtepi32_ps(abs_denominator))); |
76 | | |
77 | 894 | const __m256i product = _mm256_mullo_epi32(quotient, abs_denominator); |
78 | 894 | const __m256i too_large = _mm256_cmpgt_epi32(product, abs_numerator); |
79 | 894 | quotient = _mm256_sub_epi32( |
80 | 894 | quotient, _mm256_and_si256(too_large, _mm256_set1_epi32(1))); |
81 | | |
82 | 894 | const __m256i next_product = _mm256_mullo_epi32( |
83 | 894 | _mm256_add_epi32(quotient, _mm256_set1_epi32(1)), abs_denominator); |
84 | 894 | const __m256i too_small = _mm256_andnot_si256( |
85 | 894 | _mm256_cmpgt_epi32(next_product, abs_numerator), _mm256_set1_epi32(1)); |
86 | 894 | quotient = _mm256_add_epi32(quotient, too_small); |
87 | | |
88 | 894 | const __m256i negative = _mm256_cmpgt_epi32( |
89 | 894 | zero, _mm256_xor_si256(numerator, denominator)); |
90 | 894 | quotient = _mm256_blendv_epi8( |
91 | 894 | quotient, _mm256_sub_epi32(zero, quotient), negative); |
92 | 894 | return _mm256_andnot_si256(zero_mask, quotient); |
93 | 894 | } Unexecuted instantiation: avx2_backend.cpp:_ZN8labneuraL17divide_int32_avx2EDv4_xS0_ Unexecuted instantiation: avx2_ops_fp32.cpp:_ZN8labneuraL17divide_int32_avx2EDv4_xS0_ avx2_ops_int8.cpp:_ZN8labneuraL17divide_int32_avx2EDv4_xS0_ Line | Count | Source | 66 | 320 | static inline __m256i divide_int32_avx2(__m256i numerator, __m256i denominator) { | 67 | 320 | const __m256i zero = _mm256_setzero_si256(); | 68 | 320 | const __m256i zero_mask = _mm256_cmpeq_epi32(denominator, zero); | 69 | 320 | const __m256i safe_denominator = _mm256_blendv_epi8( | 70 | 320 | denominator, _mm256_set1_epi32(1), zero_mask); | 71 | 320 | const __m256i abs_numerator = _mm256_abs_epi32(numerator); | 72 | 320 | const __m256i abs_denominator = _mm256_abs_epi32(safe_denominator); | 73 | 320 | __m256i quotient = _mm256_cvttps_epi32(_mm256_div_ps( | 74 | 320 | _mm256_cvtepi32_ps(abs_numerator), | 75 | 320 | _mm256_cvtepi32_ps(abs_denominator))); | 76 | | | 77 | 320 | const __m256i product = _mm256_mullo_epi32(quotient, abs_denominator); | 78 | 320 | const __m256i too_large = _mm256_cmpgt_epi32(product, abs_numerator); | 79 | 320 | quotient = _mm256_sub_epi32( | 80 | 320 | quotient, _mm256_and_si256(too_large, _mm256_set1_epi32(1))); | 81 | | | 82 | 320 | const __m256i next_product = _mm256_mullo_epi32( | 83 | 320 | _mm256_add_epi32(quotient, _mm256_set1_epi32(1)), abs_denominator); | 84 | 320 | const __m256i too_small = _mm256_andnot_si256( | 85 | 320 | _mm256_cmpgt_epi32(next_product, abs_numerator), _mm256_set1_epi32(1)); | 86 | 320 | quotient = _mm256_add_epi32(quotient, too_small); | 87 | | | 88 | 320 | const __m256i negative = _mm256_cmpgt_epi32( | 89 | 320 | zero, _mm256_xor_si256(numerator, denominator)); | 90 | 320 | quotient = _mm256_blendv_epi8( | 91 | 320 | quotient, _mm256_sub_epi32(zero, quotient), negative); | 92 | 320 | return _mm256_andnot_si256(zero_mask, quotient); | 93 | 320 | } |
avx2_ops_int16.cpp:_ZN8labneuraL17divide_int32_avx2EDv4_xS0_ Line | Count | Source | 66 | 574 | static inline __m256i divide_int32_avx2(__m256i numerator, __m256i denominator) { | 67 | 574 | const __m256i zero = _mm256_setzero_si256(); | 68 | 574 | const __m256i zero_mask = _mm256_cmpeq_epi32(denominator, zero); | 69 | 574 | const __m256i safe_denominator = _mm256_blendv_epi8( | 70 | 574 | denominator, _mm256_set1_epi32(1), zero_mask); | 71 | 574 | const __m256i abs_numerator = _mm256_abs_epi32(numerator); | 72 | 574 | const __m256i abs_denominator = _mm256_abs_epi32(safe_denominator); | 73 | 574 | __m256i quotient = _mm256_cvttps_epi32(_mm256_div_ps( | 74 | 574 | _mm256_cvtepi32_ps(abs_numerator), | 75 | 574 | _mm256_cvtepi32_ps(abs_denominator))); | 76 | | | 77 | 574 | const __m256i product = _mm256_mullo_epi32(quotient, abs_denominator); | 78 | 574 | const __m256i too_large = _mm256_cmpgt_epi32(product, abs_numerator); | 79 | 574 | quotient = _mm256_sub_epi32( | 80 | 574 | quotient, _mm256_and_si256(too_large, _mm256_set1_epi32(1))); | 81 | | | 82 | 574 | const __m256i next_product = _mm256_mullo_epi32( | 83 | 574 | _mm256_add_epi32(quotient, _mm256_set1_epi32(1)), abs_denominator); | 84 | 574 | const __m256i too_small = _mm256_andnot_si256( | 85 | 574 | _mm256_cmpgt_epi32(next_product, abs_numerator), _mm256_set1_epi32(1)); | 86 | 574 | quotient = _mm256_add_epi32(quotient, too_small); | 87 | | | 88 | 574 | const __m256i negative = _mm256_cmpgt_epi32( | 89 | 574 | zero, _mm256_xor_si256(numerator, denominator)); | 90 | 574 | quotient = _mm256_blendv_epi8( | 91 | 574 | quotient, _mm256_sub_epi32(zero, quotient), negative); | 92 | 574 | return _mm256_andnot_si256(zero_mask, quotient); | 93 | 574 | } |
|
94 | | |
95 | | } // namespace labneura |
96 | | |
97 | | #endif // __AVX2__ |