packed array of bitfileds with gcc
Max S.
king@greatfreeworld.org
Mon Oct 28 18:15:00 GMT 2013
just some thoughts...
typedef union {
uint8_t B[5];
struct __attribute__ ((__packed__)) {
uint32_t block0: 20;
uint32_t block1: 20;
};
} block2_byte5_t;
uint32_t get(block2_byte5_t array[], int index){
if(index%2){
return array[index/2].block0;
}else{
return array[index/2].block1;
}
}
uint32_t set(block2_byte5_t array[], int index, uint32_t value){
if(index%2){
array[index/2].block0 = value;
}else{
array[index/2].block1 = value;
}
}
Is that what you were looking for?
On Mon, 2013-10-28 at 10:41 +0100, DröÃler Tobias wrote:
> Hi there.
>
> Problem: I get data from an external interface with an organisation/granularity of 20bit-blocks. For buffering and processing I'd like to have an array of 20bit width elements.
>
> Two Ideas:
>
> a) a "packed" array of elements with 20bits width. So in 32Bit word there is a variable boundary to the next array index.
> For a better understand here is a graphic about the memory - placement :
>
> # |<----32bit------>|
> ==============
> ..
> 3 |-B5-|-----B4-----|
> 2 |-----B3-----|-B1-|
> 1 |---B2---|---B1---|
> 0 |-B1-|-----B0-----|
>
> Is this possible in c /gcc ? The Advance will be a generic type and easy handling for processing, instead of idea b. How to get those type defined ?
>
> The following code did not work because the array elements will not be packed, so memory is lost unused:
>
> typedef struct __attribute__ ((__packed__))
> {
> uint32_t bl:20;
> } block_t;
>
> block_t buffer[30];
>
>
>
> b) I define a union about 5 Bytes and two blocks - but handling will be bad, because of the two blocks as an elementary type.
>
> typedef union
> {
> uint8_t B[5];
> struct __attribute__ ((__packed__))
> {
> uint32_t block0: 20;
> uint32_t block1: 20;
> };
> } block2_byte5_t;
>
>
> Machine: 32bit (small) embedded CPU (Freescale Coldfire), processing time low priority / efficient RAM usage high prio.
>
> So I hope there is a wise guy helping me .. Thanks to your attention.
> Tobias
More information about the Gcc-help
mailing list