Kitz ADSL Broadband Information
adsl spacer  
Support this site
Home Broadband ISPs Tech Routers Wiki Forum
 
     
   Compare ISP   Rate your ISP
   Glossary   Glossary
 
Please login or register.

Login with username, password and session length
Advanced search  

News:

Author Topic: Processor instruction set detection  (Read 288 times)

Weaver

  • Addicted Kitizen
  • *****
  • Posts: 9484
  • Retd sw dev; A&A; 4 ◊ 7km ADSL2; IPv6; Firebrick
Processor instruction set detection
« on: November 17, 2020, 03:37:27 PM »

If youíre writing some low level code, some asm in C/C++/D and you want to use one of the newer instructions available in your processor family, say the Haswell and above instructions in x86, then how do you do it with fallback to equivalent software? Well what I would do is use eg CPUID to detect the instructionís availability and store that in a static flag, then have a conditional branch and test on the flag in front of the instruction in some subroutine that gets inlined in the calling code. But this is a nightmare because accessing global statics can be slow and so can conditional branches if they are mispredicted, which hopefully shouldnít happen.

What I really want to do, which is what I once saw a compiler do with 80x87 FP instructions, is to patch the code via a patch offset table and turn it into either new instructions or calls (or short jumps to calls) to software emulation. I donít think you can easily do that because modern CPUs make sure that code segments are not modifiable? Also calculation of the offset table - how the hell would you do that? It sounds like a nightmare. That old compiler (JPI C for 8086) I mentioned could do it because it was in charge and it was generating that code so it knew where it had placed the problem FP instructions. Iíve seen VAX/VMS patch code too, at link time or is it load time[?], I forget, when they are binding in either statically linked or dynamically linked (shared) libraries.

Have I got this wrong? Am I missing something? Iím wondering what other people do.
Logged

Bill Moo

  • Just arrived
  • *
  • Posts: 6
Re: Processor instruction set detection
« Reply #1 on: November 21, 2020, 02:32:05 PM »

Having read a few more posts I may well be teaching you to suck eggs so apologies in advance.

Assuming my understanding is correct you could write yourself a library that exposes your requirements as logic tests; for example hasSSE(), hasSSE4-1(), etc. On calling these methods the eax register is set as appropriate and the ecx and edx resister values tested as need in order to return a bool.

This way you could :

if (hasSSE4.1()) {
        doThis() ;
} else {
   doThisInstead() ;
}


Of course the library could be used with ASM, C, C++ or any other language you want to write bindings for.

--
Bill
« Last Edit: November 21, 2020, 02:37:51 PM by Bill Moo »
Logged
--
Bill

Weaver

  • Addicted Kitizen
  • *****
  • Posts: 9484
  • Retd sw dev; A&A; 4 ◊ 7km ADSL2; IPv6; Firebrick
Re: Processor instruction set detection
« Reply #2 on: November 21, 2020, 03:13:39 PM »

Whatís worrying me is the overhead of those conditional jumps and the memory fetch associated with a static. It can be more than the cost of the new instruction youíre trying to use.
Logged