gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > C #define macro help

#42617 - LOst? - Fri May 13, 2005 4:19 pm

I have a problem. I need to have a cupple of functions that call other functions and do some math during a loop but looks almost the same. I can't do a loop alone and then a function pointer inside the loop because the loop is critical and needs to be fast. Here is an example of my loop:

Code:

for ( ; count < 224; )
{
 fast_asm_function (); // Always call this in the beginning of the loop

 count++; // Needs to increase the counter before doing the math

 // math goes here
 // This is the place where I want to change my code
 // but I can't let it call a function 224 times
 // because it is too slow
}


Since I'm using C, I tried to do a #define macro but it couldn't call fast_asm_function() from the macro, and it couldn't even do count++.

I know this is hard to understand. After all I can copy the code myself to multiple functions that almost work the same except for the math part, but I am afraid I will forget something after I am up to over 20 copies (brain will overflow) *dies*.

Remember that __inline functions are C++, and I can't use C++. I'm using C all the way for speed.

#42655 - tepples - Fri May 13, 2005 8:11 pm

LOst? wrote:
Remember that __inline functions are C++, and I can't use C++. I'm using C all the way for speed.

C99 has the inline keyword.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#42656 - poslundc - Fri May 13, 2005 8:28 pm

LOst? wrote:
Since I'm using C, I tried to do a #define macro but it couldn't call fast_asm_function() from the macro, and it couldn't even do count++.


Then you probably did the macro incorrectly.

Pretty much all the #define preprocessor statement does is simple text search-and-replace. Copy what you have in the #define statement and paste it in where you actually use it. If you don't get valid C code as a result, then your macro is either being incorrectly written or incorrectly used.

Dan.

#42703 - LOst? - Sat May 14, 2005 3:22 am

poslundc wrote:
LOst? wrote:
Since I'm using C, I tried to do a #define macro but it couldn't call fast_asm_function() from the macro, and it couldn't even do count++.


Then you probably did the macro incorrectly.

Pretty much all the #define preprocessor statement does is simple text search-and-replace. Copy what you have in the #define statement and paste it in where you actually use it. If you don't get valid C code as a result, then your macro is either being incorrectly written or incorrectly used.

Dan.


I get the error "function already defined" when I use the function in my macro. That's the problem. I don't really know how to write a propper macro. Someone want to help me?

#42718 - LOst? - Sat May 14, 2005 5:49 am

I figured it out by a little #define hack:

Code:

#define ACTUAL_FUNCTION fast_asm_function();

#define LOOP
 for ( ; count < 224; ) \
 { \
 ACTUAL_FUNCTION \