alternative to nested #define statements

Hi -

Though this doesn't work, it's what I'd like to do. The LOAD32 macro accepts two 16-bit values and loads them into a 32-bit integer. The LOAD96 is attempting to do these three at a time (for coding convenience).

I know that the following code doesn't do what I want; I'm showing to demonstrate what I'd like to do:

1
2
#define LOAD32(x,y) (((int16_t) x << 16) | y)
#define LOAD96(x,y,z,t,u,v) (LOAD32(x,y),LOAD32(z,t),LOAD32(u,v)) 


Any ideas for alternatives on this?
Use an inline function? Compilers are pretty good at figuring out when to inline them, so you generally don't really need to use macros for stuff like this.
I thought of that, but I'm initializing the array in its declaration. I don't know how I'd make LOAD96 a function for this purpose. Or, are you suggesting that I make LOAD32 the function?

This is what I tried with LOAD96, and it didn't work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#define LOAD32(x,y) ((x << 16) | y)

uint32_t LOAD96(uint16_t x, 
				uint16_t y, 
				uint16_t z,
				uint16_t t,
				uint16_t u,
				uint16_t v);

uint32_t LOAD96(uint16_t x, 
				uint16_t y, 
				uint16_t z,
				uint16_t t,
				uint16_t u,
				uint16_t v)
{
	LOAD32(x,y);
	LOAD32(z,t);
	LOAD32(u,v);
	return 0;
}
Last edited on
Are you trying to do something like this:

1
2
#define LOAD32(x,y) (((int16_t)(x)<< 16) | (y))
#define LOAD96(x,y,z,t,u,v) {LOAD32(x,y),LOAD32(z,t),LOAD32(u,v)} 
 
int16_t myfoo[ 3 ] = LOAD96(1,2, 3,4, a,b-15);

?

BTW, please take note of the changes I made.

Edit: Also, I think it is cleaner if you just use a little extra typing:

 
int16_t myfoo[ 3 ] = { LOAD32(1,2), LOAD32(3,4), LOAD32(a,b-15) };
Last edited on
That's sort of what I'm hoping for. I'll have an array definition that looks something like this:


1
2
3
4
5
6
	uint32_t	regs[3] = 
	{
		LOAD32(-21,3),
		LOAD32(-2,14),
		LOAD32(10,5)
	};


Except instead of 3 elements, it will have a few hundred, hence my desire for brevity. (One LOAD96 would replace 3 LOAD32s.)

But your point is well-taken; I could just put the LOAD32s three at a time on one line. You suppose that's probably a cleaner way to do it?

And, I will duplicate the changes you made to encapsulate the parameters.

Thanks.
I know it is obnoxious, but clarity should trump brevity.

You could easily write a script to take a table of numbers and generate your array declaration for you.
Agreed. I think I'll just tough it out and manually populate the array, using the LOAD32 macro (at least that helps somewhat).

Thanks, Duoas...I'll consider this (re)solved.
Topic archived. No new replies allowed.