简体   繁体   English

是否可以在一条指令中将4 uint8_t复制到4 int16?

[英]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. 我想知道是否可以尽快将存储在一个uint32_t四个uint8_t值复制到uint64_t适当位置。 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. 问题是:我不能使用MMX / SSE。

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

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

Type punning through union does not have support in the C++ standard. 在C ++标准中不支持通过联合类型修剪。 Instead, use ors and shifts to compose the value together. 而是使用ors和shifts将值组合在一起。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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