整数提升会影响64位性能吗

整数提升会影响64位性能吗

本文介绍了整数提升会影响64位性能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下面的代码为例:

uint32_t fg;
uint32_t bg;
uint32_t mask;
uint32_t dest;
...
dest = (fg & mask) | (bg & (~mask));

现在,此片段将其所有操作数键入32位无符号整数.使用具有32位int大小的C编译器,将不会发生整数提升,因此整个操作将以32位执行.

Now this fragment has all it's operands typed 32 bit unsigned ints. Using a C compiler with a 32 bit int size, no integer promotions will happen, so the entire operation is performed in 32 bits.

我的问题是,例如在Wikipedia上,它显示通常,即使是64位计算机也需要使用32位int大小的编译器.遵循C标准,它们不会将操作数提升为64位整数,因此有可能被编译为性能较差甚至可能更大的代码大小的东西(仅假设16位操作是更昂贵的周期和指令大小) 32位x86).

My problem is that for example on Wikipedia it is shown that usually even 64 bit machines get to have compilers which use a 32 bit int size. Conforming to the C standard, they wouldn't promote the operands to 64 bit ints, so potentially compiling into something having inferior performance and probably even larger code size (just assuming from how 16 bit operations are more expensive cycle and instruction size-wise on a 32 bit x86).

主要问题是:我是否需要关注? (我相信我可能不会,因为启用优化功能后,合理的编译器可能会忽略严格遵循C标准而出现的多余问题.请查看过去的示例代码,并总体上考虑一下我认为可以减少的错误.地面)

The primary question is: Do I have to be concerned? (I believe I may not, since with optimizations enabled a sane compiler might be able to omit the excess gunk which would show up from strictly following the C standard. Please see past the example code, and think in general where my belief may have less ground)

如果是这样(我实际上不得不担心),您能推荐一些涵盖该领域的方法(书籍,网站等)吗? (好吧,我知道这对于SO来说有点超出范围,但是如果我只得到三个字是,您知道!作为接受答案,我认为这没什么用)

If it is so (that I actually have to be concerned), could you recommend some method (book, site, whatever) which covers this area? (Well, I know this is a bit out-of-bounds for SO, however I see this much less useful if I would only get a three word Yes, you do! as an answer to accept)

推荐答案

不,不是这样.减少读取主存储器或磁盘的成本通常超过在64位寄存器中执行32位操作所增加的成本.使用32位整数数组的64位程序通常会比使用64位整数数组的程序快.

No, not really. The reduced cost of reading main memory or disk usually outways the added cost of performing 32 bit operations in 64 bit registers. A 64-bit program that uses 32-bit integer arrays will often be faster than one using 64-bit integer arrays.

要注意的是,在编译时,优化大小通常要比优化速度要好,因为高速缓存未命中的代价通常比节省的cpu周期要高.

On the same note, when compiling it is often better to optimize for size than for speed, because the cache misses often cost more than the cpu cycles saved.

这篇关于整数提升会影响64位性能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 19:33