SynthMaker

Code module tip: faster code when avoiding arrays and loops

Archive · 2006 3 old posts

From the original SynthMaker forum (2004–2014), with its members’ names and dates.

rl ·

when trying to make some of my code more efficient I made the interesting observation that sometimes it's far more faster not to use arrays and loops but use good old linear spagetti code instead. I could measure up to 8 times less clock cycles when avoiding loops and arrays:

As an example have a look at this nice and clean code:

float i; float cos[16], sin[16]; ...

i=0; loop (16){ cos[i]=1; sin[i]=0; i=i+1; }

as an example the cos[i]=1; assignment is translated into:

//AssignmentArray> sLeft=F1 push eax; mov eax,smIntVarArrayIndex[0]; shl eax,4; fld F1[0]; fstp cos[eax]; mov eax,smIntVarArrayIndex[1]; shl eax,4; add eax,4; fld F1[1]; fstp cos[eax]; mov eax,smIntVarArrayIndex[2]; shl eax,4; add eax,8; fld F1[2]; fstp cos[eax]; mov eax,smIntVarArrayIndex[3]; shl eax,4; add eax,12; fld F1[3]; fstp cos[eax]; pop eax;

there are no SSE instruction used!

------------------------------------

Now have a look at this ugly code:

float cos00, sin00; float cos01, sin01; ... wadda yadda; ... float cos14, sin14; float cos15, sin15;

cos00 = 1; sin00 = 0; cos01 = 1; sin01 = 0; ... ... cos13 = 1; sin13 = 0; cos14 = 1; sin14 = 0; cos15 = 1; sin15 = 0;

where a single cos00 = 1; assignments simply transforms into:

//Assignment> sLeft=F1 movaps xmm0,F1; movaps cos00,xmm0;

The compiled assembly is (in this example) not only smaller but also using SSE code and executes much faster. If there're more instructions within the loop this might change, so always check your code for improvements.

sambean ·

It can be pretty ugly when using arrays in the code.. Note: you've also got to convert the 'i' from float to integer... This method though wouldn't be much use with a variable array length though..

Just thinking out loud here: what the lengthy asm code is doing, is basically sorting out the interleving for the arrays, which have to store all the 4 SSE channels in a single array.. If we had an integer 'index' SSE data type in SM code, which basically had the interleving offset built into it at initialization, and you could only add/subtract to it and there were special operations for looping, the use of arrays in SM could be a lot faster..

Sam

doug4350 ·

Hi sambean,

You may already know this, but for the rest of the gang, what you've done is what's known to optimizing compilers as "unrolling a loop". By repeating the code within the loop the # of iterations of the loop eliminates the overhead of the loop itself. AFAIK, it can only be done if the # of iterations is constant. Also, the size of the code increases linearly for each line of code within the loop.

Just be careful when cutting/pasting the iterations. It's very easy to induce a bug like this (especially after staring at code for 10+ hours):

push eax; mov eax,smIntVarArrayIndex[0]; shl eax,4; fld F1[0]; fstp cos[eax]; mov eax,smIntVarArrayIndex[1]; shl eax,4; add eax,4; fld F1[0]; // <- bug fstp cos[eax];

Just my "too sense" trivia on a Sunday morning 😉

Aloha, Doug

Wrote something here? To have a post removed or your name hidden, write to [email protected].

Before your first post

A few rules keep this a friendly place. You agree to them once.

Shown next to what you post, here and in the app. It doesn’t have to be your real name: 3–24 lower-case letters, digits or _ (capitals become lower case, spaces become _).

    Then your post goes up straight away.

    Delete this reply?

    Open it in SynthMaker?

    It goes on the track you’re editing there, in place of that track’s sound, and into My sounds. Your other tracks stay as they are.

    Want to keep that track’s sound? Start a new song in SynthMaker first (Song › New song), then open this.

    Report this reply?

    Only the developer reads reports. The person isn’t told who reported them.

    What’s wrong with it?

    Block this person?

    You won’t see their posts and replies here any more. They aren’t told, and you can unblock them at any time.

    People you’ve blocked

    Their posts and replies are hidden from you. Unblock someone to see them again.

    The community rules

    A few rules keep this a friendly place. Everyone agrees to them once, before their first post.