src/cpuinfo/SDL_cpuinfo.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 24 Jan 2004 05:47:19 +0000
changeset 785 ca06a994f03c
parent 784 a2dde6aff60e
child 786 e1e0a0a94570
permissions -rw-r--r--
Fixed bugs in CPU feature detection and added extended feature detection
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@739
    30
#include "SDL.h"
slouken@745
    31
#include "SDL_cpuinfo.h"
slouken@739
    32
slouken@778
    33
#ifdef MACOSX
slouken@778
    34
#include <sys/sysctl.h> /* For AltiVec check */
slouken@778
    35
#endif
slouken@778
    36
slouken@745
    37
#define CPU_HAS_RDTSC	0x00000001
slouken@745
    38
#define CPU_HAS_MMX	0x00000002
slouken@785
    39
#define CPU_HAS_MMXEXT	0x00000004
slouken@785
    40
#define CPU_HAS_3DNOW	0x00000010
slouken@785
    41
#define CPU_HAS_3DNOWEXT 0x00000020
slouken@785
    42
#define CPU_HAS_SSE	0x00000040
slouken@785
    43
#define CPU_HAS_SSE2	0x00000080
slouken@785
    44
#define CPU_HAS_ALTIVEC	0x00000100
slouken@739
    45
slouken@745
    46
static __inline__ int CPU_haveCPUID()
slouken@745
    47
{
slouken@745
    48
	int has_CPUID = 0;
slouken@745
    49
#if defined(__GNUC__) && defined(i386)
slouken@745
    50
	__asm__ (
slouken@745
    51
"        pushfl                      # Get original EFLAGS             \n"
slouken@745
    52
"        popl    %%eax                                                 \n"
slouken@745
    53
"        movl    %%eax,%%ecx                                           \n"
slouken@745
    54
"        xorl    $0x200000,%%eax     # Flip ID bit in EFLAGS           \n"
slouken@745
    55
"        pushl   %%eax               # Save new EFLAGS value on stack  \n"
slouken@745
    56
"        popfl                       # Replace current EFLAGS value    \n"
slouken@745
    57
"        pushfl                      # Get new EFLAGS                  \n"
slouken@745
    58
"        popl    %%eax               # Store new EFLAGS in EAX         \n"
slouken@745
    59
"        xorl    %%ecx,%%eax         # Can not toggle ID bit,          \n"
slouken@745
    60
"        jz      1f                  # Processor=80486                 \n"
slouken@745
    61
"        movl    $1,%0               # We have CPUID support           \n"
slouken@745
    62
"1:                                                                    \n"
slouken@784
    63
	: "=m" (has_CPUID)
slouken@745
    64
	:
slouken@745
    65
	: "%eax", "%ecx"
slouken@745
    66
	);
slouken@745
    67
#elif defined(_MSC_VER)
slouken@749
    68
	__asm {
slouken@745
    69
        pushfd                      ; Get original EFLAGS
slouken@745
    70
        pop     eax
slouken@745
    71
        mov     ecx, eax
slouken@745
    72
        xor     eax, 200000h        ; Flip ID bit in EFLAGS
slouken@745
    73
        push    eax                 ; Save new EFLAGS value on stack
slouken@745
    74
        popfd                       ; Replace current EFLAGS value
slouken@745
    75
        pushfd                      ; Get new EFLAGS
slouken@745
    76
        pop     eax                 ; Store new EFLAGS in EAX
slouken@745
    77
        xor     eax, ecx            ; Can not toggle ID bit,
slouken@745
    78
        jz      done                ; Processor=80486
slouken@745
    79
        mov     has_CPUID,1         ; We have CPUID support
slouken@745
    80
done:
slouken@745
    81
	}
slouken@745
    82
#endif
slouken@745
    83
	return has_CPUID;
slouken@745
    84
}
slouken@745
    85
slouken@745
    86
static __inline__ int CPU_getCPUIDFeatures()
slouken@745
    87
{
slouken@745
    88
	int features = 0;
slouken@745
    89
#if defined(__GNUC__) && defined(i386)
slouken@745
    90
	__asm__ (
slouken@785
    91
"        movl    %%ebx,%%edi\n"
slouken@745
    92
"        xorl    %%eax,%%eax         # Set up for CPUID instruction    \n"
slouken@745
    93
"        cpuid                       # Get and save vendor ID          \n"
slouken@745
    94
"        cmpl    $1,%%eax            # Make sure 1 is valid input for CPUID\n"
slouken@745
    95
"        jl      1f                  # We dont have the CPUID instruction\n"
slouken@745
    96
"        xorl    %%eax,%%eax                                           \n"
slouken@745
    97
"        incl    %%eax                                                 \n"
slouken@745
    98
"        cpuid                       # Get family/model/stepping/features\n"
slouken@745
    99
"        movl    %%edx,%0                                              \n"
slouken@745
   100
"1:                                                                    \n"
slouken@785
   101
"        movl    %%edi,%%ebx\n"
slouken@784
   102
	: "=m" (features)
slouken@745
   103
	:
slouken@785
   104
	: "%eax", "%ebx", "%ecx", "%edx", "%edi"
slouken@745
   105
	);
slouken@745
   106
#elif defined(_MSC_VER)
slouken@749
   107
	__asm {
slouken@745
   108
        xor     eax, eax            ; Set up for CPUID instruction
slouken@745
   109
        cpuid                       ; Get and save vendor ID
slouken@745
   110
        cmp     eax, 1              ; Make sure 1 is valid input for CPUID
slouken@745
   111
        jl      done                ; We dont have the CPUID instruction
slouken@745
   112
        xor     eax, eax
slouken@745
   113
        inc     eax
slouken@745
   114
        cpuid                       ; Get family/model/stepping/features
slouken@745
   115
        mov     features, edx
slouken@745
   116
done:
slouken@745
   117
	}
slouken@745
   118
#endif
slouken@745
   119
	return features;
slouken@745
   120
}
slouken@745
   121
slouken@785
   122
static __inline__ int CPU_getCPUIDFeaturesExt()
slouken@785
   123
{
slouken@785
   124
	int features = 0;
slouken@785
   125
#if defined(__GNUC__) && defined(i386)
slouken@785
   126
	__asm__ (
slouken@785
   127
"        movl    %%ebx,%%edi\n"
slouken@785
   128
"        movl    $0x80000000,%%eax   # Query for extended functions    \n"
slouken@785
   129
"        cpuid                       # Get extended function limit     \n"
slouken@785
   130
"        cmpl    $0x80000001,%%eax                                     \n"
slouken@785
   131
"        jbe     1f                  # Nope, we dont have function 800000001h\n"
slouken@785
   132
"        movl    $0x80000001,%%eax   # Setup extended function 800000001h\n"
slouken@785
   133
"        cpuid                       # and get the information         \n"
slouken@785
   134
"        movl    %%edx,%0                                              \n"
slouken@785
   135
"1:                                                                    \n"
slouken@785
   136
"        movl    %%edi,%%ebx\n"
slouken@785
   137
	: "=m" (features)
slouken@785
   138
	:
slouken@785
   139
	: "%eax", "%ebx", "%ecx", "%edx", "%edi"
slouken@785
   140
	);
slouken@785
   141
#elif defined(_MSC_VER)
slouken@785
   142
	__asm {
slouken@785
   143
        mov     eax,80000000h       ; Query for extended functions
slouken@785
   144
        cpuid                       ; Get extended function limit
slouken@785
   145
        cmp     eax,80000001h
slouken@785
   146
        jbe     done                ; Nope, we dont have function 800000001h
slouken@785
   147
        mov     eax,80000001h       ; Setup extended function 800000001h
slouken@785
   148
        cpuid                       ; and get the information
slouken@785
   149
        mov     features,edx
slouken@785
   150
done:
slouken@785
   151
	}
slouken@785
   152
#endif
slouken@785
   153
	return features;
slouken@785
   154
}
slouken@785
   155
slouken@745
   156
static __inline__ int CPU_haveRDTSC()
slouken@745
   157
{
slouken@745
   158
	if ( CPU_haveCPUID() ) {
slouken@745
   159
		return (CPU_getCPUIDFeatures() & 0x00000010);
slouken@745
   160
	}
slouken@745
   161
	return 0;
slouken@745
   162
}
slouken@745
   163
slouken@745
   164
static __inline__ int CPU_haveMMX()
slouken@745
   165
{
slouken@745
   166
	if ( CPU_haveCPUID() ) {
slouken@745
   167
		return (CPU_getCPUIDFeatures() & 0x00800000);
slouken@745
   168
	}
slouken@745
   169
	return 0;
slouken@745
   170
}
slouken@745
   171
slouken@785
   172
static __inline__ int CPU_haveMMXExt()
slouken@785
   173
{
slouken@785
   174
	if ( CPU_haveCPUID() ) {
slouken@785
   175
		return (CPU_getCPUIDFeaturesExt() & 0x00400000);
slouken@785
   176
	}
slouken@785
   177
	return 0;
slouken@785
   178
}
slouken@785
   179
