1

I wrote a code meant to make some very fast calculations. I assume I know a certain number (because I can safely assume it is <15 and >2. Not a pretty way to implement something, but it allows loop unrolling and makes the code much faster)

(The code was written this way for practical considerations. I know it's not a good way to write a code, but in this case, that's the way it has to be)

Problem is I need to change the number in #define and compile again and again for every value (Using Visual C++ 2010)

I figured macros might be the way to go, but I couldn't find out how to do such a thing. my lame attempt filed:

#define myCustomFunc(number) void myF_number() \
                { printf("%d",number); \
                }

my goal is that something like this:

create_myfunc(2);
create_myfunc(3);
create_myfunc(4);

will expand to:

void myFunc_2(...)
{ ... #pragma unroll
for (int i<0; i<2;i++)
...
}
    void myFunc_3(...)
{ ... #pragma unroll
for (int i<0; i<3;i++)
...
}

etc. and to be able to call these functions from some function by their name, including the constant in it

if (x==2)
    myFunc_2();

But from what I understand, one of the problems with doing such a thing is that such code doesn't expand if it's not inside a function, just gives an error.

4

2 回答 2

3

You can use a macro parameter in the function name.

#define create_myfunc(number) \
void myFunc_##number(...) \
{ \
#pragma unroll \
for (int i = 0; i < number; i++) \
}

Note that I changed i < 0 to i = 0. Was this intended?

于 2013-08-14T10:50:58.557 回答
3

You could store all your function pointers in an array and then call appropriate function with array's index.

Also, if all optimized functions allow such hack you could write your own function in assembler that covers all your cases.

于 2013-08-14T10:51:44.787 回答