Preferred idiom for endianess-agnostic reads

后端 未结 3 1439
耶瑟儿~
耶瑟儿~ 2021-01-02 09:38

In the Plan 9 source code I often find code like this to read serialised data from a buffer with a well-defined endianess:

#include 

uint32_         


        
3条回答
  •  旧巷少年郎
    2021-01-02 10:04

    After some research, I found (with the help of the terrific people in ##c on Freenode), that gcc 5.0 will implement optimizations for the kind of pattern described above. In fact, it compiles the C source listed in my question to the exact assembly I listed below.

    I haven't found similar information about clang, so I filed a bug report. As of Clang 9.0, clang recognises both the read as well as the write idiom and turns it into fast code.

提交回复
热议问题