- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我很想知道 MSVC 是否为 bitset::count
使用了编译器内部 __popcnt。
环顾四周,我发现这是 VS2017 的 std::bitset::count
的实现:
size_t count() const _NOEXCEPT
{ // count number of set bits
const char *const _Bitsperbyte =
"\0\1\1\2\1\2\2\3\1\2\2\3\2\3\3\4"
"\1\2\2\3\2\3\3\4\2\3\3\4\3\4\4\5"
"\1\2\2\3\2\3\3\4\2\3\3\4\3\4\4\5"
"\2\3\3\4\3\4\4\5\3\4\4\5\4\5\5\6"
"\1\2\2\3\2\3\3\4\2\3\3\4\3\4\4\5"
"\2\3\3\4\3\4\4\5\3\4\4\5\4\5\5\6"
"\2\3\3\4\3\4\4\5\3\4\4\5\4\5\5\6"
"\3\4\4\5\4\5\5\6\4\5\5\6\5\6\6\7"
"\1\2\2\3\2\3\3\4\2\3\3\4\3\4\4\5"
"\2\3\3\4\3\4\4\5\3\4\4\5\4\5\5\6"
"\2\3\3\4\3\4\4\5\3\4\4\5\4\5\5\6"
"\3\4\4\5\4\5\5\6\4\5\5\6\5\6\6\7"
"\2\3\3\4\3\4\4\5\3\4\4\5\4\5\5\6"
"\3\4\4\5\4\5\5\6\4\5\5\6\5\6\6\7"
"\3\4\4\5\4\5\5\6\4\5\5\6\5\6\6\7"
"\4\5\5\6\5\6\6\7\5\6\6\7\6\7\7\x8";
const unsigned char *_Ptr = &reinterpret_cast<const unsigned char&>(_Array);
const unsigned char *const _End = _Ptr + sizeof (_Array);
size_t _Val = 0;
for ( ; _Ptr != _End; ++_Ptr)
_Val += _Bitsperbyte[*_Ptr];
return (_Val);
}
它看起来像是使用查找表来获取任何给定字节的位数,然后计算每个字节的 1 的数量。
According to this answer here ,GCC 是这样实现的(按照我的想法):
/// Returns the number of bits which are set.
size_t
count() const { return this->_M_do_count(); }
size_t
_M_do_count() const
{
size_t __result = 0;
for (size_t __i = 0; __i < _Nw; __i++)
__result += __builtin_popcountl(_M_w[__i]);
return __result;
}
虽然我没有进行任何基准测试,但我敢打赌 GCC 的实现在这里会快很多。
因此,MSVC 像这样实现 std::bitset::count
是否有任何令人信服的理由?我的猜测是 MSVC 有一个包罗万象的“STL 中没有编译器内在函数”政策,或者我忽略了两个平台之间的差异。
最佳答案
__builtin_popcountl
在 GCC 中的内部实现并不更好,根据体系结构,它类似于下面的内容。
i = i - ((i >> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
return (((i + (i >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24;
并且仅针对 SSE4a 指令集,仅在 2006 年之后的 AMD CPU 中支持,__builtin_popcountl
由一条汇编指令 POPCNT
组成。
MSDN 说
Each of these intrinsics generates the popcnt instruction. The size of the value that the popcnt instruction returns is the same as the size of its argument. In 32-bit mode there are no 64-bit general-purpose registers, hence no 64-bit popcnt.
To determine hardware support for the popcnt instruction, call the __cpuid intrinsic with InfoType=0x00000001 and check bit 23 of CPUInfo[2] (ECX). This bit is 1 if the instruction is supported, and 0 otherwise. If you run code that uses this intrinsic on hardware that does not support the popcnt instruction, the results are unpredictable.
我假设 MSVC 团队不想使用带有条件的内在条件,而希望使用一种独立于 CPU 和架构的通用解决方案。
关于c++ - 为什么 MSVC 在 std::bitset::count 的实现中不使用 __popcnt?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48394450/
我目前正在处理一个二进制文件,稍后将写入另一个二进制文件。这一点非常重要,也是我对使用 ArrayList 和其他列表犹豫不决的原因,因为它们往往不太适合我尝试将其直接写入文件。 我已经从这个二进制文
我需要一个 BitSet,它可以轻松连接多个 BitSet,创建一个新的 BitSet。 default implementation没有这样的方法。 你们中的任何人都知道在某些外部库中是否有任何实现
我有大量的 bitset 数据,存储在 db 中。我想把它上传到redis bitset,这样我就可以对其进行位操作了。有没有办法从 redis-cli 或 javascript 代码上传这些数据?我
在下面的函数中,我想根据突变概率来突变 BitSet。 public static Cell mutate(Cell original_){ Double mProb = 0.2; B
BitSet bits1 = new BitSet(00101010); System.out.println(bits1); 问题:为什么输出返回 { }。 List list = new Arra
我正在尝试创建一个实体/组件系统,自动匹配合适的实体和合适的系统。我正在使用 std::bitset和 RTTI自动为每个组件类型分配一个位值。 系统定义如下:MovementSystem : Sys
使用 bitset::operator[] 等同于使用 bitset::test 还是有一些底层优化? 也就是说,这两个循环是等价的吗? 使用 bitset::operator[]: static c
回答How to store binary data when you only care about speed? ,我正在尝试写一些做比较,所以我想使用 std::bitset。但是,为了公平比较
我今天刚刚学习了一些简单的加密,并编写了一个简单的程序将我的文本转换为 10 位二进制。我不确定我做的是否正确,但是代码的注释部分和实际代码有 2 个不同的 10 位输出。我很困惑。谁能用通俗易懂的语
如果我有一个 std::bitset , 我怎样才能把它转换成 std::bitset高位补0? std::bitset a = 0xFF00; std::bitset b = a; // erro
Java Bitset 类是动态的,其大小根据其中的位 ON 变化。但字节数组可以保存特定数量的位,并且很容易与 SQL Server 一起使用来从数据库复制和检索数据。这是真的吗? Bitset类可
我想使用缓存,由 boost 的 unordered_map 实现,从 dynamic_bitset 到 dynamic_bitset。当然,问题在于 bitset 中没有默认的哈希函数。这似乎不像是
我一直在查看 bitset 的头文件标准 C++ 库头文件。我发现重载的 operator[] operator[](size_t ndx)(在 bitset 类中定义)返回 reference 类的
我最近遇到了 bitset 模板,我真的很想在我当前的项目中使用它们。继续阅读,我看到 std::bitset 模板的大小必须在编译时确定。许多人建议使用 boost::dynamic_bitset
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我正在使用 std::bitset 并尝试创建两个大小为 100,000,000,000 的数组 std::bitset。结果,程序只占了我 298 MB 的 RAM,但必须占 ~24 GB。我有 3
一 问题描述 给定 N 个集合,第 i 个集合 Si 有 Ci 个元素(集合可以包含两个相同的元素)。集合中的每个元素都用 1~10000 的整数表示。查询给定的两个元素 i 和 j 是否同时属于至少
我的脑海中一直浮现着“bitset”这个词作为我的问题的解决方案,但我想我可能会让自己感到困惑。 我有一个指示某些条件的十六进制值列表,例如: 0x0001 = Outside 0x20000000
我正在寻找一种方法来用一个位向量(这将是该组整数的特征函数)表示一组整数,并能够对该组执行按位运算。 最初我认为 scala 的 BitSet 是理想的选择。但是,根据文档 1,BitSet 似乎不支
如果我在 Bitset 中设置两个不同的位 BitSet x= new BitSet(8); x.set(0);//.........Case1 x.set(7);//.........C
我是一名优秀的程序员,十分优秀!