tutorial - from sm primitives to code to asm

Until our dedicated user library is in place you can post examples and modules here

Moderators: electrogear, exonerate

tutorial - from sm primitives to code to asm

Postby MichaelBenjamin on Tue Apr 21, 2009 4:16 pm

i made a small tutorial for beginners, how to put a basic math operation into a code expression and then into an asm module. basically you should atleast do this with all sm math primitives in your schematic before you release it to the public. this easily saves ~50% of cpu processing time and makes the processing more stable, less spikey.
if you want to learn more about asm, a good next step would be exonerates asm tutorial at the wiki:
http://www.synthmaker.co.uk/dokuwiki/do ... v_follower
note, that you can usually save much more cpu the bigger the code modules and the bigger your dedication to puzzling around with 8 xmm registers is.
Attachments
tut1-from_sm_primitives_to_asm.osm
(60.28 KiB) Downloaded 371 times
Last edited by MichaelBenjamin on Wed Apr 22, 2009 12:20 am, edited 4 times in total.
User avatar
MichaelBenjamin
smaniac
 
Posts: 1439
Joined: Thu Jul 12, 2007 3:26 pm

Re: tutorial - from sm primitives to code to asm

Postby Nu Audio Science on Tue Apr 21, 2009 4:19 pm

When you say all math do you mean just stream math right ?
Not green math
I'm a little confused on that

Oh and thanks for the tutorial ;)

NAS
Oh blimey
User avatar
Nu Audio Science
smunatic
 
Posts: 2236
Joined: Thu May 05, 2005 12:29 am

Re: tutorial - from sm primitives to code to asm

Postby hansdump on Tue Apr 21, 2009 4:37 pm

Nice Tutorial. Newbies will be happy!!


H.
User avatar
hansdump
essemist
 
Posts: 95
Joined: Mon Feb 09, 2009 2:26 am

Re: tutorial - from sm primitives to code to asm

Postby AlexT on Tue Apr 21, 2009 5:08 pm

Thanks for that. It's a nice tutorial and certainly helps those of us that no nothing or very little about ASM. :)
AlexT
essemer
 
Posts: 4
Joined: Sat Mar 21, 2009 10:58 pm

Re: tutorial - from sm primitives to code to asm

Postby Neptune on Tue Apr 21, 2009 6:21 pm

hansdump wrote:Nice Tutorial. Newbies will be happy!!
H.


Yes, i'm really happy about it
Thanks a lot!!
But how about a little complexer example like this
test.jpg
test.jpg (43.14 KiB) Viewed 3636 times

I tried to do it in code but failed as expected :)
Last edited by Neptune on Tue Apr 21, 2009 11:51 pm, edited 2 times in total.
User avatar
Neptune
essemilian
 
Posts: 240
Joined: Wed Dec 19, 2007 4:10 am
Location: MUC-Bavaria SO-Swiss

Re: tutorial - from sm primitives to code to asm

Postby MichaelBenjamin on Tue Apr 21, 2009 7:56 pm

yes this is a bit harder.
look at the end - something gets added to left and substracted to right (its the result of the multiplication)

so lets say the result of the multiplication is x

out1=in1+x;
out2=in2-x;

so what is x?

x is something multiplied with the dezipper, so x=y*dezip
now y:
y is left input minus something,
so y=in1-z

almost there, now its easy, because z is (left+right)/5 - note that you need the parentheses because first left and right needs to be aded, then the result is divided.
now you can write: y: in1-((in1+in2)/5)
and x: (in1-((in1+in2)/5))*dezip

and now as code:
streamin in1;
streamin in2;
streamin amount; //was dezip, but amount makes more sense
streamout out1;
streamout out2;
float mix;//temp variable to store the number that gets added and substracted

mix=(in1-((in1+in2)/5))*amount;
out1= in1+mix;
out2= in2-mix;

as an additional optimisation you can exchange divisions with constants by a multiplication with 1/constant, this saves cpu time
so instead of /5, write *0.2

