It is often useful to compare values. Sh implements all the usual comparison and logical operations, but generalized to act over tuples. In addition, we implement some useful functions to deal with tuples of Booleans. The comparison operators are presented in Table 3.19, while logical operations are presented in Table 3.20.
|
|
Many GPUs do not have real support for booleans or condition registers, and for this reason we use an interpretation of true and false that is appropriate for floating point types. Values greater than zero are interpreted to be “true,” whereas values less than or equal to zero are taken to mean “false.” The comparison operators all return 1.0 if the comparison succeeds, and 0.0 if it does not.
Sh does not at present distinguish between boolean types and other kinds of data. We are, however, considering adding ShBool as a new semantic type -- similar to ShVector and ShTexCoord -- to represent a boolean value. This would allow us to take advantage more easily of conditional registers available on certain GPUs.
This leads to a natural definition of “and” and “or”. “And” is defined as the minimum of its arguments, whereas “or” is defined as the maximum. The operators && and || are defined correspondingly in Sh. Furthermore, boolean negation of a variable x is defined as its one complement: 1 - (x > 0).
The all and any functions collapse a Boolean tuple to a single Boolean. They are especially useful in the arguments of control contructs and to control conditional assignment function. The function all returns true only if all components of its argument are true; this is implemented by taking the minimum of these values. The function any returns true if any of the components of its argument are true; this is implemented by taking the maximum of these components.
The cond function provides a useful operation that allows (componentwise) returning one of two computations depending on a boolean value. It corresponds very closely to C++’s ternary ?: operator, which unfortunately can not be overloaded. Note that cond only discards one of the two values it might return, it still calculates both (although the optimizer might turn it into a real branch on some compilation targets).
Note: This manual is available as a bound book from AK Peters, including better formatting, in-depth examples, and about 200 pages not available on-line.