src/cpuinfo/SDL_cpuinfo.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 31 Jan 2004 06:49:18 +0000
changeset 796 dec19b813b04
parent 795 275708f2e838
child 804 b2fda076b02e
permissions -rw-r--r--
*** empty log message ***
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@745
    84
#elif defined(_MSC_VER)
slouken@749
    85
	__asm {
slouken@745
    86
        pushfd                      ; Get original EFLAGS
slouken@745
    87
        pop     eax
slouken@745
    88
        mov     ecx, eax
slouken@745
    89
        xor     eax, 200000h        ; Flip ID bit in EFLAGS
slouken@745
    90
        push    eax                 ; Save new EFLAGS value on stack
slouken@745
    91
        popfd                       ; Replace current EFLAGS value
slouken@745
    92
        pushfd                      ; Get new EFLAGS
slouken@745
    93
        pop     eax                 ; Store new EFLAGS in EAX
slouken@745
    94
        xor     eax, ecx            ; Can not toggle ID bit,
slouken@745
    95
        jz      done                ; Processor=80486
slouken@745
    96
        mov     has_CPUID,1         ; We have CPUID support
slouken@745
    97
done:
slouken@745
    98
	}
slouken@745
    99
#endif
slouken@745
   100
	return has_CPUID;
slouken@745
   101
}
slouken@745
   102
slouken@745
   103
static __inline__ int CPU_getCPUIDFeatures()
slouken@745
   104
{
slouken@745
   105
	int features = 0;
slouken@745
   106
#if defined(__GNUC__) && defined(i386)
slouken@745
   107
	__asm__ (
slouken@785
   108
"        movl    %%ebx,%%edi\n"
slouken@745
   109
"        xorl    %%eax,%%eax         # Set up for CPUID instruction    \n"
slouken@745
   110
"        cpuid                       # Get and save vendor ID          \n"
slouken@745
   111
"        cmpl    $1,%%eax            # Make sure 1 is valid input for CPUID\n"
slouken@745
   112
"        jl      1f                  # We dont have the CPUID instruction\n"
slouken@745
   113
"        xorl    %%eax,%%eax                                           \n"
slouken@745
   114
"        incl    %%eax                                                 \n"
slouken@745
   115
"        cpuid                       # Get family/model/stepping/features\n"
slouken@745
   116
"        movl    %%edx,%0                                              \n"
slouken@745
   117
"1:                                                                    \n"
slouken@785
   118
"        movl    %%edi,%%ebx\n"
slouken@784
   119
	: "=m" (features)
slouken@745
   120
	:
slouken@785
   121
	: "%eax", "%ebx", "%ecx", "%edx", "%edi"
slouken@745
   122
	);
slouken@745
   123
#elif defined(_MSC_VER)
slouken@749
   124
	__asm {
slouken@745
   125
        xor     eax, eax            ; Set up for CPUID instruction
slouken@745
   126
        cpuid                       ; Get and save vendor ID
slouken@745
   127
        cmp     eax, 1              ; Make sure 1 is valid input for CPUID
slouken@745
   128
        jl      done                ; We dont have the CPUID instruction
slouken@745
   129
        xor     eax, eax
slouken@745
   130
        inc     eax
slouken@745
   131
        cpuid                       ; Get family/model/stepping/features
slouken@745
   132
        mov     features, edx
slouken@745
   133
done:
slouken@745
   134
	}
slouken@745
   135
#endif
slouken@745
   136
	return features;
slouken@745
   137
}
slouken@745
   138
slouken@785
   139
static __inline__ int CPU_getCPUIDFeaturesExt()
slouken@785
   140
{
slouken@785
   141
	int features = 0;
slouken@785
   142
#if defined(__GNUC__) && defined(i386)
slouken@785
   143
	__asm__ (
slouken@785
   144
"        movl    %%ebx,%%edi\n"
slouken@785
   145
"        movl    $0x80000000,%%eax   # Query for extended functions    \n"
slouken@785
   146
"        cpuid                       # Get extended function limit     \n"
slouken@785
   147
"        cmpl    $0x80000001,%%eax                                     \n"
slouken@787
   148
"        jl      1f                  # Nope, we dont have function 800000001h\n"
slouken@785
   149
"        movl    $0x80000001,%%eax   # Setup extended function 800000001h\n"
slouken@785
   150
"        cpuid                       # and get the information         \n"
slouken@785
   151
"        movl    %%edx,%0                                              \n"
slouken@785
   152
"1:                                                                    \n"
slouken@785
   153
"        movl    %%edi,%%ebx\n"
slouken@785
   154
	: "=m" (features)
slouken@785
   155
	:
slouken@785
   156
	: "%eax", "%ebx", "%ecx", "%edx", "%edi"
slouken@785
   157
	);
slouken@785
   158
#elif defined(_MSC_VER)
slouken@785
   159
	__asm {
slouken@785
   160
        mov     eax,80000000h       ; Query for extended functions
slouken@785
   161
        cpuid                       ; Get extended function limit
slouken@785
   162
        cmp     eax,80000001h
slouken@787
   163
        jl      done                ; Nope, we dont have function 800000001h
slouken@785
   164
        mov     eax,80000001h       ; Setup extended function 800000001h
slouken@785
   165
        cpuid                       ; and get the information
slouken@785
   166
        mov     features,edx
slouken@785
   167
done:
slouken@785
   168
	}
slouken@785
   169
#endif
slouken@785
   170
	return features;
slouken@785
   171
}
slouken@785
   172
slouken@745
   173
static __inline__ int CPU_haveRDTSC()
slouken@745
   174
{
slouken@745
   175
	if ( CPU_haveCPUID() ) {
slouken@745
   176
		return (CPU_getCPUIDFeatures() & 0x00000010);
slouken@745
   177
	}
slouken@745
   178
	return 0;
slouken@745
   179
}
slouken@745
   180
slouken@745
   181
static __inline__ int CPU_haveMMX()
slouken@745
   182
{
slouken@745
   183
	if ( CPU_haveCPUID() ) {
slouken@745
   184
		return (CPU_getCPUIDFeatures() & 0x00800000);
slouken@745
   185
	}
slouken@745
   186
	return 0;
slouken@745
   187
}
slouken@745
   188
slouken@785
   189
static __inline__ int CPU_haveMMXExt()
slouken@785
   190
{
slouken@785
   191
	if ( CPU_haveCPUID() ) {
slouken@785
   192
		return (CPU_getCPUIDFeaturesExt() & 0x00400000);
slouken@785
   193
	}
slouken@785
   194
	return 0;
slouken@785
   195
}
slouken@785
   196
slouken@745
   197
static __inline__ int CPU_have3DNow()
slouken@745
   198
{
slouken@785
   199
	if ( CPU_haveCPUID() ) {
slouken@785
   200
		return (CPU_getCPUIDFeaturesExt() & 0x80000000);
slouken@747
   201
	}
slouken@785
   202
	return 0;
slouken@785
   203
}
slouken@785
   204
slouken@785
   205
static __inline__ int CPU_have3DNowExt()
slouken@785
   206
{
slouken@785
   207
	if ( CPU_haveCPUID() ) {
slouken@785
   208
		return (CPU_getCPUIDFeaturesExt() & 0x40000000);
slouken@745
   209
	}
slouken@785
   210
	return 0;
slouken@745
   211
}
slouken@745
   212
slouken@745
   213
static __inline__ int CPU_haveSSE()
slouken@745
   214
{
slouken@745
   215
	if ( CPU_haveCPUID() ) {
slouken@745
   216
		return (CPU_getCPUIDFeatures() & 0x02000000);
slouken@745
   217
	}
slouken@745
   218
	return 0;
slouken@745
   219
}
slouken@739
   220
slouken@785
   221
