What's the meaning of the bitset in code generation ?

7 ビュー (過去 30 日間)
MIN HOON
MIN HOON 2022 年 12 月 2 日
回答済み: Walter Roberson 2022 年 12 月 2 日
hello, everyone
I used "model advisor" for inspecting my one simulink model.
Then, i received a lot of warnings. and model advisor recommended to me a lot of things that can fix them.
One of recommendations is "Use bitsets for storing boolean data (Stateflow)".
In this sentence, do the bitsets mean union of the some amount of bits?
,I mean... Can one bit also be considered as "bitsets"?
what is the bitset?
According to the matlab doc, that option is related to the optimal code generation in terms of amount of "instructions".
Could you explain about this?

回答 (1 件)

Walter Roberson
Walter Roberson 2022 年 12 月 2 日
Suppose you have 3 logical values, say MA_on MB_on MC_on . Then at the C code level you could store those as one byte each, and test whether the byte is 0 or non-zero. Those are simple efficient instructions when working with each individually -- but it does take a total of 3 bytes.
Now suppose you had an array of 1024 x 768 logical values such as indicating whether each location in a map is blocked by an object, then that would be 1024 * 768 = 786432 bytes of storage. But you only care about whether each location is 0 or non-zero, so you are effectively wasting 7 out of every 8 bits of each location.
Now suppose you take those three logicals MA_on MB_on MC_on and you combine them into a single byte:
BS = MA_on << 2 | MB_on << 1 | MC_on
which is like
xxxxxABC %x --> don't care
87654321 %bit numbers
00000421 %mathematical weights
then you if you do
bitget(BS, 1) %gives MC_on
bitget(BS, 2) %gives MB_on
bigget(BS, 3) %gives MA_on
but at the C code level, these could be coded as
BS & 0x01 %gives MC_on
BS & 0x02 %gives MB_on
BS & 0x04 %gives MA_on
Notice the 01, 02, 04 are the same as the mathematical weights in binary positional notation
In C, you can use these techniques to pack up to 64 logical in a single 64-bit location (which is significant because 64 bit values can be transfered using a single instruction.) With a little more work you can extend this to arrays of arbitrary size. So instead of needing 1024 * 768 = 786432 bytes of storage for a 1024 x 768 array of logical, you could pack into 98304 bytes -- which would also be 8 times faster to transfer from place to place.
bitsets are coded automatically. If you enable them then all the appropriate code will be generated, and your program will barely notice.
So why not always do it? Well, if you happen to have some logical values that are used a lot then the overhead of unpacking from the containing data adds up. And it becomes more difficult to debug -- if you examine a byte in the debugger and it says 147 decimal it would take a bit of work to understand that represents 8 consecutive logicals in the array in pattern 10010011

カテゴリ

Help Center および File ExchangeModel Compatibility についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by