SynthMaker

abs() in code component

Archive · 2005 11 old posts

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

Borogove ·

Code component doesn't seem to recognize abs(). That can be done with negate and max pretty easily, but it would be high on my list for additional builtin functions.

Shifrin ·

Borogove wrote: Code component doesn't seem to recognize abs(). That can be done with negate and max pretty easily, but it would be high on my list for additional builtin functions.

Yep, there are a lot of time-saving functions that would be nice to see added (personally I'd love floor/ceiling/round and abs - there are simple workarounds but they obfuscate the code!).

Shif.

infuzion ·

I always wondered if adding a bunch more functions it would slow down the Code output....

Shifrin ·

I wouldn't have thought it'd slow down that much by having new functions. I believe SM uses the FPU to round numbers for example, at the moment I had to add code in order to force it to round down - but the FPU can do that automatically by setting a flag.

If I wanted to calculate x = abs(x), I'd much rather just write x = abs(x) and have it converted to whatever, x = (x < 0)&(-1*x) + (x >= 0)&x, then the code would be much easier to follow.

Perhaps we could have some sort of #define mechanism for things like that in the mean time?!

Oh yeah, you could also calc abs(x) with only one comparison like, x = x - (x < 0)&(2*x) 😉

Shif.

stefan ·

Actually you can do an abs() on floating point numbers by just nulling the sign bit.

Can't be done in the code component atm though, but should be fairly simple to add support for (hex defined variables would be good for such a thing).

Borogove ·

infuzion wrote: I always wondered if adding a bunch more functions it would slow down the Code output....

You don't pay for what you don't use. It would take some tiny fraction of a second longer for the Code component to *compile* code if it had to recognize and translate abs() into the equivalent machine code, but the runtime would be optimal.

Shifrin ·

stefan wrote: Actually you can do an abs() on floating point numbers by just nulling the sign bit.

Can't be done in the code component atm though, but should be fairly simple to add support for (hex defined variables would be good for such a thing).

If there's a hard way to do something I'll usually find it 😉

But as you say it's not doable in the Code Component ATM 🙂

Shif.

Borogove ·

Shifrin wrote: stefan wrote: Actually you can do an abs() on floating point numbers by just nulling the sign bit.

Can't be done in the code component atm though,

But as you say it's not doable in the Code Component ATM 🙂

Best I managed was:

foo = max( foo, 0-foo );

...which probably turns into a couple of moves, two subps and a maxps, not too bad.

Shifrin ·

Borogove wrote: Best I managed was:

foo = max( foo, 0-foo );

...which probably turns into a couple of moves, two subps and a maxps, not too bad.

Sweet 😉

Shif.

sambean ·

Interesting..

In the audio section of the waveshaper, I use the method stefan suggested using asm- creating a mask for all the magnitude bits of the float number(01111.. in binary or 2^31-1 as an int), then '&' the mask with the variable, so you disregard the sign bit..

As Stefan suggests if we could define this mask in the code component we could do it entireley in code

Here's a workaround for creating the mask until we can define hex/int values..

streamin in; streamout abs;

float absmask;

stage(0) {

// we want to define absmask to be: // 01111111111111111111111111111111 // // using bitwise 'or' (|) the bits of these 3 values cover the range:

absmask = 3.4e38|0.999999|0.1; }

stage(1) { abs = in&absmask; // keep in's magnitude only }

😀

Shifrin ·

Woah, that's great Sam, the things I learn here are very useful to me 😉

Thanks,

Shif.

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.