473,396 Members | 2,082 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

fast templated bitfield finder

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.
Dec 27 '07 #1
6 2557
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
Dec 27 '07 #2
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

Dec 28 '07 #3
>
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
Dec 29 '07 #4
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
Dec 29 '07 #5
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

Dec 29 '07 #6
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!
Dec 30 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
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? ...
1
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...
4
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 ?
9
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:...
4
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...
3
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...
4
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;
7
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;
2
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 {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.