mix=(in1-((in1+in2)*0.2))*amount;
out1= in1+mix;
out2= in2-mix;
User avatar
MichaelBenjamin
smaniac
 
Posts: 1439
Joined: Thu Jul 12, 2007 3:26 pm

Re: tutorial - from sm primitives to code to asm

Postby Nu Audio Science on Thu Apr 23, 2009 11:56 pm

just used this to shave some CPU out of one of my schematics, only simple stuff
I combined a balance and auto balancer and converted to ASM then converted my stereo wet/dry to ASM
cheers for this MB has given me a tiny understanding of ASM

NAS
Oh blimey
User avatar
Nu Audio Science
smunatic
 
Posts: 2236
Joined: Thu May 05, 2005 12:29 am

Re: tutorial - from sm primitives to code to asm

Postby Nu Audio Science on Mon Apr 27, 2009 7:54 pm

You know what i just done some heavy duty tests and to be honest i wish i had kept the coded versions over the ASM versions for some of my plugs because compiled the ASM is using significantly more CPU than the ASM
And that is a straight 1:1 code to ASM comparison
If i can find the coded versions in an older save on my drive i will put together a comparison OSM but i have to be honest at this point i dont think the 1:1 code to ASM thing is of any use at all
I would guess you need to heavily optomise the ASM once copied to even get close to the straight code in these examples

NAS
Oh blimey
User avatar
Nu Audio Science
smunatic
 
Posts: 2236
Joined: Thu May 05, 2005 12:29 am

Re: tutorial - from sm primitives to code to asm

Postby aliasant on Mon Apr 27, 2009 8:50 pm

Nu Audio Science wrote:You know what i just done some heavy duty tests and to be honest i wish i had kept the coded versions over the ASM versions for some of my plugs because compiled the ASM is using significantly more CPU than the ASM
And that is a straight 1:1 code to ASM comparison
If i can find the coded versions in an older save on my drive i will put together a comparison OSM but i have to be honest at this point i dont think the 1:1 code to ASM thing is of any use at all
I would guess you need to heavily optomise the ASM once copied to even get close to the straight code in these examples

NAS



Hmm. SOmething is wrong.
When I take a code block, convert it to asm and optimize it I at least save 40%. Often more. If I can combine several code blocks into one before optimizing it I will save even more.

Edit: I read you ENTIRE post now ;). Yes. Code to ASm 1:1 doesnt do anything. Not that I know of. Its not until you start optimizing it that you see the gains.

Update: Heres an example of what I did today.

I had the original RMS code that someone made. Sorry. Cant remember who.
rms-original.png
rms-original.png (31.26 KiB) Viewed 3386 times

It uses 78.2 on my machine. It is 4 channels and 2 blocks of code. Not very efficient.
Now I only needed 1 channel since Im using it for mono so I converted it to asm, ripped out 3 channels, optimized some
+ added the squareroot part in the same asm block.
The new one now uses 43 cycles so I gained 35 cycles on that.
And as others have mentioned, it makes the code run more stable.
Im planning to combine this part with several other parts and I think that will save me even more on this project :)

