src/cpuinfo/SDL_cpuinfo.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 11 Apr 2004 19:49:34 +0000
changeset 881 9eb85a211abd
parent 804 b2fda076b02e
child 887 b4b64bb88f2f
permissions -rw-r--r--
Date: Tue, 30 Mar 2004 18:18:13 -0600
From: Tyler Montbriand
Subject: [SDL] Detecting Opteron CPU features

I can now get SDL_cpuinfo.c to detect the AMD Opteron's RDTSC, MMX, MMXEXT,
3DNOW, 3DNOWEXT, SSE, and SSE2 instruction set extensions under Linux. It
took one #ifdef'ed block of new asm code to account for the 64-bit flags
register, but the other two blocks worked fine without modification, just
needed to modify the #ifdef's a bit.
slouken@739
     1
/*
slouken@739
     2
    SDL - Simple DirectMedia Layer
slouken@769
     3
    Copyright (C) 1997-2004 Sam Lantinga
slouken@739
     4
slouken@739
     5
    This library is free software; you can redistribute it and/or
slouken@739
     6
    modify it under the terms of the GNU Library General Public
slouken@739
     7
    License as published by the Free Software Foundation; either
slouken@739
     8
    version 2 of the License, or (at your option) any later version.
slouken@739
     9
slouken@739
    10
    This library is distributed in the hope that it will be useful,
slouken@739
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@739
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@739
    13
    Library General Public License for more details.
slouken@739
    14
slouken@739
    15
    You should have received a copy of the GNU Library General Public
slouken@739
    16
    License along with this library; if not, write to the Free
slouken@739
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@739
    18
slouken@739
    19
    Sam Lantinga
slouken@739
    20
    slouken@libsdl.org
slouken@739
    21
*/
slouken@739
    22
slouken@739
    23
#ifdef SAVE_RCSID
slouken@739
    24
static char rcsid =
slouken@739
    25
 "@(#) $Id$";
slouken@739
    26
#endif
slouken@739
    27
slouken@739
    28
/* CPU feature detection for SDL */
slouken@739
    29
slouken@793
    30
#ifdef unix /* FIXME: Better setjmp detection? */
slouken@793
    31
#define USE_SETJMP
slouken@793
    32
#include <signal.h>
slouken@793
    33
#include <setjmp.h>
slouken@793
    34
#endif
slouken@793
    35
slouken@739
    36
#include "SDL.h"
slouken@745
    37
#include "SDL_cpuinfo.h"
slouken@739
    38
slouken@778
    39
#ifdef MACOSX
slouken@778
    40
#include <sys/sysctl.h> /* For AltiVec check */
slouken@778
    41
#endif
slouken@778
    42
slouken@745
    43
#define CPU_HAS_RDTSC	0x00000001
slouken@745
    44
#define CPU_HAS_MMX	0x00000002
slouken@785
    45
#define CPU_HAS_MMXEXT	0x00000004
slouken@785
    46
#define CPU_HAS_3DNOW	0x00000010
slouken@785
    47
#define CPU_HAS_3DNOWEXT 0x00000020
slouken@785
    48
#define CPU_HAS_SSE	0x00000040
slouken@785
    49
#define CPU_HAS_SSE2	0x00000080
slouken@785
    50
#define CPU_HAS_ALTIVEC	0x00000100
slouken@739
    51
slouken@793
    52
#ifdef USE_SETJMP
slouken@793
    53
/* This is the brute force way of detecting instruction sets...
slouken@793
    54
   the idea is borrowed from the libmpeg2 library - thanks!
slouken@793
    55
 */
slouken@793
    56
static jmp_buf jmpbuf;
slouken@793
    57
static void illegal_instruction(int sig)
slouken@793
    58
{
slouken@793
    59
	longjmp(jmpbuf, 1);
slouken@793
    60
}
slouken@793
    61
#endif // USE_SETJMP
slouken@793
    62
slouken@745
    63
static __inline__ int CPU_haveCPUID()
slouken@745
    64
{
slouken@745
    65
	int has_CPUID = 0;
slouken@745
    66
#if defined(__GNUC__) && defined(i386)
slouken@745
    67
	__asm__ (
slouken@745
    68
"        pushfl                      # Get original EFLAGS             \n"
slouken@745
    69
"        popl    %%eax                                                 \n"
slouken@745
    70
"        movl    %%eax,%%ecx                                           \n"
slouken@745
    71
"        xorl    $0x200000,%%eax     # Flip ID bit in EFLAGS           \n"
slouken@745
    72
"        pushl   %%eax               # Save new EFLAGS value on stack  \n"
slouken@745
    73
"        popfl                       # Replace current EFLAGS value    \n"
slouken@745
    74
"        pushfl                      # Get new EFLAGS                  \n"
slouken@745
    75
"        popl    %%eax               # Store new EFLAGS in EAX         \n"
slouken@745
    76
"        xorl    %%ecx,%%eax         # Can not toggle ID bit,          \n"
slouken@745
    77
"        jz      1f                  # Processor=80486                 \n"
slouken@745
    78
"        movl    $1,%0               # We have CPUID support           \n"
slouken@745
    79
"1:                                                                    \n"
slouken@784
    80
	: "=m" (has_CPUID)
slouken@745
    81
	:
slouken@745
    82
	: "%eax", "%ecx"
slouken@745
    83
	);
slouken@881
    84
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@881
    85
/* Technically, if this is being compiled under __x86_64__ then it has 
slouken@881
    86
CPUid by definition.  But it's nice to be able to prove it.  :)      */
slouken@881
    87
	__asm__ (
slouken@881
    88
"        pushfq                      # Get original EFLAGS             \n"
slouken@881
    89
"        popq    %%rax                                                 \n"
slouken@881
    90
"        movq    %%rax,%%rcx                                           \n"
slouken@881
    91
"        xorl    $0x200000,%%eax     # Flip ID bit in EFLAGS           \n"
slouken@881
    92
"        pushq   %%rax               # Save new EFLAGS value on stack  \n"
slouken@881
    93
"        popfq                       # Replace current EFLAGS value    \n"
slouken@881
    94
"        pushfq                      # Get new EFLAGS                  \n"
slouken@881
    95
"        popq    %%rax               # Store new EFLAGS in EAX         \n"
slouken@881
    96
"        xorl    %%ecx,%%eax         # Can not toggle ID bit,          \n"
slouken@881
    97
"        jz      1f                  # Processor=80486                 \n"
slouken@881
    98
"        movl    $1,%0               # We have CPUID support           \n"
slouken@881
    99
"1:                                                                    \n"
slouken@881
   100
	: "=m" (has_CPUID)
slouken@881
   101
	:
slouken@881
   102
	: "%rax", "%rcx"
slouken@881
   103
	);
slouken@745
   104
#elif defined(_MSC_VER)
slouken@749
   105
	__asm {
slouken@745
   106
        pushfd                      ; Get original EFLAGS
slouken@745
   107
        pop     eax
slouken@745
   108
        mov     ecx, eax
slouken@745
   109
        xor     eax, 200000h        ; Flip ID bit in EFLAGS
slouken@745
   110
        push    eax                 ; Save new EFLAGS value on stack
slouken@745
   111
        popfd                       ; Replace current EFLAGS value
slouken@745
   112
        pushfd                      ; Get new EFLAGS
slouken@745
   113
        pop     eax                 ; Store new EFLAGS in EAX
slouken@745
   114
        xor     eax, ecx            ; Can not toggle ID bit,
slouken@745
   115
        jz      done                ; Processor=80486
slouken@745
   116
        mov     has_CPUID,1         ; We have CPUID support
slouken@745
   117
done:
slouken@745
   118
	}
slouken@745
   119
#endif
slouken@745
   120
	return has_CPUID;
slouken@745
   121
}
slouken@745
   122
slouken@745
   123
static __inline__ int CPU_getCPUIDFeatures()
slouken@745
   124
{
slouken@745
   125
	int features = 0;
slouken@881
   126
#if defined(__GNUC__) && ( defined(i386) || defined(__x86_64__) )
slouken@745
   127
	__asm__ (
slouken@785
   128
"        movl    %%ebx,%%edi\n"
slouken@745
   129
"        xorl    %%eax,%%eax         # Set up for CPUID instruction    \n"
slouken@745
   130
"        cpuid                       # Get and save vendor ID          \n"
slouken@745
   131
"        cmpl    $1,%%eax            # Make sure 1 is valid input for CPUID\n"
slouken@745
   132
"        jl      1f                  # We dont have the CPUID instruction\n"
slouken@745
   133
"        xorl    %%eax,%%eax                                           \n"
slouken@745
   134
"        incl    %%eax                                                 \n"
slouken@745
   135
"        cpuid                       # Get family/model/stepping/features\n"
slouken@745
   136
"        movl    %%edx,%0                                              \n"
slouken@745
   137
"1:                                                                    \n"
slouken@785
   138
"        movl    %%edi,%%ebx\n"
slouken@784
   139
	: "=m" (features)
slouken@745
   140
	:
slouken@785
   141
	: "%eax", "%ebx", "%ecx", "%edx", "%edi"
slouken@745
   142
	);
slouken@745
   143
#elif defined(_MSC_VER)
slouken@749
   144
	__asm {
slouken@745
   145
        xor     eax, eax            ; Set up for CPUID instruction
slouken@745
   146
        cpuid                       ; Get and save vendor ID
slouken@745
   147
        cmp     eax, 1              ; Make sure 1 is valid input for CPUID
slouken@745
   148
        jl      done                ; We dont have the CPUID instruction
slouken@745
   149
        xor     eax, eax
slouken@745
   150
        inc     eax
slouken@745
   151
        cpuid                       ; Get family/model/stepping/features
slouken@745
   152
        mov     features, edx
slouken@745
   153
done:
slouken@745
   154
	}
slouken@745
   155
#endif
slouken@745
   156
	return features;
slouken@745
   157
}
slouken@745
   158
