2011-05-23 17:18:52 +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/.
|
2011-05-23 17:18:52 +10:00
|
|
|
*
|
2018-03-20 13:32:24 +11:00
|
|
|
* Copyright 2010-2018 Danny Robson <danny@nerdcruft.net>
|
2011-05-23 17:18:52 +10:00
|
|
|
*/
|
|
|
|
|
2020-02-18 12:18:43 +11:00
|
|
|
#pragma once
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2016-07-28 13:36:23 +10:00
|
|
|
// DO NOT INCLUDE debug.hpp
|
|
|
|
// it triggers a circular dependency; debug -> format -> maths -> debug
|
|
|
|
// instead, just use cassert
|
|
|
|
|
2022-03-18 12:38:30 +11:00
|
|
|
#include "concepts/traits.hpp"
|
|
|
|
#include "concepts/named.hpp"
|
2020-02-18 12:18:43 +11:00
|
|
|
#include "concepts.hpp"
|
2022-03-18 12:38:30 +11:00
|
|
|
|
2017-11-22 16:49:37 +11:00
|
|
|
#include "types/traits.hpp"
|
|
|
|
#include "float.hpp"
|
2015-02-05 20:30:33 +11:00
|
|
|
|
2017-05-16 17:06:36 +10:00
|
|
|
#include <algorithm>
|
2016-07-28 13:36:23 +10:00
|
|
|
#include <cassert>
|
2015-04-09 17:44:50 +10:00
|
|
|
#include <cmath>
|
2015-02-02 15:25:22 +11:00
|
|
|
#include <cstdint>
|
2015-04-09 17:44:50 +10:00
|
|
|
#include <limits>
|
2016-08-15 18:42:43 +10:00
|
|
|
#include <numeric>
|
2012-04-20 18:02:54 +10:00
|
|
|
#include <type_traits>
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
|
2015-11-13 17:15:08 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// NOTE: You may be tempted to add all sorts of performance enhancing
|
|
|
|
// attributes (like gnu::const or gnu::pure). DO NOT DO THIS WITHOUT EXTENSIVE
|
|
|
|
// TESTING. Just about everything will break in some way with these attributes.
|
|
|
|
//
|
|
|
|
// In particular: it is safest to apply these only to leaf functions
|
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
|
2015-11-13 17:13:38 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-08-05 14:42:02 +10:00
|
|
|
namespace cruft {
|
2018-04-11 18:24:34 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2022-03-18 12:38:30 +11:00
|
|
|
template <concepts::traits::arithmetic T>
|
2020-02-18 12:18:43 +11:00
|
|
|
constexpr T
|
2018-04-11 18:24:34 +10:00
|
|
|
abs [[gnu::const]] (T t)
|
|
|
|
{
|
|
|
|
return t > 0 ? t : -t;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-03 12:04:47 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Useful for explictly ignore equality warnings
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wfloat-equal"
|
2019-02-03 16:38:06 +11:00
|
|
|
template <typename ValueA, typename ValueB>
|
|
|
|
constexpr auto
|
|
|
|
equal (ValueA const &a, ValueB const &b)
|
2016-02-03 12:04:47 +11:00
|
|
|
{
|
2019-02-03 16:38:06 +11:00
|
|
|
return a == b;
|
2016-02-03 12:04:47 +11:00
|
|
|
}
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
|
|
|
|
|
2018-01-31 19:33:42 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Comparisons
|
2018-04-11 18:24:52 +10:00
|
|
|
///
|
|
|
|
/// check that a query value is within a specified relative percentage of
|
|
|
|
/// a ground truth value.
|
|
|
|
///
|
|
|
|
/// eg: relatively_equal(355/113.f,M_PI,1e-2f);
|
|
|
|
template <typename ValueT, typename PercentageT>
|
|
|
|
auto
|
|
|
|
relatively_equal (ValueT truth, ValueT test, PercentageT percentage)
|
2018-03-20 13:32:24 +11:00
|
|
|
{
|
2018-03-27 16:14:41 +11:00
|
|
|
// we want to do 1 - b / a, but a might be zero. if we have FE_INVALID
|
|
|
|
// enabled then we'll pretty quickly throw an exception.
|
|
|
|
//
|
|
|
|
// instead we use |a - b | / (1 + |truth|). note that it's not as
|
|
|
|
// accurate when the test values aren't close to 1.
|
2018-04-11 18:24:52 +10:00
|
|
|
return abs (truth - test) / (1 + abs (truth)) < percentage;
|
2018-03-20 13:32:24 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
inline bool
|
2018-01-31 19:33:42 +11:00
|
|
|
almost_equal (float a, float b)
|
|
|
|
{
|
|
|
|
return ieee_single::almost_equal (a, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
inline bool
|
|
|
|
almost_equal (double a, double b)
|
|
|
|
{
|
|
|
|
return ieee_double::almost_equal (a, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename ValueA, typename ValueB>
|
|
|
|
constexpr auto
|
|
|
|
almost_equal (const ValueA &a, const ValueB &b)
|
|
|
|
{
|
|
|
|
if constexpr (std::is_floating_point_v<ValueA> && std::is_floating_point_v<ValueB>) {
|
|
|
|
using common_t = std::common_type_t<ValueA,ValueB>;
|
|
|
|
return almost_equal (common_t {a}, common_t{b});
|
|
|
|
} else if constexpr (std::is_integral_v<ValueA> &&
|
|
|
|
std::is_integral_v<ValueB> &&
|
|
|
|
std::is_signed_v<ValueA> == std::is_signed_v<ValueB>)
|
|
|
|
{
|
|
|
|
using common_t = std::common_type_t<ValueA,ValueB>;
|
2020-04-06 13:12:52 +10:00
|
|
|
return common_t {a} == common_t {b};
|
2018-01-31 19:33:42 +11:00
|
|
|
} else {
|
|
|
|
return equal (a, b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-03 12:04:47 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
2020-02-18 12:18:43 +11:00
|
|
|
constexpr bool
|
2016-02-03 12:04:47 +11:00
|
|
|
almost_zero (T t)
|
|
|
|
{
|
2020-02-18 12:18:43 +11:00
|
|
|
if constexpr (std::is_integral_v<T>) {
|
|
|
|
return t == 0;
|
|
|
|
} else {
|
|
|
|
return almost_equal (t, T{0});
|
|
|
|
}
|
2018-01-31 19:33:42 +11:00
|
|
|
}
|
2016-02-03 12:04:47 +11:00
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
2020-02-18 12:18:43 +11:00
|
|
|
constexpr bool
|
2016-02-03 12:04:47 +11:00
|
|
|
exactly_zero (T t)
|
|
|
|
{
|
2020-02-18 12:18:43 +11:00
|
|
|
if constexpr (std::is_integral_v<T>) {
|
|
|
|
return equal (t, T{0});
|
|
|
|
} else {
|
|
|
|
return equal (t, T{0});
|
|
|
|
}
|
2016-02-03 12:04:47 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// exponentials
|
2018-04-17 17:01:29 +10:00
|
|
|
|
|
|
|
// reciprocal sqrt, provided so that we can search for usages and replace
|
|
|
|
// at a later point with a more efficient implementation.
|
|
|
|
inline float
|
|
|
|
rsqrt (float val)
|
|
|
|
{
|
|
|
|
return 1 / std::sqrt (val);
|
|
|
|
}
|
|
|
|
|
2018-01-01 15:45:56 +11:00
|
|
|
template <
|
|
|
|
typename BaseT,
|
2022-03-18 12:38:30 +11:00
|
|
|
std::integral ExponentT
|
2018-01-01 15:45:56 +11:00
|
|
|
>
|
|
|
|
constexpr BaseT
|
|
|
|
pow [[gnu::const]] (BaseT base, ExponentT exponent)
|
2015-11-16 13:57:51 +11:00
|
|
|
{
|
2018-01-01 15:45:56 +11:00
|
|
|
assert (exponent >= 0);
|
2018-04-09 12:30:22 +10:00
|
|
|
|
|
|
|
if (exponent == 1)
|
|
|
|
return base;
|
|
|
|
if (exponent == 0)
|
|
|
|
return BaseT{1};
|
|
|
|
return base * pow (base, exponent - 1);
|
2015-11-16 13:57:51 +11:00
|
|
|
}
|
2015-10-06 15:21:48 +11:00
|
|
|
|
2015-01-21 23:35:34 +11:00
|
|
|
|
2019-03-28 14:27:02 +11:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <typename ValueT>
|
|
|
|
constexpr auto
|
|
|
|
pow2 [[gnu::const]] (ValueT const &val)
|
|
|
|
{ return val * val; }
|
|
|
|
|
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
//-------------------------------------------------------------------------
|
2022-03-18 12:38:30 +11:00
|
|
|
template <std::integral T>
|
2020-02-18 12:18:43 +11:00
|
|
|
constexpr bool
|
|
|
|
is_pow2 [[gnu::const]] (T value)
|
2016-02-02 11:32:29 +11:00
|
|
|
{
|
|
|
|
return value && !(value & (value - 1));
|
|
|
|
}
|
2011-08-29 14:31:22 +10:00
|
|
|
|
|
|
|
|
2020-03-16 13:51:45 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// Calculate the base-2 logarithm of an integer, truncating to the next
|
|
|
|
/// lowest integer.
|
|
|
|
///
|
|
|
|
/// `val` must be strictly greater than zero, otherwise the results are
|
|
|
|
/// undefined.
|
2022-03-18 12:38:30 +11:00
|
|
|
template <std::integral T>
|
2020-02-18 12:18:43 +11:00
|
|
|
constexpr T
|
2017-12-22 12:33:29 +11:00
|
|
|
log2 (T val)
|
|
|
|
{
|
2020-03-16 13:51:45 +11:00
|
|
|
assert (val > 0);
|
|
|
|
|
2017-12-22 12:33:29 +11:00
|
|
|
T tally = 0;
|
|
|
|
while (val >>= 1)
|
|
|
|
++tally;
|
|
|
|
return tally;
|
|
|
|
}
|
2014-09-11 15:34:59 +10:00
|
|
|
|
|
|
|
|
2020-03-16 13:51:45 +11:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// Calculates the base-2 logarithm of an integer, rounding up to the next
|
|
|
|
/// highest integer.
|
2015-11-16 11:42:20 +11:00
|
|
|
template <typename T>
|
|
|
|
T
|
2020-03-16 13:51:45 +11:00
|
|
|
log2up (T val)
|
|
|
|
{
|
|
|
|
|
|
|
|
return log2 ((val << 1) - 1);
|
|
|
|
}
|
2014-09-17 16:41:38 +10:00
|
|
|
|
|
|
|
|
2020-09-19 09:05:23 +10:00
|
|
|
/// Naively calculates the integer log of `val` in `base`, rounded down.
|
|
|
|
///
|
|
|
|
/// We deliberately restrict this to consteval to limit unexpected issues
|
|
|
|
/// with runtime performance given the simplistic construction.
|
|
|
|
///
|
|
|
|
/// It's useful for sizing temporary arrays.
|
2022-03-18 12:38:30 +11:00
|
|
|
template <std::integral T>
|
2020-09-19 09:05:23 +10:00
|
|
|
consteval T
|
|
|
|
ilog (T val, T base)
|
|
|
|
{
|
|
|
|
T tally = 0;
|
|
|
|
while (val /= base)
|
|
|
|
++tally;
|
|
|
|
return tally;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2017-08-30 15:23:42 +10:00
|
|
|
/// round T up to the nearest multiple of U
|
2022-03-18 12:38:30 +11:00
|
|
|
template <std::integral T, std::integral U>
|
2015-11-16 11:42:20 +11:00
|
|
|
inline
|
2020-02-18 12:18:43 +11:00
|
|
|
std::common_type_t<T, U>
|
2017-08-30 15:39:02 +10:00
|
|
|
round_up (T value, U size)
|
2015-11-16 11:42:20 +11:00
|
|
|
{
|
2017-08-30 15:37:39 +10:00
|
|
|
// we perform this as two steps to avoid unnecessarily incrementing when
|
|
|
|
// remainder is zero.
|
|
|
|
if (value % size)
|
|
|
|
value += size - value % size;
|
|
|
|
return value;
|
2015-11-16 11:42:20 +11:00
|
|
|
}
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2011-08-29 14:31:22 +10:00
|
|
|
|
2017-08-30 15:23:42 +10:00
|
|
|
///----------------------------------------------------------------------------
|
|
|
|
/// round T up to the nearest power-of-2
|
2022-03-18 12:38:30 +11:00
|
|
|
template <std::integral T>
|
2020-02-18 12:18:43 +11:00
|
|
|
constexpr auto
|
2017-01-04 22:40:06 +11:00
|
|
|
round_pow2 (T value)
|
|
|
|
{
|
|
|
|
--value;
|
|
|
|
|
|
|
|
for (unsigned i = 1; i < sizeof (T) * 8; i <<= 1) {
|
|
|
|
value |= value >> i;
|
|
|
|
}
|
|
|
|
|
|
|
|
++value;
|
2018-05-03 18:32:08 +10:00
|
|
|
return value;
|
2017-01-04 22:40:06 +11:00
|
|
|
}
|
2011-08-29 14:31:22 +10:00
|
|
|
|
|
|
|
|
2017-08-30 15:23:42 +10:00
|
|
|
///----------------------------------------------------------------------------
|
|
|
|
/// round T up to the nearest multiple of U and return the quotient.
|
2020-02-18 12:18:43 +11:00
|
|
|
template <
|
2022-03-18 12:38:30 +11:00
|
|
|
std::integral T,
|
|
|
|
std::integral U
|
2015-11-16 11:42:20 +11:00
|
|
|
>
|
2020-02-18 12:18:43 +11:00
|
|
|
constexpr auto
|
|
|
|
divup (T const a, U const b)
|
2015-11-16 11:42:20 +11:00
|
|
|
{
|
|
|
|
return (a + b - 1) / b;
|
|
|
|
}
|
2011-08-29 14:31:22 +10:00
|
|
|
|
2015-01-21 23:35:08 +11:00
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Properties
|
2022-03-18 12:38:30 +11:00
|
|
|
template <std::integral T>
|
2020-02-18 12:18:43 +11:00
|
|
|
constexpr bool
|
2016-05-12 17:34:47 +10:00
|
|
|
is_integer (T)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-02-18 12:18:43 +11:00
|
|
|
|
2022-03-18 12:38:30 +11:00
|
|
|
template <std::floating_point T>
|
2020-02-18 12:18:43 +11:00
|
|
|
constexpr bool
|
2016-05-12 17:34:47 +10:00
|
|
|
is_integer (T t)
|
|
|
|
{
|
|
|
|
T i = 0;
|
2018-01-31 19:33:42 +11:00
|
|
|
return equal (std::modf (t, &i), T{0});
|
2016-05-12 17:34:47 +10:00
|
|
|
}
|
2015-01-21 23:35:08 +11:00
|
|
|
|
2012-04-20 18:04:40 +10:00
|
|
|
|
2018-01-01 15:46:29 +11:00
|
|
|
//-------------------------------------------------------------------------
|
2022-03-18 12:38:30 +11:00
|
|
|
template <std::integral NumericT>
|
2018-01-01 15:46:29 +11:00
|
|
|
constexpr auto
|
|
|
|
digits10 (NumericT v) noexcept
|
2016-07-28 13:36:23 +10:00
|
|
|
{
|
2018-01-01 15:46:29 +11:00
|
|
|
// cascading conditionals are faster, but it's super annoying to write
|
|
|
|
// out for arbitrarily sized types so we use this base case unti
|
|
|
|
// there's actually a performance reason to use another algorithm.
|
|
|
|
int tally = 0;
|
|
|
|
do {
|
|
|
|
v /= 10;
|
|
|
|
++tally;
|
|
|
|
} while (v);
|
|
|
|
|
|
|
|
return tally;
|
|
|
|
|
|
|
|
/*
|
2016-07-28 13:36:23 +10:00
|
|
|
return (v >= 1000000000) ? 10 :
|
|
|
|
(v >= 100000000) ? 9 :
|
|
|
|
(v >= 10000000) ? 8 :
|
|
|
|
(v >= 1000000) ? 7 :
|
|
|
|
(v >= 100000) ? 6 :
|
|
|
|
(v >= 10000) ? 5 :
|
|
|
|
(v >= 1000) ? 4 :
|
|
|
|
(v >= 100) ? 3 :
|
|
|
|
(v >= 10) ? 2 :
|
|
|
|
1;
|
2018-01-01 15:46:29 +11:00
|
|
|
*/
|
2016-07-28 13:36:23 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-18 12:38:30 +11:00
|
|
|
template <std::integral ValueT, std::integral BaseT>
|
2020-02-18 12:18:43 +11:00
|
|
|
constexpr int
|
2016-07-28 13:36:23 +10:00
|
|
|
digits (ValueT value, BaseT base) noexcept
|
|
|
|
{
|
2017-10-05 12:45:48 +11:00
|
|
|
assert (base > 0);
|
|
|
|
|
2016-07-28 13:36:23 +10:00
|
|
|
if (value < 0)
|
2016-08-29 14:48:07 +10:00
|
|
|
value *= -1;
|
2016-07-28 13:36:23 +10:00
|
|
|
|
2017-10-05 12:45:48 +11:00
|
|
|
int tally = 1;
|
2016-07-28 13:36:23 +10:00
|
|
|
while (value /= base)
|
|
|
|
++tally;
|
|
|
|
|
|
|
|
return tally;
|
|
|
|
}
|
2012-04-20 18:04:40 +10:00
|
|
|
|
2015-11-16 13:21:38 +11:00
|
|
|
|
2015-11-16 17:57:53 +11:00
|
|
|
///----------------------------------------------------------------------------
|
|
|
|
/// return positive or negative unit value corresponding to the input.
|
2022-03-18 12:38:30 +11:00
|
|
|
template <std::signed_integral T>
|
2020-02-18 12:18:43 +11:00
|
|
|
constexpr T
|
2015-11-16 13:57:51 +11:00
|
|
|
sign (T t)
|
|
|
|
{
|
|
|
|
return t < 0 ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
2015-11-16 17:57:53 +11:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// return positive or negative unit value corresponding to the input.
|
|
|
|
/// guaranteed to give correct results for signed zeroes, use another
|
|
|
|
/// method if extreme speed is important.
|
2022-03-18 12:38:30 +11:00
|
|
|
template <std::floating_point T>
|
2020-02-18 12:18:43 +11:00
|
|
|
constexpr T
|
2015-11-16 13:57:51 +11:00
|
|
|
sign (T t)
|
|
|
|
{
|
|
|
|
return std::signbit (t) ? -1 : 1;
|
|
|
|
}
|
2012-07-30 16:31:51 +10:00
|
|
|
|
2016-02-03 13:56:40 +11:00
|
|
|
//-------------------------------------------------------------------------
|
2021-05-10 11:39:39 +10:00
|
|
|
template <typename A, typename B>
|
2016-02-03 13:56:40 +11:00
|
|
|
constexpr
|
|
|
|
bool
|
2021-05-10 11:39:39 +10:00
|
|
|
samesign (A &&a, B &&b)
|
2016-02-03 13:56:40 +11:00
|
|
|
{
|
2018-03-27 20:16:32 +11:00
|
|
|
return (a >= 0 && b >= 0) || (a <= 0 && b <= 0);
|
2016-02-03 13:56:40 +11:00
|
|
|
}
|
|
|
|
|
2012-04-20 18:04:40 +10:00
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// factorisation
|
|
|
|
template <typename T>
|
|
|
|
const T&
|
|
|
|
identity (const T& t)
|
|
|
|
{
|
|
|
|
return t;
|
|
|
|
}
|
2012-05-03 18:11:42 +10:00
|
|
|
|
2012-05-08 16:39:58 +10:00
|
|
|
|
2016-09-14 17:37:49 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Modulus/etc
|
|
|
|
|
|
|
|
// namespaced wrapper for `man 3 fmod`
|
2022-03-18 12:38:30 +11:00
|
|
|
template <std::floating_point T>
|
2020-02-18 12:18:43 +11:00
|
|
|
constexpr T
|
2016-09-14 17:37:49 +10:00
|
|
|
mod (T x, T y)
|
|
|
|
{
|
|
|
|
return std::fmod (x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-18 12:38:30 +11:00
|
|
|
template <std::integral T>
|
2020-02-18 12:18:43 +11:00
|
|
|
constexpr T
|
2016-09-14 17:37:49 +10:00
|
|
|
mod (T x, T y)
|
|
|
|
{
|
|
|
|
return x % y;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-18 12:38:30 +11:00
|
|
|
template <std::floating_point ValueT>
|
2018-05-21 15:06:07 +10:00
|
|
|
ValueT
|
|
|
|
frac (ValueT val)
|
|
|
|
{
|
|
|
|
return val - static_cast<long> (val);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// angles, trig
|
2018-03-12 23:06:15 +11:00
|
|
|
namespace detail {
|
|
|
|
template <typename T>
|
|
|
|
struct pi;
|
|
|
|
|
|
|
|
template <> struct pi<float> { static constexpr float value = 3.141592653589793238462643f; };
|
|
|
|
template <> struct pi<double> { static constexpr double value = 3.141592653589793238462643; };
|
|
|
|
};
|
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
template <typename T>
|
2018-03-12 23:06:15 +11:00
|
|
|
constexpr auto pi = detail::pi<T>::value;
|
2012-05-08 16:39:58 +10:00
|
|
|
|
2015-11-16 13:21:38 +11:00
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
2018-03-12 23:06:15 +11:00
|
|
|
constexpr T E = static_cast<T> (2.71828182845904523536028747135266250);
|
2015-11-13 17:25:21 +11:00
|
|
|
|
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
constexpr T
|
|
|
|
to_degrees (T radians)
|
|
|
|
{
|
|
|
|
static_assert (std::is_floating_point<T>::value, "undefined for integral types");
|
2018-03-12 23:06:15 +11:00
|
|
|
return radians * 180 / pi<T>;
|
2015-11-16 11:42:20 +11:00
|
|
|
}
|
2015-02-04 15:44:03 +11:00
|
|
|
|
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
constexpr T
|
|
|
|
to_radians (T degrees)
|
|
|
|
{
|
|
|
|
static_assert (std::is_floating_point<T>::value, "undefined for integral types");
|
2018-03-12 23:06:15 +11:00
|
|
|
return degrees / 180 * pi<T>;
|
2015-11-16 11:42:20 +11:00
|
|
|
}
|
2011-10-29 23:13:47 +11:00
|
|
|
|
2015-02-02 15:25:22 +11:00
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//! Normalised sinc function
|
|
|
|
template <typename T>
|
|
|
|
constexpr T
|
|
|
|
sincn (T x)
|
|
|
|
{
|
2018-03-12 23:06:15 +11:00
|
|
|
return almost_zero (x) ? 1 : std::sin (pi<T> * x) / (pi<T> * x);
|
2015-11-16 11:42:20 +11:00
|
|
|
}
|
2011-10-29 23:13:47 +11:00
|
|
|
|
2015-01-21 23:39:23 +11:00
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//! Unnormalised sinc function
|
|
|
|
template <typename T>
|
|
|
|
constexpr T
|
|
|
|
sincu (T x)
|
|
|
|
{
|
|
|
|
return almost_zero (x) ? 1 : std::sin (x) / x;
|
|
|
|
}
|
2015-01-21 23:39:23 +11:00
|
|
|
|
|
|
|
|
2018-03-13 14:36:38 +11:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// thin wrappers around std trig identities.
|
|
|
|
//
|
|
|
|
// we have these because it's a little easier to qualify templates when
|
|
|
|
// passing function objects as compared to explicitly disambiguating raw
|
|
|
|
// functions (ie, with casts or typedefs).
|
|
|
|
template <typename ValueT> ValueT cos (ValueT theta) { return ::std::cos (theta); }
|
|
|
|
template <typename ValueT> ValueT sin (ValueT theta) { return ::std::sin (theta); }
|
|
|
|
template <typename ValueT> ValueT tan (ValueT theta) { return ::std::tan (theta); }
|
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// combinatorics
|
2015-01-13 18:32:30 +11:00
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
constexpr uintmax_t
|
|
|
|
factorial (unsigned i)
|
|
|
|
{
|
|
|
|
return i <= 1 ? 0 : i * factorial (i - 1);
|
|
|
|
}
|
2015-01-13 18:32:30 +11:00
|
|
|
|
2011-10-29 23:13:47 +11:00
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
/// stirlings approximation of factorials
|
2016-05-12 17:32:41 +10:00
|
|
|
inline uintmax_t
|
2015-11-16 11:42:20 +11:00
|
|
|
stirling (unsigned n)
|
|
|
|
{
|
2016-05-12 17:32:41 +10:00
|
|
|
using real_t = double;
|
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
return static_cast<uintmax_t> (
|
2018-03-12 23:06:15 +11:00
|
|
|
std::sqrt (2 * pi<real_t> * n) * std::pow (n / E<real_t>, n)
|
2015-11-16 11:42:20 +11:00
|
|
|
);
|
|
|
|
}
|
2011-10-29 23:13:47 +11:00
|
|
|
|
2015-11-13 17:25:21 +11:00
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
constexpr uintmax_t
|
|
|
|
combination (unsigned n, unsigned k)
|
|
|
|
{
|
|
|
|
return factorial (n) / (factorial (k) / (factorial (n - k)));
|
|
|
|
}
|
2015-02-02 15:25:22 +11:00
|
|
|
|
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// kahan summation for long floating point sequences
|
2015-02-02 15:25:22 +11:00
|
|
|
|
2020-02-18 12:18:43 +11:00
|
|
|
template <typename InputT>
|
|
|
|
requires
|
2022-03-18 12:38:30 +11:00
|
|
|
concepts::named::legacy_input_iterator<InputT> &&
|
|
|
|
std::floating_point<typename std::iterator_traits<InputT>::value_type>
|
2020-02-18 12:18:43 +11:00
|
|
|
typename std::iterator_traits<InputT>::value_type
|
2016-08-15 18:42:43 +10:00
|
|
|
sum (InputT first, InputT last)
|
2015-11-16 11:42:20 +11:00
|
|
|
{
|
2016-08-15 18:42:43 +10:00
|
|
|
using T = typename std::iterator_traits<InputT>::value_type;
|
2015-02-02 15:25:22 +11:00
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
T sum = 0;
|
|
|
|
T c = 0;
|
2015-02-02 15:25:22 +11:00
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
for (auto cursor = first; cursor != last; ++cursor) {
|
2017-06-16 17:38:55 +10:00
|
|
|
// Infinities are handled poorly in this implementation. We tend
|
|
|
|
// to produce NaNs because of the subtraction where we compute
|
|
|
|
// `c'. For the time being just panic in this scenario.
|
|
|
|
assert(!std::isinf (*cursor));
|
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
T y = *cursor - c;
|
|
|
|
T t = sum + y;
|
2017-06-16 17:38:55 +10:00
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
c = (t - sum) - y;
|
|
|
|
sum = t;
|
|
|
|
}
|
2015-02-02 15:25:22 +11:00
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
return sum;
|
2015-08-25 17:25:55 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-15 18:42:43 +10:00
|
|
|
//-------------------------------------------------------------------------
|
2020-02-18 12:18:43 +11:00
|
|
|
template <typename InputT>
|
|
|
|
requires
|
2022-03-18 12:38:30 +11:00
|
|
|
concepts::named::legacy_input_iterator<InputT> &&
|
|
|
|
std::integral<typename std::iterator_traits<InputT>::value_type>
|
2020-02-18 12:18:43 +11:00
|
|
|
typename std::iterator_traits<InputT>::value_type
|
2016-08-15 18:42:43 +10:00
|
|
|
sum (InputT first, InputT last)
|
|
|
|
{
|
|
|
|
using T = typename std::iterator_traits<InputT>::value_type;
|
|
|
|
return std::accumulate (first, last, T{0});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
/// Variadic minimum
|
2015-03-06 17:49:35 +11:00
|
|
|
template <typename T, typename U, typename ...Args>
|
2021-01-20 15:18:42 +11:00
|
|
|
constexpr
|
|
|
|
std::common_type_t<T,U>
|
2015-11-13 17:15:08 +11:00
|
|
|
min (const T a, const U b, Args ...args)
|
2015-03-06 17:49:35 +11:00
|
|
|
{
|
2018-05-30 16:54:04 +10:00
|
|
|
if constexpr (sizeof... (args) > 0) {
|
|
|
|
return min (a < b ? a : b, std::forward<Args> (args)...);
|
|
|
|
} else {
|
|
|
|
return a < b ? a : b;
|
|
|
|
}
|
2015-03-06 17:49:35 +11:00
|
|
|
}
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2020-10-20 16:34:27 +11:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// Unary maximum provided to simplify application of max to template
|
|
|
|
/// parameter packs.
|
|
|
|
///
|
|
|
|
/// eg, `max (sizeof (T)...)` will otherwise fail with a single type.
|
2022-03-18 12:38:30 +11:00
|
|
|
template <std::integral ValueT>
|
2020-10-20 16:34:27 +11:00
|
|
|
constexpr decltype(auto)
|
|
|
|
max (ValueT &&val)
|
|
|
|
{
|
|
|
|
return std::forward<ValueT> (val);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-06 17:49:35 +11:00
|
|
|
/// Variadic maximum
|
|
|
|
template <typename T, typename U, typename ...Args>
|
2021-01-20 15:18:42 +11:00
|
|
|
constexpr
|
|
|
|
std::common_type_t<T,U>
|
2015-11-13 17:15:08 +11:00
|
|
|
max (const T a, const U b, Args ...args)
|
2015-03-06 17:49:35 +11:00
|
|
|
{
|
2018-05-30 16:54:04 +10:00
|
|
|
if constexpr (sizeof... (args) > 0) {
|
|
|
|
return max (a > b ? a : b, std::forward<Args> (args)...);
|
|
|
|
} else {
|
|
|
|
return a > b ? a : b;
|
|
|
|
}
|
2015-03-06 17:49:35 +11:00
|
|
|
}
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2015-11-13 17:25:21 +11:00
|
|
|
|
2018-04-23 15:40:24 +10:00
|
|
|
//-------------------------------------------------------------------------
|
2022-03-18 12:38:30 +11:00
|
|
|
template <concepts::named::container ContainerT>
|
2020-02-18 12:18:43 +11:00
|
|
|
typename ContainerT::value_type const&
|
|
|
|
max (ContainerT const &vals)
|
2018-04-23 15:40:24 +10:00
|
|
|
{
|
|
|
|
return *std::max_element (vals.begin (), vals.end ());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-18 12:38:30 +11:00
|
|
|
template <concepts::named::container ValueT>
|
2020-02-18 12:18:43 +11:00
|
|
|
typename ValueT::value_type &
|
|
|
|
max (ValueT &&) = delete;
|
2018-04-23 15:40:24 +10:00
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
2022-03-18 12:38:30 +11:00
|
|
|
template <concepts::named::container ContainerT>
|
2020-02-18 12:18:43 +11:00
|
|
|
typename ContainerT::value_type const&
|
|
|
|
min (ContainerT const &vals)
|
2018-04-23 15:40:24 +10:00
|
|
|
{
|
|
|
|
return *std::min_element (vals.begin (), vals.end ());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-18 12:38:30 +11:00
|
|
|
template <concepts::named::container ContainerT>
|
2020-02-18 12:18:43 +11:00
|
|
|
typename ContainerT::value_type&
|
|
|
|
min (ContainerT &&) = delete;
|
2018-04-23 15:40:24 +10:00
|
|
|
|
|
|
|
|
2019-03-20 16:07:30 +11:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// Returns an ordered pair where the elements come from the parameters.
|
|
|
|
template <typename ValueT>
|
2020-02-18 12:18:43 +11:00
|
|
|
std::pair<ValueT, ValueT>
|
2019-03-20 16:07:30 +11:00
|
|
|
maxmin (ValueT a, ValueT b)
|
|
|
|
{
|
|
|
|
if (a >= b)
|
|
|
|
return { a, b };
|
|
|
|
else
|
|
|
|
return { b, a };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Limiting functions
|
2014-08-02 21:13:51 +10:00
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
// min/max clamping
|
2020-02-18 12:18:43 +11:00
|
|
|
template <
|
2022-03-18 12:38:30 +11:00
|
|
|
concepts::traits::scalar T,
|
|
|
|
concepts::traits::scalar U,
|
|
|
|
concepts::traits::scalar V
|
2017-08-24 12:26:36 +10:00
|
|
|
>
|
2020-02-18 12:18:43 +11:00
|
|
|
constexpr std::common_type_t<T,U,V>
|
|
|
|
clamp (T const val, U const lo, V const hi)
|
2015-11-16 11:42:20 +11:00
|
|
|
{
|
2016-07-28 13:36:23 +10:00
|
|
|
assert (lo <= hi);
|
2015-01-21 23:35:08 +11:00
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
return val > hi ? hi:
|
|
|
|
val < lo ? lo:
|
|
|
|
val;
|
|
|
|
}
|
2015-02-05 20:30:33 +11:00
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// clamped cubic hermite interpolation
|
|
|
|
template <typename T>
|
2016-12-21 20:23:13 +11:00
|
|
|
constexpr
|
2015-11-16 11:42:20 +11:00
|
|
|
T
|
|
|
|
smoothstep (T a, T b, T x)
|
|
|
|
{
|
2016-07-28 13:36:23 +10:00
|
|
|
assert (a <= b);
|
2018-05-03 21:43:48 +10:00
|
|
|
x = clamp ((x - a) / (b - a), T{0}, T{1});
|
2015-11-16 11:42:20 +11:00
|
|
|
return x * x * (3 - 2 * x);
|
|
|
|
}
|
2015-04-09 17:44:50 +10:00
|
|
|
|
2015-10-20 16:50:44 +11:00
|
|
|
|
2016-12-21 20:23:13 +11:00
|
|
|
//-------------------------------------------------------------------------
|
2020-02-18 12:18:43 +11:00
|
|
|
template <
|
|
|
|
concepts::numeric U,
|
|
|
|
concepts::numeric T
|
|
|
|
>
|
|
|
|
constexpr U
|
|
|
|
mix (U const a, U const b, T const t)
|
2016-12-21 20:23:13 +11:00
|
|
|
{
|
2018-03-14 18:13:05 +11:00
|
|
|
// give some tolerance for floating point rounding
|
|
|
|
assert (t >= -0.00001f);
|
|
|
|
assert (t <= 1.00001f);
|
2016-12-21 20:23:13 +11:00
|
|
|
|
|
|
|
return a * (1 - t) + b * t;
|
|
|
|
}
|
|
|
|
|
2015-04-09 17:44:50 +10:00
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2017-05-24 15:15:25 +10:00
|
|
|
/// convert between different representations of normalised quantities.
|
|
|
|
///
|
|
|
|
/// * floating point values must be within [0, 1] (otherwise undefined)
|
|
|
|
/// * signed values are handled by converting to unsigned representations
|
|
|
|
/// * may introduce small biases when expanding values so that low order
|
|
|
|
/// bits have some meaning (particularly when dealing with UINTMAX)
|
|
|
|
|
|
|
|
// uint -> float
|
2020-02-18 12:18:43 +11:00
|
|
|
template <
|
2022-03-18 12:38:30 +11:00
|
|
|
std::unsigned_integral T,
|
|
|
|
std::floating_point U
|
2020-02-18 12:18:43 +11:00
|
|
|
>
|
|
|
|
constexpr U
|
2015-11-16 11:42:20 +11:00
|
|
|
renormalise (T t)
|
|
|
|
{
|
|
|
|
return t / static_cast<U> (std::numeric_limits<T>::max ());
|
|
|
|
}
|
2015-04-09 17:44:50 +10:00
|
|
|
|
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
//-------------------------------------------------------------------------
|
2017-05-24 15:15:25 +10:00
|
|
|
// float -> uint
|
2020-02-18 12:18:43 +11:00
|
|
|
template <
|
2022-03-18 12:38:30 +11:00
|
|
|
std::floating_point T,
|
|
|
|
std::unsigned_integral U
|
2020-02-18 12:18:43 +11:00
|
|
|
>
|
|
|
|
constexpr U
|
2015-11-16 11:42:20 +11:00
|
|
|
renormalise (T t)
|
|
|
|
{
|
|
|
|
// Ideally std::ldexp would be involved but it complicates handing
|
|
|
|
// integers with greater precision than our floating point type. Also it
|
|
|
|
// would prohibit constexpr and involve errno.
|
|
|
|
|
|
|
|
size_t usable = std::numeric_limits<T>::digits;
|
|
|
|
size_t available = sizeof (U) * 8;
|
|
|
|
size_t shift = std::max (available, usable) - usable;
|
|
|
|
|
2018-05-03 21:43:48 +10:00
|
|
|
t = clamp (t, 0, 1);
|
2015-11-16 11:42:20 +11:00
|
|
|
|
|
|
|
// construct an integer of the float's mantissa size, multiply it by our
|
|
|
|
// parameter, then shift it back into the full range of the integer type.
|
|
|
|
U in = std::numeric_limits<U>::max () >> shift;
|
|
|
|
U mid = static_cast<U> (t * in);
|
|
|
|
U out = mid << shift;
|
|
|
|
|
|
|
|
// use the top bits of the output to fill the bottom bits which through
|
|
|
|
// shifting would otherwise be zero. this gives us the full extent of the
|
|
|
|
// integer range, while varying predictably through the entire output
|
|
|
|
// space.
|
|
|
|
return out | out >> (available - shift);
|
|
|
|
}
|
2015-02-05 20:30:33 +11:00
|
|
|
|
2015-10-20 16:50:44 +11:00
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
//-------------------------------------------------------------------------
|
2017-05-24 15:15:25 +10:00
|
|
|
// float -> float, avoids identity conversion as we don't want to create
|
2015-11-16 11:42:20 +11:00
|
|
|
// ambiguous overloads
|
|
|
|
template <typename T, typename U>
|
2020-02-18 12:18:43 +11:00
|
|
|
requires
|
2022-03-18 12:38:30 +11:00
|
|
|
std::floating_point<T> &&
|
|
|
|
std::floating_point<U> &&
|
2020-02-18 12:18:43 +11:00
|
|
|
(!std::is_same_v<T, U>)
|
|
|
|
constexpr U
|
2015-11-16 11:42:20 +11:00
|
|
|
renormalise (T t)
|
|
|
|
{
|
|
|
|
return static_cast<U> (t);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
2017-05-24 15:15:25 +10:00
|
|
|
// hi_uint -> lo_uint
|
2015-11-16 11:42:20 +11:00
|
|
|
template <typename T, typename U>
|
2020-02-18 12:18:43 +11:00
|
|
|
requires
|
2022-03-18 12:38:30 +11:00
|
|
|
std::unsigned_integral<T> &&
|
|
|
|
std::unsigned_integral<U> &&
|
2020-02-18 12:18:43 +11:00
|
|
|
(sizeof (T) > sizeof (U))
|
|
|
|
constexpr U
|
2015-11-16 11:42:20 +11:00
|
|
|
renormalise (T t)
|
|
|
|
{
|
|
|
|
static_assert (sizeof (T) > sizeof (U),
|
|
|
|
"assumes right shift is sufficient");
|
2015-10-20 16:50:44 +11:00
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
// we have excess bits ,just shift and return
|
|
|
|
constexpr auto shift = 8 * (sizeof (T) - sizeof (U));
|
|
|
|
return t >> shift;
|
|
|
|
}
|
2015-10-20 16:50:44 +11:00
|
|
|
|
2015-10-29 18:22:46 +11:00
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
//-------------------------------------------------------------------------
|
2017-05-24 15:15:25 +10:00
|
|
|
// lo_uint -> hi_uint
|
2017-10-11 17:15:24 +11:00
|
|
|
template <
|
|
|
|
typename SrcT,
|
2020-02-18 12:18:43 +11:00
|
|
|
typename DstT
|
2017-10-11 17:15:24 +11:00
|
|
|
>
|
2020-02-18 12:18:43 +11:00
|
|
|
requires
|
2022-03-18 12:38:30 +11:00
|
|
|
std::unsigned_integral<SrcT> &&
|
|
|
|
std::unsigned_integral<DstT> &&
|
2020-02-18 12:18:43 +11:00
|
|
|
(sizeof (SrcT) < sizeof (DstT))
|
2017-10-11 17:15:24 +11:00
|
|
|
constexpr DstT
|
|
|
|
renormalise (SrcT src)
|
|
|
|
{
|
|
|
|
// we can make some simplifying assumptions for the shift distances if
|
|
|
|
// we assume the integers are powers of two. this is probably going to
|
|
|
|
// be the case for every conceivable input type, but we don't want to
|
|
|
|
// get caught out if we extend this routine to more general types
|
|
|
|
// (eg, OpenGL DEPTH24).
|
|
|
|
static_assert (is_pow2 (sizeof (SrcT)));
|
|
|
|
static_assert (is_pow2 (sizeof (DstT)));
|
|
|
|
|
|
|
|
static_assert (sizeof (SrcT) < sizeof (DstT),
|
2015-11-16 11:42:20 +11:00
|
|
|
"assumes bit creation is required to fill space");
|
2015-10-20 16:50:44 +11:00
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
// we need to create bits. fill the output integer with copies of ourself.
|
|
|
|
// this is approximately correct in the general case (introducing a small
|
2017-10-11 17:15:24 +11:00
|
|
|
// linear positive bias), but it allows us to set all output bits high
|
|
|
|
// when we receive the maximum allowable input value.
|
|
|
|
static_assert (sizeof (DstT) % sizeof (SrcT) == 0,
|
2015-11-16 11:42:20 +11:00
|
|
|
"assumes integer multiple of sizes");
|
2015-10-20 16:50:44 +11:00
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
|
2017-10-11 17:15:24 +11:00
|
|
|
// clang#xxxx: ideally we wouldn't use a multiplication here, but we
|
|
|
|
// trigger a segfault in clang-5.0 when using ld.gold+lto;
|
|
|
|
// 'X86 DAG->DAG Instruction Selection'
|
|
|
|
//
|
|
|
|
// create a mask of locations we'd like copies of the src bit pattern.
|
|
|
|
//
|
|
|
|
// this replicates repeatedly or'ing and shifting dst with itself.
|
|
|
|
DstT dst { 1 };
|
|
|
|
for (unsigned i = sizeof (SrcT) * 8; i < sizeof (DstT) * 8; i *= 2)
|
|
|
|
dst |= dst << i;
|
|
|
|
return dst * src;
|
2015-11-16 11:42:20 +11:00
|
|
|
}
|
2015-10-20 16:50:44 +11:00
|
|
|
|
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
//-------------------------------------------------------------------------
|
2017-05-24 15:15:25 +10:00
|
|
|
// identity transformation. must precede the signed cases, as they may rely
|
|
|
|
// on this as a side effect of casts.
|
2015-11-16 11:42:20 +11:00
|
|
|
template <typename T, typename U>
|
2020-02-18 12:18:43 +11:00
|
|
|
requires (std::is_same_v<T, U>)
|
|
|
|
constexpr U
|
2015-11-16 11:42:20 +11:00
|
|
|
renormalise (T t)
|
|
|
|
{ return t; }
|
2017-05-24 15:15:25 +10:00
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// anything-to-sint
|
|
|
|
template <typename T, typename U>
|
2020-02-18 12:18:43 +11:00
|
|
|
requires
|
2022-03-18 12:38:30 +11:00
|
|
|
std::signed_integral<U> &&
|
2020-02-18 12:18:43 +11:00
|
|
|
(!std::is_same<T,U>::value)
|
|
|
|
constexpr U
|
2017-05-24 15:15:25 +10:00
|
|
|
renormalise (T t)
|
|
|
|
{
|
|
|
|
using uint_t = typename std::make_unsigned<U>::type;
|
|
|
|
|
|
|
|
return static_cast<U> (
|
2018-08-05 14:42:02 +10:00
|
|
|
::cruft::renormalise<T,uint_t> (t) + std::numeric_limits<U>::min ()
|
2017-05-24 15:15:25 +10:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// sint-to-anything
|
|
|
|
template <typename T, typename U>
|
2020-02-18 12:18:43 +11:00
|
|
|
requires
|
2022-03-18 12:38:30 +11:00
|
|
|
std::signed_integral<T> &&
|
2020-02-18 12:18:43 +11:00
|
|
|
(!std::is_same<T,U>::value)
|
|
|
|
constexpr U
|
2017-05-24 15:15:25 +10:00
|
|
|
renormalise (T sint)
|
|
|
|
{
|
|
|
|
using uint_t = typename std::make_unsigned<T>::type;
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
return ::cruft::renormalise<uint_t,U> (
|
2017-05-24 15:15:25 +10:00
|
|
|
static_cast<uint_t> (sint) - std::numeric_limits<T>::min ()
|
|
|
|
);
|
|
|
|
};
|
2015-11-16 11:42:20 +11:00
|
|
|
}
|