|
85 | 85 | #endif
|
86 | 86 | #endif
|
87 | 87 |
|
| 88 | +#if defined (__FreeBSD__) |
| 89 | +#include <sys/param.h> |
| 90 | +#endif |
| 91 | + |
88 | 92 | #if defined(__ANDROID__) && defined(__arm__) && !defined(HAVE_GETAUXVAL)
|
89 | 93 | #include <cpu-features.h>
|
90 | 94 | #endif
|
@@ -891,21 +895,42 @@ static const char *SDL_GetCPUName(void)
|
891 | 895 | int SDL_GetCPUCacheLineSize(void)
|
892 | 896 | {
|
893 | 897 | const char *cpuType = SDL_GetCPUType();
|
| 898 | + int cacheline_size = SDL_CACHELINE_SIZE; /* initial guess */ |
894 | 899 | int a, b, c, d;
|
895 | 900 | (void)a;
|
896 | 901 | (void)b;
|
897 | 902 | (void)c;
|
898 | 903 | (void)d;
|
899 | 904 | if (SDL_strcmp(cpuType, "GenuineIntel") == 0 || SDL_strcmp(cpuType, "CentaurHauls") == 0 || SDL_strcmp(cpuType, " Shanghai ") == 0) {
|
900 | 905 | cpuid(0x00000001, a, b, c, d);
|
901 |
| - return ((b >> 8) & 0xff) * 8; |
| 906 | + cacheline_size = ((b >> 8) & 0xff) * 8; |
902 | 907 | } else if (SDL_strcmp(cpuType, "AuthenticAMD") == 0 || SDL_strcmp(cpuType, "HygonGenuine") == 0) {
|
903 | 908 | cpuid(0x80000005, a, b, c, d);
|
904 |
| - return c & 0xff; |
| 909 | + cacheline_size = c & 0xff; |
905 | 910 | } else {
|
906 |
| - /* Just make a guess here... */ |
907 |
| - return SDL_CACHELINE_SIZE; |
| 911 | +#if defined(HAVE_SYSCONF) && defined(_SC_LEVEL1_DCACHE_LINESIZE) |
| 912 | + if ((cacheline_size = sysconf(_SC_LEVEL1_DCACHE_LINESIZE)) > 0) { |
| 913 | + return cacheline_size; |
| 914 | + } else { |
| 915 | + cacheline_size = SDL_CACHELINE_SIZE; |
| 916 | + } |
| 917 | +#endif |
| 918 | +#if defined(__LINUX__) |
| 919 | + { |
| 920 | + FILE *f = fopen("/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size", "r"); |
| 921 | + if (f) { |
| 922 | + int size; |
| 923 | + if (fscanf(f, "%d", &size) == 1) { |
| 924 | + cacheline_size = size; |
| 925 | + } |
| 926 | + fclose(f); |
| 927 | + } |
| 928 | + } |
| 929 | +#elif defined(__FreeBSD__) && defined(CACHE_LINE_SIZE) |
| 930 | + cacheline_size = CACHE_LINE_SIZE; |
| 931 | +#endif |
908 | 932 | }
|
| 933 | + return cacheline_size; |
909 | 934 | }
|
910 | 935 |
|
911 | 936 | static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
|
|
0 commit comments