/* -*- 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.
*/
/** Compute ceiling(log2(i)). */ template <size_t I> struct CeilingLog2 { staticconst size_t value = FloorLog2<2 * I - 1>::value;
};
/** Round up to the nearest power of 2. */ template <size_t I> struct RoundUpPow2 { staticconst size_t value = size_t(1) << CeilingLog2<I>::value;
}; template <> struct RoundUpPow2<0> { staticconst size_t value = 1;
};
/** Compute the number of bits in the given unsigned type. */ template <typename T> struct BitSize { staticconst size_t value = sizeof(T) * CHAR_BIT;
};
/** * 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 { // 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. staticconst size_t checkPrecondition =
0 / size_t(N < BitSize<size_t>::value); staticconst size_t value = (size_t(1) << N) - 1 + checkPrecondition;
}; template <> struct NBitMask<BitSize<size_t>::value> { staticconst 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 <size_t N> struct MulOverflowMask { staticconst size_t value =
~NBitMask<BitSize<size_t>::value - CeilingLog2<N>::value>::value;
}; template <> struct MulOverflowMask<0> { /* Error */
}; template <> struct MulOverflowMask<1> { staticconst size_t value = 0;
};
} // namespace tl
} // namespace mozilla
#endif/* mozilla_TemplateLib_h */
¤ Dauer der Verarbeitung: 0.14 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.