[cfe-dev] Converting float to int with FJCVTZS

Johannes Hoff via cfe-dev cfe-dev at lists.llvm.org
Fri Mar 5 01:45:33 PST 2021


I'm working on a code base where a simulation needs to produce the exact same result on Aarch64 and x86_64 architectures.

This is indeed the case for the whole codebase, with one exception: Rounding floats to integers. Specifically, when we're in undefined behavior territory. In that case, you notice the difference between the emitted fcvtzu instruction on aarch64 (saturating cast) and cvttss2si on x86 (wrap-around).

Now, I know undefined behavior is not the main business of LLVM, but I wonder if it would be possible to ask it to emit FJCVTZS instead, which behaves like x86 outside of the integer range. Of course, this would be an opt-in flag.

What do you think? If it's not something that would be valuable for clang, do you have any pointers on how to patch it myself?

Of course, I can just use the compiler intrinsic __builtin_arm_jcvt to trigger this behavior, but then I need to be sure to catch all the places, and be sure that everyone on the team remembers to do the same in the future.


More information about the cfe-dev mailing list