1# include "ggml-backend-impl.h"
 2
 3#if defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__)
 4
 5#if defined(__linux__)
 6#include <sys/auxv.h>
 7#endif
 8
 9#include <string>
10
11struct powerpc_features {
12    std::string platform = "";
13    int power_version    = -1;
14
15    bool has_vsx         = false;
16
17    powerpc_features() {
18#if defined(__linux__)
19        unsigned long auxval = getauxval(AT_PLATFORM);
20        if (auxval) {
21            platform = std::string(reinterpret_cast<const char*>(auxval));
22            // TBD: Do systems exist that return this in uppercase?
23            if (platform.substr(0, 5) == "power") {
24                // Extractt a numeric suffix, if one exists
25                int vpos = -1;
26                for (int i = platform.length() - 1; i >= 0; i--) {
27                    if (std::isdigit(platform[i])) {
28                        vpos = i;
29                    } else {
30                        break;
31                    }
32                }
33                if (vpos > -1) {
34                    power_version = std::stoi(platform.substr(vpos));
35                }
36            }
37        }
38#endif
39        if (power_version >= 9) {
40            has_vsx = true;
41        }
42    }
43};
44
45static int ggml_backend_cpu_powerpc_score() {
46    int score = 1;
47    powerpc_features pf;
48
49// Platform scores
50#if defined(GGML_USE_POWER7)
51    if (pf.power_version < 7) { return 0; }
52    score += 1<<1;
53#endif
54#if defined(GGML_USE_POWER8)
55    if (pf.power_version < 8) { return 0; }
56    score += 1<<2;
57#endif
58#if defined(GGML_USE_POWER9)
59    if (pf.power_version < 9) { return 0; }
60    score += 1<<3;
61#endif
62#if defined(GGML_USE_POWER10)
63    if (pf.power_version < 10) { return 0; }
64    score += 1<<4;
65#endif
66#if defined(GGML_USE_POWER11)
67    if (pf.power_version < 11) { return 0; }
68    score += 1<<5;
69#endif
70
71// Feature scores
72#if defined(GGML_USE_VSX)
73    if (!pf.has_vsx) { return 0; }
74    score += 1<<6;
75#endif
76
77    return score;
78}
79
80GGML_BACKEND_DL_SCORE_IMPL(ggml_backend_cpu_powerpc_score)
81
82#endif // defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__)