Bitwise usage for IF

DSP related issues, mathematics, processing and techniques
Post Reply
Kirill_Neoris
Posts: 25
Joined: Fri Jan 08, 2016 5:06 pm

Bitwise usage for IF

Post by Kirill_Neoris »

Hello!

Can you give me an example how to organize the expression like this in DSP code?

If asum1 <=1 then x = 1.0
else x = asum1

Pardon my free "code" ;)

In practice I'm trying to make a module that will be keeping a sum of three inputs below 1.0
Dancing around "&" gives me the asum1 or zero on the variable. I can't figure out how to make it properly. I'll add the code later when i'll be at home.
User avatar
martinvicanek
Posts: 1334
Joined: Sat Jun 22, 2013 8:28 pm

Re: Bitwise usage for IF

Post by martinvicanek »

I can think of three possibilities:

Code: Select all

x = max(asum1,1.0);

Code: Select all

x = asum1 + (1.0 - asum1)&(asum1 <= 1);

Code: Select all

x = 1.0 + (asum1 - 1.0)&(asum1 > 1);

And a 4th one which may be easier to grasp but has more operations:

Code: Select all

x = 1.0&(asum1 <= 1) + asum1&(asum1 > 1);
Kirill_Neoris
Posts: 25
Joined: Fri Jan 08, 2016 5:06 pm

Re: Bitwise usage for IF

Post by Kirill_Neoris »

Thank you! I'll check this out when will be at home!
Extra thanks for the quick answer!
Kirill_Neoris
Posts: 25
Joined: Fri Jan 08, 2016 5:06 pm

Re: Bitwise usage for IF

Post by Kirill_Neoris »

martinvicanek wrote:I can think of three possibilities:

Code: Select all

x = max(asum1,1.0);

Code: Select all

x = asum1 + (1.0 - asum1)&(asum1 <= 1);

Code: Select all

x = 1.0 + (asum1 - 1.0)&(asum1 > 1);

And a 4th one which may be easier to grasp but has more operations:

Code: Select all

x = 1.0&(asum1 <= 1) + asum1&(asum1 > 1);


Code: Select all

streamin lfo1;
streamin lfo2;
streamin lfo3;
streamout lfo1n;
streamout lfo2n;
streamout lfo3n;
streamout k;
streamout sum;

float lfosum;
float k1;
float x;

lfosum = lfo1+lfo2+lfo3;
x = 1.0&(lfosum <= 1) + lfosum&(lfosum > 1);
k1 = 1/x;

k = k1;
sum = lfosum;

lfo1n = lfo1 * k1; //this is to make the sum of amplitudes
lfo2n = lfo2 * k1; //equal or lower than 1.0
lfo3n = lfo3 * k1;


I've tried the last right away and it works perfectly! :)
My version was:

Code: Select all

x = (lfosum>=1) & 1.0;


I still can't figure out how your code works... Maybe it need a time to settle down in my head...
Does the "plus" means it's and additional condition?
User avatar
MyCo
Posts: 718
Joined: Tue Jul 13, 2010 12:33 pm
Location: Germany
Contact:

Re: Bitwise usage for IF

Post by MyCo »

Version 3.0.9 has the ternary operator build in, so this should work:

Code: Select all

x = (asum1 <=1) ? 1.0 : asum1;
Post Reply