PocketMatrix forums

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

It's ASM CODING TIME!!!

Ok, in this thread, I will periodicly post short functions (such as the one below), that I believe would be quicker in asm. Now, it's time for the great asm coders at pm, to show their stuff, and prove me right (hopefully).

This first funtion multipliest 2 UINT32 (unsigned long ints) together.

Code:
static void multu(UINT32 v1,UINT32 v2)
{
UINT32 v1h,v1l,v2h,v2l;
UINT32 ll,hl,lh,hh,mid,mid2;

v1l = (UINT16)v1;
v1h = ((UINT32)v1)>>16;
v2l = (UINT16)v2;
v2h = ((UINT32)v2)>>16;

ll = v1l*v2l;
hl = v1h*v2l;
lh = v1l*v2h;
hh = v1h*v2h;
/*
|ll|ll|
|lh|lh|
|hl|hl|
|hh|hh|
--------------
| mid |
|carry|
*/
mid = hl+lh;
if (mid<hl) hh+=0x10000;
mid2 = mid + (ll>>16);
if (mid2<mid) hh+=0x10000;
rLO = (UINT16)ll|(mid2<<16);
rHI = hh+(mid>>16);
}


Although that may be efficient c (it still looks like it could be reduced, I bet that we can get more efficient arm asm, so have at it, and good luck.
I have some assembler questions too:

macro
ReadByte
mov r0, r1, lsr #8 ; Get page offset from Read[pc>>8]
add r0, r0, #0x100
ldr r0, [r8, r0, lsl #2]
cmp r0, #0
ldrneb r0, [r0, r1] ; If the page offset is not null, get byte
moveq r0, r1
bleq DazeRB ; If the page offset is null, call handler
mend

The question is just whether ARM has large issues pipelining and pipline stalls? (Hence I put in the NE and EQ opcodes, instead of jumping over them etc.)

My next assembler routine will have this as a conditional though...:
add r0, r0, r1 ; r0 = page offset+pc
ldrb r1, [r0, #1] ; r0 = little endian word at r0
ldrb r0, [r0]
orr r0, r0, r1, lsl #8

Is it worth doing all that as 'ne'? How many opcodes in the conditional before it's just as bad jumping over them and stalling the pipeline?
I know how to find a square root really easily and fast, if you want to know. You can easily program it in ASM. I know that for a fact, although I didn't actually do it myself, I did check to make sure that the right operators are in ASM.

It'd probably be easiest to tell you how to do it...

Actually, this specific square root thing isn't necessarily that useful, but if you have to find out some other way to do another function fast by getting it down to the simplest functions, ask me and I'll think it out and see if I can figure out how to do it. I like that sort of stuff. I'm a math kinda guy. And I'm bored.
Note: I'd stand much more of a chance of helping if somebody would PLEASE fix the damn code tags to use SPAN and not DIVs. Reading code at one symbol per line is pretty useless. :(


fused:

It doesn't have /large/ issues with pipeline stalls, but your code there does seem to use the same target and destinations registers all over the place.

In general:
don't use the result of an instruction in the next instruction
(or maybe the next two - look at the StrongARM datasheet - the result cycle counts are in there) - you're stalling the pipeline all over the place. For the same reason, don't use a target register as an address register (it might be alright, actually, but it's still not nice):
LDR R0,[R0]

DO use the conditional execution - "skipped" instructions will lose you only one cycle, IIRC, no matter what they are. Having said that, branches on the StrongARM are pretty fast.


ROB:
ARMv4s have a 32x32 bit multiply /instruction/. UMULL. If only you could embed assembler in eVC++, eh.
yeah, but i know you can with the ARM SDT, so we will try to compile it through the ARM SDT, I know it also allows for inlining in C. which is good :)

ROB:
ARMv4s have a 32x32 bit multiply /instruction/. UMULL. If only you could embed assembler in eVC++, eh


Can't you?
Not for ARM, no. Only full functions only can be linked in.

You can't inline ARM assembler within a C function, for example, but you can compile the entire function from an assembler (or compile a C function to ARM and tweak the assembler source and keep it as assembler).
... ROB why are u posting that function up though? thats from the interpreter, and... the interpreter will be done away with really soon...

????? 8O
angedelamort · 26 Jun 2002, 13:30 UTC
jadam :
... ROB why are u posting that function up though? thats from the interpreter, and... the interpreter will be done away with really soon...


I think He want to know if there a fastest way to do this than the compiler... I think any compiler is able to optimize multiplication :) The only thing i'm doing to help it is trying to find magic number (2 ^ n) to be able to shift lol
jadam wrote:
... ROB why are u posting that function up though? thats from the interpreter, and... the interpreter will be done away with really soon...

????? 8O

Yes, but compilers took time, and at the time, I had no idea how quickly we would have a compiler in working condition (still not completely sure), so it never hurts to ask for a little help.
Why don't you just write the whole dang function in ASM, then?

Do you guys need to know how to do anything mathematically "special" like sine or tangent or square-roots or something, anything, like that? I want to help!
Oooh, ooh! I'm good at math! I'm good at math! I can help too!
Down boy. Gooood doggyyy. :D
you guys are funny.. I might register here if I ever get time to do any coding.
Shut up, Moose. You suck. I'm just bored, okay? I offered to help. What's wrong with that?
Woh, calm down ... nothing wrong with wanting to help, that's why I wanted to too.
is there some kind of benchmark tool? so we can test ARM code..

or maybe someone could code one ?
angedelamort wrote:
jadam :
... ROB why are u posting that function up though? thats from the interpreter, and... the interpreter will be done away with really soon...


I think He want to know if there a fastest way to do this than the compiler... I think any compiler is able to optimize multiplication :) The only thing i'm doing to help it is trying to find magic number (2 ^ n) to be able to shift lol


uhh... a dynamic recompiler is the fastest for emulation :)