不可知性阅读的首选成语 [英] Preferred idiom for endianess-agnostic reads

查看:108
本文介绍了不可知性阅读的首选成语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Plan 9源代码中,我经常会找到这样的代码,以从具有明确定义的字节序的缓冲区中读取序列化数据:

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 <stdint.h>

uint32_t le32read(uint8_t buf[static 4]) {
    return (buf[0] | buf[1] << 8 | buf[2] << 16 | buf[3] << 24);
}

我希望gcc和clang都可以将此代码编译为与amd64上的程序集一样简单的代码:

I expected both gcc and clang to compile this code into something as simple as this assembly on amd64:

    .global le32read
    .type le32read,@function
le32read:
    mov (%rdi),%eax
    ret
    .size le32read,.-le32read

但是与我的预期相反,gcc和clang都没有意识到这种模式,而是产生了多班次的复杂装配.

But contrary to my expectations, neither gcc nor clang recognize this pattern and produce complex assembly with multiple shifts instead.

这种操作是否有一种习俗,既可以移植到所有C99实现中,又可以在各种实现中生成良好的代码(例如上面介绍的代码)?

Is there an idiom for this kind of operation that is both portable to all C99-implementations and produces good (i.e. like the one presented above) code across implementations?

推荐答案

经过研究,我发现(在Freenode上## c的出色人员的帮助下),gcc 5.0将实现这种模式的优化如上所述.实际上,它会将问题中列出的C源代码编译为下面列出的确切程序集.

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.

我没有找到有关c的类似信息,所以我提交了错误报告. Clang 9.0 ,clang识别读取和写入习惯用法,并将其转换为快速代码.

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.

这篇关于不可知性阅读的首选成语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