Optional Two-phase heap tracing
[hiphop-php.git] / hphp / util / bits.h
blobbedd6f9af2067f10b9e3faa3edde59cdddd2e418
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_BITS_H_
18 #define incl_HPHP_BITS_H_
20 namespace HPHP {
21 ///////////////////////////////////////////////////////////////////////////////
23 template <typename T>
24 struct BitInfo {
25 static const T NumBits = sizeof(T) * 8;
26 static const T HighMask = 0x1 << (NumBits - 1);
29 template <unsigned int i, unsigned int m>
30 struct BitCountImpl {
31 enum { value = ((i & m) ? 1 : 0) + BitCountImpl< i, (m >> 1) >::value };
34 template <unsigned int i>
35 struct BitCountImpl<i, 0> {
36 enum { value = 0 };
39 template <unsigned int i>
40 struct BitCount: public BitCountImpl<i,
41 BitInfo<unsigned int>::HighMask>
42 {};
44 template <unsigned int i, bool p, unsigned int m>
45 struct BitPhaseImpl {
46 enum {
47 value = ((i & m) ? (p ? 0 : 1) : (p ? 1 : 0)) +
48 BitPhaseImpl<i, ((i & m) != 0), (m >> 1)>::value
52 template <unsigned int i, bool p>
53 struct BitPhaseImpl<i, p, 0> {
54 enum { value = 0 };
57 template <unsigned int i>
58 struct BitPhase : public BitPhaseImpl<i,
59 i & BitInfo<unsigned int>::HighMask,
60 BitInfo<unsigned int>::HighMask>
61 {};
63 ///////////////////////////////////////////////////////////////////////////////
66 #endif // incl_HPHP_BITS_H_