Linux内核中HWEIGHT宏的目的是什么?
我遇到了hweight8
/hweight16
/hweight32
/...宏(宏)读取内核中的某些驱动程序代码时。我想了解,这些宏的目的以及它们在做什么,但不幸的是,我没有找到有关该主题的任何文档。
宏的定义看起来像这样,可以在
中找到 包括/asm-generic/bitops/bitops/bitops/ const_hweight.h
#define hweight8(w) (__builtin_constant_p(w) ? __const_hweight8(w) : __arch_hweight8(w))
#define hweight16(w) (__builtin_constant_p(w) ? __const_hweight16(w) : __arch_hweight16(w))
#define hweight32(w) (__builtin_constant_p(w) ? __const_hweight32(w) : __arch_hweight32(w))
#define hweight64(w) (__builtin_constant_p(w) ? __const_hweight64(w) : __arch_hweight64(w))
I came across the hweight8
/hweight16
/hweight32
/... macros when reading through some driver code in the kernel. I would like to understand, what these macros are for and what they are doing but unfortunately I failed to find any documentation on the topic.
The definition of the macros looks like this and can be found in
include/asm-generic/bitops/const_hweight.h
#define hweight8(w) (__builtin_constant_p(w) ? __const_hweight8(w) : __arch_hweight8(w))
#define hweight16(w) (__builtin_constant_p(w) ? __const_hweight16(w) : __arch_hweight16(w))
#define hweight32(w) (__builtin_constant_p(w) ? __const_hweight32(w) : __arch_hweight32(w))
#define hweight64(w) (__builtin_constant_p(w) ? __const_hweight64(w) : __arch_hweight64(w))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,找到答案!
Hewight函数似乎用于计数次数等于字节/单词中的1。我不完全理解用例,但是我找到了此代码在Linux nand驱动程序中使用它来检查块是否被删除(所有位均为1),同时接受一些要翻转的位。
示例调用:
Ok, found the answer!
The hweight functions seem to be used for counting the number of bits equals 1 in a byte/word. I do not fully understand the use case but I have found this piece of code in the Linux NAND driver using it for checking if a block is erased (all bits are 1) while accepting some bits to be flipped.
Example calls: