3 ms·
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,
by tzs 6y ago
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/superoptimizer.pdf https://web.stanford.edu/class/cs343/resources/superoptimize...
- kazinator 6y agoIsn't .l implied if it is omitted? If I just say "sub d1, d2", of course I mean the whole register width, and not just 16 or 8 bits of it.
- saagarjha 6y agoNote that with modern branch predictors such optimizations may not actually be beneficial–ARM got rid of condition codes on all its instructions in the 64-bit version. (Plus, I assume they ran out of space to encode them.)
- brandmeyer 6y agoIEEE754 has defined min and max for quite some time. Nowadays almost all FPUs have min and max instructions built-in.