static __inline__ int CPU_haveSSE2()
slouken@785
   222
{
slouken@785
   223
	if ( CPU_haveCPUID() ) {
slouken@785
   224
		return (CPU_getCPUIDFeatures() & 0x04000000);
slouken@785
   225
	}
slouken@785
   226
	return 0;
slouken@785
   227
}
slouken@785
   228
slouken@778
   229
static __inline__ int CPU_haveAltiVec()
slouken@778
   230
{
slouken@796
   231
	volatile int altivec = 0;
slouken@778
   232
#ifdef MACOSX
slouken@778
   233
	int selectors[2] = { CTL_HW, HW_VECTORUNIT }; 
slouken@778
   234
	int hasVectorUnit = 0; 
slouken@778
   235
	size_t length = sizeof(hasVectorUnit); 
slouken@778
   236
	int error = sysctl(selectors, 2, &hasVectorUnit, &length, NULL, 0); 
slouken@778
   237
	if( 0 == error )
slouken@793
   238
		altivec = (hasVectorUnit != 0); 
slouken@795
   239
#elif defined(USE_SETJMP) && defined(GCC_ALTIVEC)
slouken@793
   240
	void (*handler)(int sig);
slouken@793
   241
	handler = signal(SIGILL, illegal_instruction);
slouken@793
   242
	if ( setjmp(jmpbuf) == 0 ) {
slouken@793
   243
		asm volatile ("mtspr 256, %0\n\t"
slouken@793
   244
			      "vand %%v0, %%v0, %%v0"
slouken@793
   245
			      :
slouken@793
   246
			      : "r" (-1));
slouken@793
   247
		altivec = 1;
slouken@793
   248
	}
slouken@793
   249
	signal(SIGILL, handler);
slouken@778
   250
#endif
slouken@793
   251
	return altivec; 
slouken@778
   252
}
slouken@778
   253
slouken@739
   254
static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
slouken@739
   255
slouken@739
   256
static Uint32 SDL_GetCPUFeatures()
slouken@739
   257
{
slouken@739
   258
	if ( SDL_CPUFeatures == 0xFFFFFFFF ) {
slouken@739
   259
		SDL_CPUFeatures = 0;
slouken@745
   260
		if ( CPU_haveRDTSC() ) {
slouken@745
   261
			SDL_CPUFeatures |= CPU_HAS_RDTSC;
slouken@745
   262
		}
slouken@739
   263
		if ( CPU_haveMMX() ) {
slouken@739
   264
			SDL_CPUFeatures |= CPU_HAS_MMX;
slouken@739
   265
		}
slouken@786
   266
		if ( CPU_haveMMXExt() ) {
slouken@786
   267
			SDL_CPUFeatures |= CPU_HAS_MMXEXT;
slouken@786
   268
		}
slouken@739
   269
		if ( CPU_have3DNow() ) {
slouken@739
   270
			SDL_CPUFeatures |= CPU_HAS_3DNOW;
slouken@739
   271
		}
slouken@786
   272
		if ( CPU_have3DNowExt() ) {
slouken@786
   273
			SDL_CPUFeatures |= CPU_HAS_3DNOWEXT;
slouken@786
   274
		}
slouken@739
   275
		if ( CPU_haveSSE() ) {
slouken@739
   276
			SDL_CPUFeatures |= CPU_HAS_SSE;
slouken@739
   277
		}
slouken@786
   278
		if ( CPU_haveSSE2() ) {
slouken@786
   279
			SDL_CPUFeatures |= CPU_HAS_SSE2;
slouken@786
   280
		}
slouken@778
   281
		if ( CPU_haveAltiVec() ) {
slouken@778
   282
			SDL_CPUFeatures |= CPU_HAS_ALTIVEC;
slouken@778
   283
		}
slouken@739
   284
	}
slouken@739
   285
	return SDL_CPUFeatures;
slouken@739
   286
}
slouken@739
   287
slouken@745
   288
SDL_bool SDL_HasRDTSC()
slouken@745
   289
{
slouken@745
   290
	if ( SDL_GetCPUFeatures() & CPU_HAS_RDTSC ) {
slouken@745
   291
		return SDL_TRUE;
slouken@745
   292
	}
slouken@745
   293
	return SDL_FALSE;
slouken@745
   294
}
slouken@745
   295
