From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Most Efficient/Fastest Boolean Bit Packing Methods on RT

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:

 

BoolBitPack.PNG

 

 

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

 

CLA-300x82.png

0 Kudos
Message 1 of 15
(3,965 Views)

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.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 2 of 15
(3,957 Views)

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…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 3 of 15
(3,952 Views)

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

 

CLA-300x82.png

0 Kudos
Message 4 of 15
(3,936 Views)

Did you know you don't really need the U16 conversion?  You can just override the default behavior in boolean array to number properties.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 5 of 15
(3,921 Views)

Interesting!  I do now, thanks Bill.

 

35xigf_big.jpg

 

 

 

Patrick

 

CLA-300x82.png

0 Kudos
Message 6 of 15
(3,911 Views)

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

 

Certified-LabVIEW-Embedded-Systems-Developer_rgb.jpgCertified-LabVIEW-Architect_rgb.jpg

0 Kudos
Message 7 of 15
(3,901 Views)

@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).


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 8 of 15
(3,892 Views)

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. 

 

shift_u8.PNG

packing.PNG

 

Thanks,

Arev

 

CTO | RAFA Solutions

 

Certified-LabVIEW-Embedded-Systems-Developer_rgb.jpgCertified-LabVIEW-Architect_rgb.jpg

0 Kudos
Message 9 of 15
(3,886 Views)

Hi arev,

 

why do you build a (non-typedef'd) cluster from an U8 value?

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 10 of 15
(3,862 Views)