Bit Operations
[Utility Library]

Collaboration diagram for Bit Operations:

Modules

 Atomic Bit Operations

Defines

#define bit_mask(ws, nr)   (1 << ((nr) & ((ws) - 1)))
 Generate a ws-bit mask with only bit nr set.
#define bit_word(ws, nr)   ((nr) / (ws))
 Return the offset of the ws-bit word containing bit nr in a multi-word bitfield.
#define set_bit(nr, bitmap)
 Set bit nr in bitmap.
#define clear_bit(nr, bitmap)
 Clear bit nr in bitmap.
#define toggle_bit(nr, bitmap)
 Toggle bit nr in bitmap.
#define test_bit(nr, bitmap)   priv_test_bit_ws(nr, bitmap, 8 * sizeof(*(bitmap)))
 Test bit nr in bitmap.
#define bit_word_reverse(word)   compiler_brev(word)
 Reverse the order of the bits in word.
#define bit_word_find_first_one_bit(word)   compiler_ctz(word)
 Find the first bit set in word, counting from the LSB.
#define bit_word_find_last_one_bit(word)   (31 - compiler_clz(word))
 Find the last bit set in word, counting from the LSB.
#define bit_word_find_first_zero_bit(word)   bit_word_find_first_one_bit(~word)
 Find the first bit cleared in word, counting from the LSB.
#define bit_word_find_last_zero_bit(word)   bit_word_find_last_one_bit(~word)
 Find the last bit cleared in word, counting from the LSB.

Typedefs

typedef uint8_t bit_word_t
 Optimum type for bit operations.

Functions

static unsigned int bit_array_find_first_one_bit (const bit_word_t *bitmap, unsigned int len)
 Find the first bit set in bitmap, counting from the LSB of each word.

Detailed Description

These bit operations are helper functions for accessing individual bits in a bitmaps. Most operations are defined for arbitrary-length bitmaps, but there may be architecture-specific constraints associated with e.g. the atomic operations.


Define Documentation

#define bit_mask ( ws,
nr   )     (1 << ((nr) & ((ws) - 1)))

Generate a ws-bit mask with only bit nr set.

Parameters:
ws The word size, i.e. number of bits that fits in the mask.
nr The index of the bit that is to be set.
Precondition:
ws must be a power of two

Definition at line 79 of file bitops.h.

#define bit_word ( ws,
nr   )     ((nr) / (ws))

Return the offset of the ws-bit word containing bit nr in a multi-word bitfield.

Parameters:
ws The word size in bits
nr The index of the bit
Precondition:
ws must be a power of two

Definition at line 90 of file bitops.h.

#define bit_word_find_first_one_bit ( word   )     compiler_ctz(word)

Find the first bit set in word, counting from the LSB.

Definition at line 180 of file bitops.h.

Referenced by bit_array_find_first_one_bit().

#define bit_word_find_first_zero_bit ( word   )     bit_word_find_first_one_bit(~word)

Find the first bit cleared in word, counting from the LSB.

Definition at line 190 of file bitops.h.

#define bit_word_find_last_one_bit ( word   )     (31 - compiler_clz(word))

Find the last bit set in word, counting from the LSB.

Definition at line 185 of file bitops.h.

#define bit_word_find_last_zero_bit ( word   )     bit_word_find_last_one_bit(~word)

Find the last bit cleared in word, counting from the LSB.

Definition at line 196 of file bitops.h.

#define bit_word_reverse ( word   )     compiler_brev(word)

Reverse the order of the bits in word.

Definition at line 175 of file bitops.h.

#define clear_bit ( nr,
bitmap   ) 
Value:
do {                                                            \
                unsigned int priv_bit_ws = 8 * sizeof(*(bitmap));       \
                unsigned int priv_bit_nr = (nr);                        \
                (bitmap)[bit_word(priv_bit_ws, priv_bit_nr)]            \
                        &= ~bit_mask(priv_bit_ws, priv_bit_nr);         \
        } while (0)

Clear bit nr in bitmap.

bitmap may be of arbitrary length and type. The caller must ensure that nr is within the bounds of the bitmap.

Definition at line 112 of file bitops.h.

Referenced by atomic_clear_bit(), and atomic_test_and_clear_bit().

#define set_bit ( nr,
bitmap   ) 
Value:
do {                                                            \
                unsigned int priv_bit_ws = 8 * sizeof(*(bitmap));       \
                unsigned int priv_bit_nr = (nr);                        \
                (bitmap)[bit_word(priv_bit_ws, priv_bit_nr)]            \
                        |= bit_mask(priv_bit_ws, priv_bit_nr);          \
        } while (0)

Set bit nr in bitmap.

bitmap may be of arbitrary length and type. The caller must ensure that nr is within the bounds of the bitmap.

Definition at line 98 of file bitops.h.

Referenced by atomic_set_bit(), and atomic_test_and_set_bit().

#define test_bit ( nr,
bitmap   )     priv_test_bit_ws(nr, bitmap, 8 * sizeof(*(bitmap)))

Test bit nr in bitmap.

bitmap may be of arbitrary length and type. The caller must ensure that nr is within the bounds of the bitmap.

Return values:
true if the bit is set
false if the bit is clear

Definition at line 167 of file bitops.h.

Referenced by atomic_test_and_clear_bit(), and atomic_test_and_set_bit().

#define toggle_bit ( nr,
bitmap   ) 
Value:
do {                                                            \
                unsigned int priv_bit_ws = 8 * sizeof(*(bitmap));       \
                unsigned int priv_bit_nr = (nr);                        \
                (bitmap)[bit_word(priv_bit_ws, priv_bit_nr)]            \
                        ^= bit_mask(priv_bit_ws, priv_bit_nr);          \
        } while (0)

Toggle bit nr in bitmap.

bitmap may be of arbitrary length and type. The caller must ensure that nr is within the bounds of the bitmap.

Definition at line 126 of file bitops.h.

Referenced by atomic_toggle_bit().


Typedef Documentation

Optimum type for bit operations.

Declaring a bitmap array using this type will ensure that the bitmap is manipulated using the optimum word size for the processor.

This is the only type which is guaranteed to work with atomic bit operations.

Definition at line 57 of file bitops.h.


Function Documentation

static unsigned int bit_array_find_first_one_bit ( const bit_word_t bitmap,
unsigned int  len 
) [inline, static]

Find the first bit set in bitmap, counting from the LSB of each word.

Note:
Even though len does not need to be a multiple of the size of bit_word_t in bits, the caller must ensure that the space allocated for bitmap covers an integral number of bit_word_t objects which is enough to hold len bits.
Parameters:
bitmap An array of bit words making up a contiguous bitmap
len The length of bitmap in bits
Returns:
The index of the first `1' bit in bitmap counting from the LSB of bitmap[0], or a value >= len if not found.

Definition at line 214 of file bitops.h.

References bit_word_find_first_one_bit, and div_ceil.

Generated on Thu Apr 29 14:09:43 2010 for uart-loopback by  doxygen 1.6.3