slouken@745
   180
static __inline__ int CPU_have3DNow()
slouken@745
   181
{
slouken@785
   182
	if ( CPU_haveCPUID() ) {
slouken@785
   183
		return (CPU_getCPUIDFeaturesExt() & 0x80000000);
slouken@747
   184
	}
slouken@785
   185
	return 0;
slouken@785
   186
}
slouken@785
   187
slouken@785
   188
static __inline__ int CPU_have3DNowExt()
slouken@785
   189
{
slouken@785
   190
	if ( CPU_haveCPUID() ) {
slouken@785
   191
		return (CPU_getCPUIDFeaturesExt() & 0x40000000);
slouken@745
   192
	}
slouken@785
   193
	return 0;
slouken@745
   194
}
slouken@745
   195
slouken@745
   196
static __inline__ int CPU_haveSSE()
slouken@745
   197
{
slouken@745
   198
	if ( CPU_haveCPUID() ) {
slouken@745
   199
		return (CPU_getCPUIDFeatures() & 0x02000000);
slouken@745
   200
	}
slouken@745
   201
	return 0;
slouken@745
   202
}
slouken@739
   203
slouken@785
   204
static __inline__ int CPU_haveSSE2()
slouken@785
   205
{
slouken@785
   206
	if ( CPU_haveCPUID() ) {
slouken@785
   207
		return (CPU_getCPUIDFeatures() & 0x04000000);
slouken@785
   208
	}
slouken@785
   209
	return 0;
slouken@785
   210
}
slouken@785
   211
slouken@778
   212
static __inline__ int CPU_haveAltiVec()
slouken@778
   213
{
slouken@778
   214
#ifdef MACOSX
slouken@778
   215
	/* TODO: This check works on OS X. It would be nice to detect AltiVec
slouken@778
   216
	   properly on for example Linux/PPC, too. But I don't know how that
slouken@778
   217
	   is done in Linux (or FreeBSD, or whatever other OS you run PPC :-)
slouken@778
   218
	 */
slouken@778
   219
	int selectors[2] = { CTL_HW, HW_VECTORUNIT }; 
slouken@778
   220
	int hasVectorUnit = 0; 
slouken@778
   221
	size_t length = sizeof(hasVectorUnit); 
slouken@778
   222
	int error = sysctl(selectors, 2, &hasVectorUnit, &length, NULL, 0); 
slouken@778
   223
	if( 0 == error )
slouken@778
   224
		return hasVectorUnit != 0; 
slouken@778
   225
#endif
slouken@778
   226
	return 0; 
slouken@778
   227
}
slouken@778
   228
slouken@739
   229
static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
slouken@739
   230
slouken@739
   231
static Uint32 SDL_GetCPUFeatures()
slouken@739
   232
{
slouken@739
   233
	if ( SDL_CPUFeatures == 0xFFFFFFFF ) {
slouken@739
   234
		SDL_CPUFeatures = 0;
slouken@745
   235
		if ( CPU_haveRDTSC() ) {
slouken@745
   236
			SDL_CPUFeatures |= CPU_HAS_RDTSC;
slouken@745
   237
		}
slouken@739
   238
		if ( CPU_haveMMX() ) {
slouken@739
   239
			SDL_CPUFeatures |= CPU_HAS_MMX;
slouken@739
   240
		}
slouken@739
   241
		if ( CPU_have3DNow() ) {
slouken@739
   242
			SDL_CPUFeatures |= CPU_HAS_3DNOW;
slouken@739
   243
		}
slouken@739
   244
		if ( CPU_haveSSE() ) {
slouken@739
   245
			SDL_CPUFeatures |= CPU_HAS_SSE;
slouken@739
   246
		}
slouken@778
   247
		if ( CPU_haveAltiVec() ) {
slouken@778
   248
			SDL_CPUFeatures |= CPU_HAS_ALTIVEC;
slouken@778
   249
		}
slouken@785
   250
      if ( CPU_haveMMXExt() ) {
slouken@785
   251
         SDL_CPUFeatures |= CPU_HAS_MMXEXT;
slouken@785
   252
      }
slouken@785
   253
      if ( CPU_have3DNowExt() ) {
slouken@785
   254
         SDL_CPUFeatures |= CPU_HAS_3DNOWEXT;
slouken@785
   255
      }
slouken@785
   256
      if ( CPU_haveSSE2() ) {
slouken@785
   257
         SDL_CPUFeatures |= CPU_HAS_SSE2;
slouken@785
   258
      }
slouken@739
   259
	}
slouken@739
   260
	return SDL_CPUFeatures;
slouken@739
   261
}
slouken@739
   262
