CFLAGS chosen for kos

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
User avatar
bogglez
Moderator
Moderator
Posts: 578
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Sun Apr 20, 2014 9:45 am
Has thanked: 0
Been thanked: 0

CFLAGS chosen for kos

Post by bogglez »

Hey,

I stumbled upon a bug recently that made me take a closer look at the chosen CFLAGS for kos (kos-cc -v).

Configured with: ../gcc-4.7.3/configure --target=sh-elf --prefix=/opt/toolchains/dc/sh-elf --without-headers --with-newlib --enable-languages=c --disable-libssp --disable-tls --with-multilib-list=m4-single-only,m4-nofpu,m4 --with-endian=little --with-cpu=m4-single-only CXX=g++ : (reconfigured) ../gcc-4.7.3/configure --target=sh-elf --prefix=/opt/toolchains/dc/sh-elf --with-newlib --disable-libssp --disable-tls --enable-threads=kos --enable-languages=c,c++ --with-multilib-list=m4-single-only,m4-nofpu,m4 --with-endian=little --with-cpu=m4-single-only CXX=g++
m4-nofpu: Doesn't the DC have an FPU? Doesn't this slow down floating point ops dramatically?
m4-single-only: This causes warnings for printf("%f", 0.f); I read that the SH4 supports 64 bit precision floats, why are they turned off?

I appreciate your insight
Wiki & tutorials: http://dcemulation.org/?title=Development
Wiki feedback: viewtopic.php?f=29&t=103940
My libgl playground (not for production): https://bitbucket.org/bogglez/libgl15
My lxdream fork (with small fixes): https://bitbucket.org/bogglez/lxdream
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5658
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has thanked: 9 times
Been thanked: 69 times
Contact:

Re: CFLAGS chosen for kos

Post by BlueCrab »

Those aren't really the CFLAGS, but rather the configuration options passed to the compiler's configuration script. The multilib list covers the most common 3 options for supporting the SH4 that GCC has: always single-precision (-m4-single-only), no floating-point support (-m4-nofpu), and full FPU support (-m4).

KallistiOS makes the assumption throughout the code that you will leave the FPU in single-precision floating point mode, hence why the default is -m4-single-only. Some of the more useful operations that the FPU can do (like matrix * vector multiplication, and vector dot products) are only available as single-precision operations. Basically, if there's no reason you need to be doing double-precision arithmetic (and usually there's not unless you're doing some sort of scientific calculations), then this is a sane state to stay in. As for the warning on printf("%f", 0.0f), that's because printf("%f", ...) assumes that the argument passed is a double -- cast your argument properly and you won't get that warning. That said, that warning is awfully silly, since the C standard says exactly what should happen in that case. Yes, it does only show up with -m4-single-only, but I blame that on GCC doing something silly. :wink:

-m4-nofpu is passed to the configure script for programs in the past that have used that mode (namely Dan Potter's dcload replacement known as Slinkie). There's generally not any particularly good reason for using this mode, unless you really don't want the extra baggage of the floating point operations hanging around (i.e, you're really trying to optimize something that doesn't use floating point at all for size). You could safely leave this one out and not have to worry about anything.

-m4 gives you full double-precision support, but as I mentioned before KOS assumes you're always in single-precision mode, so using this might not be the best idea with KOS programs. The SH4's FPU has two different modes, controlled by a certain register to determine whether to do single-precision or double-precision math. KOS always sets the register at boot up to do single-precision and assumes it will never be touched after that. Certain things that you're likely to do in KOS (like matrix/vector stuff, as mentioned earlier) don't work at all in double-precision mode, and pretty much all floating point math will take longer to do in double precision mode. Simply put, there's really not much of a reason for supporting double precision in KOS, so this mode usually isn't used in KOS. If I'm not mistaken, dcload compiles with -m4, hence why we include it still.

TL;DR: -m4 and -m4-nofpu are included for the benefit of programs that used them in the past (and still do), but you should generally stick with -m4-single-only.
User avatar
bogglez
Moderator
Moderator
Posts: 578
Joined: Sun Apr 20, 2014 9:45 am
Has thanked: 0
Been thanked: 0

Re: CFLAGS chosen for kos

Post by bogglez »

BlueCrab wrote:As for the warning on printf("%f", 0.0f), that's because printf("%f", ...) assumes that the argument passed is a double -- cast your argument properly and you won't get that warning. That said, that warning is awfully silly, since the C standard says exactly what should happen in that case. Yes, it does only show up with -m4-single-only, but I blame that on GCC doing something silly. :wink:
I talked with somebody on ##c about this and they also said it looks like a GCC bug. It's just really annoying because it causes so many warnings. I didn't want to turn off -wformat though. We'll just cast then. Thanks :)
Wiki & tutorials: http://dcemulation.org/?title=Development
Wiki feedback: viewtopic.php?f=29&t=103940
My libgl playground (not for production): https://bitbucket.org/bogglez/libgl15
My lxdream fork (with small fixes): https://bitbucket.org/bogglez/lxdream
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5658
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has thanked: 9 times
Been thanked: 69 times
Contact:

Re: CFLAGS chosen for kos

Post by BlueCrab »

bogglez wrote:
BlueCrab wrote:As for the warning on printf("%f", 0.0f), that's because printf("%f", ...) assumes that the argument passed is a double -- cast your argument properly and you won't get that warning. That said, that warning is awfully silly, since the C standard says exactly what should happen in that case. Yes, it does only show up with -m4-single-only, but I blame that on GCC doing something silly. :wink:
I talked with somebody on ##c about this and they also said it looks like a GCC bug. It's just really annoying because it causes so many warnings. I didn't want to turn off -wformat though. We'll just cast then. Thanks :)
GCC has done some really silly things when it comes to compiling for SuperH over the years. This is one of the least annoying, truth be told (since it doesn't generate any invalid code in the end). :wink:
Ayla
DC Developer
DC Developer
Posts: 142
Joined: Thu Apr 03, 2008 7:01 am
Has thanked: 0
Been thanked: 4 times
Contact:

Re: CFLAGS chosen for kos

Post by Ayla »

%f is for floats IIRC, for doubles you should use %lf.
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5658
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has thanked: 9 times
Been thanked: 69 times
Contact:

Re: CFLAGS chosen for kos

Post by BlueCrab »

Ayla wrote:%f is for floats IIRC, for doubles you should use %lf.
For scant(), yes there is a difference, however for printf, %f is for double as well. The C standard specifies that floats should be automatically promoted to double in that case.

The problem is GCC is confused when with -m4-single-only (sizeof(float) == 4 && sizeof(double) == 4). That's a relatively new warning issue, and it probably should be reported on the GCC bug tracker if it hasn't already been fixed in the code.
User avatar
bogglez
Moderator
Moderator
Posts: 578
Joined: Sun Apr 20, 2014 9:45 am
Has thanked: 0
Been thanked: 0

Re: CFLAGS chosen for kos

Post by bogglez »

Why is -fno-strict-aliasing used for KOS?
Since this has serious performance implications I assume you're fighting gcc bugs again or KOS involves lots of code ignoring the strict aliasing rule?
Wiki & tutorials: http://dcemulation.org/?title=Development
Wiki feedback: viewtopic.php?f=29&t=103940
My libgl playground (not for production): https://bitbucket.org/bogglez/libgl15
My lxdream fork (with small fixes): https://bitbucket.org/bogglez/lxdream
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5658
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has thanked: 9 times
Been thanked: 69 times
Contact:

Re: CFLAGS chosen for kos

Post by BlueCrab »

-fno-strict-aliasing is used with KOS because it wouldn't compile/work properly otherwise. Low-level pieces of code like OS kernels pretty much always break GCC's silly implementation of the strict aliasing rules.

See the comments on this stackoverflow question and the original author of the Linux Kernel, Linus Torvalds', take on why GCC's implementation of the strict aliasing rule is pretty much braindead.
Post Reply