Compatibility with C++
Posted: Sat Nov 15, 2014 10:44 pm
The more I come along in my project, the more I realise it would be nice to have some object-orientation. Would SGDK work with C++? Has anyone ever used it with C++ before?
Sega Megadrive/Genesis development
https://gendev.spritesmind.net/forum/
https://gendev.spritesmind.net/forum/viewtopic.php?f=19&t=1928
I am curious as to what you mean by that. Speed or size? Or both? Did you benchmark it, and if so what optimization flags did you use? I am wondering because when I coded a bit for the GBA, I tested some code in C and C++ and didn't find any overhead in execution speed for my code. The libraries also look as if they would compile without much changing.I do not provide C++ support currently as it adds some overhead compared to C.
Well if you use the OO advantages of the C++ language (as inheritance) ans stuff like that, it would necessary add some overhead (vtable to fetch correct methods for instance). GBA already has a more powerful CPU so the overhead might be lighter also it depends a lot from your code, if you code in C with C++ of course it won't make any differences. Sometime also the compiler optimizations are different and you can even get better performance from the G++ compiler but with an older target as the m68k, generally older compilers outperform newer ones.Stef wrote: Building the toolchain with c++ support is also on my todo list.
...
I am curious as to what you mean by that. Speed or size? Or both? Did you benchmark it, and if so what optimization flags did you use? I am wondering because when I coded a bit for the GBA, I tested some code in C and C++ and didn't find any overhead in execution speed for my code. The libraries also look as if they would compile without much changing.
Compiling the toolchain can consume time, you have to take care about defining correct parameters and almost time you experience some issues during the compilation process... I know about the annoying bug, i also got it and it's true it would be nice to get rid of it. Something to know about newer GCC version, they do produce awful code for m68k target so i really recommend you to test them before fixing on a specific version (when i build the initial GCC version for SGDK i used 3 different version and the oldest one produced the best code).Btw: Is building the gnu toolchain a lot of work? I never tried it and I really would like a new version of gcc, since the one provided with SGDK has a nasty bug that randomly stops compiling ("couldn't allocate heap" [...] "died waiting for longjmp") that IIRC only happens on x64 systems.
Mostly size. These days, the gcc C++ compiler is nearly as fast as the C compiler, but the size is a bit of an issue. I'm not sure why exactly, but the optimizer has trouble removing unneeded library code, so even just adding the include for iostream adds ALL of iostream, or about half a megabyte to the size of the rom. If you look at my C++ example, compile it with and without the include for iostream and look at the difference in the output roms.twosixonetwo wrote:I am curious as to what you mean by that. Speed or size? Or both? Did you benchmark it, and if so what optimization flags did you use? I am wondering because when I coded a bit for the GBA, I tested some code in C and C++ and didn't find any overhead in execution speed for my code. The libraries also look as if they would compile without much changing.
It's super-easy in linux. I give step by step instructions in the thread. It's slightly more work in Windows since you need to setup CygWin or MinGW. If you aren't familiar with either, then you have the extra work of learning that before you can build the toolchain.Btw: Is building the gnu toolchain a lot of work? I never tried it and I really would like a new version of gcc, since the one provided with SGDK has a nasty bug that randomly stops compiling ("couldn't allocate heap" [...] "died waiting for longjmp") that IIRC only happens on x64 systems.
The old compiler does produce faster code, but my opinion on that is if you NEED code that is faster than the new compiler can make, you should be using assembly language for that part instead of C in the first place.Stef wrote:Something to know about newer GCC version, they do produce awful code for m68k target so i really recommend you to test them before fixing on a specific version (when i build the initial GCC version for SGDK i used 3 different version and the oldest one produced the best code).
I think you're overplaying it. "Bad code" was what... 10% the last time someone did any tests? Hmm - maybe we should set up some tests to see what the value is at various optimization levels.Stef wrote:You have the point But still imo the latter GCC really produces bad code for m68k and SGDK performance is really impacted by that :-/
In my case it was really more than 10%, a demo running at 16 FPS with GCC 3.4.6 was running at 11 or 12 FPS with GCC 4.4.x... And that is, at comparable optimization level. Anyway, you can easily replace the defaults SGDK compilersChilly Willy wrote:I think you're overplaying it. "Bad code" was what... 10% the last time someone did any tests? Hmm - maybe we should set up some tests to see what the value is at various optimization levels.Stef wrote:You have the point But still imo the latter GCC really produces bad code for m68k and SGDK performance is really impacted by that :-/
So 25%... definitely time for another round of checks. I wonder if they've improved that any... or if it got worse.Stef wrote:In my case it was really more than 10%, a demo running at 16 FPS with GCC 3.4.6 was running at 11 or 12 FPS with GCC 4.4.x... And that is, at comparable optimization level. Anyway, you can easily replace the defaults SGDK compilersChilly Willy wrote:I think you're overplaying it. "Bad code" was what... 10% the last time someone did any tests? Hmm - maybe we should set up some tests to see what the value is at various optimization levels.Stef wrote:You have the point But still imo the latter GCC really produces bad code for m68k and SGDK performance is really impacted by that :-/