如何计算specman中uint中的设置位数?

时间:2021-09-11 02:59:43

I want to count the number of set bits in a uint in Specman:

我想计算Specman中uint中的设置位数:

var x: uint;
gen x;
var x_set_bits: uint;
x_set_bits = ?;

What's the best way to do this?

最好的方法是什么?

2 个解决方案

#1


I don't know Specman, but another way I've seen this done looks a bit cheesy, but tends to be efficient: Keep a 256-element array; each element of the array consists of the number of bits corresponding to that value. For example (pseudocode):

我不知道Specman,但我看到这样做的另一种方式看起来有点俗气,但往往效率很高:保持一个256元素的阵列;数组的每个元素由对应于该值的位数组成。例如(伪代码):

bit_count = [0, 1, 1, 2, 1, ...]

Thus, bit_count2 == 1, because the value 2, in binary, has a single "1" bit. Simiarly, bit_count[255] == 8.

因此,bit_count2 == 1,因为二进制值2具有单个“1”位。 Simiarly,bit_count [255] == 8。

Then, break the uint into bytes, use the byte values to index into the bit_count array, and add the results. Pseudocode:

然后,将uint分解为字节,使用字节值索引到bit_count数组,并添加结果。伪代码:

total = 0
for byte in list_of_bytes
    total = total + bit_count[byte]

EDIT

This issue shows up in the book Beautiful Code, in the chapter by Henry S. Warren. Also, Matt Howells shows a C-language implementation that efficiently calculates a bit count. See this answer.

这个问题出现在Henry S. Warren的章节“美丽的代码”一书中。此外,Matt Howells展示了一种有效计算位数的C语言实现。看到这个答案。

#2


One way I've seen is:

我见过的一种方法是:

x_set_bits = pack(NULL, x).count(it == 1);

pack(NULL, x) converts x to a list of bits.
count acts on the list and counts all the elements for which the condition holds. In this case the condition is that the element equals 1, which comes out to the number of set bits.

pack(NULL,x)将x转换为位列表。 count作用于列表并计算条件所适用的所有元素。在这种情况下,条件是元素等于1,它出现在设置位的数量上。

#1


I don't know Specman, but another way I've seen this done looks a bit cheesy, but tends to be efficient: Keep a 256-element array; each element of the array consists of the number of bits corresponding to that value. For example (pseudocode):

我不知道Specman,但我看到这样做的另一种方式看起来有点俗气,但往往效率很高:保持一个256元素的阵列;数组的每个元素由对应于该值的位数组成。例如(伪代码):

bit_count = [0, 1, 1, 2, 1, ...]

Thus, bit_count2 == 1, because the value 2, in binary, has a single "1" bit. Simiarly, bit_count[255] == 8.

因此,bit_count2 == 1,因为二进制值2具有单个“1”位。 Simiarly,bit_count [255] == 8。

Then, break the uint into bytes, use the byte values to index into the bit_count array, and add the results. Pseudocode:

然后,将uint分解为字节,使用字节值索引到bit_count数组,并添加结果。伪代码:

total = 0
for byte in list_of_bytes
    total = total + bit_count[byte]

EDIT

This issue shows up in the book Beautiful Code, in the chapter by Henry S. Warren. Also, Matt Howells shows a C-language implementation that efficiently calculates a bit count. See this answer.

这个问题出现在Henry S. Warren的章节“美丽的代码”一书中。此外,Matt Howells展示了一种有效计算位数的C语言实现。看到这个答案。

#2


One way I've seen is:

我见过的一种方法是:

x_set_bits = pack(NULL, x).count(it == 1);

pack(NULL, x) converts x to a list of bits.
count acts on the list and counts all the elements for which the condition holds. In this case the condition is that the element equals 1, which comes out to the number of set bits.

pack(NULL,x)将x转换为位列表。 count作用于列表并计算条件所适用的所有元素。在这种情况下,条件是元素等于1,它出现在设置位的数量上。