Optimization issue?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Optimization issue?

575 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by fancypants on Fri Feb 01 19:40:15 MST 2013
Hi

I'm getting an operational difference between the debug and release build. I haven't been able to track down if it is me or the compiler. I'm hoping someone can shed some light.

I've created "templatizing" macros for things like queue management. The macros create unique functions depending on the datatype needed. For example, if a queue of uint32_t type needs to be managed, the macros create unique functions for that type. It can even be custom struct type.

The debug build accesses the queue elements properly. The release build does not access the queue elements properly.

/* "templatizing macros */
#define DEFINE_QUEUE_FUNCTIONS(elem_type)\
void QueueInit_##elem_type##_(tQueue *pQ, elem_type array[], size_t num_elems) {\
pQ->pData = array;\
pQ->ElementsIn = num_elems - 1;\
pQ->iHead = pQ->iTail = 0;\
}\
elem_type QueueRemoveElement_##elem_type##_(tQueue *pQ) {\
elem_type elem;\
elem = ((elem_type*)(pQ->pData))[pQ->iTail];\
pQ->iTail = (pQ->iTail + 1) % pQ->ElementsIn;\
return(elem);\
}\
void QueueAddElement_##elem_type##_(tQueue *pQ, elem_type elem) {\
((elem_type*)(pQ->pData))[pQ->iHead] = elem;\
pQ->iHead = (pQ->iHead + 1) % pQ->ElementsIn;\
}

/* generic queue macro accessor function */
/* init a queue instance */
#define QueueInit(elem_type, t_queue_ptr, elem_array_ptr, num_elems)\
QueueInit_##elem_type##_(t_queue_ptr, elem_array_ptr, num_elems)
/* remove an element from the queue instance */
#define QueueRemoveElement(elem_type, t_queue_ptr)\
QueueRemoveElement_##elem_type##_(t_queue_ptr)
/* add an element to the queue instance */
#define QueueAddElement(elem_type, t_queue_ptr, elem)\
QueueAddElement_##elem_type##_(t_queue_ptr, elem)

/* queue manager struct */
typedef struct {
void        *pData;
size_tElementsIn;
size_tiHead;
size_tiTail;
} tQueue;

/* custom data type struct */
typedef struct {
uint8_tData;
uint8_tTag;
} tCustomData;

/* creates the unique functions that operate on tCustomData types */
DEFINE_QUEUE_FUNCTIONS(tCustomData);

/* code */
tCustomData _CustomData;
main()
{
tQueue QCustomData;
tCustomData aCustomData[5];
uint32_t i;

QueueInit(tCustomData, &QCustomData, aCustomData, (sizeof(aCustomData)/sizeof(aCustomData[0])));
for(i = 0; i < (sizeof(aCustomData)/sizeof(aCustomData[0])) - 1; i++)
{
_CustomData.Data = i;
_CustomData.Tag = i;
QueueAddElement(tCustomData, &QCustomData, _CustomData);
}
for(i = 0; i < (sizeof(aCustomData)/sizeof(aCustomData[0])) - 1; i++)
{
_CustomData = QueueRemoveElement(tCustomData, &QCustomData); /* <<< the returned data is correct in "debug"; incorrect in "release" */
}
}


I'm assuming the tCustomData type should be size equivalent to uint16_t; If I add support and modify the code for uint16_t, it works as expected in both "debug" and "release":
DEFINE_QUEUE_FUNCTIONS(uint16_t);


I always assume it is my fault, so I'll assume so here too, I just can't find what I'm doing wrong, so I'm hoping someone has some insight.

I'm using Mac v5.0.14 (build 1063).

TIA.
-K
0 Kudos
Reply
1 Reply

565 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by fancypants on Wed Feb 06 23:27:44 MST 2013
Following up on this issue: it seems to be fixed in v5.1.0. I don't know what the root issue was but it appears to be working as expected in this release.
-K
0 Kudos
Reply