Acceptable use cases for reinterpret_cast for unallocated memory access vs memcpy?

There is a conditionally compiled section inside snappy elements that selects dereferencing the reinterpret_cast'ed pointer as the best implementation for reading and writing potentially unaligned 16, 32, and 64-bit integers on architectures that are known to support such operations (e.g. x86 ) A reserve for other architectures is to use a memcpy implementation .

My understanding is that the reinterpret_cast implementation demonstrates undefined behavior, and clang undefined default sanitizer does this.

What puzzles me: why not just use a memcpy based implementation? I would expect that all but the most broken compilers would use intrinsics to implement these memcpy calls, since the size is known at compile time. In fact, I would expect identical code from both implementations on any modern toolchain.

However, I also admit that instant was written by people who know what they are talking about. Therefore, it remains for me to wonder if there is any other advantage in using the reinterpret_cast mechanism, which outweighs its undefined behavior. Don't want performance to depend on compiler implementation quality? Something else I did not consider?

+4
source share
2 answers

Not knowing the programmer (s) who wrote this code in the first place, I doubt that you can get a truly authoritative answer.

Here is my best guess: the authors did not want to rely on possible optimization memcpy(which is in no way guaranteed by the specification, even if it is implemented by many compilers). On the other hand, writing is reinterpret_castvery, very likely to lead to the simply unheaded access instruction that the authors expected on almost any compiler.

memcpy, . , , , ( reinterpret_cast UB) .

+2

, ( x86) int , , .

2- . memcpy 4 , 4 ( 32- , ), . , .

4 .

-1

Source: https://habr.com/ru/post/1526047/


All Articles