home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / include / prbit.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  3.0 KB  |  89 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.  
  24. /*
  25. ** A prbitmap_t is a long integer that can be used for bitmaps
  26. */
  27. typedef unsigned long prbitmap_t;
  28.  
  29. #define PR_TEST_BIT(_map,_bit) \
  30.     ((_map)[(_bit)>>PR_BITS_PER_LONG_LOG2] & (1L << ((_bit) & (PR_BITS_PER_LONG-1))))
  31. #define PR_SET_BIT(_map,_bit) \
  32.     ((_map)[(_bit)>>PR_BITS_PER_LONG_LOG2] |= (1L << ((_bit) & (PR_BITS_PER_LONG-1))))
  33. #define PR_CLEAR_BIT(_map,_bit) \
  34.     ((_map)[(_bit)>>PR_BITS_PER_LONG_LOG2] &= ~(1L << ((_bit) & (PR_BITS_PER_LONG-1))))
  35.  
  36. /*
  37. ** Compute the log of the least power of 2 greater than or equal to n
  38. */
  39. PR_EXTERN(PRIntn) PR_CeilingLog2(PRUint32 i); 
  40.  
  41. /*
  42. ** Compute the log of the greatest power of 2 less than or equal to n
  43. */
  44. PR_EXTERN(PRIntn) PR_FloorLog2(PRUint32 i); 
  45.  
  46. /*
  47. ** Macro version of PR_CeilingLog2: Compute the log of the least power of
  48. ** 2 greater than or equal to _n. The result is returned in _log2.
  49. */
  50. #define PR_CEILING_LOG2(_log2,_n)   \
  51.   PR_BEGIN_MACRO                    \
  52.     (_log2) = 0;                    \
  53.     if ((_n) & ((_n)-1))            \
  54.     (_log2) += 1;               \
  55.     if ((_n) >> 16)                 \
  56.     (_log2) += 16, (_n) >>= 16; \
  57.     if ((_n) >> 8)                  \
  58.     (_log2) += 8, (_n) >>= 8;   \
  59.     if ((_n) >> 4)                  \
  60.     (_log2) += 4, (_n) >>= 4;   \
  61.     if ((_n) >> 2)                  \
  62.     (_log2) += 2, (_n) >>= 2;   \
  63.     if ((_n) >> 1)                  \
  64.     (_log2) += 1;               \
  65.   PR_END_MACRO
  66.  
  67. /*
  68. ** Macro version of PR_FloorLog2: Compute the log of the greatest power of
  69. ** 2 less than or equal to _n. The result is returned in _log2.
  70. **
  71. ** This is equivalent to finding the highest set bit in the word.
  72. */
  73. #define PR_FLOOR_LOG2(_log2,_n)   \
  74.   PR_BEGIN_MACRO                    \
  75.     (_log2) = 0;                    \
  76.     if ((_n) >> 16)                 \
  77.     (_log2) += 16, (_n) >>= 16; \
  78.     if ((_n) >> 8)                  \
  79.     (_log2) += 8, (_n) >>= 8;   \
  80.     if ((_n) >> 4)                  \
  81.     (_log2) += 4, (_n) >>= 4;   \
  82.     if ((_n) >> 2)                  \
  83.     (_log2) += 2, (_n) >>= 2;   \
  84.     if ((_n) >> 1)                  \
  85.     (_log2) += 1;               \
  86.   PR_END_MACRO
  87.  
  88. #endif /* prbit_h___ */
  89.