src/cpuinfo/SDL_cpuinfo.c
author Ben Avison <bavison@riscosopen.org>
Thu, 31 Oct 2019 14:00:28 +0300
branchSDL-1.2
changeset 13212 3d6dc20a0974
parent 6908 8b9fcdd925d8
child 13219 4f88e197acad
permissions -rw-r--r--
ARM: Create configure option --enable-arm-simd to govern assembly optimizations
---
configure.in | 60 +++++++++++++++++++--------------------
include/SDL_config.h.in | 1 +
include/SDL_cpuinfo.h | 3 ++
src/cpuinfo/SDL_cpuinfo.c | 53 ++++++++++++++++++++++++++++++++++
4 files changed, 87 insertions(+), 30 deletions(-)
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2012 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 /* CPU feature detection for SDL */
    25 
    26 #include "SDL.h"
    27 #include "SDL_cpuinfo.h"
    28 
    29 #if defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__))
    30 #include <sys/sysctl.h> /* For AltiVec check */
    31 #elif defined(__OpenBSD__) && defined(__powerpc__)
    32 #include <sys/param.h>
    33 #include <sys/sysctl.h> /* For AltiVec check */
    34 #include <machine/cpu.h>
    35 #elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
    36 #include <signal.h>
    37 #include <setjmp.h>
    38 #endif
    39 
    40 #define CPU_HAS_RDTSC	0x00000001
    41 #define CPU_HAS_MMX	0x00000002
    42 #define CPU_HAS_MMXEXT	0x00000004
    43 #define CPU_HAS_3DNOW	0x00000010
    44 #define CPU_HAS_3DNOWEXT 0x00000020
    45 #define CPU_HAS_SSE	0x00000040
    46 #define CPU_HAS_SSE2	0x00000080
    47 #define CPU_HAS_ALTIVEC	0x00000100
    48 #define CPU_HAS_ARM_SIMD 0x00000200
    49 
    50 #if SDL_ALTIVEC_BLITTERS && HAVE_SETJMP && !__MACOSX__ && !__OpenBSD__
    51 /* This is the brute force way of detecting instruction sets...
    52    the idea is borrowed from the libmpeg2 library - thanks!
    53  */
    54 static jmp_buf jmpbuf;
    55 static void illegal_instruction(int sig)
    56 {
    57 	longjmp(jmpbuf, 1);
    58 }
    59 #endif /* HAVE_SETJMP */
    60 
    61 static __inline__ int CPU_haveCPUID(void)
    62 {
    63 	int has_CPUID = 0;
    64 #if defined(__GNUC__) && defined(i386)
    65 	__asm__ (
    66 "        pushfl                      # Get original EFLAGS             \n"
    67 "        popl    %%eax                                                 \n"
    68 "        movl    %%eax,%%ecx                                           \n"
    69 "        xorl    $0x200000,%%eax     # Flip ID bit in EFLAGS           \n"
    70 "        pushl   %%eax               # Save new EFLAGS value on stack  \n"
    71 "        popfl                       # Replace current EFLAGS value    \n"
    72 "        pushfl                      # Get new EFLAGS                  \n"
    73 "        popl    %%eax               # Store new EFLAGS in EAX         \n"
    74 "        xorl    %%ecx,%%eax         # Can not toggle ID bit,          \n"
    75 "        jz      1f                  # Processor=80486                 \n"
    76 "        movl    $1,%0               # We have CPUID support           \n"
    77 "1:                                                                    \n"
    78 	: "=m" (has_CPUID)
    79 	:
    80 	: "%eax", "%ecx"
    81 	);
    82 #elif defined(__GNUC__) && defined(__x86_64__)
    83 /* Technically, if this is being compiled under __x86_64__ then it has 
    84 CPUid by definition.  But it's nice to be able to prove it.  :)      */
    85 	__asm__ (
    86 "        pushfq                      # Get original EFLAGS             \n"
    87 "        popq    %%rax                                                 \n"
    88 "        movq    %%rax,%%rcx                                           \n"
    89 "        xorl    $0x200000,%%eax     # Flip ID bit in EFLAGS           \n"
    90 "        pushq   %%rax               # Save new EFLAGS value on stack  \n"
    91 "        popfq                       # Replace current EFLAGS value    \n"
    92 "        pushfq                      # Get new EFLAGS                  \n"
    93 "        popq    %%rax               # Store new EFLAGS in EAX         \n"
    94 "        xorl    %%ecx,%%eax         # Can not toggle ID bit,          \n"
    95 "        jz      1f                  # Processor=80486                 \n"
    96 "        movl    $1,%0               # We have CPUID support           \n"
    97 "1:                                                                    \n"
    98 	: "=m" (has_CPUID)
    99 	:
   100 	: "%rax", "%rcx"
   101 	);
   102 #elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
   103 	__asm {
   104         pushfd                      ; Get original EFLAGS
   105         pop     eax
   106         mov     ecx, eax
   107         xor     eax, 200000h        ; Flip ID bit in EFLAGS
   108         push    eax                 ; Save new EFLAGS value on stack
   109         popfd                       ; Replace current EFLAGS value
   110         pushfd                      ; Get new EFLAGS
   111         pop     eax                 ; Store new EFLAGS in EAX
   112         xor     eax, ecx            ; Can not toggle ID bit,
   113         jz      done                ; Processor=80486
   114         mov     has_CPUID,1         ; We have CPUID support
   115 done:
   116 	}
   117 #elif defined(__sun) && defined(__i386)
   118 	__asm (
   119 "       pushfl                 \n"
   120 "	popl    %eax           \n"
   121 "	movl    %eax,%ecx      \n"
   122 "	xorl    $0x200000,%eax \n"
   123 "	pushl   %eax           \n"
   124 "	popfl                  \n"
   125 "	pushfl                 \n"
   126 "	popl    %eax           \n"
   127 "	xorl    %ecx,%eax      \n"
   128 "	jz      1f             \n"
   129 "	movl    $1,-8(%ebp)    \n"
   130 "1:                            \n"
   131 	);
   132 #elif defined(__sun) && defined(__amd64)
   133 	__asm (
   134 "       pushfq                 \n"
   135 "       popq    %rax           \n"
   136 "       movq    %rax,%rcx      \n"
   137 "       xorl    $0x200000,%eax \n"
   138 "       pushq   %rax           \n"
   139 "       popfq                  \n"
   140 "       pushfq                 \n"
   141 "       popq    %rax           \n"
   142 "       xorl    %ecx,%eax      \n"
   143 "       jz      1f             \n"
   144 "       movl    $1,-8(%rbp)    \n"
   145 "1:                            \n"
   146 	);
   147 #endif
   148 	return has_CPUID;
   149 }
   150 
   151 static __inline__ int CPU_getCPUIDFeatures(void)
   152 {
   153 	int features = 0;
   154 #if defined(__GNUC__) && defined(i386)
   155 	__asm__ (
   156 "        xorl    %%eax,%%eax         # Set up for CPUID instruction    \n"
   157 "        pushl   %%ebx                                                 \n"
   158 "        cpuid                       # Get and save vendor ID          \n"
   159 "        popl    %%ebx                                                 \n"
   160 "        cmpl    $1,%%eax            # Make sure 1 is valid input for CPUID\n"
   161 "        jl      1f                  # We dont have the CPUID instruction\n"
   162 "        xorl    %%eax,%%eax                                           \n"
   163 "        incl    %%eax                                                 \n"
   164 "        pushl   %%ebx                                                 \n"
   165 "        cpuid                       # Get family/model/stepping/features\n"
   166 "        popl    %%ebx                                                 \n"
   167 "        movl    %%edx,%0                                              \n"
   168 "1:                                                                    \n"
   169 	: "=m" (features)
   170 	:
   171 	: "%eax", "%ecx", "%edx"
   172 	);
   173 #elif defined(__GNUC__) && defined(__x86_64__)
   174 	__asm__ (
   175 "        xorl    %%eax,%%eax         # Set up for CPUID instruction    \n"
   176 "        pushq   %%rbx                                                 \n"
   177 "        cpuid                       # Get and save vendor ID          \n"
   178 "        popq    %%rbx                                                 \n"
   179 "        cmpl    $1,%%eax            # Make sure 1 is valid input for CPUID\n"
   180 "        jl      1f                  # We dont have the CPUID instruction\n"
   181 "        xorl    %%eax,%%eax                                           \n"
   182 "        incl    %%eax                                                 \n"
   183 "        pushq   %%rbx                                                 \n"
   184 "        cpuid                       # Get family/model/stepping/features\n"
   185 "        popq    %%rbx                                                 \n"
   186 "        movl    %%edx,%0                                              \n"
   187 "1:                                                                    \n"
   188 	: "=m" (features)
   189 	:
   190 	: "%rax", "%rcx", "%rdx"
   191 	);
   192 #elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
   193 	__asm {
   194         xor     eax, eax            ; Set up for CPUID instruction
   195         push    ebx
   196         cpuid                       ; Get and save vendor ID
   197         pop     ebx
   198         cmp     eax, 1              ; Make sure 1 is valid input for CPUID
   199         jl      done                ; We dont have the CPUID instruction
   200         xor     eax, eax
   201         inc     eax
   202         push    ebx
   203         cpuid                       ; Get family/model/stepping/features
   204         pop     ebx
   205         mov     features, edx
   206 done:
   207 	}
   208 #elif defined(__sun) && (defined(__i386) || defined(__amd64))
   209 	    __asm(
   210 "        xorl    %eax,%eax         \n"
   211 "        pushl   %ebx              \n"
   212 "        cpuid                     \n"
   213 "        popl    %ebx              \n"
   214 "        cmpl    $1,%eax           \n"
   215 "        jl      1f                \n"
   216 "        xorl    %eax,%eax         \n"
   217 "        incl    %eax              \n"
   218 "        pushl   %ebx              \n"
   219 "        cpuid                     \n"
   220 "        popl    %ebx              \n"
   221 #ifdef __i386
   222 "        movl    %edx,-8(%ebp)     \n"
   223 #else
   224 "        movl    %edx,-8(%rbp)     \n"
   225 #endif
   226 "1:                                \n"
   227 	    );
   228 #endif
   229 	return features;
   230 }
   231 
   232 static __inline__ int CPU_getCPUIDFeaturesExt(void)
   233 {
   234 	int features = 0;
   235 #if defined(__GNUC__) && defined(i386)
   236 	__asm__ (
   237 "        movl    $0x80000000,%%eax   # Query for extended functions    \n"
   238 "        pushl   %%ebx                                                 \n"
   239 "        cpuid                       # Get extended function limit     \n"
   240 "        popl    %%ebx                                                 \n"
   241 "        cmpl    $0x80000001,%%eax                                     \n"
   242 "        jl      1f                  # Nope, we dont have function 800000001h\n"
   243 "        movl    $0x80000001,%%eax   # Setup extended function 800000001h\n"
   244 "        pushl   %%ebx                                                 \n"
   245 "        cpuid                       # and get the information         \n"
   246 "        popl    %%ebx                                                 \n"
   247 "        movl    %%edx,%0                                              \n"
   248 "1:                                                                    \n"
   249 	: "=m" (features)
   250 	:
   251 	: "%eax", "%ecx", "%edx"
   252 	);
   253 #elif defined(__GNUC__) && defined (__x86_64__)
   254 	__asm__ (
   255 "        movl    $0x80000000,%%eax   # Query for extended functions    \n"
   256 "        pushq   %%rbx                                                 \n"
   257 "        cpuid                       # Get extended function limit     \n"
   258 "        popq    %%rbx                                                 \n"
   259 "        cmpl    $0x80000001,%%eax                                     \n"
   260 "        jl      1f                  # Nope, we dont have function 800000001h\n"
   261 "        movl    $0x80000001,%%eax   # Setup extended function 800000001h\n"
   262 "        pushq   %%rbx                                                 \n"
   263 "        cpuid                       # and get the information         \n"
   264 "        popq    %%rbx                                                 \n"
   265 "        movl    %%edx,%0                                              \n"
   266 "1:                                                                    \n"
   267 	: "=m" (features)
   268 	:
   269 	: "%rax", "%rcx", "%rdx"
   270 	);
   271 #elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
   272 	__asm {
   273         mov     eax,80000000h       ; Query for extended functions
   274         push    ebx
   275         cpuid                       ; Get extended function limit
   276         pop     ebx
   277         cmp     eax,80000001h
   278         jl      done                ; Nope, we dont have function 800000001h
   279         mov     eax,80000001h       ; Setup extended function 800000001h
   280         push    ebx
   281         cpuid                       ; and get the information
   282         pop     ebx
   283         mov     features,edx
   284 done:
   285 	}
   286 #elif defined(__sun) && ( defined(__i386) || defined(__amd64) )
   287 	    __asm (
   288 "        movl    $0x80000000,%eax \n"
   289 "        pushl   %ebx             \n"
   290 "        cpuid                    \n"
   291 "        popl    %ebx             \n"
   292 "        cmpl    $0x80000001,%eax \n"
   293 "        jl      1f               \n"
   294 "        movl    $0x80000001,%eax \n"
   295 "        pushl   %ebx             \n"
   296 "        cpuid                    \n"
   297 "        popl    %ebx             \n"
   298 #ifdef __i386
   299 "        movl    %edx,-8(%ebp)   \n"
   300 #else
   301 "        movl    %edx,-8(%rbp)   \n"
   302 #endif
   303 "1:                               \n"
   304 	    );
   305 #endif
   306 	return features;
   307 }
   308 
   309 static __inline__ int CPU_haveRDTSC(void)
   310 {
   311 	if ( CPU_haveCPUID() ) {
   312 		return (CPU_getCPUIDFeatures() & 0x00000010);
   313 	}
   314 	return 0;
   315 }
   316 
   317 static __inline__ int CPU_haveMMX(void)
   318 {
   319 	if ( CPU_haveCPUID() ) {
   320 		return (CPU_getCPUIDFeatures() & 0x00800000);
   321 	}
   322 	return 0;
   323 }
   324 
   325 static __inline__ int CPU_haveMMXExt(void)
   326 {
   327 	if ( CPU_haveCPUID() ) {
   328 		return (CPU_getCPUIDFeaturesExt() & 0x00400000);
   329 	}
   330 	return 0;
   331 }
   332 
   333 static __inline__ int CPU_have3DNow(void)
   334 {
   335 	if ( CPU_haveCPUID() ) {
   336 		return (CPU_getCPUIDFeaturesExt() & 0x80000000);
   337 	}
   338 	return 0;
   339 }
   340 
   341 static __inline__ int CPU_have3DNowExt(void)
   342 {
   343 	if ( CPU_haveCPUID() ) {
   344 		return (CPU_getCPUIDFeaturesExt() & 0x40000000);
   345 	}
   346 	return 0;
   347 }
   348 
   349 static __inline__ int CPU_haveSSE(void)
   350 {
   351 	if ( CPU_haveCPUID() ) {
   352 		return (CPU_getCPUIDFeatures() & 0x02000000);
   353 	}
   354 	return 0;
   355 }
   356 
   357 static __inline__ int CPU_haveSSE2(void)
   358 {
   359 	if ( CPU_haveCPUID() ) {
   360 		return (CPU_getCPUIDFeatures() & 0x04000000);
   361 	}
   362 	return 0;
   363 }
   364 
   365 static __inline__ int CPU_haveAltiVec(void)
   366 {
   367 	volatile int altivec = 0;
   368 #if (defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__))) || (defined(__OpenBSD__) && defined(__powerpc__))
   369 # ifdef __OpenBSD__
   370 	int selectors[2] = { CTL_MACHDEP, CPU_ALTIVEC };
   371 # else
   372 	int selectors[2] = { CTL_HW, HW_VECTORUNIT };
   373 # endif
   374 	int hasVectorUnit = 0; 
   375 	size_t length = sizeof(hasVectorUnit); 
   376 	int error = sysctl(selectors, 2, &hasVectorUnit, &length, NULL, 0); 
   377 	if( 0 == error )
   378 		altivec = (hasVectorUnit != 0); 
   379 #elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
   380 	void (*handler)(int sig);
   381 	handler = signal(SIGILL, illegal_instruction);
   382 	if ( setjmp(jmpbuf) == 0 ) {
   383 		asm volatile ("mtspr 256, %0\n\t"
   384 			      "vand %%v0, %%v0, %%v0"
   385 			      :
   386 			      : "r" (-1));
   387 		altivec = 1;
   388 	}
   389 	signal(SIGILL, handler);
   390 #endif
   391 	return altivec; 
   392 }
   393 
   394 #ifdef __linux__
   395 
   396 #include <unistd.h>
   397 #include <sys/types.h>
   398 #include <sys/stat.h>
   399 #include <fcntl.h>
   400 #include <elf.h>
   401 
   402 static __inline__ int CPU_haveARMSIMD(void)
   403 {
   404 	int arm_simd = 0;
   405 	int fd;
   406 
   407 	fd = open("/proc/self/auxv", O_RDONLY);
   408 	if (fd >= 0)
   409 	{
   410 		Elf32_auxv_t aux;
   411 		while (read(fd, &aux, sizeof aux) == sizeof aux)
   412 		{
   413 			if (aux.a_type == AT_PLATFORM)
   414 			{
   415 				const char *plat = (const char *) aux.a_un.a_val;
   416 				arm_simd = strncmp(plat, "v6l", 3) == 0 ||
   417 				           strncmp(plat, "v7l", 3) == 0;
   418 			}
   419 		}
   420 		close(fd);
   421 	}
   422 	return arm_simd;
   423 }
   424 
   425 #else
   426 
   427 static __inline__ int CPU_haveARMSIMD(void)
   428 {
   429 	return 0;
   430 }
   431 
   432 #endif
   433 
   434 static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
   435 
   436 static Uint32 SDL_GetCPUFeatures(void)
   437 {
   438 	if ( SDL_CPUFeatures == 0xFFFFFFFF ) {
   439 		SDL_CPUFeatures = 0;
   440 		if ( CPU_haveRDTSC() ) {
   441 			SDL_CPUFeatures |= CPU_HAS_RDTSC;
   442 		}
   443 		if ( CPU_haveMMX() ) {
   444 			SDL_CPUFeatures |= CPU_HAS_MMX;
   445 		}
   446 		if ( CPU_haveMMXExt() ) {
   447 			SDL_CPUFeatures |= CPU_HAS_MMXEXT;
   448 		}
   449 		if ( CPU_have3DNow() ) {
   450 			SDL_CPUFeatures |= CPU_HAS_3DNOW;
   451 		}
   452 		if ( CPU_have3DNowExt() ) {
   453 			SDL_CPUFeatures |= CPU_HAS_3DNOWEXT;
   454 		}
   455 		if ( CPU_haveSSE() ) {
   456 			SDL_CPUFeatures |= CPU_HAS_SSE;
   457 		}
   458 		if ( CPU_haveSSE2() ) {
   459 			SDL_CPUFeatures |= CPU_HAS_SSE2;
   460 		}
   461 		if ( CPU_haveAltiVec() ) {
   462 			SDL_CPUFeatures |= CPU_HAS_ALTIVEC;
   463 		}
   464 		if ( CPU_haveARMSIMD() ) {
   465 			SDL_CPUFeatures |= CPU_HAS_ARM_SIMD;
   466 		}
   467 	}
   468 	return SDL_CPUFeatures;
   469 }
   470 
   471 SDL_bool SDL_HasRDTSC(void)
   472 {
   473 	if ( SDL_GetCPUFeatures() & CPU_HAS_RDTSC ) {
   474 		return SDL_TRUE;
   475 	}
   476 	return SDL_FALSE;
   477 }
   478 
   479 SDL_bool SDL_HasMMX(void)
   480 {
   481 	if ( SDL_GetCPUFeatures() & CPU_HAS_MMX ) {
   482 		return SDL_TRUE;
   483 	}
   484 	return SDL_FALSE;
   485 }
   486 
   487 SDL_bool SDL_HasMMXExt(void)
   488 {
   489 	if ( SDL_GetCPUFeatures() & CPU_HAS_MMXEXT ) {
   490 		return SDL_TRUE;
   491 	}
   492 	return SDL_FALSE;
   493 }
   494 
   495 SDL_bool SDL_Has3DNow(void)
   496 {
   497 	if ( SDL_GetCPUFeatures() & CPU_HAS_3DNOW ) {
   498 		return SDL_TRUE;
   499 	}
   500 	return SDL_FALSE;
   501 }
   502 
   503 SDL_bool SDL_Has3DNowExt(void)
   504 {
   505 	if ( SDL_GetCPUFeatures() & CPU_HAS_3DNOWEXT ) {
   506 		return SDL_TRUE;
   507 	}
   508 	return SDL_FALSE;
   509 }
   510 
   511 SDL_bool SDL_HasSSE(void)
   512 {
   513 	if ( SDL_GetCPUFeatures() & CPU_HAS_SSE ) {
   514 		return SDL_TRUE;
   515 	}
   516 	return SDL_FALSE;
   517 }
   518 
   519 SDL_bool SDL_HasSSE2(void)
   520 {
   521 	if ( SDL_GetCPUFeatures() & CPU_HAS_SSE2 ) {
   522 		return SDL_TRUE;
   523 	}
   524 	return SDL_FALSE;
   525 }
   526 
   527 SDL_bool SDL_HasAltiVec(void)
   528 {
   529 	if ( SDL_GetCPUFeatures() & CPU_HAS_ALTIVEC ) {
   530 		return SDL_TRUE;
   531 	}
   532 	return SDL_FALSE;
   533 }
   534 
   535 SDL_bool SDL_HasARMSIMD(void)
   536 {
   537 	if ( SDL_GetCPUFeatures() & CPU_HAS_ARM_SIMD ) {
   538 		return SDL_TRUE;
   539 	}
   540 	return SDL_FALSE;
   541 }
   542 
   543 #ifdef TEST_MAIN
   544 
   545 #include <stdio.h>
   546 
   547 int main()
   548 {
   549 	printf("RDTSC: %d\n", SDL_HasRDTSC());
   550 	printf("MMX: %d\n", SDL_HasMMX());
   551 	printf("MMXExt: %d\n", SDL_HasMMXExt());
   552 	printf("3DNow: %d\n", SDL_Has3DNow());
   553 	printf("3DNowExt: %d\n", SDL_Has3DNowExt());
   554 	printf("SSE: %d\n", SDL_HasSSE());
   555 	printf("SSE2: %d\n", SDL_HasSSE2());
   556 	printf("AltiVec: %d\n", SDL_HasAltiVec());
   557 	printf("ARM SIMD: %d\n", SDL_HasARMSIMD());
   558 	return 0;
   559 }
   560 
   561 #endif /* TEST_MAIN */