(a&b)>>c 在这个SystemC代码中的含义是什么?

huangapple go评论121阅读模式
英文:

what the meaning of (a&b)>>c in this systemc code?

问题

这个函数的返回值是什么意思?

谢谢!

英文:

when I read SYSTEMC code,I find a function return int like this:

static inline int rp_get_busaccess_response(struct rp_pkt *pkt)
{
    return (pkt->busaccess_ext_base.attributes & RP_BUS_RESP_MASK) >>
                                                            RP_BUS_RESP_SHIFT;
}

pkt->busaccess_ext_base.attributes defined as uint64_t.

RP_BUS_RESP_MASK and RP_BUS_RESP_SHIFT defined as:

enum {
    RP_RESP_OK                  =  0x0,
    RP_RESP_BUS_GENERIC_ERROR   =  0x1,
    RP_RESP_ADDR_ERROR          =  0x2,
    RP_RESP_MAX                 =  0xF,
};
enum {
    RP_BUS_RESP_SHIFT    =  8,
    RP_BUS_RESP_MASK     =  (RP_RESP_MAX << RP_BUS_RESP_SHIFT),
};

What the meaning of this function's return?

Thanks!

答案1

得分: 2

该函数提取了8字节(64位)属性的第二个字节的前4位。这意味着它提取属性0xFFFF FFFF FFFFF FAFF中的以下4位,结果为0x0A

  • 首先创建了掩码,即RP_BUS_RESP_MASK = 0x0F00
  • 然后将掩码应用于属性pkt->busaccess_ext_base.attributes & 0x0F00,结果是来自示例的0x0A00
  • 接下来将A向右移动8位,得到0x0A
英文:

The function extracts the first 4-Bit of the second byte of the 8-Byte (64-Bit) Attribute. This means, it extracts the following 4-Bits of the Attribute 0xFFFF FFFF FFFFF FAFF resulting in 0x0A

  • First it creates the mask, which is RP_BUS_RESP_MASK = 0x0F00
  • Next it applies the mask to the attribute pkt->busaccess_ext_base.attributes & 0x0F00 resulting in 0x0A00 from the example
  • Next it shifts A by 8-Bit to the right side, leading to 0x0A

答案2

得分: 2

a & b 是一个位运算,它将对每一对位执行逻辑 AND 操作,假设你有 262 & 261,这将转化为 100000110 & 100000101,结果将是 100000100 (260),结果背后的逻辑是每个 1 AND 1 都会得到 1,而 1 AND 00 AND 0 都会得到 0,这些都是常规的逻辑操作,但在位级别上执行:

  100000110
& 100000101
-----------
  100000100

(a & b) >> c 中,>> 将把 a & b 的结果向右移动 c 位。例如,对于先前的结果 100000100,并且 c 值为 8,所有位都会向右移动 8 位,结果是 000000001。原始值中最左边的 1 位将成为最右边的第一个,而原始值中从右边数的第三个 1 位将被移除

有了这些知识,看看这个函数,我们可以看到 RP_BUS_RESP_MASK 常量是一个掩码,保护了从第9位到第12位的位(从右边数,即第二个字节的前四位),将它们设置为 1RP_RESP_MAX << RP_BUS_RESP_SHIFT 转化为 1111 << 8 结果是 111100000000),这将保留该范围内的位值。然后,当它对这个掩码执行位 & 运算时,将其他位设置为 0。最后,将这个字段向右移动 RP_BUS_RESP_SHIFT8)。

它基本上提取了 kt->busaccess_ext_base.attributes 的第二个字节中的前四位,并将结果作为整数返回。

它具体用于什么?如果有文档存在,你必须查阅文档,或者尝试在全局上下文中理解它的用途,就我所看到的,这属于 LibSystemCTLM-SoC(如果你不知道的话)。

英文:

a &amp; b is a bitwise operation, this will perform a logical AND to each pair of bits, let's say you have 262 &amp; 261 this will translate to 100000110 &amp; 100000101 the result will be 100000100 (260), the logic behind the result is that each 1 AND 1 will result in 1 whereas 1 AND 0 and 0 AND 0 will result in 0, these are normal logical operations but are performed at bit level:

  100000110
&amp; 100000101
-----------
  100000100

In (a &amp; b) &gt;&gt; c, &gt;&gt; will shift the bits of the resulting value of a &amp; b to the right by c positions. For example for the previous result 100000100 and having a c value of 8, all bits will shift to the right by 8, and the result is 000000001. The left most 1 bit in the original value will become the first most right whereas the third 1 bit from the right in the original value will be shifted away.

With this knowledge in mind and looking at the function, we can see that the RP_BUS_RESP_MASK constant is a mask that protects the field of bits from 9th through 12th position(from the right, i.e. the first four bits of the second byte), setting them to 1 (RP_RESP_MAX &lt;&lt; RP_BUS_RESP_SHIFT which translates to 1111 << 8 resulting in 111100000000), this will preserve the bit values in that range. Then it sets the other bits of pkt-&gt;busaccess_ext_base.attributes to 0 when it performs the bitwise &amp; against this mask. Finally it shifts this field to the right by RP_BUS_RESP_SHIFT(8).

It basically extracts the the first four bits in the second byte of kt-&gt;busaccess_ext_base.attributes and returns the result as an integer.

What it's for specifically? You must consult the documentation if it exists or try to understand its use in the global context, for what I can see this belongs to LibSystemCTLM-SoC (In case you didn't know)

huangapple
  • 本文由 发表于 2023年1月9日 17:59:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055628.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定