slouken@745
   263
SDL_bool SDL_HasRDTSC()
slouken@745
   264
{
slouken@745
   265
	if ( SDL_GetCPUFeatures() & CPU_HAS_RDTSC ) {
slouken@745
   266
		return SDL_TRUE;
slouken@745
   267
	}
slouken@745
   268
	return SDL_FALSE;
slouken@745
   269
}
slouken@745
   270
slouken@739
   271
SDL_bool SDL_HasMMX()
slouken@739
   272
{
slouken@739
   273
	if ( SDL_GetCPUFeatures() & CPU_HAS_MMX ) {
slouken@739
   274
		return SDL_TRUE;
slouken@739
   275
	}
slouken@739
   276
	return SDL_FALSE;
slouken@739
   277
}
slouken@739
   278
slouken@739
   279
SDL_bool SDL_Has3DNow()
slouken@739
   280
{
slouken@739
   281
	if ( SDL_GetCPUFeatures() & CPU_HAS_3DNOW ) {
slouken@739
   282
		return SDL_TRUE;
slouken@739
   283
	}
slouken@739
   284
	return SDL_FALSE;
slouken@739
   285
}
slouken@739
   286
slouken@739
   287
SDL_bool SDL_HasSSE()
slouken@739
   288
{
slouken@739
   289
	if ( SDL_GetCPUFeatures() & CPU_HAS_SSE ) {
slouken@739
   290
		return SDL_TRUE;
slouken@739
   291
	}
slouken@739
   292
	return SDL_FALSE;
slouken@739
   293
}
slouken@739
   294
slouken@778
   295
SDL_bool SDL_HasAltiVec()
slouken@778
   296
{
slouken@778
   297
	if ( SDL_GetCPUFeatures() & CPU_HAS_ALTIVEC ) {
slouken@778
   298
		return SDL_TRUE;
slouken@778
   299
	}
slouken@778
   300
	return SDL_FALSE;
slouken@778
   301
}
slouken@778
   302
slouken@785
   303
SDL_bool SDL_HasMMXExt()
slouken@785
   304
{
slouken@785
   305
   if ( SDL_GetCPUFeatures() & CPU_HAS_MMXEXT ) {
slouken@785
   306
      return SDL_TRUE;
slouken@785
   307
   }
slouken@785
   308
   return SDL_FALSE;
slouken@785
   309
}
slouken@785
   310
slouken@785
   311
SDL_bool SDL_Has3DNowExt()
slouken@785
   312
{
slouken@785
   313
   if ( SDL_GetCPUFeatures() & CPU_HAS_3DNOWEXT ) {
slouken@785
   314
      return SDL_TRUE;
slouken@785
   315
   }
slouken@785
   316
   return SDL_FALSE;
slouken@785
   317
}
slouken@785
   318
slouken@785
   319
SDL_bool SDL_HasSSE2()
slouken@785
   320
{
slouken@785
   321
   if ( SDL_GetCPUFeatures() & CPU_HAS_SSE2 ) {
slouken@785
   322
      return SDL_TRUE;
slouken@785
   323
   }
slouken@785
   324
   return SDL_FALSE;
slouken@785
   325
}
slouken@785
   326
slouken@739
   327
#ifdef TEST_MAIN
slouken@739
   328
slouken@739
   329
#include <stdio.h>
slouken@739
   330
slouken@739
   331
int main()
slouken@739
   332
{
slouken@778
   333
	printf("RDTSC: %d\n", SDL_HasRDTSC());
slouken@739
   334
	printf("MMX: %d\n", SDL_HasMMX());
slouken@785
   335
	printf("MMXExt: %d\n", SDL_HasMMXExt());
slouken@739
   336
	printf("3DNow: %d\n", SDL_Has3DNow());
slouken@785
   337
	printf("3DNowExt: %d\n", SDL_Has3DNowExt());
slouken@739
   338
	printf("SSE: %d\n", SDL_HasSSE());
slouken@785
   339
	printf("SSE2: %d\n", SDL_HasSSE2());
slouken@778
   340
	printf("AltiVec: %d\n", SDL_HasAltiVec());
slouken@745
   341
	return 0;
slouken@739
   342
}
slouken@739
   343
slouken@739
   344
#endif /* TEST_MAIN */