Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What standard techniques are there for using cpu specific features in DLLs?

Short version: I'm wondering if it's possible, and how best, to utilise CPU specific instructions within a DLL?

Slightly longer version: When downloading (32bit) DLLs from, say, Microsoft it seems that one size fits all processors.

Does this mean that they are strictly built for the lowest common denominator (ie. the minimum platform supported by the OS)? Or is there some technique that is used to export a single interface within the DLL but utilise CPU specific code behind the scenes to get optimal performance? And if so, how is it done?

like image 553
Andrew Edgecombe Avatar asked Dec 22 '22 14:12

Andrew Edgecombe


2 Answers

I don't know of any standard technique but if I had to make such a thing, I would write some code in the DllMain() function to detect the CPU type and populate a jump table with function pointers to CPU-optimized versions of each function.

There would also need to be a lowest common denominator function for when the CPU type is unknown.

You can find current CPU info in the registry here:

HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor
like image 106
Adam Pierce Avatar answered Dec 26 '22 11:12

Adam Pierce


The DLL is expected to work on every computer WIN32 runs on, so you are stuck to the i386 instruction set in general. There is no official method of exposing functionality/code for specific instruction sets. You have to do it by hand and transparently.

The technique used basically is as follows: - determine CPU features like MMX, SSE in runtime - if they are present, use them, if not, have fallback code ready

Because you cannot let your compiler optimise for anything else than i386, you will have to write the code using the specific instruction sets in inline assembler. I don't know if there are higher-language toolkits for this. Determining the CPU features is straight forward, but could also need to be done in assembler.

like image 42
ypnos Avatar answered Dec 26 '22 11:12

ypnos