PocketMatrix forums

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

Optimize this!

Hi,

I have been dealing with optimizing a part of main interpreter loop lately and I have come to the conclusion that the switch operator is not handled too well with eVc and ARM code.

On the PC the same piece of code gives me a great speed boost but on the PPC it's slower.

Code:
while (!endproc)
{
switch (bc->code)
{
case 1: // pushint
{
Push(bc->i);
bc = bc->next;
continue;
}
case 2: // pushstr
{
}
...
}
}

I am thinking of changing this code to something like this, I don't know if this is feasible in C.

Code:
while (!endproc)
{
if (!Addr[1])
{
Addr[1] = &C;_1;
Addr[2] = &C;_2;
...
}

goto Addr[bc->code];

C_1: // pushint
Push(bc->i);
bc = bc->next;
continue;
C_2: // pushstr
...
}


Or is there any other way of doing this in a fast way on ARM.
You could try using a function table, with one function f(bc) for each VM instruction. If your function bodies are simple enough (as it looks from the example), the compiler won't need to generate a function prologue/epilogue, so effectively both the call as well as the return become simple branch instructions.

If you turn this into assembly language, the next step would be to use threaded code, i.e. the instruction code value bc->code would actually by the jump offset directly.

And from there you'd use a JIT.

- HM
A switch statement like that should (with some provisos) compile to a branch table even with eVC++. It probably depends on the "coverage" - how many cases are there? Maybe try putting a "case 0" in there and see if that helps the compiler out...
Hi,

I use to store all VM opcodes within their own functions and call them using:

Code:
while (!endproc)
{
(bc->opcode) ();
bc = bc->next;
}


This was pretty fast however I would have liked to stay away from calling functions every cycle.

hm: Could you elaborate a little more on the functions table you are talking about. I am not sure I understand well, I am a little slow! :) Talking about assembly, how can I use assembly instructions within my C code? I tried using asm statement but I think it didn't work with eVc, only with Visual Studio it worked.

refractor: I use about 40 cases within that switch statement. I start with case 1. What would a case 0 do to the compiler?

I looked at the code generated by the compiler for the switch statement and it was pretty long. There seem to be a lot of overhead.
kornalius wrote:Hi,

I use to store all VM opcodes within their own functions and call them using:

Code:
while (!endproc)
{
(bc->opcode) ();
bc = bc->next;
}


This was pretty fast however I would have liked to stay away from calling functions every cycle.



So you have been using threaded code already. (with the function table I meant just storing an index in your instruction node, and having a lookup table to convert index to function address). Have you checked the code that is being generated for those calls (esp. if there is any prologue/epilogue in the actual functions)? As said before, if the functions are simple enough (leaf functions, not using many variables), then calling such a function is about as expensive as a branch. Which, BTW, is slightly different behavior for x86, which might explain why the switch is faster on the PC, but makes things slower on the PPC.

- HM
The code on the PPC for calling the functions is quite small and looks like a simple branch.

Btw, is there a way to speed the pointer dereferencing? bc->opcode?
As said before, if the functions are simple enough (leaf functions, not using many variables), then calling such a function is about as expensive as a branch. Which, BTW, is slightly different behavior for x86, which might explain why the switch is faster on the PC, but makes things slower on the PPC.


Functions should be slightly more expensive as a branch, since register allocation is usually broken at function boundaries.
Anyway, i fail to see, how the original switch statement would be slow. I would have guessed, that EVC generates a hash table to lookup the jump addresses, which should be very fast, even faster than using an array of function pointers.*

Btw SNES9x uses an array of function pointers for each CPU opcode. That no bad solution but it does not allow to hold the machine state in registers in between 2 consecutive opcodes. In theory using a switch statement instead of function pointers could help here as well.**

Tala

*That depends mostly on the used hash function, which may be not that easy to avaluate.

**In reality it is not that easy sadly, since SNES9x switch the emulation to different processors almost each opcode.
Evc generates a hash table with the jump addresses but the code to get the address is quite longer than just calling a __fastcall function pointer. Strange but in practice it is slower in the end. I agree with you that theorically it should be faster.
If you want to do arm assembler, you have to write the asm code in a seperate file and compile/link into your project. There are threads in the developer forum that explain how to do this. For one project I'm working on, I use asm to do a fast switch statement with the following code (assuming r0 has the switch value and is zero based, and the distance to the case statements do not exceed a range of 64k bytes from the add pc, pc, r2 statement):

Code:

