So I am doing some work in C where I have implementation of the same function in both assembly and C and I want to compare the performance of C vs assembly implementation. Now for that I would want to be able to compile and call the function conditionally i.e I would want to create a function which would act as an interface between the caller and the right function that I want to call. somehow I am not sure how to do that.
I was thinking somewhere along the line of following:
//header file containing the C definition and the assembly definition
void getstate(state* m, int* values);
extern void kalmanstate(state* m, int* values);
Then the caller can include the above header file and pass either &getstate or &kalmanstate.
void callTheRightFunction(state* m, int* values, void *fnptr(state*,int*))
{
*fnptr(m,values);
}
However the problem with this is that both getstate and kalmanstate will be compiled which kind of defeats the purpose of my simulation. It does not sound to me the best implementation of the wrapper I want to have. I know conditional execution exists in C but how would I use it to get the right function compile? I mean if I do something like this in the header file:
#ifdef __C__FUNC
void getstate(state* m, int *values);
#endif
#ifdef __kalman
void kalmanstate(state *m, int *values)
#endif
Then in the caller:
include “headerfile.h” //include the above header file
//caller defining _C_FUNC
define __C_FUNC
callTheRightFunction(m,p,&getstate);
But since I include the header file at the beginning when none of them are defined it would probably not include any of them at all and thus will generate runtime errro.
Any suggestions towards right direction would be appreciated. Thanks in advance guys!
Based on your addition to the original question, you were wondering if none of the functions would be compiled. Then you’ll have to define
__C__FUNCor__kalmanbefore you include the header file:But to avoid this problem when you don’t define anything, an approach would be for you to use just one definition, like this, on the SOURCE file:
And on the header file:
(Note the same function name, so you don’t need to modify the code when you’re calling the function)
But this would only work if you’re including the header on the source file where
getstateis being implemented too. (*1)Then if you forgot to define
__GSTATE_USE_C_FUNCTIONbefore your header inclusion, the second function would be used, because it triggered the#else.Now, you’d use it like this on a header file that BOTH source files include (that is, the file that implements the function, and the file that uses it):
Of course you’d have to include this header before including the header which contains the prototype declaration.
And on the source file:
So you’d only have to change the
#defineline on the global header.In addition, if your compiler has an option to do preprocessor defining in the command line for it, then you’d not even need to define
__GSTATE_USE_C_FUNCTIONbefore the#include, you’d just use as a command line option, something like this (for example in bcc32):This would avoid the problem (*1), and you’d not have to make a global header file that both source files have to include.