<html>
    <head>
      <base href="https://bugs.freedesktop.org/" />
    </head>
    <body>
      <p>
        <div>
            <b><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - clamp with bounds inside [0, 1] generates slow code"
   href="https://bugs.freedesktop.org/show_bug.cgi?id=76862#c2">Comment # 2</a>
              on <a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - clamp with bounds inside [0, 1] generates slow code"
   href="https://bugs.freedesktop.org/show_bug.cgi?id=76862">bug 76862</a>
              from <span class="vcard"><a class="email" href="mailto:mattst88@gmail.com" title="Matt Turner <mattst88@gmail.com>"> <span class="fn">Matt Turner</span></a>
</span></b>
        <pre>Ian's description has bugs, so let me try again:

GLSL's clamp(A, B, C) clamps A to a lower bound of B and an upper bound of C.
We implement this in the compiler with min() and max() operations: min(max(a,
b), c).

i965 assembly for clamp(A, B, C) would look like

(select from A, B the argument that is greater than or equal; i.e., max)
sel.ge tmp, A, B
(select from tmp, C the argument that is less that; i.e., min)
sel.l  dst, tmp, C

Saturate is a special case of clamp, specifically when the bounds are 0.0 to
1.0 (for floating point types). Probably all GPUs can perform saturate for free
-- it's a destination modifier in i965 assembly.

The i965 backend's try_emit_saturate() function recognizes min(max(a, 0.0),
1.0) as a saturate operation, and sets the saturate modifier (or emits a MOV
instruction with saturate).

i965 assembly for clamp(A, 0.0, 1.0) (after try_emit_saturate()) would turn
into

mov.sat dst, A

The proposed optimization idea here is that for immediate arguments that
satisfy the condition in <a href="show_bug.cgi?id=76862#c0">comment #0</a>, we can emit a single min/max instruction
with a saturate modifier instead of a min and a max instruction.

So, clamp(A, 0, 0.5) would be

(select the least of A and 0.5, saturate the result, and store in dst)
sel.l.sat dst, A, 0.5

and similarly clamp(A, 0.1, 1) -> sel.ge.sat dst, A, 0.1.</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are the QA Contact for the bug.</li>
      </ul>
    </body>
</html>