- Main Page
- Related Pages
- Data Structures
- Files

- File List
- Globals

# include/bitops.h

Go to the documentation of this file.

    00001
    00041 #ifndef BITOPS_H_INCLUDED
    00042 #define BITOPS_H_INCLUDED
    00043
    00044 #include <stdbool.h>
    00045 #include <types.h>
    00046 #include <assert.h>
    00047
    00049 #define bit_mask(nr)    (1 << ((nr) & (BITS_PER_LONG - 1)))
    00050
    00054 #define bit_word(nr)    ((nr) / BITS_PER_LONG)
    00055
    00056 #include <arch/bitops.h>
    00057
    00061 static inline void set_bit(unsigned int nr, unsigned long *bitmap)
    00062 {
    00063         bitmap[bit_word(nr)] |= bit_mask(nr);
    00064 }
    00065
    00069 static inline void clear_bit(unsigned int nr, unsigned long *bitmap)
    00070 {
    00071         bitmap[bit_word(nr)] &= ~bit_mask(nr);
    00072 }
    00073
    00077 static inline void toggle_bit(unsigned int nr, unsigned long *bitmap)
    00078 {
    00079         bitmap[bit_word(nr)] ^= bit_mask(nr);
    00080 }
    00081
    00088 static inline bool test_bit(unsigned int nr, unsigned long *bitmap)
    00089 {
    00090         return 1UL & (bitmap[bit_word(nr)] >> (nr & (BITS_PER_LONG - 1)));
    00091 }
    00092
    00093 #ifndef extract_aligned_bitfield
    00094
    00102 static inline uint32_t extract_aligned_bitfield(
    00103                 unsigned int start,
    00104                 unsigned int length,
    00105                 const uint32_t *bitmap)
    00106 {
    00107         uint32_t tmp;
    00108
    00109         tmp = bitmap[bit_word(start)] >> (start & (BITS_PER_LONG - 1));
    00110         return tmp & ((1 << length) - 1);
    00111 }
    00112 #endif
    00113
    00120 static inline uint32_t extract_bitfield(
    00121                 unsigned int start,
    00122                 unsigned int length,
    00123                 const uint32_t *bitmap)
    00124 {
    00125         assert(length <= BITS_PER_LONG);
    00126
    00127         if (bit_word(start) == bit_word(start + length)) {
    00128                 return extract_aligned_bitfield(start, length, bitmap);
    00129         } else {
    00130                 unsigned int length_first;
    00131                 uint32_t bf_first;
    00132                 uint32_t bf_second;
    00133
    00134                 length_first = BITS_PER_LONG - (start & (BITS_PER_LONG - 1));
    00135                 bf_first = extract_aligned_bitfield(start,
    00136                                 length_first, bitmap);
    00137                 bf_second = extract_aligned_bitfield(start + length_first,
    00138                                 length - length_first, bitmap);
    00139
    00140                 return bf_first | (bf_second << length_first);
    00141         }
    00142 }
    00143
    00144 #endif /* BITOPS_H_INCLUDED */