question about .. my 'unpacker'.

M

ma740988

I'm trying to unpack a 32 bit word into 3-10 bits samples. So now:

Sample0 corresponds to bits 0-9
Sample1 corresponds to bits 10-19
Sample2 corresponds to bits 20-29
Bits 30 and 31 are don't cares

So my unpacker looks like:

void unpack(
unsigned int *beg,
unsigned int *end,
float* dest)
{
const unsigned int mask=(1<<20)-1;
while (beg!=end)
{
*dest++ = *beg & mask;
*dest++ = *beg >> 20 & ((1<<10)-1);
++beg;
}
}

Usage:
unsigned int val(0xA5A5A5);
float dest[3];
unpack (&val, &val+1, dest);

I'd like a - sort of - generic approach that'll acount for floats and
doubles. I suspect I should also use a vector, nonetheless critiques
and/or a more refined implementation welcome.
 
G

Gianni Mariani

I'd like a - sort of - generic approach that'll acount for floats and
doubles.

Somthing like this ?

template < typename InT, typename OutT, unsigned N >
void unpack(
InT beg,
InT end,
OutT ( & dest )[ N ]
)
{
const unsigned int mask=(1<<20)-1;

unsigned i = 0;

while (beg!=end)
{
dest[ i++ ] = *beg & mask;
if ( i >= N ) throw Overflow;

dest[ i++ ] = *beg >> 20 & ~mask;
if ( i >= N ) throw Overflow;
++beg;
}
}



I suspect I should also use a vector, nonetheless critiques
 
A

Alf P. Steinbach

* (e-mail address removed):
I'm trying to unpack a 32 bit word into 3-10 bits samples. So now:

Sample0 corresponds to bits 0-9
Sample1 corresponds to bits 10-19
Sample2 corresponds to bits 20-29
Bits 30 and 31 are don't cares

So my unpacker looks like:

void unpack(
unsigned int *beg,
unsigned int *end,

'const' for both those arguments.

float* dest)

mysterious choice of result type.

{
const unsigned int mask=(1<<20)-1;
while (beg!=end)
{
*dest++ = *beg & mask;

Here you're copying the lower 20 bits.
 
M

mlimber

I'm trying to unpack a 32 bit word into 3-10 bits samples. So now:

Sample0 corresponds to bits 0-9
Sample1 corresponds to bits 10-19
Sample2 corresponds to bits 20-29
Bits 30 and 31 are don't cares

So my unpacker looks like:

void unpack(
unsigned int *beg,
unsigned int *end,
float* dest)
{
const unsigned int mask=(1<<20)-1;
while (beg!=end)
{
*dest++ = *beg & mask;
*dest++ = *beg >> 20 & ((1<<10)-1);
++beg;

This certainly won't work. For one, it only gives you two values, not
three.
}
}

Usage:
unsigned int val(0xA5A5A5);
float dest[3];
unpack (&val, &val+1, dest);

I'd like a - sort of - generic approach that'll acount for floats and
doubles. I suspect I should also use a vector, nonetheless critiques
and/or a more refined implementation welcome.

#include <vector>
using namespace std;

typedef unsigned int uint;

template <typename DestType>
void unpack( vector<uint>::const_iterator srcBegin,
vector<uint>::const_iterator const srcEnd,
vector<DestType>::iterator dest )
{
static const uint mask = (1<<10)-1;
while( srcBegin++ != srcEnd )
{
*dest++ = DestType( (*srcBegin ) & mask );
*dest++ = DestType( (*srcBegin >> 10) & mask );
*dest++ = DestType( (*srcBegin >> 20) & mask );
}
}

Cheers! --M
 
G

Gianni Mariani

mlimber said:
....


#include <vector>
using namespace std;

typedef unsigned int uint;

template <typename DestType>
void unpack( vector<uint>::const_iterator srcBegin,
vector<uint>::const_iterator const srcEnd,
vector<DestType>::iterator dest )
{
static const uint mask = (1<<10)-1;
while( srcBegin++ != srcEnd )
{
*dest++ = DestType( (*srcBegin ) & mask );
*dest++ = DestType( (*srcBegin >> 10) & mask );
*dest++ = DestType( (*srcBegin >> 20) & mask );
}
}

Good catch on the algorithm issues. On the templateization however,
unless you really want to hold down the parameters to be vector
iterators, you can simply let the compiler figure out the template
argument. If you do this carefully enough, your template code will be
more generic, for example, the code below works for both vectors or
plain arrays (or any combination of them). Hence, the same unpack() code
can be used generically in legacy code as well as new code that uses vector.


template <typename SrcType, typename DestType, typename DestValueType>
void unpack_helper(
SrcType srcBegin,
SrcType srcEnd,
DestType dest,
const DestValueType & /*dest_value unused*/
) {
static const unsigned mask = (1<<10)-1;
unsigned int i = 0;
while( srcBegin++ != srcEnd )
{
dest[ i ++ ] = DestValueType( (*srcBegin ) & mask );
dest[ i ++ ] = DestValueType( (*srcBegin >> 10) & mask );
dest[ i ++ ] = DestValueType( (*srcBegin >> 20) & mask );
}
}

template <typename SrcType1, typename SrcType2, typename DestType>
void unpack( SrcType1 srcBegin,
SrcType2 srcEnd,
DestType dest
) {
unpack_helper< SrcType2, DestType >(
srcBegin, srcEnd, dest, dest[ 0 ]
);
}


void fp()
{
int x[1] = {0xaaaaaa};

float v[3];

unpack( x, x+1, v );

}

#include <vector>
void fv()
{
std::vector<int> x(1);

x[1] = 0xaaaaaa;

std::vector<double> v(3);

unpack( x.begin(), x.end(), v );

}

int main()
{

fp();
fv();
}
 
M

ma740988

Gotta love comp.lang.c++. You guys are good. Thanks all. Nothing
like seeing the implementations, more specifically the different
implementation approaches. A humbling experience since it highlights
how far I have to go. Makes me think I should have been a comp sci
major as opposed to EE-DSP. :)
 
M

mlimber

Gianni said:
Good catch on the algorithm issues. On the templateization however,
unless you really want to hold down the parameters to be vector
iterators, you can simply let the compiler figure out the template
argument. If you do this carefully enough, your template code will be
more generic, for example, the code below works for both vectors or
plain arrays (or any combination of them). Hence, the same unpack() code
can be used generically in legacy code as well as new code that uses vector.
[snip]

Good points. I was assuming that the OP wants to restrict the incoming
data to unsigned int because of the specified hardware packing scheme.
Your scheme does make it more general but also allows the user to
introduce potentially dangerous code. Perhaps a via media can be found
with dumb pointers (allowing more abuse but also providing greater
flexibility than my previous post and allowing less abuse but also
providing less flexibility than your post):

template<typename DestType>
void Unpack( uint const* const begin,
uint const* const end,
DestType * const dest )
{
// Same as above
}

void Foo()
{
const uint n = 0xC0FFEE;
vector<float> dest( 3 );
Unpack( &n, &n+1, &dest[0] );
// ...
}

void Bar()
{
const vector<uint> n;
n.push_back( 0xC0FFEE );
float dest[ 3 ];
Unpack( &n[ 0 ], &n[ n.size() ], dest );
// ...
}

Presumably the OP's actual circumstances will determine what approach
is best.

Cheers! --M
 
G

Gianni Mariani

Gotta love comp.lang.c++. You guys are good. Thanks all. Nothing
like seeing the implementations, more specifically the different
implementation approaches. A humbling experience since it highlights
how far I have to go. Makes me think I should have been a comp sci
major as opposed to EE-DSP. :)

I'm an EE.
 
G

Gianni Mariani

mlimber wrote:
....
Good points. I was assuming that the OP wants to restrict the incoming
data to unsigned int because of the specified hardware packing scheme.
Your scheme does make it more general but also allows the user to
introduce potentially dangerous code. Perhaps a via media can be found
with dumb pointers (allowing more abuse but also providing greater
flexibility than my previous post and allowing less abuse but also
providing less flexibility than your post):

Yep, good points also.
Presumably the OP's actual circumstances will determine what approach
is best.

As always.

G
 
M

mlimber

Gianni said:
I'm an EE.

Me too, also with a specialization in DSP. I'm currently writing code
for dual TI DSPs in an embedded system (TI has a very conformant
compiler, possibly the EDG front end, but no STL; I have my required
parts of STLport working, though).

Reading some good books on C++ and practice can really bridge the gap,
ma740988. Don't give up, and don't change majors!

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,296
Latest member
HeikeHolli

Latest Threads

Top