(root)/
glib-2.79.0/
girepository/
cmph/
bitbool.h
       1  #ifndef _CMPH_BITBOOL_H__
       2  #define _CMPH_BITBOOL_H__
       3  #include "cmph_types.h" 
       4  
       5  static const cmph_uint8 bitmask[] = { 1, 1 << 1,  1 << 2,  1 << 3, 1 << 4, 1 << 5, 1 << 6, 1 << 7 };
       6  
       7  static const cmph_uint32 bitmask32[] = { 1,       1 << 1,  1 << 2,  1 << 3,  1 << 4,  1 << 5,  1 << 6, 1 << 7,
       8                                           1 << 8,  1 << 9,  1 << 10, 1 << 11, 1 << 12, 1 << 13, 1 << 14, 1 << 15,
       9                                           1 << 16, 1 << 17, 1 << 18, 1 << 19, 1 << 20, 1 << 21, 1 << 22, 1 << 23,
      10                                           1 << 24, 1 << 25, 1 << 26, 1 << 27, 1 << 28, 1 << 29, 1 << 30, 1U << 31
      11  				       };
      12  
      13  static const cmph_uint8 valuemask[] = { 0xfc, 0xf3, 0xcf, 0x3f};
      14  
      15  
      16  /** \def GETBIT(array, i)
      17   *  \brief get the value of an 1-bit integer stored in an array. 
      18   *  \param array to get 1-bit integer values from
      19   *  \param i is the index in array to get the 1-bit integer value from
      20   * 
      21   * GETBIT(array, i) is a macro that gets the value of an 1-bit integer stored in array.
      22   */
      23  #define GETBIT(array, i) ((array[i >> 3] & bitmask[i & 0x00000007]) >> (i & 0x00000007))
      24  
      25  /** \def SETBIT(array, i)
      26   *  \brief set 1 to an 1-bit integer stored in an array. 
      27   *  \param array to store 1-bit integer values
      28   *  \param i is the index in array to set the the bit to 1
      29   * 
      30   * SETBIT(array, i) is a macro that sets 1 to an 1-bit integer stored in an array.
      31   */
      32  #define SETBIT(array, i) (array[i >> 3] |= bitmask[i & 0x00000007])
      33  
      34  /** \def UNSETBIT(array, i)
      35   *  \brief set 0 to an 1-bit integer stored in an array. 
      36   *  \param array to store 1-bit integer values
      37   *  \param i is the index in array to set the the bit to 0
      38   * 
      39   * UNSETBIT(array, i) is a macro that sets 0 to an 1-bit integer stored in an array.
      40   */
      41  #define UNSETBIT(array, i) (array[i >> 3] ^= ((bitmask[i & 0x00000007])))
      42  
      43  //#define GETBIT(array, i) (array[(i) / 8] & bitmask[(i) % 8])
      44  //#define SETBIT(array, i) (array[(i) / 8] |= bitmask[(i) % 8])
      45  //#define UNSETBIT(array, i) (array[(i) / 8] ^= ((bitmask[(i) % 8])))
      46  
      47  
      48  /** \def SETVALUE1(array, i, v)
      49   *  \brief set a value for a 2-bit integer stored in an array initialized with 1s. 
      50   *  \param array to store 2-bit integer values
      51   *  \param i is the index in array to set the value v
      52   *  \param v is the value to be set
      53   * 
      54   * SETVALUE1(array, i, v) is a macro that set a value for a 2-bit integer stored in an array.
      55   * The array should be initialized with all bits set to 1. For example:
      56   * memset(array, 0xff, arraySize);
      57   */
      58  #define SETVALUE1(array, i, v) (array[i >> 2] &= (cmph_uint8)((v << ((i & 0x00000003) << 1)) | valuemask[i & 0x00000003]))
      59  
      60  /** \def SETVALUE0(array, i, v)
      61   *  \brief set a value for a 2-bit integer stored in an array initialized with 0s. 
      62   *  \param array to store 2-bit integer values
      63   *  \param i is the index in array to set the value v
      64   *  \param v is the value to be set
      65   * 
      66   * SETVALUE0(array, i, v) is a macro that set a value for a 2-bit integer stored in an array.
      67   * The array should be initialized with all bits set to 0. For example:
      68   * memset(array, 0, arraySize);
      69   */
      70  #define SETVALUE0(array, i, v) (array[i >> 2] |= (cmph_uint8)(v << ((i & 0x00000003) << 1)))
      71  
      72  
      73  /** \def GETVALUE(array, i)
      74   *  \brief get a value for a 2-bit integer stored in an array. 
      75   *  \param array to get 2-bit integer values from
      76   *  \param i is the index in array to get the value from
      77   * 
      78   * GETVALUE(array, i) is a macro that get a value for a 2-bit integer stored in an array.
      79   */
      80  #define GETVALUE(array, i) ((cmph_uint8)((array[i >> 2] >> ((i & 0x00000003U) << 1U)) & 0x00000003U))
      81  
      82  
      83  
      84  /** \def SETBIT32(array, i)
      85   *  \brief set 1 to an 1-bit integer stored in an array of 32-bit words. 
      86   *  \param array to store 1-bit integer values. The entries are 32-bit words.
      87   *  \param i is the index in array to set the the bit to 1
      88   * 
      89   * SETBIT32(array, i) is a macro that sets 1 to an 1-bit integer stored in an array of 32-bit words.
      90   */
      91  #define SETBIT32(array, i) (array[i >> 5] |= bitmask32[i & 0x0000001f])
      92  
      93  /** \def GETBIT32(array, i)
      94   *  \brief get the value of an 1-bit integer stored in an array of 32-bit words. 
      95   *  \param array to get 1-bit integer values from. The entries are 32-bit words.
      96   *  \param i is the index in array to get the 1-bit integer value from
      97   * 
      98   * GETBIT32(array, i) is a macro that gets the value of an 1-bit integer stored in an array of 32-bit words.
      99   */
     100  #define GETBIT32(array, i) (array[i >> 5] & bitmask32[i & 0x0000001f])
     101  
     102  /** \def UNSETBIT32(array, i)
     103   *  \brief set 0 to an 1-bit integer stored in an array of 32-bit words. 
     104   *  \param array to store 1-bit integer values. The entries ar 32-bit words
     105   *  \param i is the index in array to set the the bit to 0
     106   * 
     107   * UNSETBIT32(array, i) is a macro that sets 0 to an 1-bit integer stored in an array of 32-bit words.
     108   */
     109  #define UNSETBIT32(array, i) (array[i >> 5] ^= ((bitmask32[i & 0x0000001f])))
     110  
     111  #define BITS_TABLE_SIZE(n, bits_length) ((n * bits_length + 31) >> 5)
     112  
     113  static inline void set_bits_value(cmph_uint32 * bits_table, cmph_uint32 index, cmph_uint32 bits_string, 
     114                                    cmph_uint32 string_length, cmph_uint32 string_mask)
     115  {
     116  	register cmph_uint32 bit_idx = index * string_length;
     117  	register cmph_uint32 word_idx = bit_idx >> 5;
     118  	register cmph_uint32 shift1 = bit_idx & 0x0000001f;
     119  	register cmph_uint32 shift2 = 32 - shift1;
     120  	
     121  	bits_table[word_idx] &= ~((string_mask) << shift1);
     122  	bits_table[word_idx] |= bits_string << shift1;
     123  	
     124  	if(shift2 < string_length)
     125  	{
     126  		bits_table[word_idx+1] &= ~((string_mask) >> shift2);
     127  		bits_table[word_idx+1] |= bits_string >> shift2;
     128  	};
     129  };
     130  
     131  static inline cmph_uint32 get_bits_value(cmph_uint32 * bits_table,cmph_uint32 index, cmph_uint32 string_length, cmph_uint32 string_mask)
     132  {
     133  	register cmph_uint32 bit_idx = index * string_length;
     134  	register cmph_uint32 word_idx = bit_idx >> 5;
     135  	register cmph_uint32 shift1 = bit_idx & 0x0000001f;
     136  	register cmph_uint32 shift2 = 32-shift1;
     137  	register cmph_uint32 bits_string;
     138  	
     139  	bits_string = (bits_table[word_idx] >> shift1) & string_mask;
     140  	
     141  	if(shift2 < string_length)
     142  		bits_string |= (bits_table[word_idx+1] << shift2) & string_mask;
     143  
     144  	return bits_string;
     145  };
     146  
     147  static inline void set_bits_at_pos(cmph_uint32 * bits_table, cmph_uint32 pos, cmph_uint32 bits_string, cmph_uint32 string_length)
     148  {
     149  	register cmph_uint32 word_idx = pos >> 5;
     150  	register cmph_uint32 shift1 = pos & 0x0000001f;
     151  	register cmph_uint32 shift2 = 32-shift1;
     152  	register cmph_uint32 string_mask = (1U << string_length) - 1;
     153  	
     154  	bits_table[word_idx] &= ~((string_mask) << shift1);
     155  	bits_table[word_idx] |= bits_string << shift1;
     156  	if(shift2 < string_length)
     157  	{
     158  		bits_table[word_idx+1] &= ~((string_mask) >> shift2);
     159  		bits_table[word_idx+1] |= bits_string >> shift2;
     160  	}
     161  };
     162  
     163  static inline cmph_uint32 get_bits_at_pos(cmph_uint32 * bits_table,cmph_uint32 pos,cmph_uint32 string_length)
     164  {
     165  	register cmph_uint32 word_idx = pos >> 5;
     166  	register cmph_uint32 shift1 = pos & 0x0000001f;
     167  	register cmph_uint32 shift2 = 32 - shift1;
     168  	register cmph_uint32 string_mask = (1U << string_length) - 1;
     169  	register cmph_uint32 bits_string;
     170  	
     171  	bits_string = (bits_table[word_idx] >> shift1) & string_mask;
     172  
     173  	if(shift2 < string_length)
     174  		bits_string |= (bits_table[word_idx+1] << shift2) & string_mask;
     175  	return bits_string;
     176  }
     177  
     178  
     179  #endif