CLAMPS Option

Signed CLAMPS instruction (for saturating arithmetic)

CLAMPS option
Selected

The CLAMPS instruction tests whether a signed integral variable fits into a fixed number of bits ranging from 7 to 22. If so, the input value is returned. If not, the largest value with the same sign that does fit into the fixed number of bits is returned. This option is particularly useful for implementing saturating arithmetic. The compiler will automatically infer this instruction from the pattern.

x = min(max((x,-2n),2n-1))=

Min and max are first inferred from standard C conditional operations.

Consider the following example:

if (a < -1024) {
    result = -1024;
} else if (a > 1023) {
    result = 1023;
} else {
    result = a;
}

When compiled with optimization, the compiler will generate a CLAMPS instruction with an immediate value of 10.