til

How to know if a CPU is x86

javascript

It's possible to see whether a CPU is x86 based using the following snippet.

function isX86() {
  const f = new Float32Array(1);
  const u8 = new Uint8Array(f.buffer);
  f[0] = Infinity;
  f[0] = f[0] - f[0];
  return u8[3] == 255;
}

The exaplanation of this is commented in the Tensorflow codebase and is worthwhile to read:

Unlike most other architectures, on x86/x86-64 when floating-point instructions have no NaN arguments, but produce NaN output, the output NaN has sign bit set. We use it to distinguish x86/x86-64 from other architectures, by doing subtraction of two infinites (must produce NaN per IEEE 754 standard).

I learnt this from @robknight\_