precompiler math problem

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In my header I have the following defines

#define ee_USED_IK_KEY_POINTER			11
#define ee_USED_IK_KEY_TABLE			12
#define ee_USED_IK_KEY_TABLE_SIZE		128
#define ee_USED_IK_KEY_TABLE_END		ee_USED_IK_KEY_TABLE + ee_USED_IK_KEY_TABLE_SIZE
#define ee_USED_GC_KEY_TABLE_SIZE		128
#define ee_USED_GC_KEY_TABLE			ee_USED_IK_KEY_TABLE_END + 1
#define ee_USED_GC_KEY_TABLE_END		ee_USED_GC_KEY_TABLE + (ee_USED_GC_KEY_TABLE_SIZE * 2)

#define ee_DATA_LOG						ee_USED_GC_KEY_TABLE_END + 1
#define ee_DATA_LOG_SIZE				E2END - ee_USED_GC_KEY_TABLE_END

The value for ee_DATA_LOG_SIZE is wrong. At the start of main for debugging I added the following.

/***************************/
volatile uint16_t temp = 0; //volatile to keep from being optimized away
uint16_t Table_End = ee_USED_GC_KEY_TABLE_END;
uint16_t EE_End = E2END;

temp = ee_USED_IK_KEY_TABLE_END;		//140
temp = ee_USED_GC_KEY_TABLE;			//141
temp = ee_USED_GC_KEY_TABLE_END;		//397
temp = ee_DATA_LOG;						//398-all good to here
temp = ee_DATA_LOG_SIZE;				//884-this is wrong
temp = E2END - ee_USED_GC_KEY_TABLE_END;//884

temp = EE_End - Table_End;				//114-this is what it should be
/***************************/

The value of temp is shown in the comments at left. If I assign the values of E2END and ee_USED_GC_KEY_TABLE_END to variable and perform the same operation I get the correct result. Can anyone explain why this is?
Are captchas always on now?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Think about it. You have got two minus signs.

It is always wise to use parentheses round macro expressions. Otherwise they come back and bite you!

Oh. And a neat way of debugging MACROs

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

David, Thanks for the prompt reply. I'm careful about order of precedence and tend to use lots of parentheses but this one taught me a trick about the way the preprocessor handles types.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

eejay wrote:
the way the preprocessor handles types.
Neither does the preprocessor handle types nor does it do any math (with one exception, but that is irrelevant here). It only does text replacements.

temp = ee_DATA_LOG_SIZE;

This line is replaced by

temp = E2END - ee_USED_GC_KEY_TABLE_END;

is replaced by

temp = 0x01FF - ee_USED_GC_KEY_TABLE + (ee_USED_GC_KEY_TABLE_SIZE * 2);

is replaced by

temp = 0x01FF - ee_USED_IK_KEY_TABLE_END + 1 + (128 * 2);

is replaced by

temp = 0x01FF - ee_USED_IK_KEY_TABLE + ee_USED_IK_KEY_TABLE_SIZE + 1 + (128 * 2);

is replaced by

temp = 0x01FF - 12 + 128 + 1 + (128 * 2);

and then the compiler does the math and replaces the right side by 884.

Stefan Ernst