diff options
author | Stanisław Halik <sthalik@misaki.pl> | 2017-05-14 16:22:09 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-14 16:22:09 +0200 |
commit | 5c23666b58bb1dd4aea15c0d62a2f716d5be7f52 (patch) | |
tree | e6497e9b55c073be209ec673ef05e62bf57a2c8f /compat/util.hpp | |
parent | 4701dd3b0c8323a11cf7d5ad09c579a9864a41bd (diff) | |
parent | c392181211b245e74292424500265323c960c1aa (diff) |
Merge branch 'unstable' into unstable
Diffstat (limited to 'compat/util.hpp')
-rw-r--r-- | compat/util.hpp | 33 |
1 files changed, 26 insertions, 7 deletions
diff --git a/compat/util.hpp b/compat/util.hpp index b904978b..823b83d1 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> @@ -12,10 +13,14 @@ #include <QDebug> #define progn(...) ([&]() { __VA_ARGS__ }()) +#define prog1(x, ...) (([&]() { auto _ret1324 = (x); do { __VA_ARGS__; } while (0); return _ret1324; })()) #define once_only(...) progn(static bool once = false; if (!once) { once = true; __VA_ARGS__; }) - -#define load_time_value(x) progn(static const auto _value132((x)); return _value132;) +#define load_time_value(x) \ + progn( \ + static const auto _value132((x)); \ + return static_cast<decltype(_value132)&>(value132); \ + ) template<typename t> using mem = std::shared_ptr<t>; template<typename t> using ptr = std::unique_ptr<t>; @@ -40,20 +45,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; } @@ -61,9 +80,9 @@ inline auto clamp_(n val, n min, n max) -> n } template<typename t, typename u, typename w> -inline auto clamp(const t& val, const u& min, const w& max) -> decltype(val * min * max) +inline auto clamp(const t& val, const u& min, const w& max) -> decltype(val + min + max) { - return ::util_detail::clamp_<decltype(val * min * max)>(val, min, max); + return ::util_detail::clamp_<decltype(val + min + max)>(val, min, max); } template<typename t, typename... xs> |