@aeva clamp(x, lo, hi) = med3(x, lo, hi) if lo <= hi
proof:
suppose x < lo. then x < lo <= hi so lo is the median.
suppose lo <= x <= hi. then x is the median.
finally, suppose hi < x. then lo <= hi < x so hi is the median.
@steve @aeva Yup. And you can also use this in reverse for median nodes if you have them.
Specifically, for med3(a,b,c), during "code gen", swap all constant inputs to the right as necessary, and in nondescending order (via commutativity).
Then if b is constant, so is c, b <= c, and
med3(a,b,c) = max(min(a, c), b)
-> med3 is actually a pretty convenient IR for this. Symmetries are good!