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 (st

Your Own Constant Folder in C/C++

submited by
Style Pass
2024-06-22 10:30:05

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:

The ‘fix’ here is just to use inline assembly to guarantee you’ll get the instruction selection you want always:

But there is one additional thing I’d advocate you do if you need to use inline assembly - write your own constant folding.

See the one downside to the inline assembly above is that if test is inlined and vec was a constant, it wouldn’t constant fold it. For example:

So that even under inlining, when we could have constant folded it away entirely, we are still calling sqrtps when we don’t have to. So what is the fix?

LLVM has an intrinsic is_constant which can be got at via the Clang-supported GCC extension __builtin_constant_p. If we extend our test above to check when vec is constant, we can call _mm_sqrt_ps when it is constant, and benefit from the constant folder doing its thing and removing the call entirely. So our code becomes:

Leave a Comment