初级对应高级,底层对应高层。初级和底层不是对立关系啊。
这本书里的例子,很多会影响代码可读性。比如下面这段,7.9a比7.9.b好理解多了。而
且编译器自己也会优化。扣细节显然过头了。
Boolean variables are overdetermined in the sense that all operators that
have Boolean variables as input check if the inputs have any other value
than 0 or 1, but operators that have Booleans as output can produce no other
value than 0 or 1. This makes operations with Boolean variables as input
less efficient than necessary. Take the example:
// Example 7.9a
bool a, b, c, d;
c = a && b;
d = a || b;
This is typically implemented by the compiler in the following way:
bool a, b, c, d;
if (a != 0) {
if (b != 0) {
c = 1;
}
else {
goto CFALSE;
}
}
35
else {
CFALSE:
c = 0;
}
if (a == 0) {
if (b == 0) {
d = 0;
}
else {
goto DTRUE;
}
}
else {
DTRUE:
d = 1;
}
This is of course far from optimal. The branches may take a long time in
case of mispredictions (see page 43). The Boolean operations can be made
much more efficient if it is known with certainty that the operands have no
other values than 0 and 1. The reason why the compiler doesn't make such an
assumption is that the variables might have other values if they are
uninitialized or come from unknown sources. The above code can be optimized
if a and b have been initialized to valid values or if they come from
operators that produce Boolean output. The optimized code looks like this:
// Example 7.9b
char a = 0, b = 0, c, d;
c = a & b;
d = a | b;
在 commodity (佛说我是猪) 的大作中提到: 】