GP32 Sin() and Cos()


Toris

Certified Guru
Joined
Apr 6, 2003
Messages
82
Hi,
I've just switched my compiler from ADS to GCC, and I noticed a few odd things had started going wrong. I then realised, it was the sin() and cos() routines that weren't working correctly.
If you are using DevKitAdv, then do this, it may solve your problem:
First, make sure you are doing "#include <math.h>"
Secondly, in gp32.mk, add "-lm" onto the end of the libraries line.
This will get sin() working, BUT, the results may not be correct.
Lastly, in gp32.mk again, check in the CFLAGS or CPPFLAGS for "-fshort-double \", if that is there, REMOVE IT! - what this does, it make all of your doubles, into floats, while I think this could cause a speed boost for some programs (?), since all the math functions, sin(), cos() etc... work on doubles, it seems to cause incorrect results.

Try this fix, and let me know if it works :)
-Toris
 
For a fix i also have a lookup table, which may be faster than using math routines.
 
GP32 can use floating point maths, but it does it very slowly in some cases, Mainly division and multiplication I believe.

Look up tables are faster, but I still haven't quite got my head around fixed point math yet, so they're not much use to me right now XD
 
Thanks for the tips Toris, I will try that out. I've added -lm before but the sin() function returned results that were really strange, maybe that double-switch will fix that.
 
Jlebrech: Well, I usually make my tables at runtime from within my code, but when the sin() and cos() functions don't work correctly, it's not so easy, which is why I was looking for a way to get them to work :)

Sdw: It should do, it worked wonders for me :) - It may not work if you are not using DKA. The reason I specify DKA, is that the default makefile contains that switch, and it needs removing. If you are using your own custom makefile without that switch, then the problem most likely lies elsewhere.
 
I'm running Ricos GP32 converted DevKitAdvance, so I probably have that switch in my makefile. Will check it when I get home.
 
GP32 can use floating point maths, but it does it very slowly in some cases, Mainly division and multiplication I believe.

Look up tables are faster, but I still haven't quite got my head around fixed point math yet, so they're not much use to me right now XD
Hmm? Lookup tables have zero to do with fixed point math...... so you just weirded me out.

Lookup table is almost always worth it, since the domain of things to look up is pretty small. Just pre-look everything up and stuf finto an array. No more math.

Fixed point isn't too bad either.. just make a macro to add, sub, mult, div and use integers only, pre- and post- shifting to keep a virtual decimal point. Not too hard... but a little weird at first :) You can likely find pointers all over the web.

jeff
 
Last edited by a moderator:
Or you can steal a fixed point class :)

I wouldn't suggest too much division, as the ARM processor can't actually do it. Division in the devkit is done by software :D
 
Oh, sorry. I was refering to the lookup tables for sin & cos that jlebrech put on his website. They appeared to be in Fixed point form, but I could be wrong.
 
Lastly, in gp32.mk again, check in the CFLAGS or CPPFLAGS for "-fshort-double \", if that is there, REMOVE IT! - what this does, it make all of your doubles, into floats, while I think this could cause a speed boost for some programs (?), since all the math functions, sin(), cos() etc... work on doubles, it seems to cause incorrect results.

Try this fix, and let me know if it works :)
Worked like a charm. Great, now I can finally precalc my sintabs runltime instead of having to include them in .h files!
 
Last edited by a moderator:
I have linked all things quoted on top but I do not manage to print correctly a float with the :

char local_buffer[256];
float fval;

fval = 5.69;
gp_str_func.sprintf(local_buffer,"%f",fval);
GpTextOut16(NULL, new_bg, 50, 50, local_buffer, 0);


Do someone got an idea on how to print the value of a floating point data ??
 
I have a sample of my lookup table and the Rotate function, on my site: www.gamepark.tk
 
AlexP: yes, unfortunately, there seems to be a bug somewhere, that doesn't allow you to use floats with sprintf, or gm_sprintf. I am assuming you get something like "X", "0.X", or "0.0Y", as that is the only output I managed.
If I find a solution, I will let you know, and if you find one, please do the same ;)
 
Back
Top