2013-04-17 00:47:10 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: set ts=8 sts=4 et sw=4 tw=99:
|
2012-05-21 15:12:37 +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/. */
|
2009-09-02 05:46:19 +04:00
|
|
|
|
2013-06-20 04:59:09 +04:00
|
|
|
#ifndef js_TemplateLib_h
|
|
|
|
#define js_TemplateLib_h
|
2009-09-02 05:46:19 +04:00
|
|
|
|
2011-12-13 23:26:58 +04:00
|
|
|
#include "jstypes.h"
|
2009-09-02 05:46:19 +04:00
|
|
|
|
2011-10-05 02:33:00 +04:00
|
|
|
/*
|
|
|
|
* 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).
|
|
|
|
*/
|
2010-01-25 19:48:07 +03:00
|
|
|
|
2009-09-02 05:46:19 +04:00
|
|
|
namespace js {
|
|
|
|
namespace tl {
|
|
|
|
|
|
|
|
/* Compute min/max/clamp. */
|
|
|
|
template <size_t i, size_t j> struct Min {
|
|
|
|
static const size_t result = i < j ? i : j;
|
|
|
|
};
|
|
|
|
template <size_t i, size_t j> struct Max {
|
|
|
|
static const size_t result = i > j ? i : j;
|
|
|
|
};
|
|
|
|
template <size_t i, size_t min, size_t max> struct Clamp {
|
|
|
|
static const size_t result = i < min ? min : (i > max ? max : i);
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Compute x^y. */
|
|
|
|
template <size_t x, size_t y> struct Pow {
|
|
|
|
static const size_t result = x * Pow<x, y - 1>::result;
|
|
|
|
};
|
|
|
|
template <size_t x> struct Pow<x,0> {
|
|
|
|
static const size_t result = 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Compute floor(log2(i)). */
|
|
|
|
template <size_t i> struct FloorLog2 {
|
|
|
|
static const size_t result = 1 + FloorLog2<i / 2>::result;
|
|
|
|
};
|
|
|
|
template <> struct FloorLog2<0> { /* Error */ };
|
|
|
|
template <> struct FloorLog2<1> { static const size_t result = 0; };
|
|
|
|
|
|
|
|
/* Compute ceiling(log2(i)). */
|
|
|
|
template <size_t i> struct CeilingLog2 {
|
|
|
|
static const size_t result = FloorLog2<2 * i - 1>::result;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Round up to the nearest power of 2. */
|
|
|
|
template <size_t i> struct RoundUpPow2 {
|
2011-12-07 17:22:47 +04:00
|
|
|
static const size_t result = size_t(1) << CeilingLog2<i>::result;
|
2009-09-02 05:46:19 +04:00
|
|
|
};
|
|
|
|
template <> struct RoundUpPow2<0> {
|
|
|
|
static const size_t result = 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Compute the number of bits in the given unsigned type. */
|
|
|
|
template <class T> struct BitSize {
|
|
|
|
static const size_t result = sizeof(T) * JS_BITS_PER_BYTE;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Produce an N-bit mask, where N <= BitSize<size_t>::result. Handle the
|
|
|
|
* language-undefined edge case when N = BitSize<size_t>::result.
|
|
|
|
*/
|
|
|
|
template <size_t N> struct NBitMask {
|
2013-03-22 05:26:15 +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 |result| to assure
|
|
|
|
// its computation.
|
2013-03-22 06:01:45 +04:00
|
|
|
static const size_t checkPrecondition = 0 / size_t(N < BitSize<size_t>::result);
|
2013-03-22 05:26:15 +04:00
|
|
|
static const size_t result = (size_t(1) << N) - 1 + checkPrecondition;
|
2009-09-02 05:46:19 +04:00
|
|
|
};
|
|
|
|
template <> struct NBitMask<BitSize<size_t>::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 <size_t N> struct MulOverflowMask {
|
|
|
|
static const size_t result =
|
2010-02-16 03:07:57 +03:00
|
|
|
~NBitMask<BitSize<size_t>::result - CeilingLog2<N>::result>::result;
|
2009-09-02 05:46:19 +04:00
|
|
|
};
|
|
|
|
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 <class T> 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;
|
|
|
|
};
|
|
|
|
|
2011-03-23 21:57:27 +03:00
|
|
|
template <bool cond, typename T, T v1, T v2> struct If { static const T result = v1; };
|
|
|
|
template <typename T, T v1, T v2> struct If<false, T, v1, v2> { static const T result = v2; };
|
|
|
|
|
2012-04-12 04:14:11 +04:00
|
|
|
/*
|
|
|
|
* Traits class for identifying types that are implicitly barriered.
|
|
|
|
*/
|
2012-05-16 04:21:23 +04:00
|
|
|
template <class T> struct IsRelocatableHeapType { static const bool result = true; };
|
2012-04-12 04:14:11 +04:00
|
|
|
|
2009-09-02 05:46:19 +04:00
|
|
|
} /* namespace tl */
|
|
|
|
} /* namespace js */
|
|
|
|
|
2013-06-20 04:59:09 +04:00
|
|
|
#endif /* js_TemplateLib_h */
|