You are not logged in.

#1 2008-06-27 12:33:55

labrat
Member

C++ Compatibility

I've seen the thread "Index » General Help » GRRLIB in C++ project" (http://grrlib.santo.fr/forum/viewtopic.php?id=11) and as a result of this would suggest the following changes to GRRLIB.h / .c

1/ Add C extern declarations around extern and functions in GRRLIB.h, i.e. (this is a IMO a slightly better solution than expecting all C++ users to add an extern "C" delaration around the GRRLIB's #include)

Code:

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

extern Mtx GXmodelView2D;

void GRRLIB_FillScreen(u32 color);

...snip...

void GRRLIB_Render ();

#ifdef __cplusplus
{
#endif /* __cplusplus */

2/ Remove the inline declarations from GRRLIB.h.

I believe (depending on which C/C99 standard is being used) that functions will only be inlined when they are in the same translation unit (Both the calling and called source is visible to the compiler) If this is the case then GRRLIB functions are only being inline when called from other GRRLIB functions (within GRRLIB.c).

Again I am guessing this function inlining is intended to optimise calls to GRRLIB function than call other GRRLIB functions, such as when GRRLIB_FillScreen calls GRRLIB_Rectangle.

If this is correct then you can remove the inline keyword from the header and keep the optimisation benifit by using 'private' inline implementation functions, defined within GRRLIB.c. It's easier to show an example than describe this:

Code:

--GRRLIB.h--

/* Public functions for GRRLIB users - no inline here */
void GRRLIB_FillScreen(u32 color);
void GRRLIB_Rectangle(f32 x, f32 y, f32 width, f32 height, u32 color, u8 filled);

--GRRLIB.c--

/* declare private inline implementations */
inline void GRRLIB_FillScreen_Impl(u32 color);
inline void GRRLIB_Rectangle_Impl(f32 x, f32 y, f32 width, f32 height, u32 color, u8 filled);

/* public function calls inline private function, so should compile to contain code of private function with no additional overhead */
void GRRLIB_FillScreen(u32 color)
{ GRRLIB_FillScreen_Impl(color); }

/* public function calls inline private function, so should compile to contain code of private function with no additional overhead */
void GRRLIB_Rectangle(f32 x, f32 y, f32 width, f32 height, u32 color, u8 filled)
{ GRRLIB_Rectangle_Impl(x,y,width,height,color,filled); }

/* IMPORTANT BIT: private implementation directly calls other private implementation function - so takes advantage of inlining */
inline void GRRLIB_FillScreen_Impl(u32 color){
   GRRLIB_Rectangle_Impl(-40, -40, 680,520, color, 1);
}

inline void GRRLIB_Rectangle_Impl(f32 x, f32 y, f32 width, f32 height, u32 color, u8 filled){
   Vector  v[]={{x,y,0.0f},{x+width,y,0.0f},{x+width,y+height,0.0f},{x,y+height,0.0f},{x,y,0.0f}};
   GXColor col = GRRLIB_Splitu32(color);
   GXColor c[]={col,col,col,col,col};

   if(!filled){
      GRRLIB_NGone(v,c,5);     /* If these where inline, then the private impl would be called here */
   }
   else{
      GRRLIB_NGoneFilled(v,c,4); /* If these where inline, then the private impl would be called here */
   }
}

This is all fairly obvious stuff, but I though writting it down might help anyone maintain the library.

Cheers,

Labrat

Last edited by labrat (2008-06-27 12:39:53)

Offline

 

#2 2008-06-27 15:49:44

RedShade
Member

Re: C++ Compatibility

Inline is sort of left over from when I coded and would have 3 or 4 chains of 1 line functions

Offline

 

#3 2008-08-02 22:12:56

sshock
Member

Re: C++ Compatibility

Another issue.  GRRLIB_Printf should take a "const char*" not "char*" for the text parameter.

Last edited by sshock (2008-08-02 22:13:17)

Offline

 

Board footer

Powered by FluxBB