2013-07-24 11:41:39 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-08-27 05:58:23 +04: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/. */
|
|
|
|
|
|
|
|
/* mfbt maths algorithms. */
|
|
|
|
|
2013-07-24 11:41:39 +04:00
|
|
|
#ifndef mozilla_MathAlgorithms_h
|
|
|
|
#define mozilla_MathAlgorithms_h
|
2012-08-27 05:58:23 +04:00
|
|
|
|
|
|
|
#include "mozilla/Assertions.h"
|
2013-02-16 07:55:36 +04:00
|
|
|
|
2013-03-14 11:16:55 +04:00
|
|
|
#include <cmath>
|
2013-02-16 07:55:36 +04:00
|
|
|
#include <limits.h>
|
2013-07-30 18:25:31 +04:00
|
|
|
#include <stdint.h>
|
2020-03-28 16:57:14 +03:00
|
|
|
#include <type_traits>
|
2012-08-27 05:58:23 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
// Greatest Common Divisor
|
|
|
|
template <typename IntegerType>
|
2014-06-13 10:34:08 +04:00
|
|
|
MOZ_ALWAYS_INLINE IntegerType EuclidGCD(IntegerType aA, IntegerType aB) {
|
2012-08-27 05:58:23 +04:00
|
|
|
// Euclid's algorithm; O(N) in the worst case. (There are better
|
|
|
|
// ways, but we don't need them for the current use of this algo.)
|
2014-06-13 10:34:08 +04:00
|
|
|
MOZ_ASSERT(aA > IntegerType(0));
|
|
|
|
MOZ_ASSERT(aB > IntegerType(0));
|
2012-08-27 05:58:23 +04:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
while (aA != aB) {
|
|
|
|
if (aA > aB) {
|
|
|
|
aA = aA - aB;
|
2012-08-27 05:58:23 +04:00
|
|
|
} else {
|
2014-06-13 10:34:08 +04:00
|
|
|
aB = aB - aA;
|
2012-08-27 05:58:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
return aA;
|
2012-08-27 05:58:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Least Common Multiple
|
|
|
|
template <typename IntegerType>
|
2014-06-13 10:34:08 +04:00
|
|
|
MOZ_ALWAYS_INLINE IntegerType EuclidLCM(IntegerType aA, IntegerType aB) {
|
2012-08-27 05:58:23 +04:00
|
|
|
// Divide first to reduce overflow risk.
|
2014-06-13 10:34:08 +04:00
|
|
|
return (aA / EuclidGCD(aA, aB)) * aB;
|
2012-08-27 05:58:23 +04:00
|
|
|
}
|
|
|
|
|
2013-02-16 07:55:36 +04:00
|
|
|
namespace detail {
|
|
|
|
|
2013-03-06 03:43:23 +04:00
|
|
|
template <typename T>
|
2020-03-28 16:57:20 +03:00
|
|
|
struct AllowDeprecatedAbsFixed : std::false_type {};
|
2013-03-06 03:43:23 +04:00
|
|
|
|
|
|
|
template <>
|
2020-03-28 16:57:20 +03:00
|
|
|
struct AllowDeprecatedAbsFixed<int32_t> : std::true_type {};
|
2013-03-06 03:43:23 +04:00
|
|
|
template <>
|
2020-03-28 16:57:20 +03:00
|
|
|
struct AllowDeprecatedAbsFixed<int64_t> : std::true_type {};
|
2013-03-06 03:43:23 +04:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct AllowDeprecatedAbs : AllowDeprecatedAbsFixed<T> {};
|
|
|
|
|
|
|
|
template <>
|
2020-03-28 16:57:20 +03:00
|
|
|
struct AllowDeprecatedAbs<int> : std::true_type {};
|
2013-03-06 03:43:23 +04:00
|
|
|
template <>
|
2020-03-28 16:57:20 +03:00
|
|
|
struct AllowDeprecatedAbs<long> : std::true_type {};
|
2013-03-06 03:43:23 +04:00
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
// DO NOT USE DeprecatedAbs. It exists only until its callers can be converted
|
|
|
|
// to Abs below, and it will be removed when all callers have been changed.
|
|
|
|
template <typename T>
|
2020-03-28 16:35:31 +03:00
|
|
|
inline std::enable_if_t<detail::AllowDeprecatedAbs<T>::value, T> DeprecatedAbs(
|
|
|
|
const T aValue) {
|
2013-03-06 03:43:23 +04:00
|
|
|
// The absolute value of the smallest possible value of a signed-integer type
|
|
|
|
// won't fit in that type (on twos-complement systems -- and we're blithely
|
|
|
|
// assuming we're on such systems, for the non-<stdint.h> types listed above),
|
|
|
|
// so assert that the input isn't that value.
|
|
|
|
//
|
|
|
|
// This is the case if: the value is non-negative; or if adding one (giving a
|
|
|
|
// value in the range [-maxvalue, 0]), then negating (giving a value in the
|
|
|
|
// range [0, maxvalue]), doesn't produce maxvalue (because in twos-complement,
|
|
|
|
// (minvalue + 1) == -maxvalue).
|
2014-06-13 10:34:08 +04:00
|
|
|
MOZ_ASSERT(aValue >= 0 ||
|
|
|
|
-(aValue + 1) != T((1ULL << (CHAR_BIT * sizeof(T) - 1)) - 1),
|
2013-03-06 03:43:23 +04:00
|
|
|
"You can't negate the smallest possible negative integer!");
|
2014-06-13 10:34:08 +04:00
|
|
|
return aValue >= 0 ? aValue : -aValue;
|
2013-03-06 03:43:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
2020-03-28 19:07:06 +03:00
|
|
|
template <typename T, typename = void>
|
|
|
|
struct AbsReturnType;
|
2013-02-16 07:55:36 +04:00
|
|
|
|
|
|
|
template <typename T>
|
2020-03-28 19:07:06 +03:00
|
|
|
struct AbsReturnType<
|
|
|
|
T, std::enable_if_t<std::is_integral_v<T> && std::is_signed_v<T>>> {
|
|
|
|
using Type = std::make_unsigned_t<T>;
|
2013-03-06 03:43:38 +04:00
|
|
|
};
|
|
|
|
|
2020-03-28 16:35:31 +03:00
|
|
|
template <typename T>
|
2020-03-28 19:07:06 +03:00
|
|
|
struct AbsReturnType<T, std::enable_if_t<std::is_floating_point_v<T>>> {
|
|
|
|
using Type = T;
|
2018-11-30 13:46:48 +03:00
|
|
|
};
|
|
|
|
|
2013-02-16 07:55:36 +04:00
|
|
|
} // namespace detail
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-02-16 07:55:36 +04:00
|
|
|
template <typename T>
|
2018-02-16 04:22:14 +03:00
|
|
|
inline constexpr typename detail::AbsReturnType<T>::Type Abs(const T aValue) {
|
|
|
|
using ReturnType = typename detail::AbsReturnType<T>::Type;
|
2014-06-13 10:34:08 +04:00
|
|
|
return aValue >= 0 ? ReturnType(aValue) : ~ReturnType(aValue) + 1;
|
2013-02-16 07:55:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2014-06-13 10:34:08 +04:00
|
|
|
inline float Abs<float>(const float aFloat) {
|
|
|
|
return std::fabs(aFloat);
|
2013-02-16 07:55:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2014-06-13 10:34:08 +04:00
|
|
|
inline double Abs<double>(const double aDouble) {
|
|
|
|
return std::fabs(aDouble);
|
2013-02-16 07:55:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2014-06-13 10:34:08 +04:00
|
|
|
inline long double Abs<long double>(const long double aLongDouble) {
|
|
|
|
return std::fabs(aLongDouble);
|
2013-02-16 07:55:36 +04:00
|
|
|
}
|
|
|
|
|
2013-07-04 02:46:51 +04:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2018-08-04 01:07:34 +03:00
|
|
|
#if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64) || \
|
|
|
|
defined(_M_X64) || defined(_M_ARM64))
|
2013-07-04 02:46:51 +04:00
|
|
|
# define MOZ_BITSCAN_WINDOWS
|
|
|
|
|
2014-06-10 01:26:55 +04:00
|
|
|
# include <intrin.h>
|
2013-07-04 02:46:51 +04:00
|
|
|
# pragma intrinsic(_BitScanForward, _BitScanReverse)
|
|
|
|
|
2018-08-04 01:07:34 +03:00
|
|
|
# if defined(_M_AMD64) || defined(_M_X64) || defined(_M_ARM64)
|
2013-07-04 02:46:51 +04:00
|
|
|
# define MOZ_BITSCAN_WINDOWS64
|
|
|
|
# pragma intrinsic(_BitScanForward64, _BitScanReverse64)
|
|
|
|
# endif
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
#if defined(MOZ_BITSCAN_WINDOWS)
|
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
inline uint_fast8_t CountLeadingZeroes32(uint32_t aValue) {
|
|
|
|
unsigned long index;
|
2015-07-17 20:24:30 +03:00
|
|
|
if (!_BitScanReverse(&index, static_cast<unsigned long>(aValue))) return 32;
|
2014-06-13 10:34:08 +04:00
|
|
|
return uint_fast8_t(31 - index);
|
|
|
|
}
|
2013-07-04 02:46:51 +04:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
inline uint_fast8_t CountTrailingZeroes32(uint32_t aValue) {
|
|
|
|
unsigned long index;
|
2015-07-17 20:24:30 +03:00
|
|
|
if (!_BitScanForward(&index, static_cast<unsigned long>(aValue))) return 32;
|
2014-06-13 10:34:08 +04:00
|
|
|
return uint_fast8_t(index);
|
|
|
|
}
|
2013-07-04 02:46:51 +04:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
inline uint_fast8_t CountPopulation32(uint32_t aValue) {
|
|
|
|
uint32_t x = aValue - ((aValue >> 1) & 0x55555555);
|
|
|
|
x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
|
|
|
|
return (((x + (x >> 4)) & 0xf0f0f0f) * 0x1010101) >> 24;
|
|
|
|
}
|
2014-06-25 20:54:34 +04:00
|
|
|
inline uint_fast8_t CountPopulation64(uint64_t aValue) {
|
|
|
|
return uint_fast8_t(CountPopulation32(aValue & 0xffffffff) +
|
|
|
|
CountPopulation32(aValue >> 32));
|
|
|
|
}
|
2013-08-19 23:32:22 +04:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
inline uint_fast8_t CountLeadingZeroes64(uint64_t aValue) {
|
|
|
|
# if defined(MOZ_BITSCAN_WINDOWS64)
|
|
|
|
unsigned long index;
|
2015-07-17 20:24:30 +03:00
|
|
|
if (!_BitScanReverse64(&index, static_cast<unsigned __int64>(aValue)))
|
|
|
|
return 64;
|
2014-06-13 10:34:08 +04:00
|
|
|
return uint_fast8_t(63 - index);
|
|
|
|
# else
|
|
|
|
uint32_t hi = uint32_t(aValue >> 32);
|
|
|
|
if (hi != 0) {
|
|
|
|
return CountLeadingZeroes32(hi);
|
2013-07-04 02:46:51 +04:00
|
|
|
}
|
2014-06-13 10:34:08 +04:00
|
|
|
return 32u + CountLeadingZeroes32(uint32_t(aValue));
|
|
|
|
# endif
|
|
|
|
}
|
2013-07-04 02:46:51 +04:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
inline uint_fast8_t CountTrailingZeroes64(uint64_t aValue) {
|
|
|
|
# if defined(MOZ_BITSCAN_WINDOWS64)
|
|
|
|
unsigned long index;
|
2015-07-17 20:24:30 +03:00
|
|
|
if (!_BitScanForward64(&index, static_cast<unsigned __int64>(aValue)))
|
|
|
|
return 64;
|
2014-06-13 10:34:08 +04:00
|
|
|
return uint_fast8_t(index);
|
|
|
|
# else
|
|
|
|
uint32_t lo = uint32_t(aValue);
|
|
|
|
if (lo != 0) {
|
|
|
|
return CountTrailingZeroes32(lo);
|
2013-07-04 02:46:51 +04:00
|
|
|
}
|
2014-06-13 10:34:08 +04:00
|
|
|
return 32u + CountTrailingZeroes32(uint32_t(aValue >> 32));
|
|
|
|
# endif
|
|
|
|
}
|
2013-07-04 02:46:51 +04:00
|
|
|
|
|
|
|
#elif defined(__clang__) || defined(__GNUC__)
|
|
|
|
|
|
|
|
# if defined(__clang__)
|
|
|
|
# if !__has_builtin(__builtin_ctz) || !__has_builtin(__builtin_clz)
|
|
|
|
# error "A clang providing __builtin_c[lt]z is required to build"
|
|
|
|
# endif
|
|
|
|
# else
|
|
|
|
// gcc has had __builtin_clz and friends since 3.4: no need to check.
|
|
|
|
# endif
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
inline uint_fast8_t CountLeadingZeroes32(uint32_t aValue) {
|
2019-04-02 20:00:53 +03:00
|
|
|
return static_cast<uint_fast8_t>(__builtin_clz(aValue));
|
2014-06-13 10:34:08 +04:00
|
|
|
}
|
2013-07-04 02:46:51 +04:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
inline uint_fast8_t CountTrailingZeroes32(uint32_t aValue) {
|
2019-04-02 20:00:53 +03:00
|
|
|
return static_cast<uint_fast8_t>(__builtin_ctz(aValue));
|
2014-06-13 10:34:08 +04:00
|
|
|
}
|
2013-07-04 02:46:51 +04:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
inline uint_fast8_t CountPopulation32(uint32_t aValue) {
|
2019-04-02 20:00:53 +03:00
|
|
|
return static_cast<uint_fast8_t>(__builtin_popcount(aValue));
|
2014-06-13 10:34:08 +04:00
|
|
|
}
|
2013-08-19 23:32:22 +04:00
|
|
|
|
2014-06-25 20:54:34 +04:00
|
|
|
inline uint_fast8_t CountPopulation64(uint64_t aValue) {
|
2019-04-02 20:00:53 +03:00
|
|
|
return static_cast<uint_fast8_t>(__builtin_popcountll(aValue));
|
2014-06-25 20:54:34 +04:00
|
|
|
}
|
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
inline uint_fast8_t CountLeadingZeroes64(uint64_t aValue) {
|
2019-04-02 20:00:53 +03:00
|
|
|
return static_cast<uint_fast8_t>(__builtin_clzll(aValue));
|
2014-06-13 10:34:08 +04:00
|
|
|
}
|
2013-07-04 02:46:51 +04:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
inline uint_fast8_t CountTrailingZeroes64(uint64_t aValue) {
|
2019-04-02 20:00:53 +03:00
|
|
|
return static_cast<uint_fast8_t>(__builtin_ctzll(aValue));
|
2014-06-13 10:34:08 +04:00
|
|
|
}
|
2013-07-04 02:46:51 +04:00
|
|
|
|
|
|
|
#else
|
|
|
|
# error "Implement these!"
|
2015-01-07 02:35:02 +03:00
|
|
|
inline uint_fast8_t CountLeadingZeroes32(uint32_t aValue) = delete;
|
|
|
|
inline uint_fast8_t CountTrailingZeroes32(uint32_t aValue) = delete;
|
|
|
|
inline uint_fast8_t CountPopulation32(uint32_t aValue) = delete;
|
|
|
|
inline uint_fast8_t CountPopulation64(uint64_t aValue) = delete;
|
|
|
|
inline uint_fast8_t CountLeadingZeroes64(uint64_t aValue) = delete;
|
|
|
|
inline uint_fast8_t CountTrailingZeroes64(uint64_t aValue) = delete;
|
2013-07-04 02:46:51 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
/**
|
2014-06-13 10:34:08 +04:00
|
|
|
* Compute the number of high-order zero bits in the NON-ZERO number |aValue|.
|
|
|
|
* That is, looking at the bitwise representation of the number, with the
|
|
|
|
* highest- valued bits at the start, return the number of zeroes before the
|
|
|
|
* first one is observed.
|
2013-07-04 02:46:51 +04:00
|
|
|
*
|
|
|
|
* CountLeadingZeroes32(0xF0FF1000) is 0;
|
|
|
|
* CountLeadingZeroes32(0x7F8F0001) is 1;
|
|
|
|
* CountLeadingZeroes32(0x3FFF0100) is 2;
|
|
|
|
* CountLeadingZeroes32(0x1FF50010) is 3; and so on.
|
|
|
|
*/
|
2014-06-13 10:34:08 +04:00
|
|
|
inline uint_fast8_t CountLeadingZeroes32(uint32_t aValue) {
|
|
|
|
MOZ_ASSERT(aValue != 0);
|
|
|
|
return detail::CountLeadingZeroes32(aValue);
|
2013-07-04 02:46:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-13 10:34:08 +04:00
|
|
|
* Compute the number of low-order zero bits in the NON-ZERO number |aValue|.
|
|
|
|
* That is, looking at the bitwise representation of the number, with the
|
|
|
|
* lowest- valued bits at the start, return the number of zeroes before the
|
|
|
|
* first one is observed.
|
2013-07-04 02:46:51 +04:00
|
|
|
*
|
|
|
|
* CountTrailingZeroes32(0x0100FFFF) is 0;
|
|
|
|
* CountTrailingZeroes32(0x7000FFFE) is 1;
|
|
|
|
* CountTrailingZeroes32(0x0080FFFC) is 2;
|
|
|
|
* CountTrailingZeroes32(0x0080FFF8) is 3; and so on.
|
|
|
|
*/
|
2014-06-13 10:34:08 +04:00
|
|
|
inline uint_fast8_t CountTrailingZeroes32(uint32_t aValue) {
|
|
|
|
MOZ_ASSERT(aValue != 0);
|
|
|
|
return detail::CountTrailingZeroes32(aValue);
|
2013-07-04 02:46:51 +04:00
|
|
|
}
|
|
|
|
|
2013-08-19 23:32:22 +04:00
|
|
|
/**
|
2014-06-13 10:34:08 +04:00
|
|
|
* Compute the number of one bits in the number |aValue|,
|
2013-08-19 23:32:22 +04:00
|
|
|
*/
|
2014-06-13 10:34:08 +04:00
|
|
|
inline uint_fast8_t CountPopulation32(uint32_t aValue) {
|
|
|
|
return detail::CountPopulation32(aValue);
|
2013-08-19 23:32:22 +04:00
|
|
|
}
|
|
|
|
|
2016-05-11 20:00:53 +03:00
|
|
|
/** Analogous to CountPopulation32, but for 64-bit numbers */
|
2014-06-25 20:54:34 +04:00
|
|
|
inline uint_fast8_t CountPopulation64(uint64_t aValue) {
|
|
|
|
return detail::CountPopulation64(aValue);
|
|
|
|
}
|
|
|
|
|
2013-07-04 02:46:51 +04:00
|
|
|
/** Analogous to CountLeadingZeroes32, but for 64-bit numbers. */
|
2014-06-13 10:34:08 +04:00
|
|
|
inline uint_fast8_t CountLeadingZeroes64(uint64_t aValue) {
|
|
|
|
MOZ_ASSERT(aValue != 0);
|
|
|
|
return detail::CountLeadingZeroes64(aValue);
|
2013-07-04 02:46:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Analogous to CountTrailingZeroes32, but for 64-bit numbers. */
|
2014-06-13 10:34:08 +04:00
|
|
|
inline uint_fast8_t CountTrailingZeroes64(uint64_t aValue) {
|
|
|
|
MOZ_ASSERT(aValue != 0);
|
|
|
|
return detail::CountTrailingZeroes64(aValue);
|
2013-07-04 02:46:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
template <typename T, size_t Size = sizeof(T)>
|
|
|
|
class CeilingLog2;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class CeilingLog2<T, 4> {
|
2014-06-13 10:34:08 +04:00
|
|
|
public:
|
|
|
|
static uint_fast8_t compute(const T aValue) {
|
|
|
|
// Check for <= 1 to avoid the == 0 undefined case.
|
|
|
|
return aValue <= 1 ? 0u : 32u - CountLeadingZeroes32(aValue - 1);
|
|
|
|
}
|
2013-07-04 02:46:51 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class CeilingLog2<T, 8> {
|
2014-06-13 10:34:08 +04:00
|
|
|
public:
|
|
|
|
static uint_fast8_t compute(const T aValue) {
|
|
|
|
// Check for <= 1 to avoid the == 0 undefined case.
|
2015-01-23 00:29:36 +03:00
|
|
|
return aValue <= 1 ? 0u : 64u - CountLeadingZeroes64(aValue - 1);
|
2014-06-13 10:34:08 +04:00
|
|
|
}
|
2013-07-04 02:46:51 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
/**
|
2014-06-13 10:34:08 +04:00
|
|
|
* Compute the log of the least power of 2 greater than or equal to |aValue|.
|
2013-07-04 02:46:51 +04:00
|
|
|
*
|
|
|
|
* CeilingLog2(0..1) is 0;
|
|
|
|
* CeilingLog2(2) is 1;
|
|
|
|
* CeilingLog2(3..4) is 2;
|
|
|
|
* CeilingLog2(5..8) is 3;
|
|
|
|
* CeilingLog2(9..16) is 4; and so on.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2014-06-13 10:34:08 +04:00
|
|
|
inline uint_fast8_t CeilingLog2(const T aValue) {
|
|
|
|
return detail::CeilingLog2<T>::compute(aValue);
|
2013-07-04 02:46:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** A CeilingLog2 variant that accepts only size_t. */
|
2014-06-13 10:34:08 +04:00
|
|
|
inline uint_fast8_t CeilingLog2Size(size_t aValue) {
|
|
|
|
return CeilingLog2(aValue);
|
2013-07-04 02:46:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
template <typename T, size_t Size = sizeof(T)>
|
|
|
|
class FloorLog2;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class FloorLog2<T, 4> {
|
2014-06-13 10:34:08 +04:00
|
|
|
public:
|
|
|
|
static uint_fast8_t compute(const T aValue) {
|
|
|
|
return 31u - CountLeadingZeroes32(aValue | 1);
|
|
|
|
}
|
2013-07-04 02:46:51 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class FloorLog2<T, 8> {
|
2014-06-13 10:34:08 +04:00
|
|
|
public:
|
|
|
|
static uint_fast8_t compute(const T aValue) {
|
|
|
|
return 63u - CountLeadingZeroes64(aValue | 1);
|
|
|
|
}
|
2013-07-04 02:46:51 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
/**
|
2014-06-13 10:34:08 +04:00
|
|
|
* Compute the log of the greatest power of 2 less than or equal to |aValue|.
|
2013-07-04 02:46:51 +04:00
|
|
|
*
|
|
|
|
* FloorLog2(0..1) is 0;
|
|
|
|
* FloorLog2(2..3) is 1;
|
|
|
|
* FloorLog2(4..7) is 2;
|
|
|
|
* FloorLog2(8..15) is 3; and so on.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2014-06-13 10:34:08 +04:00
|
|
|
inline uint_fast8_t FloorLog2(const T aValue) {
|
|
|
|
return detail::FloorLog2<T>::compute(aValue);
|
2013-07-04 02:46:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** A FloorLog2 variant that accepts only size_t. */
|
2014-06-13 10:34:08 +04:00
|
|
|
inline uint_fast8_t FloorLog2Size(size_t aValue) { return FloorLog2(aValue); }
|
2013-07-04 02:46:51 +04:00
|
|
|
|
|
|
|
/*
|
2013-07-26 07:01:45 +04:00
|
|
|
* Compute the smallest power of 2 greater than or equal to |x|. |x| must not
|
|
|
|
* be so great that the computed value would overflow |size_t|.
|
2013-07-04 02:46:51 +04:00
|
|
|
*/
|
2014-06-13 10:34:08 +04:00
|
|
|
inline size_t RoundUpPow2(size_t aValue) {
|
|
|
|
MOZ_ASSERT(aValue <= (size_t(1) << (sizeof(size_t) * CHAR_BIT - 1)),
|
2013-07-26 07:01:45 +04:00
|
|
|
"can't round up -- will overflow!");
|
2014-06-13 10:34:08 +04:00
|
|
|
return size_t(1) << CeilingLog2(aValue);
|
2013-07-04 02:46:51 +04:00
|
|
|
}
|
|
|
|
|
2013-11-13 04:24:56 +04:00
|
|
|
/**
|
|
|
|
* Rotates the bits of the given value left by the amount of the shift width.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2018-03-02 04:05:58 +03:00
|
|
|
MOZ_NO_SANITIZE_UNSIGNED_OVERFLOW inline T RotateLeft(const T aValue,
|
2014-06-13 10:34:08 +04:00
|
|
|
uint_fast8_t aShift) {
|
2020-03-28 16:57:14 +03:00
|
|
|
static_assert(std::is_unsigned_v<T>, "Rotates require unsigned values");
|
2018-03-02 04:05:58 +03:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
MOZ_ASSERT(aShift < sizeof(T) * CHAR_BIT, "Shift value is too large!");
|
2015-11-04 01:25:48 +03:00
|
|
|
MOZ_ASSERT(aShift > 0,
|
|
|
|
"Rotation by value length is undefined behavior, but compilers "
|
|
|
|
"do not currently fold a test into the rotate instruction. "
|
|
|
|
"Please remove this restriction when compilers optimize the "
|
|
|
|
"zero case (http://blog.regehr.org/archives/1063).");
|
2018-03-02 04:05:58 +03:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
return (aValue << aShift) | (aValue >> (sizeof(T) * CHAR_BIT - aShift));
|
2013-11-13 04:24:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rotates the bits of the given value right by the amount of the shift width.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2018-03-02 04:05:58 +03:00
|
|
|
MOZ_NO_SANITIZE_UNSIGNED_OVERFLOW inline T RotateRight(const T aValue,
|
2014-06-13 10:34:08 +04:00
|
|
|
uint_fast8_t aShift) {
|
2020-03-28 16:57:14 +03:00
|
|
|
static_assert(std::is_unsigned_v<T>, "Rotates require unsigned values");
|
2018-03-02 04:05:58 +03:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
MOZ_ASSERT(aShift < sizeof(T) * CHAR_BIT, "Shift value is too large!");
|
2015-11-04 01:25:48 +03:00
|
|
|
MOZ_ASSERT(aShift > 0,
|
|
|
|
"Rotation by value length is undefined behavior, but compilers "
|
|
|
|
"do not currently fold a test into the rotate instruction. "
|
|
|
|
"Please remove this restriction when compilers optimize the "
|
|
|
|
"zero case (http://blog.regehr.org/archives/1063).");
|
2018-03-02 04:05:58 +03:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
return (aValue >> aShift) | (aValue << (sizeof(T) * CHAR_BIT - aShift));
|
2013-11-13 04:24:56 +04:00
|
|
|
}
|
|
|
|
|
2015-09-04 23:33:10 +03:00
|
|
|
/**
|
|
|
|
* Returns true if |x| is a power of two.
|
|
|
|
* Zero is not an integer power of two. (-Inf is not an integer)
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
constexpr bool IsPowerOfTwo(T x) {
|
2020-03-28 16:57:14 +03:00
|
|
|
static_assert(std::is_unsigned_v<T>, "IsPowerOfTwo requires unsigned values");
|
2015-09-04 23:33:10 +03:00
|
|
|
return x && (x & (x - 1)) == 0;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-09-04 23:33:10 +03:00
|
|
|
template <typename T>
|
|
|
|
inline T Clamp(const T aValue, const T aMin, const T aMax) {
|
2020-03-28 16:57:15 +03:00
|
|
|
static_assert(std::is_integral_v<T>,
|
2015-09-04 23:33:10 +03:00
|
|
|
"Clamp accepts only integral types, so that it doesn't have"
|
|
|
|
" to distinguish differently-signed zeroes (which users may"
|
|
|
|
" or may not care to distinguish, likely at a perf cost) or"
|
|
|
|
" to decide how to clamp NaN or a range with a NaN"
|
|
|
|
" endpoint.");
|
|
|
|
MOZ_ASSERT(aMin <= aMax);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-09-04 23:33:10 +03:00
|
|
|
if (aValue <= aMin) return aMin;
|
|
|
|
if (aValue >= aMax) return aMax;
|
|
|
|
return aValue;
|
|
|
|
}
|
|
|
|
|
2012-08-27 05:58:23 +04:00
|
|
|
} /* namespace mozilla */
|
|
|
|
|
2013-07-24 11:41:39 +04:00
|
|
|
#endif /* mozilla_MathAlgorithms_h */
|