/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- * vim: set ts=8 sts=4 et sw=4 tw=99: * 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/. */ #ifndef js_template_lib_h__ #define js_template_lib_h__ #include "jstypes.h" /* * Library of reusable template meta-functions (that is, functions on types and * compile-time values). Meta-functions are placed inside the 'tl' namespace to * avoid conflict with non-meta functions that logically have the same name * (e.g., js::tl::Min vs. js::Min). */ namespace js { namespace tl { /* Compute min/max/clamp. */ template struct Min { static const size_t result = i < j ? i : j; }; template struct Max { static const size_t result = i > j ? i : j; }; template struct Clamp { static const size_t result = i < min ? min : (i > max ? max : i); }; /* Compute x^y. */ template struct Pow { static const size_t result = x * Pow::result; }; template struct Pow { static const size_t result = 1; }; /* Compute floor(log2(i)). */ template struct FloorLog2 { static const size_t result = 1 + FloorLog2::result; }; template <> struct FloorLog2<0> { /* Error */ }; template <> struct FloorLog2<1> { static const size_t result = 0; }; /* Compute ceiling(log2(i)). */ template struct CeilingLog2 { static const size_t result = FloorLog2<2 * i - 1>::result; }; /* Round up to the nearest power of 2. */ template struct RoundUpPow2 { static const size_t result = size_t(1) << CeilingLog2::result; }; template <> struct RoundUpPow2<0> { static const size_t result = 1; }; /* Compute the number of bits in the given unsigned type. */ template struct BitSize { static const size_t result = sizeof(T) * JS_BITS_PER_BYTE; }; /* * Produce an N-bit mask, where N <= BitSize::result. Handle the * language-undefined edge case when N = BitSize::result. */ 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 |result| to assure // its computation. static const size_t checkPrecondition = 0 / size_t(N < BitSize::result); static const size_t result = (size_t(1) << N) - 1 + checkPrecondition; }; template <> struct NBitMask::result> { static const size_t result = 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 result = ~NBitMask::result - CeilingLog2::result>::result; }; template <> struct MulOverflowMask<0> { /* Error */ }; template <> struct MulOverflowMask<1> { static const size_t result = 0; }; /* * Generate a mask for T such that if (X & sUnsafeRangeSizeMask), an X-sized * array of T's is big enough to cause a ptrdiff_t overflow when subtracting * a pointer to the end of the array from the beginning. */ template struct UnsafeRangeSizeMask { /* * The '2' factor means the top bit is clear, sizeof(T) converts from * units of elements to bytes. */ static const size_t result = MulOverflowMask<2 * sizeof(T)>::result; }; template struct If { static const T result = v1; }; template struct If { static const T result = v2; }; /* * Traits class for identifying types that are implicitly barriered. */ template struct IsRelocatableHeapType { static const bool result = true; }; } /* namespace tl */ } /* namespace js */ #endif /* js_template_lib_h__ */