3.9 Logical and Comparison Functions

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.





Operation r <-- a,b,c,...Description






<  N <-- N,N ri <-- ai < bi



<  N <-- 1,N ri <-- a < bi



<  N <-- N,1 ri <-- ai < b



<=  N <-- N,N ri <-- ai < bi



<=  N <-- 1,N ri <-- a < bi



<=  N <-- N,1 ri <-- ai < b



==  N <-- N,N ri <-- ai = bi



==  N <-- 1,N ri <-- a = bi



==  N <-- N,1 ri <-- ai = b



!=  N <-- N,N ri <-- ai /= bi



!=  N <-- 1,N ri <-- a /= bi



!=  N <-- N,1 ri <-- ai /= b



>=  N <-- N,N ri <-- ai > bi



>=  N <-- 1,N ri <-- a > bi



>=  N <-- N,1 ri <-- ai > b



>  N <-- N,N ri <-- ai > bi



>  N <-- 1,N ri <-- a > bi



>  N <-- N,1 ri <-- ai > b



 


Table 3.19: Comparison functions. Each component of the output vector is set to 0 or 1 to represent the result of the comparison on each component.





Operation r <-- a,b,c,... Description






!  N <-- N ri <-- 1 - (ai > 0)



/ E  &&  N <-- N,N ri = min(ai,bi)



/ E  ||  N <-- N,N ri = max(ai,bi)



/ E  all  1 <-- N r = min(a0,a1,...,aN-1)



/ E  any  1 <-- N r = max(a0,a1,...,aN-1)



cond  N <-- N,N,NComponentwise conditional
     {
ri <--   bi  if ai > 0
       ci  otherwise



cond  N <-- 1,N,N Tuple conditional
     {
       b  if a > 0
r <--    c  otherwise



 


Table 3.20: Logical functions. Values greater than zero mean “true” and all others mean “false.”

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.