add r1, pc, r0, lsl #1 ; this will load the current pc address,
; plus an offset into a list of 16 bit values (r0 lsl #1 = index*2)

ldrh r2, [r1, #4] ; this will load a relative offset from the list.
; the #4 is to adjust for reading pc above and skip over
; the next arm opcode
add pc, pc, r2 ; jump to code by adding relative offset to pc register

; list of relative offsets to case statements

DCW xxxxxx ; distance in bytes from top of list to case 0 code - 4
DCW xxxxxx ; distance in bytes from top of list to case 1 code - 4



I know this is confusing, but this is exactly the kind of code eVC produces for switch statements. The difficult part about this method is generating the list of relative offsets, as this technique does not use absolute addresses to each case statement. If you need more info, email me at sgalatis@gmail.com
Thanks for the information. Here is the code eVc 4 generates for my switch statement.

Code:
2207A970 ldr r0, [pc, #0x5C4]
2207A974 ldr r1, [r0]
2207A978 ldrb r0, [r1, #0x2C]
2207A97C str r0, [r11, #-0x578]
2207A980 ldr r1, [r11, #-0x578]
2207A984 cmp r1, #0x2F
2207A988 bhi |start+3E88h (2207e7e8)|
2207A98C ldr r1, [r11, #-0x578]
2207A990 mov r0, r1, lsl #2
2207A994 add r0, r0, pc
2207A998 ldr r0, [r0, #4]
2207A99C add pc, pc, r0
2207A9A0 streqh r0, [r0], -r12
2207A9A4 andeq r0, r0, r8, ror #1
2207A9A8 andeq r0, r0, r8, lsl #4
2207A9AC andeq r0, r0, r0, lsl #6


2207A9A4 is the start of the jump list. That is quite some lengthy code to do what you can do in a few opcodes.

Do you think there is an easy way to generate the jump list myself and use your asm switch code?
And, is there a way to optimize this code in assembly to call a void __fastcall func () type procedure?

This is what eVc 4 generates:

Code:
10275: (bc->opcode) ();
2207AA60 ldr r0, [pc, #0x4D4]
2207AA64 ldr r1, [r0]
2207AA68 ldr r2, [r1, #0x28]
2207AA6C mov lr, pc
2207AA70 mov pc, r2
well considering your probably using a class object for the switch, thats what much of that asm code before the actual switch code is doing, loading up pointers and values, as well as making sure the input case value is in the case list (thats what the bhi is for). My code does no checking of the input value, and its assuming r0 already has the switch value. It did not show where the switch value comes from or any preprocessing before even doing the switch =)

as for the fastcall..dunno, but that looks like what I would do if I were using class objects
The difficult part about this method is generating the list of relative offsets, as this technique does not use absolute addresses to each case statement.
Personally I wouldn't bother screwing around with all of that 16-bit relative offset stuff. Something like:

Code:
cmp r1,#8
addls pc,pc,r1,lsl #2
B |$BAD_JUMP$|
B |$Function0$| /* Case 0 */
B |$Function1$| /* Case 1 */
B |$Function2$| /* Case 2 */
B |$Function3$| /* Case 3 */
B |$Function4$| /* Case 4 */
B |$Function5$| /* Case 5 */
B |$Function6$| /* Case 6 */
B |$Function7$| /* Case 7 */
.. is much cleaner IMHO (and keeps stuff in the instruction cache and not the data cache).
I don't use a class object but a pointer to a structure like bc->code. That must be why.

Is there an easy way to mix an assembly code with C functions. ie: I would like to call my C functions from the assembly code just like refractor mentionned.
nope...no inline asm. I wish evc would support it, but i suppose the different architectures evc supports kinda makes that a difficult option
refractor wrote: is much cleaner IMHO (and keeps stuff in the instruction cache and not the data cache).


Ill have to try that technique, but I think we were trying to avoid a branch in the switch routine...lemme see how that works out
Is there an easy way to mix an assembly code with C functions. ie: I would like to call my C functions from the assembly code just like refractor mentionned.

Sure, just import the function's name into your assembler file (bearing in mind that the names are mangled).

I.e. it'll end up being something like:

Code:
IMPORT |?my_function0@@BLORGLE@@XX@Y|
IMPORT |?my_function1@@BLORGLE@@XX@Y|
IMPORT |?my_function2@@BLORGLE@@XX@Y|
IMPORT |?my_function3@@BLORGLE@@XX@Y|
IMPORT |?my_function4@@BLORGLE@@XX@Y|
IMPORT |?my_function5@@BLORGLE@@XX@Y|
IMPORT |?my_function6@@BLORGLE@@XX@Y|
IMPORT |?my_function7@@BLORGLE@@XX@Y|

...

cmp r1,#8
addls pc,pc,r1,lsl #2
B |$BAD_JUMP$|
B |?my_function0@@BLORGLE@@XX@Y| /* Case 0 */
B |?my_function1@@BLORGLE@@XX@Y| /* Case 1 */
B |?my_function2@@BLORGLE@@XX@Y| /* Case 2 */
B |?my_function3@@BLORGLE@@XX@Y| /* Case 3 */
B |?my_function4@@BLORGLE@@XX@Y| /* Case 4 */
B |?my_function5@@BLORGLE@@XX@Y| /* Case 5 */
B |?my_function6@@BLORGLE@@XX@Y| /* Case 6 */
B |?my_function7@@BLORGLE@@XX@Y| /* Case 7 */


(kornalius and I discussed this in an IM session and basically the function pointer method is practically equivalent to the hand-coded branch table for this implementation so I suggested it was left in C).

Using the same kind of imports/exports you can even replace class functions with optimized assembler equivalents. :mrgreen: