[llvm-dev] [cfe-dev] Should isnan be optimized out in fast-math mode?

James Y Knight via llvm-dev llvm-dev at lists.llvm.org
Mon Sep 13 04:59:45 PDT 2021


On Mon, Sep 13, 2021, 2:02 AM Serge Pavlov via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> The working construct is `reinterpret_cast<uint32_t&>(x)`. It however
> possesses the same drawback, it requires `x` be in memory.
>

We're getting rather far afield of the thread topic here, but .. that is
UB, don't do that.

Instead, always memcpy, e.g.
uint32_t y;
memcpy(&y, &flo, sizeof(uint32_t));

Or use a wrapper like std::bit_cast or absl::bit_cast (
https://github.com/abseil/abseil-cpp/blob/cfbf5bf948a2656bda7ddab59d3bcb29595c144c/absl/base/casts.h#L106
).

This has effectively no runtime overhead, the compiler is extremely good at
deleting calls to memcpy when it has a constant smallish size. And remember
that *every* local variable started out in memory. Only through
optimizations does the memory location and the loads/stores for every
access get eliminated.

>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20210913/d34aafc8/attachment.html>


More information about the llvm-dev mailing list