为什么使用1而不是-1? [英] Why use 1 instead of -1?
问题描述
在
然后他说更喜欢常数1而不是-1。 -1 IIRC实际上并不特殊,但是因为它为负,所以将在CPU上设置符号标志。从我目前的理解来看,1只是一个正数,没有处理器标志位,也无法与0或其他正数区分开。
但是Andrei说比-1更喜欢1。为什么?硬件用比-1更好的1做些什么?在零和其他两个好常数之间,使用一个负数之间的差异不那么显着。他还将编译器问题与硬件问题捆绑在一起,即,硬件可能能够有效地执行操作,但是如果使用所选的高级语言给出了合理清晰的表达,则编译器将不会生成适当的机器代码。
虽然我看不懂他的想法,但至少有两个方面可以使一个方面好于否定方面。
- 许多ISA提供的比较操作(或GPR传输的标志)返回零或一(例如,MIPS设置为小于)零或负一。 (SIMD指令是一个例外; SIMD比较通常会生成零或负1(所有位均已设置。)。
- 至少有一种SPARC实现使加载较小的有符号值更昂贵,而且我看来回想一下,至少一个ISA没有提供加载有符号字节的指令。天真的实现符号扩展会增加延迟,因为在加载该值之前才知道是设置还是清除更高有效位。
负数确实有一些好处。正如您所提到的,负性测试通常相对容易,因此,如果负数是唯一使用的负值,则处理起来可能会更便宜。同样,有条件地清除基于零或负数的值只是一个和
操作。 (对于有条件地设置或清除单个位,最好是一个而不是一个负的位,因为这样只会涉及移位和和
。)
At 29min mark of http://channel9.msdn.com/Events/GoingNative/2013/Writing-Quick-Code-in-Cpp-Quickly Andrei Alexandrescu says when using constants to prefer 0 and mentions hardware knows how to handle it. I did some assembly and I know what he is talking about and about the zero flag on CPUs
Then he says prefer the constant 1 rather then -1. -1 IIRC is not actually special but because it is negative the sign flag on CPUs would be set. 1 from my current understanding is simply a positive number there is no bit on the processor flag for it and no way to distinguish from 0 or other positive numbers.
But Andrei says to prefer 1 over -1. Why? What does hardware do with 1 that is better then -1?
First, it should be noted that Andrea Alexandrescu emphasized the difference between zero and the other two good constants, that the difference between using one and negative one is less significant. He also bundles compiler issues with hardware issues, i.e., the hardware might be able to perform the operation efficiently but the compiler will not generate the appropriate machine code given a reasonably clear expression in the chosen higher level language.
While I cannot read his mind, there are at least two aspects that may make one better than negative one.
- Many ISAs provide comparison operations (or flag to GPR transfers) that return zero or one (e.g., MIPS has Set on Less Than) not zero or negative one. (SIMD instructions are an exception; SIMD comparisons typically generate zero or negative one [all bits set].)
- At least one implementation of SPARC made loading smaller signed values more expensive, and I seem to recall that at least one ISA did not provide an instruction for loading signed bytes. Naive implementation of sign extension adds latency because whether to set or clear the more significant bits is not known until the value has been loaded.
Negative one does have some benefits. As you mentioned, testing for negativity is often relatively easy, so if negative one is the only negative value used it may be handled less expensively. Also, conditionally clearing a value based on zero or negative one is simply an and
operation. (For conditionally setting or clearing a single bit, one rather than negative one would be preferred since such would involve only a shift and an and
.)
这篇关于为什么使用1而不是-1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!