slouken@785
   159
static __inline__ int CPU_getCPUIDFeaturesExt()
slouken@785
   160
{
slouken@785
   161
	int features = 0;
slouken@881
   162
#if defined(__GNUC__) && (defined(i386) || defined (__x86_64__) )
slouken@785
   163
	__asm__ (
slouken@785
   164
"        movl    %%ebx,%%edi\n"
slouken@785
   165
"        movl    $0x80000000,%%eax   # Query for extended functions    \n"
slouken@785
   166
"        cpuid                       # Get extended function limit     \n"
slouken@785
   167
"        cmpl    $0x80000001,%%eax                                     \n"
slouken@787
   168
"        jl      1f                  # Nope, we dont have function 800000001h\n"
slouken@785
   169
"        movl    $0x80000001,%%eax   # Setup extended function 800000001h\n"
slouken@785
   170
"        cpuid                       # and get the information         \n"
slouken@785
   171
"        movl    %%edx,%0                                              \n"
slouken@785
   172
"1:                                                                    \n"
slouken@785
   173
"        movl    %%edi,%%ebx\n"
slouken@785
   174
	: "=m" (features)
slouken@785
   175
	:
slouken@785
   176
	: "%eax", "%ebx", "%ecx", "%edx", "%edi"
slouken@785
   177
	);
slouken@785
   178
#elif defined(_MSC_VER)
slouken@785
   179
	__asm {
slouken@785
   180
        mov     eax,80000000h       ; Query for extended functions
slouken@785
   181
        cpuid                       ; Get extended function limit
slouken@785
   182
        cmp     eax,80000001h
slouken@787
   183
        jl      done                ; Nope, we dont have function 800000001h
slouken@785
   184
        mov     eax,80000001h       ; Setup extended function 800000001h
slouken@785
   185
        cpuid                       ; and get the information
slouken@785
   186
        mov     features,edx
slouken@785
   187
done:
slouken@785
   188
	}
slouken@785
   189
#endif
slouken@785
   190
	return features;
slouken@785
   191
}
slouken@785
   192
slouken@745
   193
static __inline__ int CPU_haveRDTSC()
slouken@745
   194
{
slouken@745
   195
	if ( CPU_haveCPUID() ) {
slouken@745
   196
		return (CPU_getCPUIDFeatures() & 0x00000010);
slouken@745
   197
	}
slouken@745
   198
	return 0;
slouken@745
   199
}
slouken@745
   200
slouken@745
   201
static __inline__ int CPU_haveMMX()
slouken@745
   202
{
slouken@745
   203
	if ( CPU_haveCPUID() ) {
slouken@745
   204
		return (CPU_getCPUIDFeatures() & 0x00800000);
slouken@745
   205
	}
slouken@745
   206
	return 0;
slouken@745
   207
}
slouken@745
   208
slouken@785
   209
static __inline__ int CPU_haveMMXExt()
slouken@785
   210
{
slouken@785
   211
	if ( CPU_haveCPUID() ) {
slouken@785
   212
		return (CPU_getCPUIDFeaturesExt() & 0x00400000);
slouken@785
   213
	}
slouken@785
   214
	return 0;
slouken@785
   215
}
slouken@785
   216
slouken@745
   217
static __inline__ int CPU_have3DNow()
slouken@745
   218
{
slouken@785
   219
	if ( CPU_haveCPUID() ) {
slouken@785
   220
		return (CPU_getCPUIDFeaturesExt() & 0x80000000);
slouken@747
   221
	}
slouken@785
   222
	return 0;
slouken@785
   223
}
slouken@785
   224
