summaryrefslogtreecommitdiffhomepage
path: root/compat
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2017-05-06 11:08:29 +0200
committerStanislaw Halik <sthalik@misaki.pl>2017-05-10 11:19:22 +0200
commit604914db84d0468c6c8d04dfe6491e3c15b670f7 (patch)
treec6ea9f132ed3044fa27a845eaf6e3234658d011a /compat
parent5c3c5a01285245ec046a8203dd7064fad45a02fe (diff)
compat/util: more utils
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;
}