Making The Quake Code More Efficient
I've been looking through the FixedPointMath.c file and have noticed a couple places where Dan has said the code could be more efficient. Do the two new functions do the trick for those old ones?
OLD
//TODO: could be more efficient
fixedpoint_t fpm_Ceil(fixedpoint_t fxp) {
if (fxp&0x0000ffff) {
if (fxp<=0) return -(fixedpoint_t)((-fxp)&0xffff0000);
return (fxp&0xffff0000)+FPM_FROMLONGC(1);
}
return fxp;
}
//TODO: could be more efficient
fixedpoint_t fpm_Floor(fixedpoint_t fxp) {
if (fxp&0x0000ffff) {
if (fxp<0) return -(long)(((-fxp)&0xffff0000)+FPM_FROMLONGC(1));
return fxp&0xffff0000;
}
return fxp;
}
NEW
fixedpoint_t fpm_Ceil(fixedpoint_t fxp) {
if (fxp <= 0) return fxp & 0xffff0000;
else return (fxp + 0xffff) & ~0xffff;
}
fixedpoint_t fpm_Floor(fixedpoint_t fxp) {
if (fxp >= 0) return fxp & 0xffff0000;
else return (fxp + 0xffff) & ~0xffff;
}
Your best bet would be to compile and test it out for FPS to see if it is faster.
The FixedPointMath.c routines are not being used at this time. After the PQ project was at a stable point I foolishly decided to attempt a global, generic replacement of all Floating point math with Fixed point. I created a new fixed point type, and the routines you see in FixedPointMath to handle fixed point operations. I then went through the source and replaced floating point operators (+, -, *, etc) with function calls to my new routines. After a huge amount of effort, it wouldn't work. What I found was that the range and precision of the various floating point vars was far greater than what can be stored with 32 bit fixed point (16.16).
So, to convert a set of routines from floating point to fixed point, I follow the following tedious steps:
1: I identify the potential floating point vars I want to convert.
2: I identify every routine that modifies that var.
3: I use a logging function at every point that var is modified to record the min, max and smallest precision value that variable is assigned throughout the course of execution (usually allowing a full demo to run).
4: Based on the results of the logging I determine what fixed point precision (if any) to use for that particular variable.
5: I then have to study what operations occur between those variables. Some fixed point operations result in a loss of precision, others in a loss of range. I have to try and synchonize the fixed point precision so that multiplication and division operations don't result in overflow / underflow. I usually have a little precision to work with, but there are cases when I literaly did not have 1 single extra bit of precision to spare.
Dan East
Then, could you point me in the direction of a place that you think that I could optimize to increase performance. That change actually gave me .1 fps greater, but that could just be a fluctuation in speed for some reason too.
This question is mainly for Dan, but if you think you know the answer (and many of you probably do) feel free to respond. I am going to start rewriting functions from PQ in asm, and I wouldlike to know which ones will be the most benificial, and give the best speed increases. Please just point me in the right direction.
Wow Dan, that sounds remarkably similar to something I did a few months back. I wrote a C++ CFixedPoint class and supported every operator that you would use with floats. I had the debug build actual do a true floating point routine under the hood along side the fixed point operation. I keep track of the difference in an attempt at seeing what could break due to the fixed point implementation's lack of precision compared to the true floating point result.
I don't remember the exact results from profiling matrix concatentation routines, but it was quite a bit faster than the floating point library on my iPaq. If anyone is interested I can dig up the test results and post here.
Sure, post away.
I'll have to look into that. If you download the original Quake source from ID software you'll find a lot of x86 asm code. That may be a place to start, converting their asm to ARM. Also, they undoubtedly only wrote the more critical routines in ASM.
Dan East
Well, the main issue I ran into wasn't that the precision was off a bit, it was when a very small value fell off to zero, or when I overflowed / underflowed and ended up changing sign and all that crap. In the debug build I would check, for example, to see if the result of a division with a non-zero dividend was 0.
I guess Quake could be converted to C++ if such a class seemed like it would work / help. I assume it dynamically adjusts precision?
Dan East
Rob,
Your function implementations above seem to me to be incorrect. Try comparing the result of your functions to the values returned from the CRT ceil/floor functions.
For what it's worth, this is a pretty tight implementation for 16:16 fixed point floor and ceil. You'll notice that you don't have to special-case if the value is less than zero, which would be a slight improvement over the original code.
- Code:
typedef FIXED32 long;
inline FIXED32 Ceil (FIXED32 a)
{
if (a & 0x0000ffff)
{
a += 0x00010000;
return (FIXED32)(a & 0xffff0000);
}
return a;
}
inline FIXED32 Floor (FIXED32 a)
{
return a & 0xffff0000;
}
Dan,
Nope, the current implementation doesn't dynamically adjust its precision. I threw in the debug floating point calculation so that when things started to get out of whack on-screen, I would have a place to start looking.
It's probably a tough job to try to convert to some arbitrary fixed point precision if you have an existing code base like Quake which uses floating point. Much easier if you can start a project from the get-go and author your content with fixed-point in mind.
Maybe I've asked this before (and if so I've forgotten the reply), what are you guys using for profiling your code? When you take a huge code base you aren't familiar with (like Quake) and want to start optimizing, how to you find the hotspots?
I profile the sections I'm interested in manually. I worked my way down the function trees to see what is doing what.
The precision is totally variable. In some cases I had to use all 32 bits to store the fraction portion, and in others I had to use most of the bits just for the integer portion.
Dan East
That's kind of what I do. I picout a file that has a lot of short functions, like 5-7 lines (sometimes more and sometimes less), and then I print the whole thing out and take it to work with me and read it over there, making notes on the paper about what everything does, and why the auther chose to use that bit of code. Everyone at work wonders why a dishwasher is reading to source code to Quake, but noone would dare say anything. I love me just

