Funny FPU trick I discovered yesterday to detect arm64 or x86_64 architecture at runtime

@pancake

Interesting.

Drop stdint and workaround it. See what happens.

@SpaceLifeForm nothing happens. same results. use "unsigned long long" instead of uint64_t if you want

@pancake

This gets curiouser and curiouser.

I am thinking it is due to microcode differences.

Actually, just the instruction set.

Something is happening differently because sqrt(-1) is using an invalid parameter to the call in the first place.

errno needs to be checked immediately.

@pancake

Also, compile without optimization.

@SpaceLifeForm same results with -O0, or -O3 for me, clang and gcc on linux and mac
@pancake pretty neat; I have tested on Linux arm64 and x86_64; both works!

@pancake now try with musl, i386, m68k, ppc64el, sparc64, s390x, …

As if there were only two architectures in existence.

@mirabilos musl/glibc/bsd/linux/macos/windows have nothing to do with that. it's just how the FPU behaves (already tested anyway). About other archs would be cool to test indeed, but i have other important tasks to do and this finding was just a corner case of what I was looking for. Feel free to post the results
@pancake only if it maps directly to the FPU and does not do anything else.
@mirabilos and that's what it is

@pancake

Another thought. Did you rebuild libm from source without optimisation?

@pancake absolutely cursed. please make more.
@pancake I hardly know any C/C++ but even I can tell this is cursed.
@pancake What? Lmao. What strange behavior.
@pancake How did you even discover this?
@pancake According to IEEE 754, that’s just the difference between a quiet NaN and a signaling NaN. No idea why both platforms would handle that differently by default, but I think to get reproducible behavior you’d have to explicitly configure the FPU before you try to take the square root of a number smaller than -0. So I would not depend on it in any kind of portable code. Interesting behavior, though.
@pancake ah, is this taking advantage of the fact that x87 has an 80-bit fpu while arm is 64-bit?
@pancake how does this behave on RISCV & other cute less common architectures?
@pancake isn't there a more respectable function that already checks that?