Get the latest tech news

Own Constant Folder in C/C++


I was talking with someone today that really really wanted the sqrtps to be used in some code they were writing. And because of a quirk with clang (still there as of clang 18.1.0), if you happened to use -ffast-math clang would butcher the use of the intrinsic. So for the code: __m128 test(const __m128 vec) { return _mm_sqrt_ps(vec); } Clang would compile it correctly without fast-math: test: # @test sqrtps xmm0, xmm0 ret And create this monstrosity with -ffast-math:

But that has precision issues between Intel and AMD due to a high ULP tolerance for the rsqrtps instruction. So LLVM does two newton-raphson iterations anytime it calls rsqrtps to correct the precision between the CPU implementations. Turns out GCC is a bit picky with this builtin, and it looks like LLVM has inherited that funky behaviour.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of C++

C++

Photo of Constant Folder

Constant Folder

Related news:

News photo

Is C++ More Popular Than C?

News photo

Counting words in Python, Go, C++, C, Awk, Forth, and Rust (2021)

News photo

Webview: Tiny cross-platform webview library for C/C++