Here is the Mono RMS code. Maybe someone can optimize it even further?
Code: Select all
streamin in;
streamout out;
float smIntVarTemp=0.0;
int smIntVarArrayIndex=0;
float smIntVarZero=0;
float arymax=255;
float n=0;
float acum=0;
float in2=0;
float F0=0;
float F256=256;
float F1=1;
float ary[256];
movaps xmm3,F0;
movaps xmm7,in;
mulps xmm7,xmm7;
movaps in2,xmm7;
addps xmm7,acum;
movaps xmm6,n;
maxps xmm6,smIntVarZero;
minps xmm6,arymax;
cvtps2dq xmm6,xmm6;
movaps smIntVarArrayIndex,xmm6;
push eax;
mov eax,smIntVarArrayIndex[0];
shl eax,4;
fld ary[eax];
fstp smIntVarTemp[0];
movaps xmm1,smIntVarTemp;
pop eax;
subps xmm7,xmm1;
maxps xmm7,xmm3;
movaps acum,xmm7;
movaps xmm4,n;
maxps xmm4,smIntVarZero;
minps xmm4,arymax;
cvtps2dq xmm4,xmm4;
movaps smIntVarArrayIndex,xmm4;
push eax;
mov eax,smIntVarArrayIndex[0];
shl eax,4;
fld in2[0];
fstp ary[eax];
pop eax;
divps xmm7,F256;
movaps xmm0,n;
addps xmm0,F1;
movaps n,xmm0;
mov eax,ecx;
and eax,255;
cmp eax,0;
jnz end0;
movaps n,xmm3;
end0:
sqrtps xmm1,xmm7;
movaps out,xmm1;
It's never to late to be late.....
http://martinrodensjo.smugmug.com/
User avatar
aliasant
smunatic
 
Posts: 2386
Joined: Sat Dec 30, 2006 5:49 pm
Location: Sweden

Re: tutorial - from sm primitives to code to asm

Postby infuzion on Tue Apr 28, 2009 6:57 am

aliasant wrote:Here is the Mono RMS code. Maybe someone can optimize it even further?
Code: Select all
...
divps xmm7,F256;...
Maybe acum/256 = aculm*(1/256)?
Need help? First search the forum & WiKi, then post in the help forum with a clear topic, request, & OSM. Then please WiKi the correct solution. If you want my personal assistance, I charge by the hour or for an exchange of services.
infuzion
smstar
smstar
 
Posts: 6163
Joined: Wed May 04, 2005 8:02 pm
Location: Earth, USA, CO, Denver

Re: tutorial - from sm primitives to code to asm

Postby MyCo on Tue Apr 28, 2009 10:48 am

@aliasant: in the code you removed the other 3 SSE channels, so it won't work on a polyphone system
Some of my SynthMaker examples: TranceDrive, LoopDrive
or go directly to my Blog
User avatar
MyCo
smaniac
 
Posts: 1016
Joined: Mon Dec 19, 2005 1:43 am
Location: Germany

Re: tutorial - from sm primitives to code to asm

Postby aliasant on Tue Apr 28, 2009 10:52 am

MyCo wrote:@aliasant: in the code you removed the other 3 SSE channels, so it won't work on a polyphone system


Lol. Yes. I said so in my post too. I only needed a mono version.
It's never to late to be late.....
http://martinrodensjo.smugmug.com/
User avatar
aliasant
smunatic
 
Posts: 2386
Joined: Sat Dec 30, 2006 5:49 pm
Location: Sweden

Re: tutorial - from sm primitives to code to asm

Postby MyCo on Tue Apr 28, 2009 5:37 pm

damn... haven't read it... too much text 3:)
Some of my SynthMaker examples: TranceDrive, LoopDrive
or go directly to my Blog
User avatar
MyCo
smaniac
 
Posts: 1016
Joined: Mon Dec 19, 2005 1:43 am
Location: Germany

Re: tutorial - from sm primitives to code to asm

Postby aliasant on Tue Apr 28, 2009 6:44 pm

MyCo wrote:damn... haven't read it... too much text 3:)



LOL Sorry :)
It's never to late to be late.....
http://martinrodensjo.smugmug.com/
User avatar
aliasant
smunatic
 
Posts: 2386
Joined: Sat Dec 30, 2006 5:49 pm
Location: Sweden

Re: tutorial - from sm primitives to code to asm

Postby robrokken on Sun Aug 30, 2009 7:57 am

This is really great stuff! I'm just at the point of learning whats actually inside the code boxes!!!

Excellent!

Rob.
robrokken
essemist
 
Posts: 102
Joined: Wed Jul 22, 2009 5:57 am


Return to Examples

Who is online

Users browsing this forum: No registered users and 2 guests