slouken@785
   225
static __inline__ int CPU_have3DNowExt()
slouken@785
   226
{
slouken@785
   227
	if ( CPU_haveCPUID() ) {
slouken@785
   228
		return (CPU_getCPUIDFeaturesExt() & 0x40000000);
slouken@745
   229
	}
slouken@785
   230
	return 0;
slouken@745
   231
}
slouken@745
   232
slouken@745
   233
static __inline__ int CPU_haveSSE()
slouken@745
   234
{
slouken@745
   235
	if ( CPU_haveCPUID() ) {
slouken@745
   236
		return (CPU_getCPUIDFeatures() & 0x02000000);
slouken@745
   237
	}
slouken@745
   238
	return 0;
slouken@745
   239
}
slouken@739
   240
slouken@785
   241
static __inline__ int CPU_haveSSE2()
slouken@785
   242
{
slouken@785
   243
	if ( CPU_haveCPUID() ) {
slouken@785
   244
		return (CPU_getCPUIDFeatures() & 0x04000000);
slouken@785
   245
	}
slouken@785
   246
	return 0;
slouken@785
   247
}
slouken@785
   248
slouken@778
   249
static __inline__ int CPU_haveAltiVec()
slouken@778
   250
{
slouken@796
   251
	volatile int altivec = 0;
slouken@778
   252
#ifdef MACOSX
slouken@778
   253
	int selectors[2] = { CTL_HW, HW_VECTORUNIT }; 
slouken@778
   254
	int hasVectorUnit = 0; 
slouken@778
   255
	size_t length = sizeof(hasVectorUnit); 
slouken@778
   256
	int error = sysctl(selectors, 2, &hasVectorUnit, &length, NULL, 0); 
slouken@778
   257
	if( 0 == error )
slouken@793
   258
		altivec = (hasVectorUnit != 0); 
slouken@795
   259
#elif defined(USE_SETJMP) && defined(GCC_ALTIVEC)
slouken@793
   260
	void (*handler)(int sig);
slouken@793
   261
	handler = signal(SIGILL, illegal_instruction);
slouken@793
   262
	if ( setjmp(jmpbuf) == 0 ) {
slouken@793
   263
		asm volatile ("mtspr 256, %0\n\t"
slouken@793
   264
			      "vand %%v0, %%v0, %%v0"
slouken@793
   265
			      :
slouken@793
   266
			      : "r" (-1));
slouken@793
   267
		altivec = 1;
slouken@793
   268
	}
slouken@793
   269
	signal(SIGILL, handler);
slouken@778
   270
#endif
slouken@793
   271
	return altivec; 
slouken@778
   272
}
slouken@778
   273
slouken@739
   274
static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
slouken@739
   275
slouken@739
   276
static Uint32 SDL_GetCPUFeatures()
slouken@739
   277
{
slouken@739
   278
	if ( SDL_CPUFeatures == 0xFFFFFFFF ) {
slouken@739
   279
		SDL_CPUFeatures = 0;
slouken@745
   280
		if ( CPU_haveRDTSC() ) {
slouken@745
   281
			SDL_CPUFeatures |= CPU_HAS_RDTSC;
slouken@745
   282
		}
slouken@739
   283
		if ( CPU_haveMMX() ) {
slouken@739
   284
			SDL_CPUFeatures |= CPU_HAS_MMX;
slouken@739
   285
		}
slouken@786
   286
		if ( CPU_haveMMXExt() ) {
slouken@786
   287
			SDL_CPUFeatures |= CPU_HAS_MMXEXT;
slouken@786
   288
		}
slouken@739
   289
		if ( CPU_have3DNow() ) {
slouken@739
   290
			SDL_CPUFeatures |= CPU_HAS_3DNOW;
slouken@739
   291
		}
slouken@786
   292
		if ( CPU_have3DNowExt() ) {
slouken@786
   293
			SDL_CPUFeatures |= CPU_HAS_3DNOWEXT;
slouken@786
   294
		}
slouken@739
   295
		if ( CPU_haveSSE() ) {
slouken@739
   296
			SDL_CPUFeatures |= CPU_HAS_SSE;
slouken@739
   297
		}
slouken@786
   298
		if ( CPU_haveSSE2() ) {
slouken@786
   299
			SDL_CPUFeatures |= CPU_HAS_SSE2;
slouken@786
   300
		}
slouken@778
   301
		if ( CPU_haveAltiVec() ) {
slouken@778
   302
			SDL_CPUFeatures |= CPU_HAS_ALTIVEC;
slouken@778
   303
		}
slouken@739
   304
	}
slouken@739
   305
	return SDL_CPUFeatures;
slouken@739
   306
}
slouken@739
   307
