PocketMatrix forums

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

16.16 multiplies and __int64

Sorry for all the questions recently, its been probably 8 years since I last had to do fixed point and I guess I've grown accustomed to having accurate 3d calculations that floats provide.

I'm using 16.16 fixed point as my standard element for vectors, matricies, etc and had originally be using intels GPP libraries. Now I'm trying to free myself from GPP, and am writing my own fixed point stuff.

I wrote a fixed point macro:
#define FixedMUL(x,y) (int)((__int64(x)*__int64(y)) >> 16)

And it seems to not only give the same results of the GPP multiply function (gppMul_16_32s), but its quite a bit faster. Is the int64 a native type on an Xscale or something? I originally figured the macro would end up being a bit slow, but seeing as I was ok with the GPP's performance, this macro seems to be more than fast enough.

My question is, is this the best way on an Xscale? What about a strongarm processor?
Both the XScale and StrongARM have both signed and unsigned native 64-bit multiplies (i.e. two 32-bit operands and a 64-bit result).

I never actually looked at GPP much, but if it's calling it a function then that's why it's slower.
GPP's call is certainly a function, and I know there is function call overhead involved.

That is part of what makes me wonder what I'm missing..seeing as gpp could have just defined it as a macro like they did other functions...what are they doing that needed more? The GPP function actually seems to have less range, and returns -1 as a result on big multiplies. Strange.
It would seem to be doing some overflow checking then, this would take some time I guess
That is part of what makes me wonder what I'm missing..seeing as gpp could have just defined it as a macro like they did other functions...what are they doing that needed more?


You simply cannot access the 32x32->64 bit multiply instruction from a high level language like C. Part of the reason is, that result types have to match target types for each operator.
So you are left with either casting the source operands to 64 bit or using assembly.

Regarding your question, i do not see a problem with defining a macro the way you did.

Tala
Tala wrote:You simply cannot access the 32x32->64 bit multiply instruction from a high level language like C.


Oh sure you can. What he wrote works, doesn't it? I use this all the time on Symbian GCC and EVC4:

Code:

int32_t FixedMul( int32_t a, int32_t b )
{
int64_t temp = int64_t(a) * int64_t(b);
return int32_t( temp >> 16 );
}

int32_t FixedDiv( int32_t a, int32_t b )
{
int64_t temp = int64_t(a) << 16;
return int32_t( temp / b )
}


PS: code typed from memory, I don't have it right here.
I just took a look at this within the last week.

This 64 bit multiply:

Code:

int n, i;
__int64 n64;

n64=n;
n64*=i;
n64>>=16;
n=(int)n64;


Only generates 3 or 4 more instructions than the 32 bit version:

Code:

int n, i;

n*=i;
n>>=16;


Not too shabby.

Dan East
The code does seem to be quite fast using eVC's compiler (and it works fine so its using a 64bit variable of some sort).

Next week I'll be building all this code on linux using gcc for ARM, so I guess I'll find out if the compiler I have to use works as well (using long long).
You shouldn't see that much of a performance difference.

Code:

int test32(int n, int m) {
return (n*m)>>16;
}

mov r2, r0
mul r0, r2, r1
mov r0, r0, asr #16
mov pc, lr

Code:

int test64(int n, int m) {
return (int)((__int64(n)*__int64(m))>>16);
}

mov r3, r0
smull r2, r0, r3, r1
mov r0, r0, lsl #16
orr r0, r0, r2, lsr #16
mov pc, lr


Dan East
Yep. And if you use inline functions (like you should), this code will be interleaved/scheduled with whatever is around, making it even less of a hit.
Thierry wrote:Yep. And if you use inline functions (like you should), this code will be interleaved/scheduled with whatever is around, making it even less of a hit.


Converted it all to inline functions last night after I made the post. :wink:

Thanks for all the info.
Try changing the compiler optimization settings to inline any suitable function. That gives me several more FPS in Varium.

