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: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2013-07-08 23:42:13 +04:00
|
|
|
* 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/. */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reusable template meta-functions on types and compile-time values. Meta-
|
|
|
|
* functions are placed inside the 'tl' namespace to avoid conflict with non-
|
|
|
|
* meta functions of the same name (e.g., mozilla::tl::FloorLog2 vs.
|
|
|
|
* mozilla::FloorLog2).
|
|
|
|
*
|
|
|
|
* When constexpr support becomes universal, we should probably use that instead
|
|
|
|
* of some of these templates, for simplicity.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef mozilla_TemplateLib_h
|
|
|
|
#define mozilla_TemplateLib_h
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stddef.h>
|
2020-03-28 16:57:18 +03:00
|
|
|
#include <type_traits>
|
2015-05-22 05:33:39 +03:00
|
|
|
|
2013-07-08 23:42:13 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
namespace tl {
|
|
|
|
|
|
|
|
/** Compute min/max. */
|
2017-01-31 02:56:05 +03:00
|
|
|
template <size_t Size, size_t... Rest>
|
2013-07-08 23:42:13 +04:00
|
|
|
struct Min {
|
2017-01-31 02:56:05 +03:00
|
|
|
static constexpr size_t value =
|
|
|
|
Size < Min<Rest...>::value ? Size : Min<Rest...>::value;
|
2013-07-08 23:42:13 +04:00
|
|
|
};
|
2017-01-31 02:56:05 +03:00
|
|
|
|
|
|
|
template <size_t Size>
|
|
|
|
struct Min<Size> {
|
|
|
|
static constexpr size_t value = Size;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <size_t Size, size_t... Rest>
|
2013-07-08 23:42:13 +04:00
|
|
|
struct Max {
|
2017-01-31 02:56:05 +03:00
|
|
|
static constexpr size_t value =
|
|
|
|
Size > Max<Rest...>::value ? Size : Max<Rest...>::value;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <size_t Size>
|
|
|
|
struct Max<Size> {
|
|
|
|
static constexpr size_t value = Size;
|
2013-07-08 23:42:13 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Compute floor(log2(i)). */
|
|
|
|
template <size_t I>
|
|
|
|
struct FloorLog2 {
|
2014-07-11 06:10:17 +04:00
|
|
|
static const size_t value = 1 + FloorLog2<I / 2>::value;
|
2013-07-08 23:42:13 +04:00
|
|
|
};
|
|
|
|
template <>
|
|
|
|
struct FloorLog2<0> { /* Error */
|
|
|
|
};
|
|
|
|
template <>
|
|
|
|
struct FloorLog2<1> {
|
|
|
|
static const size_t value = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Compute ceiling(log2(i)). */
|
|
|
|
template <size_t I>
|
|
|
|
struct CeilingLog2 {
|
2014-07-11 06:10:17 +04:00
|
|
|
static const size_t value = FloorLog2<2 * I - 1>::value;
|
2013-07-08 23:42:13 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Round up to the nearest power of 2. */
|
|
|
|
template <size_t I>
|
|
|
|
struct RoundUpPow2 {
|
2014-07-11 06:10:17 +04:00
|
|
|
static const size_t value = size_t(1) << CeilingLog2<I>::value;
|
2013-07-08 23:42:13 +04:00
|
|
|
};
|
|
|
|
template <>
|
|
|
|
struct RoundUpPow2<0> {
|
2014-07-11 06:10:17 +04:00
|
|
|
static const size_t value = 1;
|
2013-07-08 23:42:13 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Compute the number of bits in the given unsigned type. */
|
|
|
|
template <typename T>
|
|
|
|
struct BitSize {
|
2014-07-11 06:10:17 +04:00
|
|
|
static const size_t value = sizeof(T) * CHAR_BIT;
|
2013-07-08 23:42:13 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Produce an N-bit mask, where N <= BitSize<size_t>::value. Handle the
|
|
|
|
* language-undefined edge case when N = BitSize<size_t>::value.
|
|
|
|
*/
|
|
|
|
template <size_t N>
|
|
|
|
struct NBitMask {
|
2014-07-11 06:10:17 +04:00
|
|
|
// Assert the precondition. On success this evaluates to 0. Otherwise it
|
|
|
|
// triggers divide-by-zero at compile time: a guaranteed compile error in
|
|
|
|
// C++11, and usually one in C++98. Add this value to |value| to assure
|
|
|
|
// its computation.
|
|
|
|
static const size_t checkPrecondition =
|
|
|
|
0 / size_t(N < BitSize<size_t>::value);
|
|
|
|
static const size_t value = (size_t(1) << N) - 1 + checkPrecondition;
|
2013-07-08 23:42:13 +04:00
|
|
|
};
|
|
|
|
template <>
|
|
|
|
struct NBitMask<BitSize<size_t>::value> {
|
2014-07-11 06:10:17 +04:00
|
|
|
static const size_t value = size_t(-1);
|
2013-07-08 23:42:13 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For the unsigned integral type size_t, compute a mask M for N such that
|
|
|
|
* for all X, !(X & M) implies X * N will not overflow (w.r.t size_t)
|
|
|
|
*/
|
|
|
|
template <size_t N>
|
|
|
|
struct MulOverflowMask {
|
2014-07-11 06:10:17 +04:00
|
|
|
static const size_t value =
|
|
|
|
~NBitMask<BitSize<size_t>::value - CeilingLog2<N>::value>::value;
|
2018-11-30 13:46:48 +03:00
|
|
|
};
|
2013-07-08 23:42:13 +04:00
|
|
|
template <>
|
|
|
|
struct MulOverflowMask<0> { /* Error */
|
2018-11-30 13:46:48 +03:00
|
|
|
};
|
2013-07-08 23:42:13 +04:00
|
|
|
template <>
|
|
|
|
struct MulOverflowMask<1> {
|
|
|
|
static const size_t value = 0;
|
|
|
|
};
|
|
|
|
|
2015-05-22 05:33:39 +03:00
|
|
|
/**
|
|
|
|
* And<bool...> computes the logical 'and' of its argument booleans.
|
|
|
|
*
|
|
|
|
* Examples:
|
|
|
|
* mozilla::t1::And<true, true>::value is true.
|
|
|
|
* mozilla::t1::And<true, false>::value is false.
|
|
|
|
* mozilla::t1::And<>::value is true.
|
|
|
|
*/
|
|
|
|
|
2020-03-28 16:57:18 +03:00
|
|
|
template <bool... C>
|
|
|
|
struct And : std::integral_constant<bool, (C && ...)> {};
|
2015-05-22 05:33:39 +03:00
|
|
|
|
2013-07-08 23:42:13 +04:00
|
|
|
} // namespace tl
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2013-07-24 11:41:39 +04:00
|
|
|
#endif /* mozilla_TemplateLib_h */
|