Back to index

lightning-sunbird  0.9+nobinonly
prbit.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 the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef prbit_h___
00039 #define prbit_h___
00040 
00041 #include "prtypes.h"
00042 PR_BEGIN_EXTERN_C
00043 
00044 /*
00045 ** A prbitmap_t is a long integer that can be used for bitmaps
00046 */
00047 typedef unsigned long prbitmap_t;
00048 
00049 #define PR_TEST_BIT(_map,_bit) \
00050     ((_map)[(_bit)>>PR_BITS_PER_LONG_LOG2] & (1L << ((_bit) & (PR_BITS_PER_LONG-1))))
00051 #define PR_SET_BIT(_map,_bit) \
00052     ((_map)[(_bit)>>PR_BITS_PER_LONG_LOG2] |= (1L << ((_bit) & (PR_BITS_PER_LONG-1))))
00053 #define PR_CLEAR_BIT(_map,_bit) \
00054     ((_map)[(_bit)>>PR_BITS_PER_LONG_LOG2] &= ~(1L << ((_bit) & (PR_BITS_PER_LONG-1))))
00055 
00056 /*
00057 ** Compute the log of the least power of 2 greater than or equal to n
00058 */
00059 NSPR_API(PRIntn) PR_CeilingLog2(PRUint32 i); 
00060 
00061 /*
00062 ** Compute the log of the greatest power of 2 less than or equal to n
00063 */
00064 NSPR_API(PRIntn) PR_FloorLog2(PRUint32 i); 
00065 
00066 /*
00067 ** Macro version of PR_CeilingLog2: Compute the log of the least power of
00068 ** 2 greater than or equal to _n. The result is returned in _log2.
00069 */
00070 #define PR_CEILING_LOG2(_log2,_n)   \
00071   PR_BEGIN_MACRO                    \
00072     PRUint32 j_ = (PRUint32)(_n);  \
00073     (_log2) = 0;                    \
00074     if ((j_) & ((j_)-1))            \
00075        (_log2) += 1;               \
00076     if ((j_) >> 16)                 \
00077        (_log2) += 16, (j_) >>= 16; \
00078     if ((j_) >> 8)                  \
00079        (_log2) += 8, (j_) >>= 8;   \
00080     if ((j_) >> 4)                  \
00081        (_log2) += 4, (j_) >>= 4;   \
00082     if ((j_) >> 2)                  \
00083        (_log2) += 2, (j_) >>= 2;   \
00084     if ((j_) >> 1)                  \
00085        (_log2) += 1;               \
00086   PR_END_MACRO
00087 
00088 /*
00089 ** Macro version of PR_FloorLog2: Compute the log of the greatest power of
00090 ** 2 less than or equal to _n. The result is returned in _log2.
00091 **
00092 ** This is equivalent to finding the highest set bit in the word.
00093 */
00094 #define PR_FLOOR_LOG2(_log2,_n)   \
00095   PR_BEGIN_MACRO                    \
00096     PRUint32 j_ = (PRUint32)(_n);  \
00097     (_log2) = 0;                    \
00098     if ((j_) >> 16)                 \
00099        (_log2) += 16, (j_) >>= 16; \
00100     if ((j_) >> 8)                  \
00101        (_log2) += 8, (j_) >>= 8;   \
00102     if ((j_) >> 4)                  \
00103        (_log2) += 4, (j_) >>= 4;   \
00104     if ((j_) >> 2)                  \
00105        (_log2) += 2, (j_) >>= 2;   \
00106     if ((j_) >> 1)                  \
00107        (_log2) += 1;               \
00108   PR_END_MACRO
00109 
00110 PR_END_EXTERN_C
00111 #endif /* prbit_h___ */