是否可以在单个命令中存储8个字节
Is it possible to store 8 bytes in single command
我正在写一个实时代码,所以时间很重要
我使用C++英特尔本能
我想知道是否可以在一个命令中存储8个无符号字符(8个字节(,而不是使用循环?
char* buff = foo();
for (auto i = 0; i < 8; i++)
{
buff[i] = 0x00;
}
正在寻找类似的东西
_mm256_store_ps(...)
我想过这样做,但这不起作用(我不知道为什么(
_int64* buff = foo();
*buff = 0x00000000;
有关于更快代码的建议吗?
让编译器担心最好的方法:
memset(buff, 0, 8);
当然,如果这真的很关键,那么看看它生成的代码。
您不需要内在指令,因为普通整数指令是以8位、16位、32位或64位增量存储零的最有效方法。
char* buff = foo();
*((uint64_t*)buff) = 0;
由于char*
可以别名任何东西,因此这是安全的。但如果buff
有不同的类型,如short *
,则不安全。
优化时,始终从查看编译器的功能开始。
#include <string.h>
#include <cstdint>
extern char* foo();
char* f1() {
char* buf = foo();
for (auto i = 0; i < 8; ++i)
buf[i] = 0;
return buf;
}
char* f2() {
char* buf = foo();
memset(buf, 0, 8);
return buf;
}
char* f3() {
char* buf = foo();
*((uint64_t*)buf) = 0ULL;
return buf;
}
用带有-O3的GCC编译,上面每一个的输出都是用于存储0s的单个指令:
godbolt 演示
f1():
subq $8, %rsp
call foo()
movq $0, (%rax) ; << Here
addq $8, %rsp
ret
f2():
subq $8, %rsp
call foo()
movq $0, (%rax) ; << Here
addq $8, %rsp
ret
f3():
subq $8, %rsp
call foo()
movq $0, (%rax) ; << Here
addq $8, %rsp
ret
相关文章:
- 单个变量如何存储多个值?
- 初始化一个由 p 指向的新 INTSTK,它最多可以存储 m 个
- 是否值得使用位移在单个字节中存储多个小数据成员?
- 如何在阵列中存储多个队列?
- 如何存储多个用户输入,然后以C++显示它们
- 在一个变量中存储多个 2D 数组
- C 存储两个类对象之间的差异
- Boost::p roperty_tree:在XML解析器中使用std::vector<>在一个键中存储多个值
- 可以使用单个内存块存储多个对象是可以的吗?
- 为什么双精度不存储两个整数的乘积
- DirectX:在矢量缓冲区中存储多个顶点
- std::variant 存储多个字符串类型并区分它们
- 如何在C 中使用HASHIS存储多个值
- 有没有办法在一个枚举条目中存储多个值
- 存储两个3-D向量的数据并计算点产品
- 在提升shared_ptr中存储多个 Lua 状态
- 使用 SSE 内部函数存储四个 16 位整数
- 如何比较和存储 2 个向量的数据元素位置
- C++ std::stringstream >> std::string 仅存储第一个条目?
- 如何在一个数组(C++)中存储两个整数