4 ms·
> It seems Intel CPU's have such a bloated and varied instruction set you have to query to see what flavor of CPU you have to create portable code. Writing comp
by loeg 6y ago
> It seems Intel CPU's have such a bloated and varied instruction set you have to query to see what flavor of CPU you have to create portable code. Writing compilers for this must be a real pain.
This is equally applicable to other x86 vendors.
The alternatives are either (a) never introducing new instructions, or (b) making portable binaries impossible. Neither alternative seems better? It doesn't look quite as tortured in C:
bool has_popcnt() {
unsigned regs[4];
cpuid(1, regs);
// You could name 23 CPUID_POPCOUNT_SHIFT or something.
return ((regs[2] >> 23) & 1);
}
In normal C compilers (GCC, Clang) you can use ifuncs to do this resolution once at module load time, and then the correct function for the current CPU is dispatched cheaply afterwards.
- waynecochran 6y agoYes, adding features to a CPU is not a vendor specific problem. I wonder if this can be handled at the link / binary loader level so it is more transparent to the programmer. Almost like a patch using diff. You have a base binary that runs on any machine, but there are added diffs (added by the static linker) for different CPU versions and the OS loader patches and loads the code appropriately.
- loeg 6y agohttps://gcc.gnu.org/wiki/FunctionMultiVersioning https://gcc.gnu.org/wiki/FunctionMultiVersioning helps make this somewhat ergonomic, although it's still manual.
- Const-me 6y agoThese SIMD instructions are low-level micro-optimizations. Abstract an instruction behind a function call, and even for the very best case when all source data is already in correct registers and no need to save or restore anything, that indirect jump is likely to take the time comparable to the instructions being used there. One way to do it in C++, implement dispatching at higher level, where the function being dispatched takes much more time than a call overhead. Sometimes C++ templates or even C preprocessor can help reduce copy-pasting. Another way to do it is JIT from byte code. .NET tries to do that, I don’t think it’s too efficient currently, but I believe couple major versions in the future it might actually become good. It doesn’t have performance budget for expensive optimization modern C++ compilers do in optimized release builds. OTOH there’re other optimizations unavailable in C++: JIT may use whatever max AVX version is present, may compile immutable or rarely changing input data into code.
- creato 6y ago> This is equally applicable to other x86 vendors. I think it's equally applicable to any processor vendor, x86 has just been around the longest.
- eqvinox 6y agoIt's actually particularly shitty on x86, because, unlike SSE2, it has been added after AMD64 came around, and thus you can't necessarily assume its existence on a random 64-bit CPU. Meanwhile any ARM64 CPU will have CNT.