Home > Articles > Programming

This chapter is from the book

This chapter is from the book

2–9 Three-Valued Compare Function

The three-valued compare function, a slight generalization of the sign function, is defined by

021equ03.jpg

There are both signed and unsigned versions, and unless otherwise specified, this section applies to both.

Comparison predicate instructions permit a three-instruction solution, an obvious generalization of Equations in (4):

021equ04.jpg

A solution for unsigned integers on PowerPC is shown below [CWG]. On this machine, “carry” is “not borrow.”

If limited to the instructions of the basic RISC, there does not seem to be any particularly good way to compute this function. The comparison predicates x < y, xy, and so on, require about five instructions (see Section 2–12), leading to a solution in about 12 instructions (using a small amount of commonality in computing x < y and x > y). On the basic RISC it’s probably preferable to use compares and branches (six instructions executed worst case if compares can be commoned).

  • + Share This
  • 🔖 Save To Your Account