But anyway, I like to look at the whole picture too, exspecialy on shorter programs (Quake not being one of them), where you can start at the beginning and work your way to the end with out many sidetracks.
Here is a quick question for you (dealing with asm).
Lets say you had the type vec3_t (look formilar Dan?), which is a list of three floating point values.
Now you have a function
void test (vec3_t a, vec3_t b)
{
//This code is not important to the question
}
Where are the values in 'a' and 'b' stored when they get passed to the function. Like, I know that if it was "void test(int a, int b)" that reg a1 would have a's value and a2 would have b's value. So, where are the values for a and b stored now?
This question is dealing with the strongarm processor, but it should still be rather similar to other types of asm.
Rob would'nt the compiler allocate a base address for a memory area for variables like b and index into that area to assign and read the value for b.
It seems like you could use a debugger to look at the offsets be able to get the address of b as an offset from the start of program memory.
The C compiler will use the registers, r0..r3 for passing up to 4 parameters in a function call. Remaining parameters are passed on the stack (from what I remember). All of this is covered in the Pocket PC SDK help file under ARM Calling Convention.
You can also write a function in C, then either get the compiler to generate a .asm or .cod file, or step through the disassembled code with the debugger to find out what's really going on.
Yeah, there is a switch you can set in the Project Settings for the compiler which will force it to save the ASM code. I can't remember the switch, but it's in the docs. I use that technique to find which C/C++ syntax results in the most efficient code by comparing the resulting ASM.
Dan East
i know this is an anal request, because you probably don't have time or the knowledge to do it. But i would love asm for mips too

The command line option is /FA.
Dan East
Huston, We have a problem.
I was reading through the microsoft help file and came across this piece of information ander the arm processor section using CLARM.exe.
"Inline assembly is not supported."
Does this mean that I cannot include specific arm routines in place of the originals. Is this right, and if so, how do I work my way around it?
Right. Inline assembly is not supported. You would have to create your own .ASM file, which would then generate an object file, which would have to be linked in with the rest. Of course you would have to create entire functions in ASM, and not just code snippets. I've never used ASM with eVC before, so I don't know if it is smart enough for you to just add that type of file into the project or not. Probably not, since the ASM files would be processor / build configuration specific.

Dan East
BTW, Inline Assembly is supported by the SH3 and MIPS compilers.

Dan East
What about creating a lib with those functions in it, you know, kind of like how HPC2000 users linke to Larry's lib, just making the lib work with asm. Would that work, or would it be a waste of time. Digby, I know that you said that you have been doing some programing for teh arm processor lately, has that been for programs for a ppc that uses an arm processor, or something else. I think that as of now, you would are the athority on asm programing here.
Yep, I've done ARM assembly using the assembler provided with eVC. I added the .asm files to the list of source files for the project, then added a custom build step that calls armasm.exe for those files. Works like a champ.
BTW, it's "Houston" (my birthplace) rather than "Huston". Huston sounds close to Heston, which is the guy who fought the apes in Planet of the Apes, and now is running around the country reminding people they should feel good about arming themselves. Coincidence?
How do you make a funtion in asm and then call it from a function in C. For instance
int test(int x){
x = asmtest(x);
return x
}
//ASM
//What would go here to start the function and allow me to call it from that C function?
Rob,
I can send you something via e-mail that will get you started using ARM assembly from C.
I'm at the MS DirectX Meltdown conference in Seattle today so I can't get to my code from here, but will get around to it later tonight.
Ok, thanks, I will patiently await the arival of this new code. And thanks again for all of your help.
BTW, what is the MS DirectX Meltdown conference? That wouldn't have anything to do with DirectX for the PocketPC would it. If so, don't forget to put in a good work for the HPC2000 too.

Damn, I live in Seattle but I am in Spokane right now (300 miles east of Seattle).
Digby, do you live in the Seattle area, or did you fly in?
Rob,
In the meantime, just create a .c file with a simple function and ask the compiler to generate an assembly file. That's the sure-fire way to see what works.
Randall,
I live in Kirkland.
Wow! You could have been my neighbor this entire time and I wouldn't have known it. I live just below Kirkland in a little town called NewCastle off I-405.
ahhh tahts cute. But no one on this board lives in Va.
sure... Dan does.
I know where you live, now. I should visit you sometime. My sister works for Northwest Airlines, so if I get bored one day, maybe I'll come over.

(j/k, of course.)
HEY! What happened to Digby's post about him being the crotchety old man on the block? He must have deleted it.
I thought it was rather funny.
I thought about it after I had posted it and it seemed to contribute to a problem we already have here - off topic posts, so I nuked it. I suppose you were one of the fortunate to have found it funny in the 5 mins or so it was up.