68K assembly, no branches: # d0 = num, d1 = low, d2 = high, d3 = scratch sub.l d1, d0 subx.l d3, d3 or.l d3, d0 addx.l d1, d0 # d0 = max(num,low) sub.l d2, d0 subx.l d3, d3 and.l d3, d0 add.l d2, d0 # d0 = min(max(num,low),high) Adapted from "Superoptimizer -- A Look at the Smallest Program" by Henry Massalin [1]. [1] https://web.stanford.edu/class/cs343/resources/superoptimize...
If I just say "sub d1, d2", of course I mean the whole register width, and not just 16 or 8 bits of it.