slouken@739
   296
SDL_bool SDL_HasMMX()
slouken@739
   297
{
slouken@739
   298
	if ( SDL_GetCPUFeatures() & CPU_HAS_MMX ) {
slouken@739
   299
		return SDL_TRUE;
slouken@739
   300
	}
slouken@739
   301
	return SDL_FALSE;
slouken@739
   302
}
slouken@739
   303
slouken@739
   304
SDL_bool SDL_Has3DNow()
slouken@739
   305
{
slouken@739
   306
	if ( SDL_GetCPUFeatures() & CPU_HAS_3DNOW ) {
slouken@739
   307
		return SDL_TRUE;
slouken@739
   308
	}
slouken@739
   309
	return SDL_FALSE;
slouken@739
   310
}
slouken@739
   311
slouken@739
   312
SDL_bool SDL_HasSSE()
slouken@739
   313
{
slouken@739
   314
	if ( SDL_GetCPUFeatures() & CPU_HAS_SSE ) {
slouken@739
   315
		return SDL_TRUE;
slouken@739
   316
	}
slouken@739
   317
	return SDL_FALSE;
slouken@739
   318
}
slouken@739
   319
slouken@778
   320
SDL_bool SDL_HasAltiVec()
slouken@778
   321
{
slouken@778
   322
	if ( SDL_GetCPUFeatures() & CPU_HAS_ALTIVEC ) {
slouken@778
   323
		return SDL_TRUE;
slouken@778
   324
	}
slouken@778
   325
	return SDL_FALSE;
slouken@778
   326
}
slouken@778
   327
slouken@785
   328
SDL_bool SDL_HasMMXExt()
slouken@785
   329
{
slouken@785
   330
   if ( SDL_GetCPUFeatures() & CPU_HAS_MMXEXT ) {
slouken@785
   331
      return SDL_TRUE;
slouken@785
   332
   }
slouken@785
   333
   return SDL_FALSE;
slouken@785
   334
}
slouken@785
   335
slouken@785
   336
SDL_bool SDL_Has3DNowExt()
slouken@785
   337
{
slouken@785
   338
   if ( SDL_GetCPUFeatures() & CPU_HAS_3DNOWEXT ) {
slouken@785
   339
      return SDL_TRUE;
slouken@785
   340
   }
slouken@785
   341
   return SDL_FALSE;
slouken@785
   342
}
slouken@785
   343
slouken@785
   344
SDL_bool SDL_HasSSE2()
slouken@785
   345
{
slouken@785
   346
   if ( SDL_GetCPUFeatures() & CPU_HAS_SSE2 ) {
slouken@785
   347
      return SDL_TRUE;
slouken@785
   348
   }
slouken@785
   349
   return SDL_FALSE;
slouken@785
   350
}
slouken@785
   351
slouken@739
   352
#ifdef TEST_MAIN
slouken@739
   353
slouken@739
   354
#include <stdio.h>
slouken@739
   355
slouken@739
   356
int main()
slouken@739
   357
{
slouken@778
   358
	printf("RDTSC: %d\n", SDL_HasRDTSC());
slouken@739
   359
	printf("MMX: %d\n", SDL_HasMMX());
slouken@785
   360
	printf("MMXExt: %d\n", SDL_HasMMXExt());
slouken@739
   361
	printf("3DNow: %d\n", SDL_Has3DNow());
slouken@785
   362
	printf("3DNowExt: %d\n", SDL_Has3DNowExt());
slouken@739
   363
	printf("SSE: %d\n", SDL_HasSSE());
slouken@785
   364
	printf("SSE2: %d\n", SDL_HasSSE2());
slouken@778
   365
	printf("AltiVec: %d\n", SDL_HasAltiVec());
slouken@745
   366
	return 0;
slouken@739
   367
}
slouken@739
   368
slouken@739
   369
#endif /* TEST_MAIN */