|
36 | 36 | #define NPY__HWCAP 16 |
37 | 37 | #define NPY__HWCAP2 26 |
38 | 38 |
|
39 | | -// arch/arm/include/uapi/asm/hwcap.h |
40 | | -#define NPY__HWCAP_HALF (1 << 1) |
41 | | -#define NPY__HWCAP_NEON (1 << 12) |
42 | | -#define NPY__HWCAP_VFPv3 (1 << 13) |
43 | | -#define NPY__HWCAP_VFPv4 (1 << 16) |
44 | | -#define NPY__HWCAP2_AES (1 << 0) |
45 | | -#define NPY__HWCAP2_PMULL (1 << 1) |
46 | | -#define NPY__HWCAP2_SHA1 (1 << 2) |
47 | | -#define NPY__HWCAP2_SHA2 (1 << 3) |
48 | | -#define NPY__HWCAP2_CRC32 (1 << 4) |
49 | | -// arch/arm64/include/uapi/asm/hwcap.h |
50 | | -#define NPY__HWCAP_FP (1 << 0) |
51 | | -#define NPY__HWCAP_ASIMD (1 << 1) |
52 | | -#define NPY__HWCAP_FPHP (1 << 9) |
53 | | -#define NPY__HWCAP_ASIMDHP (1 << 10) |
54 | | -#define NPY__HWCAP_ASIMDDP (1 << 20) |
55 | | -#define NPY__HWCAP_SVE (1 << 22) |
56 | | -#define NPY__HWCAP_ASIMDFHM (1 << 23) |
57 | | -/* |
| 39 | +#ifdef __arm__ |
| 40 | + // arch/arm/include/uapi/asm/hwcap.h |
| 41 | + #define NPY__HWCAP_HALF (1 << 1) |
| 42 | + #define NPY__HWCAP_NEON (1 << 12) |
| 43 | + #define NPY__HWCAP_VFPv3 (1 << 13) |
| 44 | + #define NPY__HWCAP_VFPv4 (1 << 16) |
| 45 | + |
| 46 | + #define NPY__HWCAP_FPHP (1 << 22) |
| 47 | + #define NPY__HWCAP_ASIMDHP (1 << 23) |
| 48 | + #define NPY__HWCAP_ASIMDDP (1 << 24) |
| 49 | + #define NPY__HWCAP_ASIMDFHM (1 << 25) |
| 50 | + |
| 51 | + #define NPY__HWCAP2_AES (1 << 0) |
| 52 | + #define NPY__HWCAP2_PMULL (1 << 1) |
| 53 | + #define NPY__HWCAP2_SHA1 (1 << 2) |
| 54 | + #define NPY__HWCAP2_SHA2 (1 << 3) |
| 55 | + #define NPY__HWCAP2_CRC32 (1 << 4) |
| 56 | +#else |
| 57 | + // arch/arm64/include/uapi/asm/hwcap.h |
| 58 | + #define NPY__HWCAP_FP (1 << 0) |
| 59 | + #define NPY__HWCAP_ASIMD (1 << 1) |
| 60 | + |
| 61 | + #define NPY__HWCAP_FPHP (1 << 9) |
| 62 | + #define NPY__HWCAP_ASIMDHP (1 << 10) |
| 63 | + #define NPY__HWCAP_ASIMDDP (1 << 20) |
| 64 | + #define NPY__HWCAP_ASIMDFHM (1 << 23) |
| 65 | + |
| 66 | + #define NPY__HWCAP_AES (1 << 3) |
| 67 | + #define NPY__HWCAP_PMULL (1 << 4) |
| 68 | + #define NPY__HWCAP_SHA1 (1 << 5) |
| 69 | + #define NPY__HWCAP_SHA2 (1 << 6) |
| 70 | + #define NPY__HWCAP_CRC32 (1 << 7) |
| 71 | + #define NPY__HWCAP_SVE (1 << 22) |
| 72 | +#endif |
| 73 | + |
| 74 | + |
| 75 | +/* |
58 | 76 | * Get the size of a file by reading it until the end. This is needed |
59 | 77 | * because files under /proc do not always return a valid size when |
60 | 78 | * using fseek(0, SEEK_END) + ftell(). Nor can they be mmap()-ed. |
@@ -87,7 +105,7 @@ get_file_size(const char* pathname) |
87 | 105 | return result; |
88 | 106 | } |
89 | 107 |
|
90 | | -/* |
| 108 | +/* |
91 | 109 | * Read the content of /proc/cpuinfo into a user-provided buffer. |
92 | 110 | * Return the length of the data, or -1 on error. Does *not* |
93 | 111 | * zero-terminate the content. Will not read more |
@@ -123,7 +141,7 @@ read_file(const char* pathname, char* buffer, size_t buffsize) |
123 | 141 | return count; |
124 | 142 | } |
125 | 143 |
|
126 | | -/* |
| 144 | +/* |
127 | 145 | * Extract the content of a the first occurrence of a given field in |
128 | 146 | * the content of /proc/cpuinfo and return it as a heap-allocated |
129 | 147 | * string that must be freed by the caller. |
@@ -182,7 +200,7 @@ extract_cpuinfo_field(const char* buffer, int buflen, const char* field) |
182 | 200 | return result; |
183 | 201 | } |
184 | 202 |
|
185 | | -/* |
| 203 | +/* |
186 | 204 | * Checks that a space-separated list of items contains one given 'item'. |
187 | 205 | * Returns 1 if found, 0 otherwise. |
188 | 206 | */ |
@@ -220,44 +238,51 @@ has_list_item(const char* list, const char* item) |
220 | 238 | return 0; |
221 | 239 | } |
222 | 240 |
|
223 | | -static void setHwcap(char* cpuFeatures, unsigned long* hwcap) { |
224 | | - *hwcap |= has_list_item(cpuFeatures, "neon") ? NPY__HWCAP_NEON : 0; |
225 | | - *hwcap |= has_list_item(cpuFeatures, "half") ? NPY__HWCAP_HALF : 0; |
226 | | - *hwcap |= has_list_item(cpuFeatures, "vfpv3") ? NPY__HWCAP_VFPv3 : 0; |
227 | | - *hwcap |= has_list_item(cpuFeatures, "vfpv4") ? NPY__HWCAP_VFPv4 : 0; |
228 | | - |
229 | | - *hwcap |= has_list_item(cpuFeatures, "asimd") ? NPY__HWCAP_ASIMD : 0; |
230 | | - *hwcap |= has_list_item(cpuFeatures, "fp") ? NPY__HWCAP_FP : 0; |
231 | | - *hwcap |= has_list_item(cpuFeatures, "fphp") ? NPY__HWCAP_FPHP : 0; |
232 | | - *hwcap |= has_list_item(cpuFeatures, "asimdhp") ? NPY__HWCAP_ASIMDHP : 0; |
233 | | - *hwcap |= has_list_item(cpuFeatures, "asimddp") ? NPY__HWCAP_ASIMDDP : 0; |
234 | | - *hwcap |= has_list_item(cpuFeatures, "asimdfhm") ? NPY__HWCAP_ASIMDFHM : 0; |
235 | | -} |
236 | | - |
237 | 241 | static int |
238 | 242 | get_feature_from_proc_cpuinfo(unsigned long *hwcap, unsigned long *hwcap2) { |
239 | | - char* cpuinfo = NULL; |
240 | | - int cpuinfo_len; |
241 | | - cpuinfo_len = get_file_size("/proc/cpuinfo"); |
| 243 | + *hwcap = 0; |
| 244 | + *hwcap2 = 0; |
| 245 | + |
| 246 | + int cpuinfo_len = get_file_size("/proc/cpuinfo"); |
242 | 247 | if (cpuinfo_len < 0) { |
243 | 248 | return 0; |
244 | 249 | } |
245 | | - cpuinfo = malloc(cpuinfo_len); |
| 250 | + char *cpuinfo = malloc(cpuinfo_len); |
246 | 251 | if (cpuinfo == NULL) { |
247 | 252 | return 0; |
248 | 253 | } |
| 254 | + |
249 | 255 | cpuinfo_len = read_file("/proc/cpuinfo", cpuinfo, cpuinfo_len); |
250 | | - char* cpuFeatures = extract_cpuinfo_field(cpuinfo, cpuinfo_len, "Features"); |
251 | | - if(cpuFeatures == NULL) { |
| 256 | + char *cpuFeatures = extract_cpuinfo_field(cpuinfo, cpuinfo_len, "Features"); |
| 257 | + if (cpuFeatures == NULL) { |
| 258 | + free(cpuinfo); |
252 | 259 | return 0; |
253 | 260 | } |
254 | | - setHwcap(cpuFeatures, hwcap); |
255 | | - *hwcap2 |= *hwcap; |
| 261 | + *hwcap |= has_list_item(cpuFeatures, "fphp") ? NPY__HWCAP_FPHP : 0; |
| 262 | + *hwcap |= has_list_item(cpuFeatures, "asimdhp") ? NPY__HWCAP_ASIMDHP : 0; |
| 263 | + *hwcap |= has_list_item(cpuFeatures, "asimddp") ? NPY__HWCAP_ASIMDDP : 0; |
| 264 | + *hwcap |= has_list_item(cpuFeatures, "asimdfhm") ? NPY__HWCAP_ASIMDFHM : 0; |
| 265 | +#ifdef __arm__ |
| 266 | + *hwcap |= has_list_item(cpuFeatures, "neon") ? NPY__HWCAP_NEON : 0; |
| 267 | + *hwcap |= has_list_item(cpuFeatures, "half") ? NPY__HWCAP_HALF : 0; |
| 268 | + *hwcap |= has_list_item(cpuFeatures, "vfpv3") ? NPY__HWCAP_VFPv3 : 0; |
| 269 | + *hwcap |= has_list_item(cpuFeatures, "vfpv4") ? NPY__HWCAP_VFPv4 : 0; |
256 | 270 | *hwcap2 |= has_list_item(cpuFeatures, "aes") ? NPY__HWCAP2_AES : 0; |
257 | 271 | *hwcap2 |= has_list_item(cpuFeatures, "pmull") ? NPY__HWCAP2_PMULL : 0; |
258 | 272 | *hwcap2 |= has_list_item(cpuFeatures, "sha1") ? NPY__HWCAP2_SHA1 : 0; |
259 | 273 | *hwcap2 |= has_list_item(cpuFeatures, "sha2") ? NPY__HWCAP2_SHA2 : 0; |
260 | 274 | *hwcap2 |= has_list_item(cpuFeatures, "crc32") ? NPY__HWCAP2_CRC32 : 0; |
| 275 | +#else |
| 276 | + *hwcap |= has_list_item(cpuFeatures, "asimd") ? NPY__HWCAP_ASIMD : 0; |
| 277 | + *hwcap |= has_list_item(cpuFeatures, "fp") ? NPY__HWCAP_FP : 0; |
| 278 | + *hwcap |= has_list_item(cpuFeatures, "aes") ? NPY__HWCAP_AES : 0; |
| 279 | + *hwcap |= has_list_item(cpuFeatures, "pmull") ? NPY__HWCAP_PMULL : 0; |
| 280 | + *hwcap |= has_list_item(cpuFeatures, "sha1") ? NPY__HWCAP_SHA1 : 0; |
| 281 | + *hwcap |= has_list_item(cpuFeatures, "sha2") ? NPY__HWCAP_SHA2 : 0; |
| 282 | + *hwcap |= has_list_item(cpuFeatures, "crc32") ? NPY__HWCAP_CRC32 : 0; |
| 283 | +#endif |
| 284 | + free(cpuinfo); |
| 285 | + free(cpuFeatures); |
261 | 286 | return 1; |
262 | 287 | } |
263 | 288 | #endif /* NUMPY_CORE_SRC_COMMON_NPY_CPUINFO_PARSER_H_ */ |
0 commit comments