Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Mesa 3-D graphics library
  3.  *
  4.  * Copyright (C) 2006  Brian Paul   All Rights Reserved.
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a
  7.  * copy of this software and associated documentation files (the "Software"),
  8.  * to deal in the Software without restriction, including without limitation
  9.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10.  * and/or sell copies of the Software, and to permit persons to whom the
  11.  * Software is furnished to do so, subject to the following conditions:
  12.  *
  13.  * The above copyright notice and this permission notice shall be included
  14.  * in all copies or substantial portions of the Software.
  15.  *
  16.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  19.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20.  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21.  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22.  * OTHER DEALINGS IN THE SOFTWARE.
  23.  */
  24.  
  25. /**
  26.  * \file bitset.h
  27.  * \brief Bitset of arbitrary size definitions.
  28.  * \author Michal Krol
  29.  */
  30.  
  31. #ifndef BITSET_H
  32. #define BITSET_H
  33.  
  34. #include "imports.h"
  35.  
  36. /****************************************************************************
  37.  * generic bitset implementation
  38.  */
  39.  
  40. #define BITSET_WORD GLuint
  41. #define BITSET_WORDBITS (sizeof (BITSET_WORD) * 8)
  42.  
  43. /* bitset declarations
  44.  */
  45. #define BITSET_WORDS(bits) (ALIGN(bits, BITSET_WORDBITS) / BITSET_WORDBITS)
  46. #define BITSET_DECLARE(name, bits) BITSET_WORD name[BITSET_WORDS(bits)]
  47.  
  48. /* bitset operations
  49.  */
  50. #define BITSET_COPY(x, y) memcpy( (x), (y), sizeof (x) )
  51. #define BITSET_EQUAL(x, y) (memcmp( (x), (y), sizeof (x) ) == 0)
  52. #define BITSET_ZERO(x) memset( (x), 0, sizeof (x) )
  53. #define BITSET_ONES(x) memset( (x), 0xff, sizeof (x) )
  54.  
  55. #define BITSET_BITWORD(b) ((b) / BITSET_WORDBITS)
  56. #define BITSET_BIT(b) (1 << ((b) % BITSET_WORDBITS))
  57.  
  58. /* single bit operations
  59.  */
  60. #define BITSET_TEST(x, b) ((x)[BITSET_BITWORD(b)] & BITSET_BIT(b))
  61. #define BITSET_SET(x, b) ((x)[BITSET_BITWORD(b)] |= BITSET_BIT(b))
  62. #define BITSET_CLEAR(x, b) ((x)[BITSET_BITWORD(b)] &= ~BITSET_BIT(b))
  63.  
  64. #define BITSET_MASK(b) ((b) == BITSET_WORDBITS ? ~0 : BITSET_BIT(b) - 1)
  65. #define BITSET_RANGE(b, e) (BITSET_MASK((e) + 1) & ~BITSET_MASK(b))
  66.  
  67. /* bit range operations
  68.  */
  69. #define BITSET_TEST_RANGE(x, b, e) \
  70.    (BITSET_BITWORD(b) == BITSET_BITWORD(e) ? \
  71.    ((x)[BITSET_BITWORD(b)] & BITSET_RANGE(b, e)) : \
  72.    (assert (!"BITSET_TEST_RANGE: bit range crosses word boundary"), 0))
  73. #define BITSET_SET_RANGE(x, b, e) \
  74.    (BITSET_BITWORD(b) == BITSET_BITWORD(e) ? \
  75.    ((x)[BITSET_BITWORD(b)] |= BITSET_RANGE(b, e)) : \
  76.    (assert (!"BITSET_SET_RANGE: bit range crosses word boundary"), 0))
  77. #define BITSET_CLEAR_RANGE(x, b, e) \
  78.    (BITSET_BITWORD(b) == BITSET_BITWORD(e) ? \
  79.    ((x)[BITSET_BITWORD(b)] &= ~BITSET_RANGE(b, e)) : \
  80.    (assert (!"BITSET_CLEAR_RANGE: bit range crosses word boundary"), 0))
  81.  
  82. /* Get first bit set in a bitset.
  83.  */
  84. static inline int
  85. __bitset_ffs(const BITSET_WORD *x, int n)
  86. {
  87.    int i;
  88.  
  89.    for (i = 0; i < n; i++) {
  90.       if (x[i])
  91.          return ffs(x[i]) + BITSET_WORDBITS * i;
  92.    }
  93.  
  94.    return 0;
  95. }
  96.  
  97. #define BITSET_FFS(x) __bitset_ffs(x, Elements(x))
  98.  
  99. #endif
  100.