For a bit of seasonal festive fun, I thought I'd try making a bitfield
function, i.e. a function returning, for example, the value of bits 1 to
5 of a word as an integer, when the exact bits are known at compile
time.
The data word is always 32 bit unsigned integer (its a data stream from
some embedded controller).
The legacy code I have has lines like:
errNum = (dataWord>>1) & 31; //bits 1 to 5 is an error code
and what I would aim for is something like:
errNum=errBits(word);
the catch is that speed is all important, so I thought maybe templates
could help me to compile in the bitfields. My feeble attempts are below;
they are both about 40 times slower than the existing code (using gcc 4
on a mac).
Can anyone show me a better solution, not using macros (I am considering
using a union of a bitfield struct and an unsigned int, though I might
have to worry about endianness and what happens when I go to a 64 bit
machine) ?
attempt a)
template <int BIT1, int BIT2, typename T>
inline
T getBitField2( const T word){
static const unsigned int bitLen = BIT2 - BIT1 + 1;
static const unsigned int mask = (1<<bitLen) -1;
return (word >BIT1) & mask;
}
used like:
errNum = getBitField2<1,5>(word);
attempt b)
template <int MASKLENGTH>
inline
const int maskit(){
return (1<<MASKLENGTH)-1;
}
template<class NumericalType>
inline
NumericalType getBitField(const NumericalType thisNumber, unsigned int
bitStart, unsigned int bitEnd){
const unsigned int bitLength = bitEnd-bitStart+1;
const NumericalType bitMask = (1<<bitLength) -1;
return (thisNumber>>bitStart) & bitMask;
}
template <unsigned int BIT1, unsigned int BIT2>
class BitField{
private:
const unsigned int mask;
public:
BitField():mask(maskit<BIT2>()){
}
inline int
operator()(const unsigned int word) const{
return (word>BIT1) & mask;
}
};
(b) is used like:
BitField<1,5errBits;
errNum = errBits(word);
surprisingly (for me) both (a) and the function call in using (b) have
approximately the same dismal timing, when I thought that instantiating
the class first (in b) would hardwire the bit numbers, so that the
function call would be much quicker. 6 2524
shaun roe wrote:
For a bit of seasonal festive fun, I thought I'd try making a bitfield
function, i.e. a function returning, for example, the value of bits 1 to
5 of a word as an integer, when the exact bits are known at compile
time.
The data word is always 32 bit unsigned integer (its a data stream from
some embedded controller).
The legacy code I have has lines like:
errNum = (dataWord>>1) & 31; //bits 1 to 5 is an error code
and what I would aim for is something like:
errNum=errBits(word);
the catch is that speed is all important, so I thought maybe templates
could help me to compile in the bitfields. My feeble attempts are below;
they are both about 40 times slower than the existing code (using gcc 4
on a mac).
Can anyone show me a better solution, not using macros (I am considering
using a union of a bitfield struct and an unsigned int, though I might
have to worry about endianness and what happens when I go to a 64 bit
machine) ?
attempt a)
template <int BIT1, int BIT2, typename T>
inline
T getBitField2( const T word){
static const unsigned int bitLen = BIT2 - BIT1 + 1;
static const unsigned int mask = (1<<bitLen) -1;
return (word >BIT1) & mask;
}
used like:
errNum = getBitField2<1,5>(word);
attempt b)
template <int MASKLENGTH>
inline
const int maskit(){
return (1<<MASKLENGTH)-1;
}
template<class NumericalType>
inline
NumericalType getBitField(const NumericalType thisNumber, unsigned int
bitStart, unsigned int bitEnd){
const unsigned int bitLength = bitEnd-bitStart+1;
const NumericalType bitMask = (1<<bitLength) -1;
return (thisNumber>>bitStart) & bitMask;
}
template <unsigned int BIT1, unsigned int BIT2>
class BitField{
private:
const unsigned int mask;
public:
BitField():mask(maskit<BIT2>()){
}
inline int
operator()(const unsigned int word) const{
return (word>BIT1) & mask;
}
};
(b) is used like:
BitField<1,5errBits;
errNum = errBits(word);
surprisingly (for me) both (a) and the function call in using (b) have
approximately the same dismal timing, when I thought that instantiating
the class first (in b) would hardwire the bit numbers, so that the
function call would be much quicker.
In (a) and (b) you compute the bitMask inside a function to be called at
run-time. What about :
template < unsigned int from_, unsigned int to_ >
struct bit_mask {
static unsigned long const from = from_;
static unsigned long const to = to_;
private:
static unsigned long const mask = ( 1 << ( to - from ) ) - 1;
public:
static
unsigned long get ( unsigned long word ) {
return ( ( word >from ) & mask );
}
};
#include <iostream>
int main ( void ) {
std::cout << bit_mask<1,6>::get( 1024 + 16 + 8 + 4 ) << '\n';
}
Best
Kai-Uwe Bux
On 2007-12-27 16:27:34 -0600, jk********@gmx.net said:
>
In (a) and (b) you compute the bitMask inside a function to be called at
run-time. What about:
template < unsigned int from_, unsigned int to_ >
struct bit_mask {
static unsigned long const from = from_;
static unsigned long const to = to_;
private:
static unsigned long const mask = ( 1 << ( to - from ) ) - 1;
What do from and to mean, and what are their ranges? If I read your
code correctly, then from has the range 0..31 and to has the range
1..32. If so this statement will cause a potential overflow when to ==
32 and from == 0.
>
public:
static
unsigned long get ( unsigned long word ) {
return ( ( word >from ) & mask );
}
};
#include <iostream>
int main ( void ) {
std::cout << bit_mask<1,6>::get( 1024 + 16 + 8 + 4 ) << '\n';
}
How bout...
// Constraint: T has to be an integer type.
// Generate a mask containing bitCount_ number of one bits in the LSB position
template <int bitCount_, typename T>
struct Mask
{
static const T value = (T(1) << bitCount_) |
Mask<bitCount_ - 1, T>::value;
};
template <typename T>
struct Mask<0, T>
{
static const T value = 1;
};
// bitCount_ - Number of bits we are interested in
// lsb_ - Position of least significant bit we are interested in
template <int bitCount_, int lsb_, typename T>
inline T bitMask(const T& val)
{
return (val >lsb_) & Mask<bitCount_, T>::value;
}
#include <iostream>
int main()
{
std::cout << bitMask<5, 3>(0x14AF24AB) << std::endl; // "21"
}
-dr
>
How bout...
// Constraint: T has to be an integer type.
// Generate a mask containing bitCount_ number of one bits in the LSB position
template <int bitCount_, typename T>
struct Mask
{
static const T value = (T(1) << bitCount_) |
Mask<bitCount_ - 1, T>::value;
};
template <typename T>
struct Mask<0, T>
{
static const T value = 1;
};
// bitCount_ - Number of bits we are interested in
// lsb_ - Position of least significant bit we are interested in
template <int bitCount_, int lsb_, typename T>
inline T bitMask(const T& val)
{
return (val >lsb_) & Mask<bitCount_, T>::value;
}
#include <iostream>
int main()
{
std::cout << bitMask<5, 3>(0x14AF24AB) << std::endl; // "21"
}
-dr
thanks, this definitely looks promising, and the recursive template is
sexy enough... and yet I find the same timing again as my own attempts;
I'm guessing that theres an 'inline' being ignored or some lazy
instantiation to overcome...
I'll give it another couple of goes to see what happens, maybe trying to
force an early instantiation
On 2007-12-29 16:54, shaun roe wrote:
>> How bout...
// Constraint: T has to be an integer type.
// Generate a mask containing bitCount_ number of one bits in the LSB position template <int bitCount_, typename T> struct Mask { static const T value = (T(1) << bitCount_) | Mask<bitCount_ - 1, T>::value; };
template <typename T> struct Mask<0, T> { static const T value = 1; };
// bitCount_ - Number of bits we are interested in // lsb_ - Position of least significant bit we are interested in template <int bitCount_, int lsb_, typename T> inline T bitMask(const T& val) { return (val >lsb_) & Mask<bitCount_, T>::value; }
#include <iostream> int main() { std::cout << bitMask<5, 3>(0x14AF24AB) << std::endl; // "21" }
-dr
thanks, this definitely looks promising, and the recursive template is
sexy enough... and yet I find the same timing again as my own attempts;
I'm guessing that theres an 'inline' being ignored or some lazy
instantiation to overcome...
I'll give it another couple of goes to see what happens, maybe trying to
force an early instantiation
Instantiation of templates always occurs during compilation and can not
affect run- time performance.
--
Erik Wikström
On 2007-12-29 09:54:35 -0600, shaun roe <sh*******@wanadoo.frsaid:
>>
thanks, this definitely looks promising, and the recursive template is
sexy enough... and yet I find the same timing again as my own attempts;
I'm guessing that theres an 'inline' being ignored or some lazy
instantiation to overcome...
I'll give it another couple of goes to see what happens, maybe trying to
force an early instantiation
Compile with optimizations turned on, then examine the assembler
output. The invocation of bitMask<>() should turn into an inlined
shift-and-mask operation (on almost all microprocessors, it's a single
instruction). Can't get any more efficient than that!
-dr
In article <2007122914441675249-drahardjaplaceat@signherepoboxcom>,
Dave Rahardja <dr****************@sign.here.pobox.comwrote:
On 2007-12-29 09:54:35 -0600, shaun roe <sh*******@wanadoo.frsaid:
>
thanks, this definitely looks promising, and the recursive template is
sexy enough... and yet I find the same timing again as my own attempts;
I'm guessing that theres an 'inline' being ignored or some lazy
instantiation to overcome...
I'll give it another couple of goes to see what happens, maybe trying to
force an early instantiation
Compile with optimizations turned on, then examine the assembler
output. The invocation of bitMask<>() should turn into an inlined
shift-and-mask operation (on almost all microprocessors, it's a single
instruction). Can't get any more efficient than that!
-dr
doh! of course I was compiling in debug mode; optimizing made a LOT of
difference, many thanks! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: tirath |
last post by:
Hi all,
I have a templated class that derives from a non-templated abstract class.
How do I then cast a base class pointer to a <templated> derived class
pointer in a generalised fashion?
...
|
by: Rich |
last post by:
Hi,
I have a query regarding VC6 and its handling of templated copy
constructors. Here goes:
Take a look at the following code sample...
template<class _Ty, size_t t_uiSize = 10 >
class...
|
by: Wanhua Yi |
last post by:
Hi all,
anybody out there with experience in using EMC's Time Finder Software
and DB2 UDB EEE on AIX ? Especially in using BCV for Backup ? Any white
papers ?
|
by: Davide Bruzzone |
last post by:
Greetings all...
I need to create a number of bitfield structs whose contents are
smaller than the size of an int. For example:
typedef struct foo FOO;
struct foo {
unsigned char fieldOne:...
|
by: Ray |
last post by:
When a single-bit bitfield that was formed from an enum
is promoted/cast into an integer, does ANSI C say anything
about whether that integer should be signed or unsigned?
SGI IRIX cc thinks it is...
|
by: Andy Venikov |
last post by:
Sometimes you want to use a bitfield to hold an enum value. In such
cases you would only use as many bits as are needed to encode the
full set of enum values. And it is a pain to recompute and...
|
by: |
last post by:
Hi,
How do we marshall a type like this from a C++/CLI class wrapper to an
unmanaged method?
typedef struct
{
UINT32 blah : 1;
UINT32 blah2 : 1;
UINT32 blah3: 1;
|
by: arne |
last post by:
Hi all,
cleaning up some elderly code, I stumbled across the following:
/**************************************************/
struct {
uint bf:8;
char a1;
char a2;
|
by: domehead100 |
last post by:
I have a templated class, CDerived:
template <typename TValue, typename TDraw, typename TEdit ...>
class CDerived : public CBase
{
TValue m_Value
public:
TValue& GetValue() const
{
|
by: erikbower65 |
last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps:
1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal.
2. Connect to...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
| |