AW: packed array of bitfileds with gcc

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxx [mailto:gcc-help-owner@xxxxxxxxxxx] Im Auftrag von Max S.
Gesendet: Montag, 28. Oktober 2013 19:28
An: gcc-help@xxxxxxxxxxx
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









[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux