This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
AW: packed array of bitfileds with gcc
- From: DrÃÃler Tobias <Droessler dot Tobias at ik-elektronik dot com>
- To: "Max S." <king at greatfreeworld dot org>
- Cc: <gcc-help at gcc dot gnu dot org>
- Date: Tue, 29 Oct 2013 08:11:21 +0100
- Subject: AW: packed array of bitfileds with gcc
- Authentication-results: sourceware.org; auth=none
- References: <4E2B590BB57293428A539C9A4B6FCD21026CB0C1 at server1 dot ikhf dot local> <1382984873 dot 9712 dot 9 dot camel at blackbox>
Hi Max and thanks.
Finally i need a buffer with about 1..2kiB size. With the set/get functions it's a way - but itâs a bit ugly i think.
So I ask myself if there is a way, described in my Idea a), compiler and linker have to do the job.
So if anyone knows something about, the problem in my opinion could be named:
" is it possible to define PACKED ARRAYs in c, with gcc ?"
If, it would be a more elegant way .
-----UrsprÃngliche Nachricht-----
Von: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org] Im Auftrag von Max S.
Gesendet: Montag, 28. Oktober 2013 19:28
An: gcc-help@gcc.gnu.org
Betreff: Re: packed array of bitfileds with gcc
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