|
|
1.1 root 1: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2: /* ***** BEGIN LICENSE BLOCK *****
3: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4: *
5: * The contents of this file are subject to the Mozilla Public License Version
6: * 1.1 (the "License"); you may not use this file except in compliance with
7: * the License. You may obtain a copy of the License at
8: * http://www.mozilla.org/MPL/
9: *
10: * Software distributed under the License is distributed on an "AS IS" basis,
11: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12: * for the specific language governing rights and limitations under the
13: * License.
14: *
15: * The Original Code is Mozilla Communicator client code, released
16: * March 31, 1998.
17: *
18: * The Initial Developer of the Original Code is
19: * Netscape Communications Corporation.
20: * Portions created by the Initial Developer are Copyright (C) 1998
21: * the Initial Developer. All Rights Reserved.
22: *
23: * Contributor(s):
24: *
25: * Alternatively, the contents of this file may be used under the terms of
26: * either of the GNU General Public License Version 2 or later (the "GPL"),
27: * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28: * in which case the provisions of the GPL or the LGPL are applicable instead
29: * of those above. If you wish to allow use of your version of this file only
30: * under the terms of either the GPL or the LGPL, and not to allow others to
31: * use your version of this file under the terms of the MPL, indicate your
32: * decision by deleting the provisions above and replace them with the notice
33: * and other provisions required by the GPL or the LGPL. If you do not delete
34: * the provisions above, a recipient may use your version of this file under
35: * the terms of any one of the MPL, the GPL or the LGPL.
36: *
37: * ***** END LICENSE BLOCK ***** */
38:
39: #ifndef jsbit_h___
40: #define jsbit_h___
41:
42: #include "jstypes.h"
1.1.1.2 ! root 43: #include "jsutil.h"
! 44:
1.1 root 45: JS_BEGIN_EXTERN_C
46:
47: /*
48: ** A jsbitmap_t is a long integer that can be used for bitmaps
49: */
50: typedef JSUword jsbitmap_t; /* NSPR name, a la Unix system types */
51: typedef jsbitmap_t jsbitmap; /* JS-style scalar typedef name */
52:
53: #define JS_TEST_BIT(_map,_bit) \
54: ((_map)[(_bit)>>JS_BITS_PER_WORD_LOG2] & (1L << ((_bit) & (JS_BITS_PER_WORD-1))))
55: #define JS_SET_BIT(_map,_bit) \
56: ((_map)[(_bit)>>JS_BITS_PER_WORD_LOG2] |= (1L << ((_bit) & (JS_BITS_PER_WORD-1))))
57: #define JS_CLEAR_BIT(_map,_bit) \
58: ((_map)[(_bit)>>JS_BITS_PER_WORD_LOG2] &= ~(1L << ((_bit) & (JS_BITS_PER_WORD-1))))
59:
60: /*
61: ** Compute the log of the least power of 2 greater than or equal to n
62: */
63: extern JS_PUBLIC_API(JSIntn) JS_CeilingLog2(JSUint32 i);
64:
65: /*
66: ** Compute the log of the greatest power of 2 less than or equal to n
67: */
68: extern JS_PUBLIC_API(JSIntn) JS_FloorLog2(JSUint32 i);
69:
70: /*
1.1.1.2 ! root 71: * Check if __builtin_clz is available which apeared first in GCC 3.4.
! 72: * The built-in allows to speedup calculations of ceiling/floor log2,
! 73: * see bug 327129.
! 74: */
! 75: #if __GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
! 76: # define JS_HAS_GCC_BUILTIN_CLZ
! 77: #endif
! 78:
! 79: /*
1.1 root 80: ** Macro version of JS_CeilingLog2: Compute the log of the least power of
81: ** 2 greater than or equal to _n. The result is returned in _log2.
82: */
1.1.1.2 ! root 83: #ifdef JS_HAS_GCC_BUILTIN_CLZ
! 84: /*
! 85: * Use __builtin_clz or count-leading-zeros to calculate ceil(log2(_n)).
! 86: * The macro checks for "n <= 1" and not "n != 0" as __builtin_clz(0) is
! 87: * undefined.
! 88: */
! 89: # define JS_CEILING_LOG2(_log2,_n) \
! 90: JS_BEGIN_MACRO \
! 91: JS_STATIC_ASSERT(sizeof(unsigned int) == sizeof(JSUint32)); \
! 92: unsigned int j_ = (unsigned int)(_n); \
! 93: (_log2) = (j_ <= 1 ? 0 : 32 - __builtin_clz(j_ - 1)); \
! 94: JS_END_MACRO
! 95: #else
! 96: # define JS_CEILING_LOG2(_log2,_n) \
1.1 root 97: JS_BEGIN_MACRO \
98: JSUint32 j_ = (JSUint32)(_n); \
99: (_log2) = 0; \
100: if ((j_) & ((j_)-1)) \
101: (_log2) += 1; \
102: if ((j_) >> 16) \
103: (_log2) += 16, (j_) >>= 16; \
104: if ((j_) >> 8) \
105: (_log2) += 8, (j_) >>= 8; \
106: if ((j_) >> 4) \
107: (_log2) += 4, (j_) >>= 4; \
108: if ((j_) >> 2) \
109: (_log2) += 2, (j_) >>= 2; \
110: if ((j_) >> 1) \
111: (_log2) += 1; \
112: JS_END_MACRO
1.1.1.2 ! root 113: #endif
1.1 root 114:
115: /*
116: ** Macro version of JS_FloorLog2: Compute the log of the greatest power of
117: ** 2 less than or equal to _n. The result is returned in _log2.
118: **
119: ** This is equivalent to finding the highest set bit in the word.
120: */
1.1.1.2 ! root 121: #if JS_GCC_HAS_BUILTIN_CLZ
! 122: /*
! 123: * Use __builtin_clz or count-leading-zeros to calculate floor(log2(_n)).
! 124: * Since __builtin_clz(0) is undefined, the macro set the loweset bit to 1
! 125: * to ensure 0 result when _n == 0.
! 126: */
! 127: # define JS_FLOOR_LOG2(_log2,_n) \
! 128: JS_BEGIN_MACRO \
! 129: JS_STATIC_ASSERT(sizeof(unsigned int) == sizeof(JSUint32)); \
! 130: (_log2) = 31 - __builtin_clz(((unsigned int)(_n)) | 1); \
! 131: JS_END_MACRO
! 132: #else
! 133: # define JS_FLOOR_LOG2(_log2,_n) \
1.1 root 134: JS_BEGIN_MACRO \
135: JSUint32 j_ = (JSUint32)(_n); \
136: (_log2) = 0; \
137: if ((j_) >> 16) \
138: (_log2) += 16, (j_) >>= 16; \
139: if ((j_) >> 8) \
140: (_log2) += 8, (j_) >>= 8; \
141: if ((j_) >> 4) \
142: (_log2) += 4, (j_) >>= 4; \
143: if ((j_) >> 2) \
144: (_log2) += 2, (j_) >>= 2; \
145: if ((j_) >> 1) \
146: (_log2) += 1; \
147: JS_END_MACRO
1.1.1.2 ! root 148: #endif
! 149:
! 150: /*
! 151: * Internal function.
! 152: * Compute the log of the least power of 2 greater than or equal to n.
! 153: * This is a version of JS_CeilingLog2 that operates on jsuword with
! 154: * CPU-dependant size.
! 155: */
! 156: #define JS_CEILING_LOG2W(n) ((n) <= 1 ? 0 : 1 + JS_FLOOR_LOG2W((n) - 1))
! 157:
! 158: /*
! 159: * Internal function.
! 160: * Compute the log of the greatest power of 2 less than or equal to n.
! 161: * This is a version of JS_FloorLog2 that operates on jsuword with
! 162: * CPU-dependant size and requires that n != 0.
! 163: */
! 164: #define JS_FLOOR_LOG2W(n) (JS_ASSERT((n) != 0), js_FloorLog2wImpl(n))
! 165:
! 166: #ifdef JS_HAS_GCC_BUILTIN_CLZ
! 167:
! 168: # if JS_BYTES_PER_WORD == 4
! 169: JS_STATIC_ASSERT(sizeof(unsigned) == sizeof(JSUword));
! 170: # define js_FloorLog2wImpl(n) \
! 171: ((JSUword)(JS_BITS_PER_WORD - 1 - __builtin_clz(n)))
! 172: # elif JS_BYTES_PER_WORD == 8
! 173: JS_STATIC_ASSERT(sizeof(unsigned long long) == sizeof(JSUword));
! 174: # define js_FloorLog2wImpl(n) \
! 175: ((JSUword)(JS_BITS_PER_WORD - 1 - __builtin_clzll(n)))
! 176: # else
! 177: # error "NOT SUPPORTED"
! 178: # endif
! 179:
! 180: #else
! 181:
! 182: # if JS_BYTES_PER_WORD == 4
! 183: # define js_FloorLog2wImpl(n) ((JSUword)JS_FloorLog2(n))
! 184: # elif JS_BYTES_PER_WORD == 8
! 185: extern JSUword
! 186: js_FloorLog2wImpl(JSUword n);
! 187: # else
! 188: # error "NOT SUPPORTED"
! 189: # endif
! 190:
! 191: #endif
! 192:
1.1 root 193:
194: JS_END_EXTERN_C
195: #endif /* jsbit_h___ */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.