!GCC on RISC OS with -O2 and cexpf

Discuss all aspects of programming here. From 8-bit through to modern architectures.
Post Reply
zolbatar
Posts: 36
Joined: Sat Sep 22, 2018 12:12 pm
Location: Nottingham, UK
Contact:

!GCC on RISC OS with -O2 and cexpf

Post by zolbatar » Wed Aug 14, 2019 8:37 am

In my 3D matrices code there are a bunch of trig functions doing rotation. If I run GCC/make normally, I get a working executable, but if I use any optimisation flags such as -O1 then I get a link error with it unable to find cexpf.

A google suggests that GCC is doing some clever optimisation where multiple trig calls are made using complex numbers. However, I don't think that gcc on RISC OS has the library. I can see the math library and others, but no complex library which would suggest why -lcomplex is not working for me.

Any ideas?

Cheers, Daryl :-)
Master 128 with DataCentre and RPi co-pro.
RPi B+ & 3B+ both running RISC OS.
Poorly A4000 (battery damage, partially repaired).

Phlamethrower
Posts: 112
Joined: Fri Nov 24, 2017 1:35 pm
Contact:

Re: !GCC on RISC OS with -O2 and cexpf

Post by Phlamethrower » Wed Aug 14, 2019 10:51 am

Which version of GCC? I don't think I've run into any problems like that (but, I'm not sure if I've ever compiled anything that uses complex numbers)

zolbatar
Posts: 36
Joined: Sat Sep 22, 2018 12:12 pm
Location: Nottingham, UK
Contact:

Re: !GCC on RISC OS with -O2 and cexpf

Post by zolbatar » Wed Aug 14, 2019 11:19 am

It's the latest version from !Packman.

I don't actually do any complex number stuff myself, it seems to be a compiler optimisation. Some online searching suggests changing to doubles and it no longer does it. I have gcc to leave the intermediate assembler so I will take a look at it.
Master 128 with DataCentre and RPi co-pro.
RPi B+ & 3B+ both running RISC OS.
Poorly A4000 (battery damage, partially repaired).

User avatar
Rich Talbot-Watkins
Posts: 1512
Joined: Thu Jan 13, 2005 5:20 pm
Location: Palma, Mallorca
Contact:

Re: !GCC on RISC OS with -O2 and cexpf

Post by Rich Talbot-Watkins » Wed Aug 14, 2019 11:40 am

I would highly recommend for performance that you use fixed point representation for your matrix maths, and use a sine table to lookup sines/cosines rather than calculating them with sinf()/cosf().

Also, remember there are some nice trig identities you can use to avoid multiplications:

e.g.
sin A * sin B = (cos(A - B) - cos(A + B)) / 2
cos A * cos B = (cos(A - B) + cos(A + B)) / 2
sin A * cos B = (sin(A + B) + sin(A - B)) / 2

This should increase your performance enormously!

zolbatar
Posts: 36
Joined: Sat Sep 22, 2018 12:12 pm
Location: Nottingham, UK
Contact:

Re: !GCC on RISC OS with -O2 and cexpf

Post by zolbatar » Wed Aug 14, 2019 11:50 am

Rich Talbot-Watkins wrote:
Wed Aug 14, 2019 11:40 am
I would highly recommend for performance that you use fixed point representation for your matrix maths, and use a sine table to lookup sines/cosines rather than calculating them with sinf()/cosf().

Also, remember there are some nice trig identities you can use to avoid multiplications:

e.g.
sin A * sin B = (cos(A - B) - cos(A + B)) / 2
cos A * cos B = (cos(A - B) + cos(A + B)) / 2
sin A * cos B = (sin(A + B) + sin(A - B)) / 2

This should increase your performance enormously!
Thanks for the advice. I was intending to optimise going forward, using look up tables and fixed point arithmetic to replace the floating point code.

Even divide and multiply makes me worry when I'm updating thousands of vertices each frame!
Master 128 with DataCentre and RPi co-pro.
RPi B+ & 3B+ both running RISC OS.
Poorly A4000 (battery damage, partially repaired).

User avatar
Rich Talbot-Watkins
Posts: 1512
Joined: Thu Jan 13, 2005 5:20 pm
Location: Palma, Mallorca
Contact:

Re: !GCC on RISC OS with -O2 and cexpf

Post by Rich Talbot-Watkins » Wed Aug 14, 2019 12:16 pm

zolbatar wrote:
Wed Aug 14, 2019 11:50 am
Thanks for the advice. I was intending to optimise going forward, using look up tables and fixed point arithmetic to replace the floating point code.

Even divide and multiply makes me worry when I'm updating thousands of vertices each frame!
Yeah, exactly. You can't get around divide when doing perspective correction (although you can possibly at least use a reciprocal table).

Not sure what your current approach is, but if you're using Euler angles to represent the rotation, and want to compose them as a single rotation matrix (which has elements like (-cos X sin Z + sin X sin Y cos Z)), using the sum identities can save you a whole bunch of multiplies.

There are some other handy ones too, for rewriting a product of three sines/cosines as a sum of three sine/cosine lookups.

sin A * sin B * sin C = (sin(A+B-C) - sin(A+B+C) + sin(A-B+C) - sin(A-B-C)) / 4
sin A * sin B * cos C = (cos(A-B-C) - cos(A+B-C) + cos(A-B+C) - cos(A+B+C)) / 4
sin A * cos B * cos C = (sin(A+B+C) + sin(A+B-C) + sin(A-B+C) + sin(A-B-C)) / 4
cos A * cos B * cos C = (cos(A+B+C) + cos(A+B-C) + cos(A-B+C) + cos(A-B-C)) / 4

Post Reply