Page 1 of 1

C++ on the MD

Posted: Sun Mar 20, 2011 7:04 pm
by Chilly Willy
I just made a C++ example for the MD using my new toolchain (see the thread in the tools forum). It's a simple Tic-Tac-Toe game, but demonstrates a basic C++ project on the MD. The archive contains a binary as well as the source and an updated linker script for the toolchain.

The game itself is very simple: two players take turns using pad 0 to enter X or O to a spot selected using the DPAD when A is pressed. Press C to exit. After a tie or win, the game restarts.

It's nothing special, it's just a skeleton on which to base your own project.


TicTacToe-MD-Example.7z

Posted: Sun Mar 20, 2011 9:26 pm
by Stef
Excellent :)
Did you tried to compare C++ compiler vs C Compiler ?
I heard severals time that GNU C++ compiler produced better code than C compiler. If this is true i think it could be nice to use C++ compiler instead of C one (just need to use cpp extension on sources files).

Posted: Sun Mar 20, 2011 9:51 pm
by Chilly Willy
I probably should dump main.o to see how the code looks, but I'm willing to bet it's not any better than the corresponding C would be. I'll convert this example into a C one just to be sure. Basically, I want to make a C and C++ version of this for the MD and the 32X to serve as basic skeleton code for the toolchain. Then maybe work in some more complete examples and support for more stuff to make a complete SDK for the MD/32X.

Posted: Mon Mar 21, 2011 9:07 am
by KanedaFr
same question here...
I really would like to use class for object in C++....
it would be a lot easier to pass object as argument :)

Posted: Mon Mar 21, 2011 7:02 pm
by Chilly Willy
I added -fomit-frame-pointers to the C++ makefile to make it more directly comparable to the C code. Here's the dump of the same program as C and C++ compiled at the same level with comparable options:

http://www.mediafire.com/download.php?0eevuz7yeb6e4e1

You'll notice they are virtually identical... just as I figured they would be. While C++ has gotten better, so has the regular C compiler. The main difference people may see between the two is C++ does inlining by default unless you tell it not to, while C only does inlining when you specifically tell it to.

Posted: Tue Mar 22, 2011 7:03 pm
by Stef
Thanks for testing it !
Well, i guess that guys which were reporting that C++ does better optimisation didn't enabled all optimisations on C compiler.
By the way, does -O1 imply the function inlining ? I remember i never saw any inlining in my generated code and i'm using GCC 3.6.x with -O1 optimisation level (-O2 or greater produce worst code).

Posted: Tue Mar 22, 2011 8:17 pm
by Chilly Willy
Stef wrote:Thanks for testing it !
Well, i guess that guys which were reporting that C++ does better optimisation didn't enabled all optimisations on C compiler.
By the way, does -O1 imply the function inlining ? I remember i never saw any inlining in my generated code and i'm using GCC 3.6.x with -O1 optimisation level (-O2 or greater produce worst code).
On certain platforms at least, it does - I noticed that with dumps I did the other day of disk_io.c for the 68000: -O1 used jsr to the functions, while -O2 made them inline. This is on NEW gcc - 4.1.1 did not inline code at -O2.

Posted: Tue Mar 22, 2011 11:06 pm
by Stef
I guess it only inlines small functions... I wonder if there is a specific switch with my GCC version to enable this optimization, it can bring a nice speed boost in some particular cases (we can always do it by hand by the way but registers usage is probably not as good than what compiler can do by itself).

Posted: Tue Mar 22, 2011 11:21 pm
by Chilly Willy
It might be interesting to do some speed tests... if you make a C speed test with your version of gcc, I could try it on my new one and we could see how they really compare. I know people say the newer gcc is much better, but I've never actually seen any published head-to-head comparisons.

Posted: Wed Mar 23, 2011 1:28 am
by antime
Here's the documentation for the current version of GCC. The inlining thresholds has been tweaked back and forth over the years in order to balance performance and space requirements, and there's a couple of command-line parameters you can tweak if you want to.

Posted: Wed Mar 23, 2011 2:24 am
by Chilly Willy
antime wrote:Here's the documentation for the current version of GCC. The inlining thresholds has been tweaked back and forth over the years in order to balance performance and space requirements, and there's a couple of command-line parameters you can tweak if you want to.
That will be handy to folks working on their own projects. I don't know what half of them do. :lol: