C++ on the MD

Announce (tech) demos or games releases

Moderator: Mask of Destiny

Post Reply
Chilly Willy
Very interested
Posts: 2984
Joined: Fri Aug 17, 2007 9:33 pm

C++ on the MD

Post by Chilly Willy » Sun Mar 20, 2011 7:04 pm

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

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post by Stef » Sun Mar 20, 2011 9:26 pm

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).

Chilly Willy
Very interested
Posts: 2984
Joined: Fri Aug 17, 2007 9:33 pm

Post by Chilly Willy » Sun Mar 20, 2011 9:51 pm

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.

KanedaFr
Administrateur
Posts: 1139
Joined: Tue Aug 29, 2006 10:56 am
Contact:

Post by KanedaFr » Mon Mar 21, 2011 9:07 am

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 :)

Chilly Willy
Very interested
Posts: 2984
Joined: Fri Aug 17, 2007 9:33 pm

Post by Chilly Willy » Mon Mar 21, 2011 7:02 pm

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.

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post by Stef » Tue Mar 22, 2011 7:03 pm

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).

Chilly Willy
Very interested
Posts: 2984
Joined: Fri Aug 17, 2007 9:33 pm

Post by Chilly Willy » Tue Mar 22, 2011 8:17 pm

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.

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post by Stef » Tue Mar 22, 2011 11:06 pm

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).

Chilly Willy
Very interested
Posts: 2984
Joined: Fri Aug 17, 2007 9:33 pm

Post by Chilly Willy » Tue Mar 22, 2011 11:21 pm

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.

antime
Interested
Posts: 22
Joined: Sun Feb 06, 2011 9:18 pm
Contact:

Post by antime » Wed Mar 23, 2011 1:28 am

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.

Chilly Willy
Very interested
Posts: 2984
Joined: Fri Aug 17, 2007 9:33 pm

Post by Chilly Willy » Wed Mar 23, 2011 2:24 am

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:

Post Reply