summaryrefslogtreecommitdiffhomepage
path: root/compat
diff options
context:
space:
mode:
Diffstat (limited to 'compat')
-rw-r--r--compat/util.hpp21
1 files changed, 18 insertions, 3 deletions
diff --git a/compat/util.hpp b/compat/util.hpp
index b904978b..735fbae6 100644
--- a/compat/util.hpp
+++ b/compat/util.hpp
@@ -3,6 +3,7 @@
#include "ndebug-guard.hpp"
#include "run-in-thread.hpp"
#include "meta.hpp"
+#include "functional.hpp"
#include <memory>
#include <cmath>
@@ -40,20 +41,34 @@ template<typename t> using ptr = std::unique_ptr<t>;
# define unused_on_unix(t, i) t i
#endif
+#if defined __GNUC__
+# define likely(x) __builtin_expect((x),1)
+# define unlikely(x) __builtin_expect((x),0)
+#else
+# define likely(x) (x)
+# define unlikely(x) (x)
+#endif
+
template<typename t>
-int iround(const t& val)
+inline int iround(const t& val)
{
return int(std::round(val));
}
+template<typename t>
+inline unsigned uround(const t& val)
+{
+ return std::round(std::fmax(t(0), val));
+}
+
namespace util_detail {
template<typename n>
inline auto clamp_(n val, n min, n max) -> n
{
- if (val > max)
+ if (unlikely(val > max))
return max;
- if (val < min)
+ if (unlikely(val < min))
return min;
return val;
}