Dual CPU code

Walter Bright newshound1 at digitalmars.com
Mon Feb 2 11:59:44 PST 2009


bearophile wrote:
> So, it may be useful to have a way to build executables able to run
> well on both CPUs (Apple has done something like this two or more
> times in the past). There are several ways to do this, a solution is
> to compile just critical functions for different CPUs, but that may
> require compiler support. My executables are generally small, so
> doubling their size isn't a problem. So a simple solution is to
> bundle two whole executables into an executable and add a small
> header that looks for the current CPU, and runs the right executable.


This is a very old problem, it even cropped up in the bad old DOS days 
where you had the choice of emulator or FPU. The solution is fairly 
simple - you don't need to bind together two executables. Simply put a 
runtime switch in:


import std.cpuid;
import sse;
import nosse;

...

if (std.cpuid.sse2())
     sse2.foo();
else
     nosse2.foo();


and then compile sse.d and nosse.d with different compiler switches. The 
std.cpuid module will tell you what you've got at runtime. To see a real 
example of this, look at the array op implementation code in the 
standard library, such as internal/arrayfloat.d, it does a runtime 
switch for several different FPU flavors.



More information about the Digitalmars-d mailing list