02-04-2016 01:00 PM
Hello Everyone,
After much searching, I can't find a definitive answer for the best way to take individual Boolean values and combine them into a single word (U16) representation. We do this in a lot of places in our code, so the performance savings on our sbRIO-9606 could be significant.
My colleague tends to build them into an array using Build Array, then use Boolean Array to Number. I know the consensus is to allocate a constant array then use Replace Array Subset, but benchmarking these techniques provides identical RT Trace results, so it doesn't seem to be better. I have also tried allocating a U16, using Number to Boolean Array, then using Replace Array Subset and Boolean Array to Number. Here's what I mean:
Does anyone know the best method to do this operation? Am I overlooking something obvious? Do you have a resource to back it up? My company requires proof or justification for a lot of what we do, hence my benchmarking these seemingly minor code improvements. What a headache.
Thanks!
Patrick
02-04-2016 01:22 PM
Well, the Build Array is technically using a fixed size array. Where the Build Array runs into issues is inside of a loop with a growing array (add an element with each iteration of the loop). Here, I would follow the Build Array folowed by the Boolean Array To Number. And with that said, the compiler probably optimizes both to the same instruction set. The first is easier to understand, in my opinion.
02-04-2016 01:25 PM
Hi Patrick,
do you need to work all those places with scalar boolean values?
Sometimes you might work directly with an U16 value…
On your question: the compiler gets better from LabVIEW version to version. I guess the compiler recognizes the BuildArray/BoolArrayToNum part already as "one clump of code". On a FPGA this is the recommended way…
02-04-2016 01:40 PM
Thanks for the responses.
Crossrulz, I agree with you about the readability and compiler optimization. That would explain why an RT Trace of all the different implementations shows the exact same result.
GerdW, we are taking Booleans from various places and combining them into U16 in order to work directly with that word, as you suggest. The "one clump of code" idea is an interesting one that hadn't occurred to me.
Guess it's settled, Build Array is easiest to read and isn't too terrible to use after all. Thanks again!
Patrick
02-04-2016 01:46 PM - edited 02-04-2016 01:47 PM
Did you know you don't really need the U16 conversion? You can just override the default behavior in boolean array to number properties.
02-04-2016 01:56 PM
Interesting! I do now, thanks Bill.
Patrick
02-04-2016 02:06 PM
I'm not sure about the RT, but according to my tests the most efficient way for the FPGA (from area saving perspectives) of the functionality you need, is using Rotate with carry operations with inserting the bits.
Thanks,
Arev
CTO | RAFA Solutions
02-04-2016 02:09 PM
@arevh wrote:I'm not sure about the RT, but according to my tests the most efficient way for the FPGA (from area saving perspectives) of the functionality you need, is using Rotate with carry operations with inserting the bits.
Really? Build Array is FREE in FPGA (it is just routing). And should be the same for Boolean Array To Number (nothing is happening to the bits themselves).
02-04-2016 02:19 PM
Yes 🙂
Initially I was using build array, and index array functions in the FPGA, but after I started to get out of area, due to added functionality, I replaced mentioned operations with Rotate operations. And was able to reduce the area consumption, and fit the code.
The images below are some parts from my FPGA code.
Thanks,
Arev
CTO | RAFA Solutions
02-04-2016 02:46 PM - edited 02-04-2016 02:46 PM