简体   繁体   中英

concatenate bits C++

I am looking for an elegant C++ using SystemC to concatenate bits.

A System Verilog example in one nice line:

bool my_variable;
uint bits_combination = {8'b0, {8{my_variable}}, 8'b1, 4'b0, 2'b1, 2'b0};

My best solution for C++:

bool my_variable;
sc_uint<32> bits_combination;
bits_combination.range(31,24) = 0;
bits_combination.range(23,16) = my_variable ? (1 << 8)-1  : 0;
bits_combination.range(15,8)  = (1 << 8)-1;
bits_combination.range(7,4)   = 0;
bits_combination.range(3,2)   = (1 << 2)-1;
bits_combination.range(1,0)   = 0;

Improving this line to a non ternary operator will also help:

my_variable ? (1 << 8)-1  : 0

Looks like

  0b0000'0000'0000'0000'1111'1111'0000'1100 | 
( 0b0000'0000'1111'1111'0000'0000'0000'0000 * myVariable )

In SystemC comma operator is overloaded for concatenation. But there is no replication operator like {8{my_variable}}.

But, you can write a function that will do replication. For example:

template <int N, int M>
sc_uint<N*M> replicate( sc_uint<M> val) {
    sc_uint<N*M> res = 0;
    for (int i = 0; i < N; ++i)
        res = (res << M) | val;
    return res;
};

So SystemC version of your SystemVerilog sample can look like:

sc_uint<1> my_variable = 1;
sc_uint<32> bits_concatenation = (sc_uint<8>(0), replicate<8>(my_variable), sc_uint<8>(1), sc_uint<4>(0), sc_uint<2>(1), sc_uint<4>(0) );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM