Convert clip and sign operations to branchless one-liners.
Testing on 50 million random doubles we have branchless clip is 1.14x faster than the branched version and branchless sign is 2.27x faster than the branched version. PiperOrigin-RevId: 943812426 Change-Id: If8003a602365601a31bec1d8b080c44d16000a4e
This commit is contained in:
committed by
Copybara-Service
parent
ee28b334ef
commit
62e80eee38
@@ -1738,25 +1738,13 @@ mjtNum mju_max(mjtNum a, mjtNum b) {
|
||||
|
||||
// clip x to the range [min, max]
|
||||
mjtNum mju_clip(mjtNum x, mjtNum min, mjtNum max) {
|
||||
if (x < min) {
|
||||
return min;
|
||||
} else if (x > max) {
|
||||
return max;
|
||||
} else {
|
||||
return x;
|
||||
}
|
||||
return x < min ? min : (x > max ? max : x);
|
||||
}
|
||||
|
||||
|
||||
// sign function
|
||||
mjtNum mju_sign(mjtNum x) {
|
||||
if (x < 0) {
|
||||
return -1;
|
||||
} else if (x > 0) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
return (x > 0.0) - (x < 0.0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user