📄 bitutilc.h
字号:
/*
* Utility Library: Bit Utility macros
* Copyright (C) ARM Limited 1998-1999. All rights reserved.
*/
#ifndef _BITUTIL_C_
#define _BITUTIL_C_
/**** BITREVERSEWORD ****************************************************************
*
* Version & Date
* ------- ----
* 1.0.0, 30/06/1998
*
* Description
* -----------
* given a word reverse the bits so that bit x becomes bit 31-x in the new word
*
* Inputs
* ------
* WORD
* - the word to reverse bits of
* REVERSED
* - a word to hold the bit reversed version of WORD
* Outputs
* -------
* REVERSED
* - the bit reversed version of WORD
*
* History (with dates)
* ------- ---- -----
* 1.0.0, 30/06/1998 first release
*
************************************************************************************/
#define BITREVERSEWORD( WORD, REVERSED ) { \
BYTEREVERSEWORD( WORD, REVERSED ) ; \
REVERSEUNITS( REVERSED, REVERSED, 0x0F0F0F0F, 4 ) ; \
REVERSEUNITS( REVERSED, REVERSED, 0x33333333, 2 ) ; \
REVERSEUNITS( REVERSED, REVERSED, 0x55555555, 1 ) ; \
}
/**** BYTEREVERSEWORD ***************************************************************
*
* Version & Date
* ------- ----
* 1.0.0, 30/06/1998
*
* Description
* -----------
* given a word (a,b,c,d), reverse the bytes to give the word (d,c,b,a)
*
* Inputs
* ------
* WORD
* - the word to reverse bytes of
* REVERSED
* - a word to hold the byte reversed version of WORD
* Outputs
* -------
* REVERSED
* - the byte reversed version of WORD
*
* History (with dates)
* ------- ---- -----
* 1.0.0, 30/06/1998 first release
*
************************************************************************************/
#define BYTEREVERSEWORD( WORD, REVERSED ) { \
unsigned int rorred ; \
ROR( WORD, 16, rorred ) ; \
rorred = ( WORD ^ rorred ) & ~0xFF0000 ; \
ROR( WORD, 8, REVERSED ) ; \
REVERSED ^= rorred >> 8 ; \
}
/**** ISPOWEROF2 ********************************************************************
*
* Version & Date
* ------- ----
* 1.0.0, 30/06/1998
*
* Description
* -----------
* determines if the given integer number is a power of 2 (zero is not!)
*
* Inputs
* ------
* NUMBER
* - the integer number to test
* IS
* - integer to hold the result of the test
* Outputs
* -------
* IS
* - the result of the test for power of 2
* 0 the given number is not a power of 2 (false)
* 1 the given number is a power of 2 (true)
*
* History (with dates)
* ------- ---- -----
* 1.0.0, 30/06/1998 first release
*
************************************************************************************/
#define ISPOWEROF2( NUMBER, IS ) { \
IS = 0 ; \
if( ( NUMBER != 0 ) && ( NUMBER == ( NUMBER & ( -NUMBER ) ) ) ) { \
IS = 1 ; \
} \
}
/**** MAXBITSET *********************************************************************
*
* Version & Date
* ------- ----
* 1.0.0, 30/06/1998
*
* Description
* -----------
* determine the maximum bit position that is set in the given word
*
* negative values are first made positive then the maximum bit position is found
*
* Inputs
* ------
* WORD
* - a 32-bit integer word to find the maximum bit position set
* MAXBIT
* - integer to hold the maximum bit set from { -1, 0..30 }
* Outputs
* -------
* MAXBIT
* - the maximum bit set in the given word
* 0..30 the maximum bit set
* -1 the given word was zero
*
* Notes
* -----
* the bit range for a word is from bit 0 to bit 30 (not including sign bit)
*
* to determine the number of bits required for data add two (2) to the returned value:
* one (1) for the sign bit and one (1) to shift the range 0..30 to 1..31, the number of
* bits the data actually uses
*
* a zero value (-1 returned) still requires one bit to hold value
*
* History (with dates)
* ------- ---- -----
* 1.0.0, 30/06/1998 first release
*
************************************************************************************/
#define MAXBITSET( WORD, MAXBIT ) { \
unsigned int posWord = ( unsigned int )WORD ; \
unsigned int test ; \
unsigned int newTest ; \
if( WORD == 0 ) { \
MAXBIT = -1 ; \
/* -1 since data requires MAXBIT + 2 bits (1 for sign, 1 to shift 0..30 to 1..31) */ \
} \
else { \
MAXBIT = 0 ; \
if( WORD < 0 ) { \
posWord = ( unsigned int )-WORD ; \
} \
test = posWord >> 16 ; \
if( !test ) { \
test = posWord & 0x0000FFFF ; \
} \
else { \
MAXBIT = 16 ; \
} \
if( ( newTest = test >> 8 ) == 0 ) { \
newTest = test &0x00FF ; \
} \
else { \
MAXBIT += 8 ; \
} \
if( ( test = newTest >> 4 ) == 0 ) { \
test = newTest & 0x0F ; \
} \
else { \
MAXBIT += 4 ; \
} \
if( ( newTest = test >> 2 ) == 0 ) { \
newTest = test & 0x6 ; \
} \
else { \
MAXBIT += 2 ; \
} \
if( ( test = newTest >> 1 ) != 0 ) { \
MAXBIT += 1 ; \
} \
} \
}
/**** POPULATIONCOUNT ***************************************************************
*
* Version & Date
* ------- ----
* 1.0.0, 30/06/1998
*
* Description
* -----------
* determine number of set bits in the binary representation of the given word (including
* the sign bit)
*
* Inputs
* ------
* WORD
* - the word to count the number of bits set
* COUNT
* - an integer to hold the number of bits set in WORD
* Outputs
* -------
* COUNT
* - the result of the count on the number of bits set in WORD
*
* History (with dates)
* ------- ---- -----
* 1.0.0, 30/06/1998 first release
*
************************************************************************************/
#define POPULATIONCOUNT( WORD, COUNT ) { \
unsigned int temp1 ; \
unsigned int temp2 ; \
unsigned int constant1 = 0x49249249 ; \
temp2 = WORD & ( constant1 << 1 ) ; \
temp1 = WORD - ( temp2 >> 1 ) ; \
temp2 = temp1 & ( constant1 >> 1 ) ; \
temp1 += temp2 ; \
temp1 += ( temp1 >> 3 ) ; \
temp1 &= 0xC71C71C7 ; \
temp1 += ( temp1 >> 6 ) ; \
temp1 += ( temp1 >> 12 ) ; \
temp1 += ( temp1 >> 24 ) ; \
COUNT = temp1 & 63 ; \
}
/**** REMOVESIGNEXTEND **************************************************************
*
* Version & Date
* ------- ----
* 1.0.0, 30/06/1998
*
* Description
* -----------
* remove any sign extension from the given data value that is of the given number
* of bits in size after removing sign extension
*
* Inputs
* ------
* DATA
* - the data value to remove sign extend from
* BITS
* - the size of the data in bits (including sign bit) after removing sign extension
* Outputs
* -------
* DATA
* - the data with any sign extension removed
*
* History (with dates)
* ------- ---- -----
* 1.0.0, 30/06/1998 first release
*
************************************************************************************/
#define REMOVESIGNEXTEND( DATA, BITS ) { \
unsigned int mask = 0xFFFFFFFF ; \
if( ( BITS > 0 ) && ( BITS < 32 ) ) { \
DATA = ( int )( ( int )DATA & ( mask >> ( 31 - ( BITS - 1 ) ) ) ) ; \
} \
}
/**** REMOVESIGNEXTENDARRAY *********************************************************
*
* Version & Date
* ------- ----
* 1.0.0, 30/06/1998
*
* Description
* -----------
* remove any sign extension from the given array of data, each entry of the given
* number of bits in size after removing the sign extension
*
* Inputs
* ------
* ARRAY
* - the array of data values to remove any sign extension
* SIZE
* - the number of entries/data points in the array
* BITS
* - the size of each data item in bits (including sign bit) after removing sign extension
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -