From fd7cb99f47e5f3dd39a9069b225d8c4ee27680e8 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Mon, 14 Nov 2016 15:12:53 -0500 Subject: [PATCH] stepcompress: Optimize safe_sqrt() code Optimize the code by putting the uncommon case out-of-line. Signed-off-by: Kevin O'Connor --- klippy/stepcompress.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/klippy/stepcompress.c b/klippy/stepcompress.c index 992155d2..afd9886b 100644 --- a/klippy/stepcompress.c +++ b/klippy/stepcompress.c @@ -256,16 +256,21 @@ check_line(struct stepcompress *sc, struct step_move move) * Step compress interface ****************************************************************/ +#define likely(x) __builtin_expect(!!(x), 1) + // Wrapper around sqrt() to handle small negative numbers -static inline double -safe_sqrt(double v) +static double +_safe_sqrt(double v) { - if (v < 0. && v > -0.001) + if (v > -0.001) // Due to floating point truncation, it's possible to get a // small negative number - treat it as zero. return 0.; return sqrt(v); } +static inline double safe_sqrt(double v) { + return likely(v >= 0.) ? sqrt(v) : _safe_sqrt(v); +} // Allocate a new 'stepcompress' object struct stepcompress *