This section of the archives stores flipcode's complete Developer Toolbox collection,
featuring a variety of mini-articles and source code contributions from our readers.

CPU Detect
Submitted by

I've had programmers block for a while now and I just had to write something. I remembered
something about the assembler 'cpuid' instruction so I decided to test it out. I figured this sort
of thing might be useful if I am writting an application that required a certain processor or I was
just writting really optimized code. So I wrote this and compiled it as a static library. I also
wrote an application that will spit out the features of your processor, thats availabe at my website.

Heres the first part of the code, the [.h] file. Each of the differant features detectable are defined.
The cpuID function returns a pointer to the processor string (like the one in system under control
panel for you Windows people, like the string GenuineIntel or AuthenticAMD)
(Editor's note: see cpuid.h)
And now the actual function definitions. Some of it is written in assembler, I optimized
most of the code (instruction pairing, etc.) but I didn't look at it real closely. One more thing,
you might notice that there isn't a function to tell the frequency of the processor. As far
as I know (after reading the intel documents), 'cpuid' doesn't support this, well the Penium4
does. If you know how, I would love to hear it! Email me if you do-> Assembler015@hotmail.com
(Editor's note: see cpuid.c)
If you have any questions, email me. You can freely use this code, I don't care,
but if you do, I'd at least like to hear from you! Hope this stuff is useful.
-Andrew

/* When called, obtains a list of features the cpu supports */externunsignedint __cdecl cpuFeatures (void);
/*
Pass this one of the above defined features, -1 = error and
if it returns the same value passed the feature exists if it
returns 0 then the feature is un supported
*/externunsignedint __cdecl cpuCheckFeature (unsignedint feature);
/*
Returns a pointer to a string that the processor sends to
the EBX, EDX, and ECX registers. e.g. GenuineIntel or
AuthenticAMD.
*/externchar* __cdecl cpuID (void);