Also there have been functions I declared as inline that the compiler did not inline. I had a hunch that it wasn't because the FPS did not improve, so I checked the ASM to see what was going on. I had to rewrite the function (about 10-12 lines) as a macro to avoid the function call overhead. That gave a noticable performance improvement.

With your small function it ought to be able to inline it, but it's good to keep an eye on the compiler to be sure it does.

Dan East
It is also good to make sure that force-inlining a function (my using a macro for example) is actually improving the overall performance of your app.

There is this common misconception that inlining is good all the time and that inlining = faster code. Not always true.
The reason I wrote them as macros first was to make sure they were inlining. I also wrote everything as inline functions and I have an ifdef to swap back and forth between the two so that I can verify at any point in time the compiler is doing as it should (since I'll be using a few compilers on this code).

If one compiler ends up not acting like it should I can always set it up to use the macro versions rather than the inline function versions.

My fixedpoint test function that tests to make sure all the code works also times it so its easy to make sure the performance stays the same (and at least with eVC, the timer seems to be nearly exactly the same between the two versions, give or take a small margin of error).

Ive not tried it with GCC yet, that'll be next week when I try and move all this code over to the phone platform.
see: __forceinline
Dan East wrote:You shouldn't see that much of a performance difference.

Code:

int test32(int n, int m) {
return (n*m)>>16;
}

mov r2, r0
mul r0, r2, r1
mov r0, r0, asr #16
mov pc, lr

Code:

int test64(int n, int m) {
return (int)((__int64(n)*__int64(m))>>16);
}

mov r3, r0
smull r2, r0, r3, r1
mov r0, r0, lsl #16
orr r0, r0, r2, lsr #16
mov pc, lr


Dan East


Dan, I get something just a little different when I compile with evc3...

Code:

00004 e1a0b000 mov r11, r0
00008 e1a02fc1 mov r2, r1, asr #31
0000c e1a03fc0 mov r3, r0, asr #31
00010 e002029b mul r2, r11, r2
00014 e00b0193 mul r11, r3, r1
00018 e082300b add r3, r2, r11
0001c e082b190 umull r11, r2, r0, r1
00020 e0010190 mul r1, r0, r1
00024 e0830002 add r0, r3, r2
00028 e1a03800 mov r3, r0, lsl #16
0002c e1830821 orr r0, r3, r1, lsr #16


I've written most of our engine in ASM, so I don't actually have a multiply call like the above... but it does give me a little scare to think that the compiler is *that* bad. What compiler did you generate the above code with???
Probably EVC4 (or perhaps Intel C++). You don't want to use EVC3, it is crap.
Oh sure you can. What he wrote works, doesn't it? I use this all the time on Symbian GCC and EVC4:


Dan, I get something just a little different when I compile with evc3...


It seems that EVC4 has a nonstandard optimization in place.
In general multiplying 2 64 bit numbers should produce the code rcp posted above and thats the reason i said there is no way to access the 32x32->64 multiply directly.
In this particular case i was wrong (depending on the compiler), infact i am quite surprised that the compiler does this optimization.

Tala
I get this with eVC 3:

Code:


int test64(int n, int m) {
return (int)((__int64(n)*__int64(m))>>16);
}

mov r4, r0
mov r3, r1
mov r0, r3, asr #31
mov r2, r4, asr #31
mul r0, r4, r0
mla r1, r2, r3, r0
umull r2, r0, r4, r3
add r0, r1, r0
mov r1, r0, lsl #16
orr r0, r1, r2, lsr #16
ldmia sp, {r4, sp, pc} ; ldmfd


So eVC 4 certainly seems to be doing a better job.

However, I get lower FPS with Varium when built with eVC 4 compared to eVC 3 (using the same compiler optimization settings).

Dan East
Nobody tries the intel compiler ?
Picard once mentioned that GCC does a good job too.
It might be worth a try.
StephC wrote:Nobody tries the intel compiler ?


For this specific code, it has the same behavior.

- HM