home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / nspr30-e.zip / nspr30-e / include / prbit.h < prev    next >
C/C++ Source or Header  |  1998-11-02  |  3KB  |  93 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #ifndef prbit_h___
  20. #define prbit_h___
  21.  
  22. #include "prtypes.h"
  23. PR_BEGIN_EXTERN_C
  24.  
  25. /*
  26. ** A prbitmap_t is a long integer that can be used for bitmaps
  27. */
  28. typedef unsigned long prbitmap_t;
  29.  
  30. #define PR_TEST_BIT(_map,_bit) \
  31.     ((_map)[(_bit)>>PR_BITS_PER_LONG_LOG2] & (1L << ((_bit) & (PR_BITS_PER_LONG-1))))
  32. #define PR_SET_BIT(_map,_bit) \
  33.     ((_map)[(_bit)>>PR_BITS_PER_LONG_LOG2] |= (1L << ((_bit) & (PR_BITS_PER_LONG-1))))
  34. #define PR_CLEAR_BIT(_map,_bit) \
  35.     ((_map)[(_bit)>>PR_BITS_PER_LONG_LOG2] &= ~(1L << ((_bit) & (PR_BITS_PER_LONG-1))))
  36.  
  37. /*
  38. ** Compute the log of the least power of 2 greater than or equal to n
  39. */
  40. PR_EXTERN(PRIntn) PR_CeilingLog2(PRUint32 i); 
  41.  
  42. /*
  43. ** Compute the log of the greatest power of 2 less than or equal to n
  44. */
  45. PR_EXTERN(PRIntn) PR_FloorLog2(PRUint32 i); 
  46.  
  47. /*
  48. ** Macro version of PR_CeilingLog2: Compute the log of the least power of
  49. ** 2 greater than or equal to _n. The result is returned in _log2.
  50. */
  51. #define PR_CEILING_LOG2(_log2,_n)   \
  52.   PR_BEGIN_MACRO                    \
  53.     PRUint32 j_ = (PRUint32)(_n);     \
  54.     (_log2) = 0;                    \
  55.     if ((j_) & ((j_)-1))            \
  56.     (_log2) += 1;               \
  57.     if ((j_) >> 16)                 \
  58.     (_log2) += 16, (j_) >>= 16; \
  59.     if ((j_) >> 8)                  \
  60.     (_log2) += 8, (j_) >>= 8;   \
  61.     if ((j_) >> 4)                  \
  62.     (_log2) += 4, (j_) >>= 4;   \
  63.     if ((j_) >> 2)                  \
  64.     (_log2) += 2, (j_) >>= 2;   \
  65.     if ((j_) >> 1)                  \
  66.     (_log2) += 1;               \
  67.   PR_END_MACRO
  68.  
  69. /*
  70. ** Macro version of PR_FloorLog2: Compute the log of the greatest power of
  71. ** 2 less than or equal to _n. The result is returned in _log2.
  72. **
  73. ** This is equivalent to finding the highest set bit in the word.
  74. */
  75. #define PR_FLOOR_LOG2(_log2,_n)   \
  76.   PR_BEGIN_MACRO                    \
  77.     PRUint32 j_ = (PRUint32)(_n);     \
  78.     (_log2) = 0;                    \
  79.     if ((j_) >> 16)                 \
  80.     (_log2) += 16, (j_) >>= 16; \
  81.     if ((j_) >> 8)                  \
  82.     (_log2) += 8, (j_) >>= 8;   \
  83.     if ((j_) >> 4)                  \
  84.     (_log2) += 4, (j_) >>= 4;   \
  85.     if ((j_) >> 2)                  \
  86.     (_log2) += 2, (j_) >>= 2;   \
  87.     if ((j_) >> 1)                  \
  88.     (_log2) += 1;               \
  89.   PR_END_MACRO
  90.  
  91. PR_END_EXTERN_C
  92. #endif /* prbit_h___ */
  93.