首页 > 解决方案 > 16 到 32 位整数转换与性能

问题描述

我想从数组中加载 16 位无符号整数,并将这些值用于 C++ 中的 32 位无符号计算。我可以选择将值存储为 16 位数组(更少内存)或 32 位数组(更多内存消耗)。

我的代码应该可以用常见的 C++ 编译器编译,并且可以在尽可能多的架构上运行。对于这些组合中的许多组合,进行性能测量和汇编代码阅读将很困难,所以我要求进行理论考试。

换句话说:在什么情况下,16 位到 32 位无符号整数的转换通常会消耗 CPU 周期?我什么时候可以在不丢失 CPU 周期的情况下使用内存减少的 16 位阵列?

标签: c++performancetype-conversioncpu-architectureunsigned-integer

解决方案


I think all major architectures support loads from memory with sign extension and zero extension. x86, ARM and MIPS definitely do have such load instructions. Old architectures and primitive microcontrollers, especially 8-bit and 16-bit ones, may not have such instructions and therefore may require multiple instructions to achieve the same result. If you aren't mentioning those, you probably don't really care. So, just write portable C/C++ code and be done with it.


推荐阅读