PocketMatrix forums

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

Any ideas on this problem?

I'm trying to do some ASM for my fixed point multiply. I'm using 16.16, so I need to do some trickery with the resulting 64 bit number. Here is my if-defd C macro, and ASM code:

Code:

#ifdef ARM
extern "C" inline long fmul(long f1, long f2);
#else
#define fmul(f1, f2) (long) (((__int64) f1 * (__int64) f2) >> 16)
#endif

// ASM

AREA FIXEDPOINT,CODE,READONLY
EXPORT fmul

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



So, as expected the C macro performs a bit slower than the ASM code. However, I'm getting some weird artifacts in my zbuffering when I compile for ARM at different camera Z positions. I swear, the only way to get rid of the artifacts (disappearing polys) is to turn off the ARM ASM routine and use the macro. FPS drops by 2 in my particular test (1000 polygon flat shaded object), but it looks great at all camera Z positions.

I finally came up with this when I noticed it was working fine on the x86 emulator, but not PPC. So I compiled for ARM with the macro and all was fine... I just don't get it. Is my ARM ASM fmul somehow less accurate than the slower library call?

Chris
I don't have a clue what's going wrong, it looks ok.

But what you can do to find out yourself, is to turn your c-macro into a function, calcuate the result, calculate the same result using your asm-function and then compare the two values. If you have different results log the parameters into a file and exit the program.

That way you have a numerical example where one version fails, and can trace down what exactly what's going wrong.

Nils
torus wrote:
I don't have a clue what's going wrong, it looks ok.

But what you can do to find out yourself, is to turn your c-macro into a function, calcuate the result, calculate the same result using your asm-function and then compare the two values. If you have different results log the parameters into a file and exit the program.

That way you have a numerical example where one version fails, and can trace down what exactly what's going wrong.

Nils


Yeah, that was going to be my next step. I'll be logging the differences and line numbers where they occur the next chance I get... Just hoping there was something immediately wrong with my ASM :).

Chris
That's my guess..

The function looks nice that way. It will most probably a stupid error. It's been quite a while since I looked at arm assembler, but maybe the function does correct calculations but you forgot to save registers on stack or so?

You use R2 and R3, maybe those aren't temporaries.. I don't know, but worth checking.
torus wrote:
That's my guess..

The function looks nice that way. It will most probably a stupid error. It's been quite a while since I looked at arm assembler, but maybe the function does correct calculations but you forgot to save registers on stack or so?

You use R2 and R3, maybe those aren't temporaries.. I don't know, but worth checking.


Actually, I was going to post about that, but didn't. My original asm code saved the stack and restored it at the end. However, this turned out to be slower than the C macro! Seriously, my FPS dropped by about 2-3 when I stored/loaded those. I assume that is pretty strong overhead for every multiply. But am I damaging anything? I don't know :).

Bringing this back to my problem, saving/restoring the stack produced the same "error" I'm seeing now. So that doesn't seem to be my problem.

Hmph. I'm stumped. But I'm at work and can't do crap about it now :)

Chris
i dunno about your routine, but here is mine.

Code:

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


I don't think these are different, but I do know that mine works.

-rcp
rcp wrote:
i dunno about your routine, but here is mine.

Code:

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


I don't think these are different, but I do know that mine works.

-rcp


Well, I tried yours on lunch break and it does the same thing. I guess it doesn't surprise me, because they appear to be the same code really.

Well crap. This is going to be an annoying one to fix :(. Thanks for the help guys.

Chris
You do a signed long multiply and then a logical shift right... clue?

(Might not be - I'm tired).
This is really strange. I just really don't think that the ASM version of your mul function is incorrect. I am wondering if you have some type of pointer bug that is 'fixed' by the 'C' code. I've had many a bug 'move' when I try to track it.

However, if there is something wrong with the ASM mul function, PLEASE make sure you post that information here. :)

-rcp
refractor wrote:
You do a signed long multiply and then a logical shift right... clue?

(Might not be - I'm tired).


Yeah I was just thinking about that actually. I'll have to take a look next chance I get. Can't hurt to try :).

rcp - more than likely its something stupid with my code somewhere. BUT, if for some reason I find something different, you'll be the first to know :).

Chris
Ok, ignore the comment about the shift - that's not the problem.

I just ran this on one of my StrongARM Linux boxes:
Code:

#include <linux/types.h>
#include <math.h>
#include <stdio.h>
#define fmuld(f1,f2) (long) (((__s64)f1 * (__s64)f2)>>16)

long fmull(long,long);

int main()
{
long count = 0x100000;
long x;
long y;
long resultd;
long result;

srandom(1234);

while(count--!=0)
{
x = random();
y = random();

resultd = fmuld(x,y);
result = fmull(x,y);

if(resultd!=result)
{
printf("Whoops, %d * %d failed - ARM said %d, C said %d",x,y,result,resultd);
break;
}
}


exit(0);
}

long fmull(long f1, long f2)
{
long result, tmp;

asm volatile("
smull %0,%1,%2,%3
mov %0,%0,lsr #16
orr %1,%0,%1,lsl #16
"
: "=&r" (tmp), "=&r" (result)
: "r" (f1) ,"r" (f2));

// %0 = tmp, %1 = result, %2 = f1, %3 = f2

return result;
}


.. and it passed with flying colours (I even did several variant to make sure that positive and negative values were being thrown up for it to miltiply).

So, my next suggestion is for you to post a dump of the disassembly with the "problem" and we can take a look at it and see what's going wrong.

Cheers,

Ref.