简体   繁体   中英

Is it possible to copy 4 uint8_t into 4 int16 in one instruction?

I wonder if it is possible to copy four uint8_t values stored in one uint32_t into proper places in uint64_t as fast as possible. I am looking for equivalent of:

union
{
  struct {uint8_t a; uint8_t b; uint8_t c; uint8_t d};
  uint32_t whole;
} x32;

 union
{
  struct {int16_t a; int16_t b; int16_t c; int16_t d};
  uint64_t whole;
} x64;

x64.a=x32.a;
x64.b=x32.b;
x64.c=x32.c;
x64.d=x32.d;

The problem is: I cannot use MMX/SSE.

不会。没有其他方法可以像执行操作那样移动数据并将其零扩展

不,这是不可能的,因为几乎没有硬件会提供这种(非常特定的)组装指令。

Type punning through union does not have support in the C++ standard. Instead, use ors and shifts to compose the value together. Correctness is more important than fast but broken code.

uint8_t a,b,c,d;
uint64_t whole;

whole = a | (uint64_t (b) << 1*16) | (uint64_t (c) << 2*16) | (uint64_t (d) << 3*16)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM