gpt4 book ai didi

c - 常见架构的最快整数类型

转载 作者:太空狗 更新时间:2023-10-29 16:38:15 25 4
gpt4 key购买 nike

stdint.h header 缺少 int_fastest_tuint_fastest_t 以对应于 {,u}int_fastX_t 类型。在整数类型的宽度无关紧要的情况下,如何选择允许处理最大数量位且对性能影响最小的整数类型?例如,如果使用天真的方法在缓冲区中搜索第一个设置位,则可能会考虑这样的循环:

// 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 的开销更昂贵的操作。

我目前的假设是针对主流架构,使用 long 是最安全的选择:它在 32 位系统上是 32 位,在 64 位系统上是 64 位。

最佳答案

int_fast8_t 始终是正确实现中最快的整数类型。永远不会有小于 8 位的整数类型(因为 CHAR_BIT>=8 是必需的),并且由于 int_fast8_t 是至少 8 位的最快的整数类型,因此最快的整数类型,句点。

关于c - 常见架构的最快整数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3694131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com