2016-08-02 18:49:42 +10:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2016-08-02 18:49:42 +10:00
|
|
|
*
|
2018-12-26 15:10:21 +11:00
|
|
|
* Copyright 2011-2018 Danny Robson <danny@nerdcruft.net>
|
2016-08-02 18:49:42 +10:00
|
|
|
*/
|
|
|
|
|
2018-12-26 15:10:21 +11:00
|
|
|
#pragma once
|
2016-08-02 18:49:42 +10:00
|
|
|
|
2018-12-26 15:10:21 +11:00
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// Forwards the supplied `value` and annotates the result as likely to be the
|
|
|
|
/// value `typical`.
|
|
|
|
///
|
|
|
|
/// Must be an integral type.
|
|
|
|
template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
|
|
|
|
constexpr inline
|
|
|
|
T
|
|
|
|
expect (T val, T typical)
|
|
|
|
{
|
|
|
|
return __builtin_expect (val, typical);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Coerces the argument into a bool and annotates the result as likely to be
|
|
|
|
/// true.
|
2016-08-02 18:49:42 +10:00
|
|
|
template <typename T>
|
|
|
|
constexpr inline
|
|
|
|
bool
|
|
|
|
likely (T &&t)
|
|
|
|
{ return __builtin_expect (!!t, true); }
|
|
|
|
|
|
|
|
|
2018-12-26 15:10:21 +11:00
|
|
|
/// Coerces the argument into a bool and annotates the result as likely to be
|
|
|
|
/// false.
|
2016-08-02 18:49:42 +10:00
|
|
|
template <typename T>
|
|
|
|
constexpr inline
|
|
|
|
bool
|
|
|
|
unlikely (T &&t)
|
|
|
|
{ return __builtin_expect (!!t, false); }
|