slouken@745
   308
SDL_bool SDL_HasRDTSC()
slouken@745
   309
{
slouken@745
   310
	if ( SDL_GetCPUFeatures() & CPU_HAS_RDTSC ) {
slouken@745
   311
		return SDL_TRUE;
slouken@745
   312
	}
slouken@745
   313
	return SDL_FALSE;
slouken@745
   314
}
slouken@745
   315
slouken@739
   316
SDL_bool SDL_HasMMX()
slouken@739
   317
{
slouken@739
   318
	if ( SDL_GetCPUFeatures() & CPU_HAS_MMX ) {
slouken@739
   319
		return SDL_TRUE;
slouken@739
   320
	}
slouken@739
   321
	return SDL_FALSE;
slouken@739
   322
}
slouken@739
   323
slouken@804
   324
SDL_bool SDL_HasMMXExt()
slouken@804
   325
{
slouken@804
   326
	if ( SDL_GetCPUFeatures() & CPU_HAS_MMXEXT ) {
slouken@804
   327
		return SDL_TRUE;
slouken@804
   328
	}
slouken@804
   329
	return SDL_FALSE;
slouken@804
   330
}
slouken@804
   331
slouken@739
   332
SDL_bool SDL_Has3DNow()
slouken@739
   333
{
slouken@739
   334
	if ( SDL_GetCPUFeatures() & CPU_HAS_3DNOW ) {
slouken@739
   335
		return SDL_TRUE;
slouken@739
   336
	}
slouken@739
   337
	return SDL_FALSE;
slouken@739
   338
}
slouken@739
   339
slouken@804
   340
SDL_bool SDL_Has3DNowExt()
slouken@804
   341
{
slouken@804
   342
	if ( SDL_GetCPUFeatures() & CPU_HAS_3DNOWEXT ) {
slouken@804
   343
		return SDL_TRUE;
slouken@804
   344
	}
slouken@804
   345
	return SDL_FALSE;
slouken@804
   346
}
slouken@804
   347
slouken@739
   348
SDL_bool SDL_HasSSE()
slouken@739
   349
{
slouken@739
   350
	if ( SDL_GetCPUFeatures() & CPU_HAS_SSE ) {
slouken@739
   351
		return SDL_TRUE;
slouken@739
   352
	}
slouken@739
   353
	return SDL_FALSE;
slouken@739
   354
}
slouken@739
   355
slouken@804
   356
SDL_bool SDL_HasSSE2()
slouken@804
   357
{
slouken@804
   358
	if ( SDL_GetCPUFeatures() & CPU_HAS_SSE2 ) {
slouken@804
   359
		return SDL_TRUE;
slouken@804
   360
	}
slouken@804
   361
	return SDL_FALSE;
slouken@804
   362
}
slouken@804
   363
slouken@778
   364
SDL_bool SDL_HasAltiVec()
slouken@778
   365
{
slouken@778
   366
	if ( SDL_GetCPUFeatures() & CPU_HAS_ALTIVEC ) {
slouken@778
   367
		return SDL_TRUE;
slouken@778
   368
	}
slouken@778
   369
	return SDL_FALSE;
slouken@778
   370
}
slouken@778
   371
slouken@739
   372
#ifdef TEST_MAIN
slouken@739
   373
slouken@739
   374
#include <stdio.h>
slouken@739
   375
slouken@739
   376
int main()
slouken@739
   377
{
slouken@778
   378
	printf("RDTSC: %d\n", SDL_HasRDTSC());
slouken@739
   379
	printf("MMX: %d\n", SDL_HasMMX());
slouken@785
   380
	printf("MMXExt: %d\n", SDL_HasMMXExt());
slouken@739
   381
	printf("3DNow: %d\n", SDL_Has3DNow());
slouken@785
   382
	printf("3DNowExt: %d\n", SDL_Has3DNowExt());
slouken@739
   383
	printf("SSE: %d\n", SDL_HasSSE());
slouken@785
   384
	printf("SSE2: %d\n", SDL_HasSSE2());
slouken@778
   385
	printf("AltiVec: %d\n", SDL_HasAltiVec());
slouken@745
   386
	return 0;
slouken@739
   387
}
slouken@739
   388
slouken@739
   389
#endif /* TEST_MAIN */