Short-circuit evaluation of && and || operators.

We now follow C's approach of evaluating these: we don't evaluate
the second expression in the operator if the value of the first one
determines the overall result.  Thus, these can now be used 
idiomatically like (index < limit && array[index] > 0) and such.

For varying expressions, the mask is set appropriately when evaluating
the second expression.

(For expressions that can be determined to be both simple and safe to
evaluate with the mask all off, we still evaluate both sides and compute
the logical op result directly, which saves a number of branches and tests.
However, the effect of this should never be visible to the programmer.)

Issue #4.
This commit is contained in:
Matt Pharr
2012-01-30 05:58:41 -08:00
parent 0575b1f38d
commit e19f4931d1
16 changed files with 658 additions and 136 deletions

View File

@@ -1184,7 +1184,6 @@ C++:
There are a number of features of C89 that are not supported in ``ispc``
but are likely to be supported in future releases:
* Short circuiting of logical operations
* There are no types named ``char``, ``short``, or ``long`` (or ``long
double``). However, there are built-in ``int8``, ``int16``, and
``int64`` types
@@ -1969,6 +1968,18 @@ operator also work as expected.
(*fp).a = 0;
fp->b = 1;
As in C and C++, evaluation of the ``||`` and ``&&`` logical operators is
"short-circuited"; the right hand side won't be evaluated if the value from
the left-hand side determines the logical operator's value. For example,
in the following code, ``array[index]`` won't be evaluated for values of
``index`` that are greater than or equal to ``NUM_ITEMS``.
::
if (index < NUM_ITEMS && array[index] > 0) {
// ...
}
Dynamic Memory Allocation
-------------------------