From 5f8de423f190bbb79a62f804151bc24824fa32d8 Mon Sep 17 00:00:00 2001 From: "Matt A. Tobin" Date: Fri, 2 Feb 2018 04:16:08 -0500 Subject: Add m-esr52 at 52.6.0 --- mfbt/TemplateLib.h | 133 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 mfbt/TemplateLib.h (limited to 'mfbt/TemplateLib.h') diff --git a/mfbt/TemplateLib.h b/mfbt/TemplateLib.h new file mode 100644 index 000000000..6286452d8 --- /dev/null +++ b/mfbt/TemplateLib.h @@ -0,0 +1,133 @@ +/* -*- 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 + * 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 +#include + +#include "mozilla/TypeTraits.h" + +namespace mozilla { + +namespace tl { + +/** Compute min/max. */ +template +struct Min +{ + static const size_t value = I < J ? I : J; +}; +template +struct Max +{ + static const size_t value = I > J ? I : J; +}; + +/** Compute floor(log2(i)). */ +template +struct FloorLog2 +{ + static const size_t value = 1 + FloorLog2::value; +}; +template<> struct FloorLog2<0> { /* Error */ }; +template<> struct FloorLog2<1> { static const size_t value = 0; }; + +/** Compute ceiling(log2(i)). */ +template +struct CeilingLog2 +{ + static const size_t value = FloorLog2<2 * I - 1>::value; +}; + +/** Round up to the nearest power of 2. */ +template +struct RoundUpPow2 +{ + static const size_t value = size_t(1) << CeilingLog2::value; +}; +template<> +struct RoundUpPow2<0> +{ + static const size_t value = 1; +}; + +/** Compute the number of bits in the given unsigned type. */ +template +struct BitSize +{ + static const size_t value = sizeof(T) * CHAR_BIT; +}; + +/** + * Produce an N-bit mask, where N <= BitSize::value. Handle the + * language-undefined edge case when N = BitSize::value. + */ +template +struct NBitMask +{ + // 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::value); + static const size_t value = (size_t(1) << N) - 1 + checkPrecondition; +}; +template<> +struct NBitMask::value> +{ + static const size_t value = size_t(-1); +}; + +/** + * 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 +struct MulOverflowMask +{ + static const size_t value = + ~NBitMask::value - CeilingLog2::value>::value; +}; +template<> struct MulOverflowMask<0> { /* Error */ }; +template<> struct MulOverflowMask<1> { static const size_t value = 0; }; + +/** + * And computes the logical 'and' of its argument booleans. + * + * Examples: + * mozilla::t1::And::value is true. + * mozilla::t1::And::value is false. + * mozilla::t1::And<>::value is true. + */ + +template +struct And; + +template<> +struct And<> : public TrueType { }; + +template +struct And + : public Conditional, FalseType>::Type { }; + +} // namespace tl + +} // namespace mozilla + +#endif /* mozilla_TemplateLib_h */ -- cgit v1.2.3