GLAST/LAT > DAQ and FSW > FSW > Doxygen Index > LDT / V0-4-0

Constituent: encdec     Tag: rad750


Interface   Data Structures   File List   Data Fields   Globals  

ffs.h File Reference

Provides the interface and implementation of the Find First Set routines. More...

This graph shows which files directly or indirectly include this file:


Functions

static __inline int FFS (unsigned int word)
 Finds the first set bit (MSB = bit 0) in a 32-bit word.
static __inline
unsigned int 
FFS_eliminate (unsigned int word, int bit)
 Eliminates the specified bit, bit from word.
static __inline
unsigned int 
FFS_mask (int bit)
 Converts the bit to a bit mask.

Detailed Description

Provides the interface and implementation of the Find First Set routines.

Author:
JJRussell - russell@slac.stanford.edu

CVS $Id

ABSTRACT
--------
This facility provides the interface and implementation of the Find First Set routines. These routines provide a machine independent interface to a machine dependent implementation to find the first set bit in a 32 bit word. On most machines, this consists of a single machine instruction

USAGE
-----
Two routines are provided, FFS() and FFS_eliminate(). These routines find and then eliminate the first set bit in a 32 bit word.

Example
-------
As example, consider counting the number of set bits in a 32-bit word.

     int count_bits (unsigned int word)
     {
        int cnt = 0;
        while (word)
        {
            int bit;
            bit  = FFS (word);
            cnt += 1;
            word = FFS_eliminate (word, bit);
        }
        return cnt;
     }

Function Documentation

static __inline int FFS ( unsigned int  word  )  [static]

Finds the first set bit (MSB = bit 0) in a 32-bit word.

Returns:
Returns the bit number of the first set bit
This routine scans a 32 bit longword from left (MSB) to right (LSB) looking for the first bit set. If it finds a bit it returns a value in the range 0-31. The routine is not protected against 0 as an input and the result is undefined.

On the PowerPCs, this is a direct map to the PPC instruction 'cntlz', count leading zeros.

The Intel implementation is an 'bsr' instruction with the bit numbers reversed to match a big endian machine.

The non PPC604 implementation is a straighforward piece of C code.

static __inline unsigned int FFS_eliminate ( unsigned int  word,
int  bit 
) [static]

Eliminates the specified bit, bit from word.

Returns:
The value of the input word with the specified bit eliminated.
Parameters:
word The word to eliminate the bit from
bit The bit (MSB = 0) to eliminate
This is merely a convenience routine. Nothing fancy is going on here, just a straightforward elimination of the specified bit using a mask operation. It is provided to eliminate the inevitable mistake of misspecifying the mask word (0x80000000 >> bit), for instance by omitting one of the trailing 0's. That kind of mistake is almost impossible to spot.

static __inline unsigned int FFS_mask ( int  bit  )  [static]

Converts the bit to a bit mask.

Returns:
A 32 bit value with the specified bit set (MSB = bit 0)
Parameters:
bit The bit (MSB = 0) to set.
Nothing fancy, this routine exists for consistency across the FFS routines.


Generated on Fri Jun 19 01:48:10 2009 by  doxygen 1.5.3