Back to index

lightning-sunbird  0.9+nobinonly
jsbit.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code, released
00016  * March 31, 1998.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1998
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #ifndef jsbit_h___
00040 #define jsbit_h___
00041 
00042 #include "jstypes.h"
00043 #include "jsutil.h"
00044 
00045 JS_BEGIN_EXTERN_C
00046 
00047 /*
00048 ** A jsbitmap_t is a long integer that can be used for bitmaps
00049 */
00050 typedef JSUword     jsbitmap_t;     /* NSPR name, a la Unix system types */
00051 typedef jsbitmap_t  jsbitmap;       /* JS-style scalar typedef name */
00052 
00053 #define JS_TEST_BIT(_map,_bit) \
00054     ((_map)[(_bit)>>JS_BITS_PER_WORD_LOG2] & (1L << ((_bit) & (JS_BITS_PER_WORD-1))))
00055 #define JS_SET_BIT(_map,_bit) \
00056     ((_map)[(_bit)>>JS_BITS_PER_WORD_LOG2] |= (1L << ((_bit) & (JS_BITS_PER_WORD-1))))
00057 #define JS_CLEAR_BIT(_map,_bit) \
00058     ((_map)[(_bit)>>JS_BITS_PER_WORD_LOG2] &= ~(1L << ((_bit) & (JS_BITS_PER_WORD-1))))
00059 
00060 /*
00061 ** Compute the log of the least power of 2 greater than or equal to n
00062 */
00063 extern JS_PUBLIC_API(JSIntn) JS_CeilingLog2(JSUint32 i);
00064 
00065 /*
00066 ** Compute the log of the greatest power of 2 less than or equal to n
00067 */
00068 extern JS_PUBLIC_API(JSIntn) JS_FloorLog2(JSUint32 i);
00069 
00070 /*
00071  * Check if __builtin_clz is available which apeared first in GCC 3.4.
00072  * The built-in allows to speedup calculations of ceiling/floor log2,
00073  * see bug 327129.
00074  */
00075 #if __GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
00076 # define JS_HAS_GCC_BUILTIN_CLZ
00077 #endif
00078 
00079 /*
00080 ** Macro version of JS_CeilingLog2: Compute the log of the least power of
00081 ** 2 greater than or equal to _n. The result is returned in _log2.
00082 */
00083 #ifdef JS_HAS_GCC_BUILTIN_CLZ
00084 /*
00085  * Use __builtin_clz or count-leading-zeros to calculate ceil(log2(_n)).
00086  * The macro checks for "n <= 1" and not "n != 0" as __builtin_clz(0) is
00087  * undefined.
00088  */
00089 # define JS_CEILING_LOG2(_log2,_n)                                            \
00090     JS_BEGIN_MACRO                                                            \
00091         JS_STATIC_ASSERT(sizeof(unsigned int) == sizeof(JSUint32));           \
00092         unsigned int j_ = (unsigned int)(_n);                                 \
00093         (_log2) = (j_ <= 1 ? 0 : 32 - __builtin_clz(j_ - 1));                 \
00094     JS_END_MACRO
00095 #else
00096 # define JS_CEILING_LOG2(_log2,_n)                                            \
00097     JS_BEGIN_MACRO                                                            \
00098         JSUint32 j_ = (JSUint32)(_n);                                         \
00099         (_log2) = 0;                                                          \
00100         if ((j_) & ((j_)-1))                                                  \
00101             (_log2) += 1;                                                     \
00102         if ((j_) >> 16)                                                       \
00103             (_log2) += 16, (j_) >>= 16;                                       \
00104         if ((j_) >> 8)                                                        \
00105             (_log2) += 8, (j_) >>= 8;                                         \
00106         if ((j_) >> 4)                                                        \
00107             (_log2) += 4, (j_) >>= 4;                                         \
00108         if ((j_) >> 2)                                                        \
00109             (_log2) += 2, (j_) >>= 2;                                         \
00110         if ((j_) >> 1)                                                        \
00111             (_log2) += 1;                                                     \
00112     JS_END_MACRO
00113 #endif
00114 
00115 /*
00116 ** Macro version of JS_FloorLog2: Compute the log of the greatest power of
00117 ** 2 less than or equal to _n. The result is returned in _log2.
00118 **
00119 ** This is equivalent to finding the highest set bit in the word.
00120 */
00121 #if JS_GCC_HAS_BUILTIN_CLZ
00122 /*
00123  * Use __builtin_clz or count-leading-zeros to calculate floor(log2(_n)).
00124  * Since __builtin_clz(0) is undefined, the macro set the loweset bit to 1
00125  * to ensure 0 result when _n == 0.
00126  */
00127 # define JS_FLOOR_LOG2(_log2,_n)                                              \
00128     JS_BEGIN_MACRO                                                            \
00129         JS_STATIC_ASSERT(sizeof(unsigned int) == sizeof(JSUint32));           \
00130         (_log2) = 31 - __builtin_clz(((unsigned int)(_n)) | 1);               \
00131     JS_END_MACRO
00132 #else
00133 # define JS_FLOOR_LOG2(_log2,_n)                                              \
00134     JS_BEGIN_MACRO                                                            \
00135         JSUint32 j_ = (JSUint32)(_n);                                         \
00136         (_log2) = 0;                                                          \
00137         if ((j_) >> 16)                                                       \
00138             (_log2) += 16, (j_) >>= 16;                                       \
00139         if ((j_) >> 8)                                                        \
00140             (_log2) += 8, (j_) >>= 8;                                         \
00141         if ((j_) >> 4)                                                        \
00142             (_log2) += 4, (j_) >>= 4;                                         \
00143         if ((j_) >> 2)                                                        \
00144             (_log2) += 2, (j_) >>= 2;                                         \
00145         if ((j_) >> 1)                                                        \
00146             (_log2) += 1;                                                     \
00147     JS_END_MACRO
00148 #endif
00149 
00150 /*
00151  * Internal function.
00152  * Compute the log of the least power of 2 greater than or equal to n.
00153  * This is a version of JS_CeilingLog2 that operates on jsuword with
00154  * CPU-dependant size.
00155  */
00156 #define JS_CEILING_LOG2W(n) ((n) <= 1 ? 0 : 1 + JS_FLOOR_LOG2W((n) - 1))
00157 
00158 /*
00159  * Internal function.
00160  * Compute the log of the greatest power of 2 less than or equal to n.
00161  * This is a version of JS_FloorLog2 that operates on jsuword with
00162  * CPU-dependant size and requires that n != 0.
00163  */
00164 #define JS_FLOOR_LOG2W(n) (JS_ASSERT((n) != 0), js_FloorLog2wImpl(n))
00165 
00166 #ifdef JS_HAS_GCC_BUILTIN_CLZ
00167 
00168 # if JS_BYTES_PER_WORD == 4
00169 JS_STATIC_ASSERT(sizeof(unsigned) == sizeof(JSUword));
00170 #  define js_FloorLog2wImpl(n)                                                \
00171     ((JSUword)(JS_BITS_PER_WORD - 1 - __builtin_clz(n)))
00172 # elif JS_BYTES_PER_WORD == 8
00173 JS_STATIC_ASSERT(sizeof(unsigned long long) == sizeof(JSUword));
00174 #  define js_FloorLog2wImpl(n)                                                \
00175     ((JSUword)(JS_BITS_PER_WORD - 1 - __builtin_clzll(n)))
00176 # else
00177 #  error "NOT SUPPORTED"
00178 # endif
00179 
00180 #else
00181 
00182 # if JS_BYTES_PER_WORD == 4
00183 #  define js_FloorLog2wImpl(n) ((JSUword)JS_FloorLog2(n))
00184 # elif JS_BYTES_PER_WORD == 8
00185 extern JSUword
00186 js_FloorLog2wImpl(JSUword n);
00187 # else
00188 #  error "NOT SUPPORTED"
00189 # endif
00190 
00191 #endif
00192 
00193 
00194 JS_END_EXTERN_C
00195 #endif /* jsbit_h___ */