常见架构的最快整数类型 [英] Fastest integer type for common architectures
问题描述
stdint.h
标头缺少 int_fastest_t
和 uint_fastest_t
对应于 {,u}int_fastX_t
类型.对于整数类型的宽度无关紧要的情况,如何选择允许处理最大数量的位而对性能损失最小的整数类型?例如,如果使用一种简单的方法在缓冲区中搜索第一个设置位,则可能会考虑这样的循环:
The stdint.h
header lacks an int_fastest_t
and uint_fastest_t
to correspond with the {,u}int_fastX_t
types. For instances where the width of the integer type does not matter, how does one pick the integer type that allows processing the greatest quantity of bits with the least penalty to performance? For example, if one was searching for the first set bit in a buffer using a naive approach, a loop such as this might be considered:
// return the bit offset of the first 1 bit
size_t find_first_bit_set(void const *const buf)
{
uint_fastest_t const *p = buf; // use the fastest type for comparison to zero
for (; *p == 0; ++p); // inc p while no bits are set
// return offset of first bit set
return (p - buf) * sizeof(*p) * CHAR_BIT + ffsX(*p) - 1;
}
自然,使用char
会导致比int
更多的操作.但是 long long
可能会导致比在 32 位系统上使用 int
等的开销更昂贵的操作.
Naturally, using char
would result in more operations than int
. But long long
might result in more expensive operations than the overhead of using int
on a 32 bit system and so on.
我目前的假设是针对主流架构,使用 long
是最安全的选择:它在 32 位系统上是 32 位,在 64 位系统上是 64 位.
My current assumption is for the mainstream architectures, the use of long
is the safest bet: It's 32 bit on 32 bit systems, and 64 bit on 64 bit systems.
推荐答案
对于所有现有的主流架构,long
是目前循环吞吐量最快的类型.
For all existing mainstream architectures long
is the fastest type at present for loop throughput.
这篇关于常见架构的最快整数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!