Linux内核中HWEIGHT宏的目的是什么?

发布于 2025-02-02 08:01:42 字数 794 浏览 2 评论 0原文

我遇到了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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丑疤怪 2025-02-09 08:01:42

好的,找到答案!

Hewight函数似乎用于计数次数等于字节/单词中的1。我不完全理解用例,但是我找到了此代码在Linux nand驱动程序中使用它来检查块是否被删除(所有位均为1),同时接受一些要翻转的位。

示例调用:

hweight8(1)  --> 1
hweight8(2)  --> 1
hweight8(3)  --> 2
hweight8(4)  --> 1
hweight8(5)  --> 2
hweight8(6)  --> 2
hweight8(7)  --> 3
hweight8(8)  --> 1
and so on...

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:

hweight8(1)  --> 1
hweight8(2)  --> 1
hweight8(3)  --> 2
hweight8(4)  --> 1
hweight8(5)  --> 2
hweight8(6)  --> 2
hweight8(7)  --> 3
hweight8(8)  --> 1
and so on...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文