我试图使用以下代码来模拟软件中的16位半浮点:
typedef struct half
{
unsigned short mantissa:10;
unsigned short exponent:5;
unsigned short sign:1;
} half;
unsigned short from_half(half h)
{
return h.mantissa | h.exponent << 10 | h.sign << 15;
}
half to_half(unsigned short s)
{
half result = { s, s >> 10, s >> 15 };
return result;
}我将其设置为便于将其优化为移动指令,但瞧,在from_half中,GCC无论如何都会做位转换(甚至在-O3):
from_half:
mov edx, edi
mov eax, edi
and di, 1023
shr dx, 15
and eax, 31744
movzx edx, dl
sal edx, 15
or eax, edx
or eax, edi
ret当to_half被很好地优化时:
to_half:
mov eax, edi
ret我尝试过不同的优化级别(-O1、-O2、-Os),但没有人将其优化到我所希望的程度。
即使在-O1,Clang也是这样做的
from_half: # @from_half
mov eax, edi
ret
to_half: # @to_half
mov eax, edi
ret我怎么才能让GCC把这个优化成一个动作呢?为什么还没有这样优化呢?
发布于 2020-03-07 20:03:44
除了布布的回答之外,您还可以尝试以下方法来回答您的问题
我怎么才能让GCC把这个优化成一个动作呢?
只将每个移位的位场表达式转换为unsigned short。
unsigned short from_half(half h)
{
return (unsigned short)h.mantissa | (unsigned short)(h.exponent << 10) | (unsigned short)(h.sign << 15);
}其结果是:
from_half:
mov eax, edi
ret为什么还没有这样优化呢?
我不确定我对这个问题有一个确切的答案。显然,将位字段提升到int的中间过程混淆了优化器.但这只是猜测。
发布于 2020-03-07 18:20:06
我已经有一段时间没有用C编写代码了,但似乎union的使用应该是有效的:
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
static bool useUnion;
__attribute__ ((__constructor__)) // supported by gcc compiler
static void initUseUnion()
{
union {
uint16_t i;
char c[2];
} n = { 0x0001 };
useUnion = n.c[0]; // little endian
}
typedef struct half
{
unsigned short mantissa:10;
unsigned short exponent:5;
unsigned short sign:1;
} half;
typedef union half_short
{
half h;
uint16_t s;
} half_short;
unsigned short from_half(half h)
{
if (useUnion) {
half_short hs;
hs.h = h;
return hs.s;
}
else {
return h.mantissa | h.exponent << 10 | h.sign << 15;
}
}
half to_half(unsigned short s)
{
if (useUnion) {
half_short hs;
hs.s = s;
return hs.h;
}
else {
half result = { s, s >> 10, s >> 15 };
return result;
}
}
int main(int argc, char* argv[])
{
printf("%d\n", useUnion);
return 0;
}https://stackoverflow.com/questions/60580591
复制相似问题