1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
15 * The Original Code is Mozilla Communicator client code, released
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.
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.
37 * ***** END LICENSE BLOCK ***** */
49 ** A jsbitmap_t is a long integer that can be used for bitmaps
51 typedef jsuword jsbitmap_t
; /* NSPR name, a la Unix system types */
52 typedef jsbitmap_t jsbitmap
; /* JS-style scalar typedef name */
54 #define JS_BITMAP_SIZE(bits) (JS_HOWMANY(bits, JS_BITS_PER_WORD) * \
57 #define JS_TEST_BIT(_map,_bit) ((_map)[(_bit)>>JS_BITS_PER_WORD_LOG2] & \
58 ((jsbitmap)1<<((_bit)&(JS_BITS_PER_WORD-1))))
59 #define JS_SET_BIT(_map,_bit) ((_map)[(_bit)>>JS_BITS_PER_WORD_LOG2] |= \
60 ((jsbitmap)1<<((_bit)&(JS_BITS_PER_WORD-1))))
61 #define JS_CLEAR_BIT(_map,_bit) ((_map)[(_bit)>>JS_BITS_PER_WORD_LOG2] &= \
62 ~((jsbitmap)1<<((_bit)&(JS_BITS_PER_WORD-1))))
65 ** Compute the log of the least power of 2 greater than or equal to n
67 extern JS_PUBLIC_API(JSIntn
) JS_CeilingLog2(JSUint32 i
);
70 ** Compute the log of the greatest power of 2 less than or equal to n
72 extern JS_PUBLIC_API(JSIntn
) JS_FloorLog2(JSUint32 i
);
75 * Replace bit-scanning code sequences with CPU-specific instructions to
76 * speedup calculations of ceiling/floor log2.
78 * With GCC 3.4 or later we can use __builtin_clz for that, see bug 327129.
80 * SWS: Added MSVC intrinsic bitscan support. See bugs 349364 and 356856.
82 #if defined(_WIN32) && (_MSC_VER >= 1300) && (defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64))
84 unsigned char _BitScanForward(unsigned long * Index
, unsigned long Mask
);
85 unsigned char _BitScanReverse(unsigned long * Index
, unsigned long Mask
);
86 # pragma intrinsic(_BitScanForward,_BitScanReverse)
88 __forceinline
static int
89 __BitScanForward32(unsigned int val
)
93 _BitScanForward(&idx
, (unsigned long)val
);
96 __forceinline
static int
97 __BitScanReverse32(unsigned int val
)
101 _BitScanReverse(&idx
, (unsigned long)val
);
102 return (int)(31-idx
);
104 # define js_bitscan_ctz32(val) __BitScanForward32(val)
105 # define js_bitscan_clz32(val) __BitScanReverse32(val)
106 # define JS_HAS_BUILTIN_BITSCAN32
108 #if defined(_M_AMD64) || defined(_M_X64)
109 unsigned char _BitScanForward64(unsigned long * Index
, unsigned __int64 Mask
);
110 unsigned char _BitScanReverse64(unsigned long * Index
, unsigned __int64 Mask
);
111 # pragma intrinsic(_BitScanForward64,_BitScanReverse64)
113 __forceinline
static int
114 __BitScanForward64(unsigned __int64 val
)
118 _BitScanForward64(&idx
, val
);
121 __forceinline
static int
122 __BitScanReverse64(unsigned __int64 val
)
126 _BitScanReverse64(&idx
, val
);
127 return (int)(63-idx
);
129 # define js_bitscan_ctz64(val) __BitScanForward64(val)
130 # define js_bitscan_clz64(val) __BitScanReverse64(val)
131 # define JS_HAS_BUILTIN_BITSCAN64
133 #elif (__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
135 # define js_bitscan_ctz32(val) __builtin_ctz(val)
136 # define js_bitscan_clz32(val) __builtin_clz(val)
137 # define JS_HAS_BUILTIN_BITSCAN32
138 # if (JS_BYTES_PER_WORD == 8)
139 # define js_bitscan_ctz64(val) __builtin_ctzll(val)
140 # define js_bitscan_clz64(val) __builtin_clzll(val)
141 # define JS_HAS_BUILTIN_BITSCAN64
147 ** Macro version of JS_CeilingLog2: Compute the log of the least power of
148 ** 2 greater than or equal to _n. The result is returned in _log2.
150 #ifdef JS_HAS_BUILTIN_BITSCAN32
152 * Use intrinsic function or count-leading-zeros to calculate ceil(log2(_n)).
153 * The macro checks for "n <= 1" and not "n != 0" as js_bitscan_clz32(0) is
156 # define JS_CEILING_LOG2(_log2,_n) \
158 unsigned int j_ = (unsigned int)(_n); \
159 (_log2) = (j_ <= 1 ? 0 : 32 - js_bitscan_clz32(j_ - 1)); \
162 # define JS_CEILING_LOG2(_log2,_n) \
164 JSUint32 j_ = (JSUint32)(_n); \
166 if ((j_) & ((j_)-1)) \
169 (_log2) += 16, (j_) >>= 16; \
171 (_log2) += 8, (j_) >>= 8; \
173 (_log2) += 4, (j_) >>= 4; \
175 (_log2) += 2, (j_) >>= 2; \
182 ** Macro version of JS_FloorLog2: Compute the log of the greatest power of
183 ** 2 less than or equal to _n. The result is returned in _log2.
185 ** This is equivalent to finding the highest set bit in the word.
187 #ifdef JS_HAS_BUILTIN_BITSCAN32
189 * Use js_bitscan_clz32 or count-leading-zeros to calculate floor(log2(_n)).
190 * Since js_bitscan_clz32(0) is undefined, the macro set the loweset bit to 1
191 * to ensure 0 result when _n == 0.
193 # define JS_FLOOR_LOG2(_log2,_n) \
195 (_log2) = 31 - js_bitscan_clz32(((unsigned int)(_n)) | 1); \
198 # define JS_FLOOR_LOG2(_log2,_n) \
200 JSUint32 j_ = (JSUint32)(_n); \
203 (_log2) += 16, (j_) >>= 16; \
205 (_log2) += 8, (j_) >>= 8; \
207 (_log2) += 4, (j_) >>= 4; \
209 (_log2) += 2, (j_) >>= 2; \
217 * Compute the log of the least power of 2 greater than or equal to n. This is
218 * a version of JS_CeilingLog2 that operates on unsigned integers with
219 * CPU-dependant size.
221 #define JS_CEILING_LOG2W(n) ((n) <= 1 ? 0 : 1 + JS_FLOOR_LOG2W((n) - 1))
225 * Compute the log of the greatest power of 2 less than or equal to n.
226 * This is a version of JS_FloorLog2 that operates on unsigned integers with
227 * CPU-dependant size and requires that n != 0.
229 #define JS_FLOOR_LOG2W(n) (JS_ASSERT((n) != 0), js_FloorLog2wImpl(n))
231 #if JS_BYTES_PER_WORD == 4
233 # ifdef JS_HAS_BUILTIN_BITSCAN32
234 # define js_FloorLog2wImpl(n) \
235 ((size_t)(JS_BITS_PER_WORD - 1 - js_bitscan_clz32(n)))
237 # define js_FloorLog2wImpl(n) ((size_t)JS_FloorLog2(n))
240 #elif JS_BYTES_PER_WORD == 8
242 # ifdef JS_HAS_BUILTIN_BITSCAN64
243 # define js_FloorLog2wImpl(n) \
244 ((size_t)(JS_BITS_PER_WORD - 1 - js_bitscan_clz64(n)))
246 extern size_t js_FloorLog2wImpl(size_t n
);
251 # error "NOT SUPPORTED"
258 CountTrailingZeros(size_t n
)
261 #if JS_BYTES_PER_WORD != 4 && JS_BYTES_PER_WORD != 8
262 # error "NOT SUPPORTED"
265 #if JS_BYTES_PER_WORD == 4 && defined JS_HAS_BUILTIN_BITSCAN32
266 return js_bitscan_ctz32(n
);
267 #elif JS_BYTES_PER_WORD == 8 && defined JS_HAS_BUILTIN_BITSCAN64
268 return js_bitscan_ctz64(n
);
271 # if JS_BYTES_PER_WORD == 8
272 if (!(n
& size_t(0xFFFFFFFFU
))) { count
+= 32; n
>>= 32; }
274 if (!(n
& 0xFFFF)) { count
+= 16; n
>>= 16; }
275 if (!(n
& 0xFF)) { count
+= 8; n
>>= 8; }
276 if (!(n
& 0xF)) { count
+= 4; n
>>= 4; }
277 if (!(n
& 0x3)) { count
+= 2; n
>>= 2; }
278 if (!(n
& 0x1)) { count
+= 1; n
>>= 1; }
279 return count
+ 1 - (n
& 0x1);
286 * Macros for rotate left. There is no rotate operation in the C Language so
287 * the construct (a << 4) | (a >> 28) is used instead. Most compilers convert
288 * this to a rotate instruction but some versions of MSVC don't without a
289 * little help. To get MSVC to generate a rotate instruction, we have to use
290 * the _rotl intrinsic and use a pragma to make _rotl inline.
292 * MSVC in VS2005 will do an inline rotate instruction on the above construct.
295 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64) || \
298 #pragma intrinsic(_rotl)
299 #define JS_ROTATE_LEFT32(a, bits) _rotl(a, bits)
301 #define JS_ROTATE_LEFT32(a, bits) (((a) << (bits)) | ((a) >> (32 - (bits))))
305 #endif /* jsbit_h___ */