/home/runner/work/LabNeura/LabNeura/src/labneura/backends/common/backend_selection.h
Line | Count | Source (jump to first uncovered line) |
1 | | #pragma once |
2 | | |
3 | | #include <stdexcept> |
4 | | #include <string> |
5 | | |
6 | | namespace labneura::detail { |
7 | | |
8 | | enum class BackendKind { |
9 | | GENERIC, |
10 | | AVX2, |
11 | | NEON, |
12 | | }; |
13 | | |
14 | | inline BackendKind select_backend(const char* requested, |
15 | | bool avx2_compiled, |
16 | | bool avx2_supported, |
17 | | bool neon_compiled, |
18 | 4.08k | bool neon_supported) { |
19 | 4.08k | if (requested != nullptr) { |
20 | 1.92k | const std::string name(requested); |
21 | 1.92k | if (name == "GENERIC") { |
22 | 1.06k | return BackendKind::GENERIC; |
23 | 1.06k | } |
24 | 864 | if (name == "AVX2") { |
25 | 803 | if (!avx2_compiled) { |
26 | 0 | throw std::invalid_argument( |
27 | 0 | "Requested backend AVX2 is not available in this build"); |
28 | 0 | } |
29 | 803 | if (!avx2_supported) { |
30 | 0 | throw std::invalid_argument( |
31 | 0 | "Requested backend AVX2 is not supported by this CPU"); |
32 | 0 | } |
33 | 803 | return BackendKind::AVX2; |
34 | 803 | } |
35 | 61 | if (name == "NEON") { |
36 | 61 | if (!neon_compiled) { |
37 | 61 | throw std::invalid_argument( |
38 | 61 | "Requested backend NEON is not available in this build"); |
39 | 61 | } |
40 | 0 | if (!neon_supported) { |
41 | 0 | throw std::invalid_argument( |
42 | 0 | "Requested backend NEON is not supported by this platform"); |
43 | 0 | } |
44 | 0 | return BackendKind::NEON; |
45 | 0 | } |
46 | 0 | throw std::invalid_argument( |
47 | 0 | "Invalid LABNEURA_BACKEND value: " + name + |
48 | 0 | " (expected GENERIC, AVX2, or NEON)"); |
49 | 61 | } |
50 | | |
51 | 2.15k | if (avx2_compiled && avx2_supported) { |
52 | 2.15k | return BackendKind::AVX2; |
53 | 2.15k | } |
54 | 0 | if (neon_compiled && neon_supported) { |
55 | 0 | return BackendKind::NEON; |
56 | 0 | } |
57 | 0 | return BackendKind::GENERIC; |
58 | 0 | } |
59 | | |
60 | 702 | inline const char* backend_name(BackendKind backend) { |
61 | 702 | switch (backend) { |
62 | 295 | case BackendKind::GENERIC: return "GENERIC"; |
63 | 407 | case BackendKind::AVX2: return "AVX2"; |
64 | 0 | case BackendKind::NEON: return "NEON"; |
65 | 702 | } |
66 | 0 | throw std::logic_error("Unknown backend kind"); |
67 | 702 | } |
68 | | |
69 | | } // namespace labneura::detail |