PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

PocketQuake FPM?

How is PocketQuake's source organized as far as floating point math? It seems that my fpm optimizations in FloatingPointMath.c don't matter at all (to the emulator), even if I comment out both my optimization and the original function! Help me, I only need guidance so that I can know what to optimize!

Dan, could you just explain the source and what we should work on, especially in relation to floating point math?
I basically programmed a way to find the fixed-point square root of a fixed point number using only multiplication and like addition stuff (no division or floating point anything!). This code would be simple to convert to ARM asm (I looked at the asm and it seems simple to do this stuff). I don't think that my conversions matter, though, because I don't know how PQ's source is set up to handle deciding whether or not to do fixed point. I really need to speak with someone who has helped with PocketQuake in order to figure this out...
This is what I started out with (in FloatingPointMath.c):
Code:

//TODO: Implement sqrt mathematically instead of converting to float and back
fixedpoint_t fpm_Sqrt(fixedpoint_t fxp) {
return fpm_FromFloat(sqrt(fpm_ToFloat(fxp)));
}


And this is what I changed it into:
Code:

fixedpoint_t fpm_Sqrt(fixedpoint_t fxp) {
//fixedpoint_t is a fixed point 16.16 number
unsigned long int NACTUAL = fxp;

unsigned long int ASQRT = 0x00008000;

if( ASQRT*ASQRT <= NACTUAL )
ASQRT = ASQRT^0x00004000;
else
ASQRT = NACTUAL^0x0000C000;

if( ASQRT*ASQRT <= NACTUAL )
ASQRT = ASQRT^0x00002000;
else
ASQRT = NACTUAL^0x00006000;

if( ASQRT*ASQRT <= NACTUAL )
ASQRT = ASQRT^0x00001000;
else
ASQRT = NACTUAL^0x00003000;

if( ASQRT*ASQRT <= NACTUAL )
ASQRT = ASQRT^0x00000800;
else
ASQRT = (NACTUAL^0x00001800;

if( ASQRT*ASQRT <= NACTUAL )
ASQRT = ASQRT^0x00000400;
else
ASQRT = NACTUAL^0x00000C00;

if( ASQRT*ASQRT <= NACTUAL )
ASQRT = ASQRT^0x00000200;
else
ASQRT = NACTUAL^0x00000600;

if( ASQRT*ASQRT <= NACTUAL )
ASQRT = ASQRT^0x00000100;
else
ASQRT = NACTUAL^0x00000300;

if( ASQRT*ASQRT <= NACTUAL )
ASQRT = ASQRT^0x00000080;
else
ASQRT = NACTUAL^0x00000180;

if( ASQRT*ASQRT <= NACTUAL )
ASQRT = ASQRT^0x00000040;
else
ASQRT = NACTUAL^0x000000C0;

if( ASQRT*ASQRT <= NACTUAL )
ASQRT = ASQRT^0x00000020;
else
ASQRT = NACTUAL^0x00000060;

if( ASQRT*ASQRT <= NACTUAL )
ASQRT = ASQRT^0x00000010;
else
ASQRT = NACTUAL^0x00000030;

if( ASQRT*ASQRT <= NACTUAL )
ASQRT = ASQRT^0x00000008;
else
ASQRT = NACTUAL^0x00000018;

if( ASQRT*ASQRT <= NACTUAL )
ASQRT = ASQRT^0x00000004;
else
ASQRT = NACTUAL^0x0000000C;

if( ASQRT*ASQRT <= NACTUAL )
ASQRT = ASQRT^0x00000002;
else
ASQRT = NACTUAL^0x00000006;

if( ASQRT*ASQRT <= NACTUAL )
ASQRT = ASQRT^0x00000001;
else
ASQRT = NACTUAL^0x00000003;

if( ASQRT*ASQRT != NACTUAL )
ASQRT = NACTUAL^0x00000001;
//result is basically a 8.8 fixed point number
fxp = NACTUAL << 8;
//that translates it into an 16.16 fixed point number
return fxp;
}


Well? What do you think? Floating point will produce a more accurate number (8.16), but mine is all in integer and is using very fast math. My code could easily be transfered into asm, as you can see.
BTW, I could easily do this in 64-bit, 32.32, which would give more accuracy (a 16.16 bit number, which would need to be translated into a 8.16 number, if you understand what I'm talking about). In fact, the first time I tried this algorithm, I did it for finding the square root of a 64-bit regular (not-floating-point) integer.

It would be also easy to convert into doing 8.24 bit stuff, but you would get a 4.12 result (as opposed to 4.24 bit for floating point).
Exactly how many times does PQ actually need to find the square-root of something? I bet it is like hardly at all, now that I think about it! Oh well.

Anyways, I don't think that fpm_Sqrt is even really used, since when I comment it out (both ways of doing it) it still runs fine in the emulator.
mips Mips MIPS!!!

btw, if that works we would have a fixed point quake, right? wouldnt that make it fast as a lightening and stuff?
-Eike
Shapeshifter, calm down. All that this is is a single little optimization that perhaps will not make a practical difference in fps (.0001 fps, probably). I don't know. That's why I'm asking Dan.
0.0001 fps more but an executable 15 times as large
not really. if you write it in asm and stuff, you could get it more efficient and probably also reduce the size of the executable.

Another thing, they do call this the General Developer Discussion for a reason...
None of the routines in FloatingPointMath.c are actually used. All the current conversions are in-place substitutions.

Dan East
Mirek Czerwinski at uni · 29 Mar 2002, 10:36 UTC
robotbeat:as far as I understood you have tested your fixedpoint math routines under the emulator, right?
well if you do so, it might get slower, because PC do have a fast FPU and therefore fixedpoint math stuff will often work SLOWER on PC. To see the performace difference you have to test your code on a real device.
hope it hepls.
Well I think the square root might be helpful for things like the pythagorean theorum, but ... LOL, a SLIGHTLY faster root conversion wouldn't really help much guys.

Robotbeat, you should probably debug all the routines and find out which ones take the longest to execute, optimize those first for the biggest effect. Just making random optimizations can only help so much.
Moose, you are right. However, if you needed to do a square root a lot, my algorithm would save a lot of time. There are only so many ways of finding a square root, you know, and my way is quite fast.

Dan, thanks for letting me know. Is there a specific place that I should look to start optimizing? My math teacher will be glad to know that my Precalculus isn't going to waste! I like this programming stuff. It's kind of fun. :P
See, Moose, I don't have much programming experience, but I know enough to do optimizations and stuff. Programming asm (and simple/fast C) is fun, I think. I like actually making up ways to do functions! I am pretty smart when it comes to math, but I don't know much about programming or debugging. Dan should post a todo list for programmers so that certain people can work in certain areas of PocketQuake code.

As you can see from my code, I'm serious about this programming business. I hope that I can spark up interest in other developers to help fix up PocketQuake and maybe PocketQuake2. We should organize this a bit more so that programmers can be more productive in their optimizing.
Maybe this thread is better suited for the PocketQuake forum?
Wow.. this is exciting! PocketQuake may get a speed boost! Now all you have to do is learn a little MIPS assembley... *pictures PQ at 50fps* *drools*

I think your work will be appreciated :)
This thread belongs in the PocketQuake Developer Forum, but that doesn't exist, yet. (hint, hint, Chris Edwards/Matt!) I didn't want to get lost in all the other threads in the PQ forum, threads like "what's gx.dll?" and "PQ for Palm! Please?", so I thought that this was a more appropriate forum.
I feel like a soldier without an officer right nnow :( . I need guidance from Dan as to what is needed. I think that more people should contribute to PocketQuake!
Heck, anyone who knows more about the PQ source code than I do could help tell me what I can do!
ESW, PocketQuake will never run at 50FPS until plenty after the XSCALE processor comes out. No amount of optimization can realistically increase the speed beyond 25 FPS on the current crop of devices.

Robotbeat, I've taken a look at the PocketQuake source, although I don't have it at the moment, I'm not even sure if I have eVC installed right now. But I could help you find your way around, and I could help you with the math with my Calc II knowledge if you'd like. ... But not now, I have this huge headache. I think the palmade I got from the hair dresser is causing it.
yeah, I know. I was just joking about 50fps. Anyway, PQ is fairly optimized as it is. Randall's maps are fairly playable in landscape on an E-125, and so is the one I'm working on right now. It might be nice to have another FPS or two, though :)
I know enough about math, I just need to know what file specifically could be optimized and give a performance increase. Especially I want to know what can be done without worrying about the other files. Is there any other math function that is slow? I love doing this type of math. I have programmed stuff on calculators to find the rest of the sides and/or angles of any triangle given any 3 other variables (well, you either need to give me ASA, AAS, SSS, or SAS. SSA took too long to program how to solve, so I didn't finish that one, yet.).

Dan, it sure looks like "#ifdef USEFPM" is used a lot. It seems like you attempted a quite global floating point to fixed point conversion of PocketQuake! Wow. So, I am assuming that you never got it to work, right? Do you think that someone (if not you) could get it to work? That would be a huge project just to understand all of that.
First of all, it's pomade.

Second of all, you never really need anything above trig to speed up quake.

Third of all, learn to program first. Then you would know how to look at a source code, and know what would need to be sped up. and also, you wouldn't have to ask Dan what to do every 5 minutes.
If you don't know how to profile the code, you're wasting your time. You need to profile the code, find the routines that are taking the most time, along with the routines that are called frequently, then spend your time improving those portions of code. It's also important to profile using the same code path through the application each time. Maybe you can use Quake's timedemo for this.

We have discussed a number of development topics over the last year in the PocketQuake forum. I personally think it's better to post PocketQuake specific dev issues there. While you might not want your thread to get lost in the plethora of "how do I install..."-type messages from newbies, it might be nice for the users to know that someone is considering performance improvments for the game. Heck, some might even volunteer to help you. It's your call though. I was just making a suggestion.
Jaybot, I understand what you are trying to say, but if you are going to have multiple people working on the same project, then you need to organize the work. I will learn right now what needs to be done in order to figure out what routines take longest. Thanks, everyone, for your input.
Jaybot wrote:
First of all, it's pomade.
LOL. Thanks Jay, for that bit of info.

Guys, you need to lighten up a little on this "you need to learn this and that" type stuff. That's what he's trying to do by posting here. I'm sure he realises Dan isn't online 24/7 and that we don't exactly want to hold his hand the entire way, but let's try to help him with what we know how to do already, atleast if it's easy enough to explain.

Personally, I think this thread should be a "Sticky" in the PocketQuake page, if Matt is online and would move it there and apply that attribute.
The act of converting to or from floating point / fixed integer requires a floating point operation.

Thus my last large optimization project was to convert all the model verticies to fixed point at load-time, and perform all math in fixed point throughout the rendering process. It is a reasonable thing to attempt, and is of course possible to implement. Search for USE_PQ_OPT3 for all the work that's been done so far on that conversion.

I was hoping for a 50% or so speed increase from that optimization.

Dan East
yeah. we're all hoping for that.
Thanks, Dan. That would probably make quake run good on my e115 (with overclock, autoexec tweaks, etc.), which would be nice with its awesome screen and good buttons.

Thanks, Moose, for defending me! :wink: I actually do know the very basics of C (no pun intended!), and really, programming itself is not hard, it's trying to do stuff like a global conversion to fixed-point, like Dan was (is?) doing. But since Dan knows the code best, I choose to ask him. Now I have an idea of what he is trying to do, so maybe I'll be able to get on the road to actually doing something useful with my time (specifically, playing Quake at a decent framerate during school :wink: :D :D).

I'll be reading through my C programming book, if anyone wants me...