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: Dec 05, 2018

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!

Orion_
Verified
Legendary Programmer
Legendary Programmer
Posts: 240
Joined: Aug 13, 2012
I am a: Programmer
PlayStation Model: Net Yaroze
Location: France
Contact:

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:


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 18, 2015

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
Verified
Psy-Q Enthusiast
Psy-Q Enthusiast
Posts: 388
Joined: Apr 10, 2013
I am a: Hobbyist Game Developer
Motto: Commercial or not, play it!
PlayStation Model: H2000/7000
Location: Philippines
Contact:

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: Dec 05, 2018

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

Thank you all.

Xavi92
Verified
C Programming Expert
C Programming Expert
Posts: 161
Joined: Oct 06, 2012
PlayStation Model: SCPH-5502
Contact:

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
Verified
Admin / PSXDEV
Admin / PSXDEV
Posts: 2670
Joined: Dec 31, 2012
PlayStation Model: H2000/5502
Discord: Shadow^PSXDEV

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), PSIO Switch Board, DB-9 breakout headers for both RGB and Serial output and an Xplorer with CAETLA 0.34.

PlayStation Development PC: Windows 98 SE, Pentium 3 at 400MHz, 128MB SDRAM, DTL-H2000, DTL-H2010, DTL-H201A, DTL-S2020 (with 4GB SCSI-2 HDD), 21" Sony G420, CD-R burner, 3.25" and 5.25" Floppy Diskette Drives, ZIP 100 Diskette Drive and an IBM Model M keyboard.

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests