Page 2 of 5

Re: PositionSyncedStepLFO - help?

PostPosted: Sun Dec 02, 2012 12:30 pm
by trogluddite
Looking good - you're picking up DSP coding pretty quick there!.
Nubeat7 wrote:tricky thing was the " round down to int for stream"

Rounding is a bit of a funny one in code, the function you need is 'rndint(x)'. The trouble is that it uses the standard rounding built into the CPU - and because PC's used to be used mostly by businesses, that's usually what is known as 'bankers rounding'. This is a form of 'round to nearest' except that exactly half-way values round alternately up and down. e.g. rndint(6.5) = 6, but rndint(7.5) = 8.
To always round down, you first have to subtract a number which is nearly 0.5, but not quite - which for our 32bit float numbers ends up being 0.49999997 (six 'nines' there); that's the number that's less than 0.5 by only one bit, so as close as you can get without actually being 0.5.

So to always round down...

Code: Select all
y = rndint(x-0.49999997);


The modulus function does work as expected, so you can still do...

Code: Select all
bar_loop = bars % loop_length.

Re: PositionSyncedStepLFO - help?

PostPosted: Sun Dec 02, 2012 3:33 pm
by Nubeat7
thanks trog, hmm about the rounding down, i tried it with your way too but it didn`t work, i think its because you get values less than the int to be rounded.... am not really shure why it don`t work, the thing is when it should jump to 16 (4bars) the impulse is coming at 20 not at 16....

so the way how it works (don`t ask me why :)) is to round the value -> divide the not rounded value with the rounded value -> if it is less than 1 subtract 0.5 from not rounded value and round it ! so it is always rounded down when it would be rounded up but you never round a value which is less than the int where it should be rounded to

so i needed to code it like this:
Code: Select all
streamin floating;
streamout rndintfl;

float x,y,z,sub,rnd;

x = rndint(floating);
y = floating/x;
z = y < 1 & 1;
sub = z*0.5;
rnd = floating-sub;
rndintfl = rndint(rnd);


Re: PositionSyncedStepLFO - help?

PostPosted: Sun Dec 02, 2012 8:32 pm
by Nubeat7
after finishing the code, this is the the finished version, from my point, notice that it only works correct in 4/4 atm! you can also find the codes for the "special rndint" to round streams down or up ....

edit: check first file for latest version!

Re: PositionSyncedStepLFO - help?

PostPosted: Wed Dec 05, 2012 12:50 am
by trogluddite
Nubeat7 wrote:thanks trog, hmm about the rounding down, i tried it with your way too but it didn`t work

Sorry, my bad! :oops:
I was thinking of another situation when interpolating sample counters, where you have to keep the integer indexes in the right order for the lowest CPU - as you say, my "fix" just moves the "0.5" problem bang onto the integers, so that whole numbers don't always round properly. Very silly mistake! :roll:
For proper rounding down, there is a simpler way...
Code: Select all
rounded = rndint(x)
rounded = rounded - 1 & (rounded > x)

If the rounded value is greater then the number you started with, then it must have rounded up - in which case, just subtract one. This will use way less CPU, as divides are one of the most greedy operations you can do, and the DSP code rounding is also not always very efficient (so that it can support CPU's without SSE2).
And of course, you can round up by doing the opposite - add one if the rounded number is less than the start value.

I tested it this time! :D

Re: PositionSyncedStepLFO - help?

PostPosted: Fri Dec 07, 2012 9:07 pm
by Nubeat7
ahh, :D great that there is an easier way, always asking myself why i don`t see the easy ways first :) thanks!

Re: PositionSyncedStepLFO - help?

PostPosted: Tue Dec 11, 2012 9:25 pm
by Nubeat7
hmmm, but! like you can see at this schematic it is not working correctly! looks like it is still rounding up too !

i tried also a second way with the subtract method, looks that it is not working or did i confuse something in the code?

only the dividing method changes the int exactly in the same moment the float changes to the next int...

Re: PositionSyncedStepLFO - help?

PostPosted: Mon May 06, 2013 9:23 pm
by Nubeat7
ok, i tried to optimize the code via assembler and could reduce around 12 cycles and 4 variables,
buti only did delete the useless cycles so i think there is more space to optimise, just don`t know where to start, at the end the code is not very complex but this rounding thing is still very strange, but i couldn`t get it work correct in a different way

edit: check first file for latest version!

Re: PositionSyncedStepLFO - help?

PostPosted: Tue May 07, 2013 12:22 pm
by Tronic
for me in the module, PPQ to Ramp, in the Totalizer section,
there is an error:

//Totaliser
addps xmm0,xmm1;
//addps xmm0,negHalf; //**// this must be removed
movaps Ramp,xmm0;

otherwise when it resets, the value will start from -0.5, not from 0.

then, so the Trog trick, work good.

Code: Select all
rounded = rndint(x)
rounded = rounded - 1 & (rounded > x)

Re: PositionSyncedStepLFO - help?

PostPosted: Tue May 07, 2013 6:12 pm
by Nubeat7
ah yeah, thats interesting but i think it was not the reason, anyway i tried it again and this time i win :o

think i did something wrong all the time i tried before :?

so thanks for figuring this out and motivating me to try it again:)

so here is it with trogs rounding method, i also could eliminate a few cycles, is there still more to optimise?

edit: saved some cycles more with SSE2 rounding method, from trogs asm optimizing toolkit
edit: check first file for latest version!

Re: PositionSyncedStepLFO - help?

PostPosted: Tue May 07, 2013 9:53 pm
by Drnkhobo
Nice Nubeat7!!

:lol: