blob: 4ccbee38b2f54b2d18bf5fae5174e3e47a6a2145 [file] [log] [blame]
nethercote288b7662004-01-20 09:24:53 +00001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
nethercoteb625bcb2004-10-19 16:29:30 +00005// We return 0 if the machine matches the asked-for cpu, 1 otherwise.
6
7#ifdef __x86__
nethercote1018bdd2004-02-11 23:33:29 +00008static __inline__ void cpuid(unsigned int n,
nethercote288b7662004-01-20 09:24:53 +00009 unsigned int *a, unsigned int *b,
10 unsigned int *c, unsigned int *d)
11{
12 __asm__ __volatile__ (
13 "cpuid"
14 : "=a" (*a), "=b" (*b), "=c" (*c), "=d" (*d) /* output */
15 : "0" (n) /* input */
16 );
17}
18
nethercoteb625bcb2004-10-19 16:29:30 +000019static int go(char* cpu)
20{
21 unsigned int level = 0, mask = 0, a, b, c, d;
nethercote288b7662004-01-20 09:24:53 +000022
nethercoteb625bcb2004-10-19 16:29:30 +000023 if ( strcmp( cpu, "x86" ) == 0 ) {
24 level = 1;
25 mask = 1 << 0;
26 } else if ( strcmp( cpu, "x86-fpu" ) == 0 ) {
27 level = 1;
28 mask = 1 << 0;
29 } else if ( strcmp( cpu, "x86-cmov" ) == 0 ) {
30 level = 1;
31 mask = 1 << 15;
32 } else if ( strcmp( cpu, "x86-mmx" ) == 0 ) {
33 level = 1;
34 mask = 1 << 23;
35 } else if ( strcmp( cpu, "x86-mmxext" ) == 0 ) {
36 level = 0x80000001;
37 mask = 1 << 22;
38 } else if ( strcmp( cpu, "x86-sse" ) == 0 ) {
39 level = 1;
40 mask = 1 << 25;
41 } else if ( strcmp( cpu, "x86-sse2" ) == 0 ) {
42 level = 1;
43 mask = 1 << 26;
44 } else {
45 return 1;
nethercote288b7662004-01-20 09:24:53 +000046 }
47
nethercote1018bdd2004-02-11 23:33:29 +000048 cpuid( level & 0x80000000, &a, &b, &c, &d );
nethercote288b7662004-01-20 09:24:53 +000049
nethercote1018bdd2004-02-11 23:33:29 +000050 if ( a >= level ) {
51 cpuid( level, &a, &b, &c, &d );
nethercote288b7662004-01-20 09:24:53 +000052
nethercoteb625bcb2004-10-19 16:29:30 +000053 if ( ( d & mask ) != 0 ) return 0;
nethercote288b7662004-01-20 09:24:53 +000054 }
nethercoteb625bcb2004-10-19 16:29:30 +000055 return 1;
56}
57#endif // __x86__
nethercote288b7662004-01-20 09:24:53 +000058
nethercoteb625bcb2004-10-19 16:29:30 +000059
60#ifdef __ppc__
61static int go(char* cpu)
62{
63 if ( strcmp( cpu, "ppc" ) == 0 )
64 return 0;
65 else
66 return 1;
67}
68#endif // __ppc__
69
70
71int main(int argc, char **argv)
72{
73 if ( argc != 2 ) {
74 fprintf( stderr, "usage: cputest <cpu-type>\n" );
75 exit( 1 );
76 }
77 return go( argv[1] );
nethercote288b7662004-01-20 09:24:53 +000078}