Optimization question floating point

General Programming help in C, C++ or ASM, Compiling / Debugging, and R3000A Central Processing Unit (CPU) information
Post Reply
sleepingburrito
Interested PSXDEV User
Interested PSXDEV User
Posts: 5
Joined: December 5th, 2018, 5:32 am

Optimization question floating point

Post by sleepingburrito » January 20th, 2019, 8:12 am

I know the PS1 does not have native floating point on the CPU but I was making a game where not many things use it so I felt like it was an ok trade off to use it. I did come across one instance where its noticeable but its not a big deal if I can't fix it.

I have a knock back function and its basically like this, I omitted not necessary code:

Code: Select all

float angle, tmpYSub, tmpXSub;
tmpYSub = npc.x - knockback.x;
tmpYSub = npc.y - knockback.y;

if (sqrt(tmpYSub * tmpYSub + tmpXSub * tmpXSub) < knockback.range){
	angle = atan2(tmpYSub, tmpXSub);
	*npcHspeedInOut += knockback->intensity * cos(angle);
	*npcVspeedInOut += knockback->intensity * sin(angle);
}
I have commented out and tested each part of the code. I have found that atan2 is slowing it down a lot, It skips like 1-3 frames when I use it. I only use it for one frame when something uses knock back so its not too bad, and its only really bad in big explosions with lots of objects getting hit. I did google "fast atan2 in c" but what I found was slower than the one from the SDK or was X86 specific.

Im ok with replacing it with something that's much less accurate. Finding the distance is the next slowest part but "abs(tmpYSub) + abs(tmpXSub)" was a good trade off for me, so im mostly looking to replace atan2. Thanks for reading!

User avatar
Orion_
Legendary Programmer
Legendary Programmer
Posts: 226
Joined: August 13th, 2012, 2:48 am
I am a: Programmer
PlayStation Model: Net Yaroze
Location: France
Contact:

Re: Optimization question floating point

Post by Orion_ » January 21st, 2019, 12:03 am

sqrt is slow too, since you only use it for a comparison, you can omit the squareroot by squaring knockback.range
if ((tmpYSub * tmpYSub + tmpXSub * tmpXSub) < (knockback.range * knockback.range))

I searched for fixed point atan2 on my harddrive and found this code:
https://pastebin.com/qzJ3RSnj

I don't know where it's from, but maybe that can help you speed your code.
doing fixed point is easy, instead of "float" you declare an "int" value, and do some bit shifting.
for example:
float a = 1.2;
float b = 1.5;
float c = a*b;

fixed point equivalent:
int a = (int)(1.2 * 256.0);
int b = (int)(1.5 * 256.0);
int c = (a*b)>>8;

if you want to get the float value of C, you just divide by 256.0, or the integer part: >> 8
Retro game development on Playstation and other consoles http://orionsoft.free.fr/

Sblorgz
Curious PSXDEV User
Curious PSXDEV User
Posts: 22
Joined: May 18th, 2015, 1:47 pm

Re: Optimization question floating point

Post by Sblorgz » January 21st, 2019, 7:19 am

Take advantage of fixed point math (float operations are horribly slow) and use SquareRoot0 for magnitude and catan instead of atan2 for direction.

User avatar
LameGuy64
Psy-Q Enthusiast
Psy-Q Enthusiast
Posts: 349
Joined: April 10th, 2013, 3:47 pm
I am a: Hobbyist Game Developer
Motto: Commercial or not, play it!
PlayStation Model: H2000/7000
Skype: lameguy64
Location: Philippines

Re: Optimization question floating point

Post by LameGuy64 » January 21st, 2019, 11:32 am

I don't recommend making extensive use of floating point math on the PS1 as FPU emulation is just very slow on the already slow R3000 so fixed point integer math is recommended.

Try using ratan2(), SquareRoot0(), csin() and ccos() and convert your arithmetic to fixed point integer math. Using 12-bit fractions (4096 = 1.0f) is recommended as most of the fixed point math functions in the GTE library use that format.
Please don't forget to include my name if you share my work around. Credit where it is due.

Dev. Console: SCPH-7000 with SCPH-7501 ROM, MM3, PAL color fix, Direct AV ports, DB-9 port for Serial I/O, and a Xplorer FX with Caetla 0.35.

DTL-H2000 PC: Dell Optiplex GX110, Windows 98SE & Windows XP, Pentium III 933MHz, 384MB SDRAM, ATI Radeon 7000 VE 64MB, Soundblaster Audigy, 40GB Seagate HDD, Hitachi Lite-on CD-RW Drive, ZIP 250 and 3.5" Floppy.

sleepingburrito
Interested PSXDEV User
Interested PSXDEV User
Posts: 5
Joined: December 5th, 2018, 5:32 am

Re: Optimization question floating point

Post by sleepingburrito » January 21st, 2019, 7:16 pm

Thank you all.

Xavi92
C Programming Expert
C Programming Expert
Posts: 122
Joined: October 6th, 2012, 12:53 am

Re: Optimization question floating point

Post by Xavi92 » February 5th, 2019, 11:41 pm

libfixmath provides handy functions and types for fixed-point arithmetic on C and C++.

User avatar
Shadow
Admin / PSXDEV
Admin / PSXDEV
Posts: 2233
Joined: December 31st, 2012, 5:37 pm
PlayStation Model: H2000/5502

Re: Optimization question floating point

Post by Shadow » April 1st, 2019, 4:06 am

You could also use a pre-compiled or pre-calculated table so all the math is already processed to your application needs.
Development Console: SCPH-5502 with 8MB RAM, MM3 Modchip, PAL 60 Colour Modification (for NTSC), DB-9 breakout headers for both RGB and Serial output and an Xplorer with CAETLA 0.34.

Development Computer: Windows 98, Pentium 3 [400MHz], 128MB SDRAM, DTL-H2000, DTL-H201A, 21" Sony Trinitron CRT, CD-ROM burner, 3.25" and 5.25" Floppy Diskette Drives and a ZIP 100 Diskette Drive.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest