DSP code help needed (again)

For general discussion related FlowStone
Post Reply
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

DSP code help needed (again)

Post by tulamide »

If you know me, then you are aware of my DSP code weakness. Especially the bitmask used to do branching. I need code that does a "either or" (boolean xor).

Imagine this situation: There are two values, one is positive, one negative. The absolute products of the two values are compared and only one of them will be true (and passed), while the other is false. On occasion, both values will be exactly the same, so no xor conclusion, and in that case a default value (for example 0) is the result. For this example, let's assume the larger absolute product is the goal and passes the value. But it should work for the smaller as well, by just applying the math as I understand it from your example.

How to do that in DSP code?

Mockup code (please provide actual working code)

Code: Select all

streamin a;
streamin b;
streamout out;

//abs(a) and abs(b) will be compared
out = ?
//out needs to be 0, or the larger of the two values (not abs(value))
"There lies the dog buried" (German saying translated literally)
Tepeix
Posts: 361
Joined: Sat Oct 16, 2021 3:11 pm

Re: DSP code help needed (again)

Post by Tepeix »

Here some way, not sure the best in dsp, i forget a little when switching to asm.

Code: Select all

streamin in; streamin in2;
streamin default;
streamout out;
float a; float b; float c;
a=abs(in); b=abs(in2);
c=in2+(a>b)&(in-in2);
c=c+(a==b)&(default-c);

out=c;
For asm i would do like this

Code: Select all

streamin in; streamin in2;
streamin default;
streamout out;
int abs=2147483647;
//2 abs//
movaps xmm0,in; movaps xmm1,in2;
movaps xmm2,xmm0; movaps xmm3,xmm1;
movaps xmm4,abs;
andps xmm2,xmm4; andps xmm3,xmm4;
//compare//
movaps xmm5,xmm2; cmpps xmm5,xmm3,1;
subps xmm1,xmm0; andps xmm1,xmm5;
addps xmm0,xmm1;
cmpps xmm2,xmm3,0;
movaps xmm3,default; subps xmm3,xmm0;
andps xmm3,xmm2; addps xmm0,xmm3;
movaps out,xmm0;
adamszabo
Posts: 667
Joined: Sun Jul 11, 2010 7:21 am

Re: DSP code help needed (again)

Post by adamszabo »

I still need to wrap my head around the question. Could you perhaps do this in Ruby with basic numbers? If so, I can translate that to DSP.
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: DSP code help needed (again)

Post by tulamide »

adamszabo wrote:I still need to wrap my head around the question. Could you perhaps do this in Ruby with basic numbers? If so, I can translate that to DSP.
Damn, whenever I explain something, it tends to confuse more than help. Let's try it with this:

Code: Select all

true || true
## or, when both true, returns true

true ^ true
## xor, when both true, returns false
I need DSP code that compares two boolean results (imagine instead of true or false, there might be (a > b) or (c <= a) or something along those lines) and gets true ONLY when exactly ONE statement is true. If both are true, it should result in false. That way, I can set the output to the default value, when false. And to the input streams when true.

Does that help?
"There lies the dog buried" (German saying translated literally)
Tepeix
Posts: 361
Joined: Sat Oct 16, 2021 3:11 pm

Re: DSP code help needed (again)

Post by Tepeix »

Not sure i really understand the meaning of the operation.
In dsp only & is documented as and, but i know that | work as or, but not what could do xor..

But if you need to have a final boolean value like xor you could invert the result of and.

Like out= in1 + (a==b) &(default-in1);
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: DSP code help needed (again)

Post by tulamide »

Tepeix wrote:Not sure i really understand the meaning of the operation.
Exclusive Or. It means that comparing two booleans only returns true if they are different.

Code: Select all

true ^ true = false
true ^ false = true
false ^ true = true
false ^ false = false
That's unlike Or, which returns true if at least one is true, but doesn't mind both.

Code: Select all

true || true = true
true || false= true
false || true = true
false || false = false
Tepeix wrote:But if you need to have a final boolean value like xor you could invert the result of and.

Like out= in1 + (a==b) &(default-in1);
I'm not sure I understand this. Does this mean, result is in1 when a != b, but default when a==b? That would help indeed, but I don't know how to turn a and b into comparisons?
"There lies the dog buried" (German saying translated literally)
User avatar
Spogg
Posts: 3368
Joined: Thu Nov 20, 2014 4:24 pm
Location: Birmingham, England
Contact:

Re: DSP code help needed (again)

Post by Spogg »

Here’s a couple of solutions.

Now I eagerly await Martin to blow me out of the water… :lol:
Attachments
DSP XOR dev 2 .fsm
3.06
(4.04 KiB) Downloaded 599 times
adamszabo
Posts: 667
Joined: Sun Jul 11, 2010 7:21 am

Re: DSP code help needed (again)

Post by adamszabo »

Spogg beat me to it :lol:

I am not sure if this is supported in FS 3, but in the alpha there is actually a xor instruction in assembler, the simplest is:

Code: Select all

streamboolin bool1;
streamboolin bool2;
streamboolout out;

movaps xmm0,bool1; //move first boolean "bool1" to xmm0,
xorps xmm0,bool2; //do XOR operation with "bool2" 
movaps out,xmm0; //send result to "out"
Or you can also use a not equal (!=) in the DSP box, again I am not sure if its supported in FS 3:

Code: Select all

streamin a;
streamin b;
streamout out;

out = a != b;
This is the same as this assembly code which should work in any FS version:

Code: Select all

streamin bool1; 
streamin bool2;
streamout out;

movaps xmm0,bool1;
cmpps xmm0,bool2,4; //4 is not equal
movaps out,xmm0;
Tepeix
Posts: 361
Joined: Sat Oct 16, 2021 3:11 pm

Re: DSP code help needed (again)

Post by Tepeix »

a and b are abs in my reverse think.
If i really understand the code needed it might work to reverse the conditional.

When abs(in1) and abs(in2) are equal it will give default, if they are different, it give in1.

so :

Code: Select all

streamin in1; streamin in2; streamout out;
streamin default;
float a=0; float b=0;
a=abs(in1); b=abs(in2);
out= in1+(a==b)&(default-in1);
But if you need to take the greater value of in1 or in2 or the greater abs value it might be different, but could end up in the same.

Code: Select all

streamin in1; streamin in2; streamout out;
streamin default;
float a=0; float b=0; float c=0;
a=abs(in1); b=abs(in2);
c=in1+(a<b)&(in2-in1);
out= c+(a==b)&(default-c);
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: DSP code help needed (again)

Post by tulamide »

Hi guys!

I was quiet for a while, because my goal was to provide you with the result of my work and your help. But I couldn't get it to work in a meaningful way. I since got a template of sorts from Rex, that may help me at least finding out if the core idea works or not.

I am really thankful for all your help! Who would have thought that XOR'ing streams based on parameters could be that difficult? And it's not your codes. They work for their intentions. No, it's my idea, I'm afraid.

Thank you!
"There lies the dog buried" (German saying translated literally)
Post Reply