- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 boost::multiprecision 来获得固定但任意精度的整数。我打算使用 number<cpp_int<W, W, unsigned_magnitude, unchecked, void>>
.第一个明显的问题是:
此数据类型是否具有给定精度的任何无符号整数的标准位模式?我听说有符号的扩展精度数不使用 2 的补码,但我认为无符号的应该使用标准表示,还是我遗漏了什么?
如果是这种情况,那么我可以得到整数的人口数吗?我似乎没有任何公共(public)界面可以做到这一点。我也很高兴有办法获得内部内存,这样我就可以计算用作存储的单个单词的数量。
谢谢
最佳答案
在寻找同样的东西时偶然发现了这个。我最终写了以下内容:
#include <bitset>
#include <limits>
#include <type_traits>
#include <boost/multiprecision/cpp_int.hpp>
// N.B.: Prior to Boost 1.79 MinBits and MaxBits were unsigned,
// not size_t - using auto to paper over this difference.
template <
auto MinBits,
auto MaxBits,
boost::multiprecision::cpp_integer_type SignType,
boost::multiprecision::cpp_int_check_type Checked,
class Allocator>
size_t popcount(
const boost::multiprecision::number<
boost::multiprecision::cpp_int_backend<
MinBits,
MaxBits,
SignType,
Checked,
Allocator>>& bits) {
const auto& backend = bits.backend();
// Using std::bitset::count to access a native popcnt.
// In principle the limb type could be larger than what a
// bitset can natively handle, in practice it likely isn't.
using BitsetNativeType = unsigned long long;
constexpr size_t kNativeBits = std::numeric_limits<BitsetNativeType>::digits;
using LimbType = std::decay_t<decltype(*backend.limbs())>;
constexpr size_t kLimbBits = std::numeric_limits<LimbType>::digits;
constexpr size_t kNativesToCount = (kLimbBits + kNativeBits - 1) / kNativeBits;
constexpr size_t kShiftPerNative = kNativesToCount > 1 ? kNativeBits : 0;
static_assert(kNativesToCount > 0, "bad bit counts");
size_t result = 0;
for (size_t i = 0; i != backend.size(); ++i) {
auto limb_value = backend.limbs()[i];
for (size_t j = 0; j != kNativesToCount; ++j) {
const std::bitset<kNativeBits> limb_bitset{BitsetNativeType(limb_value)};
result += limb_bitset.count();
limb_value >>= kShiftPerNative;
}
}
return result;
}
对于原生字大小(即 <= 64 位),这将编译为单个 popcnt
。上面它编译为两个 popcnt
最多 128 位。除此之外,它会在 clang 中自动矢量化 - 大概是受 Faster Population Counts Using AVX2 Instructions 启发的东西但坦率地说,我没有进一步调查。在 gcc 中,它只是不展开循环,仍然使用 popcnt
。
clang 演示:https://godbolt.org/z/fve195TeM
gcc 演示:https://godbolt.org/z/P4ndY8ccq
关于c++ - 如何使用 boost::multiprecision 获取无符号未检查数的人口数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24824681/
在 rng 中使用固定种子时,如果精度变化,结果将无法重现。即,如果更改模板参数 cpp_dec_float并运行以下代码,会看到不同的输出(对于精度的每次变化)。 #include #includ
我一直在关注 documentation from the boost library生成多精度随机整数,但在文档中没有提到如何设置种子。 我不知道如何在不出现编译错误的情况下设置种子。 #inclu
我需要将IPv6字符串地址转换为boost::multiprecision::uint128_t 对于IPv4,我使用以下算法: uint32_t byte1 = 0, byte2 = 0, byte
自从调整了一些代码以启用多精度后,我的一些单元测试开始失败。头文件: #ifndef SCRATCH_UNITTESTBOOST_INCLUDED #define SCRATCH_UNITTESTBO
我正在尝试使用 boost::multiprecision 库进行浮点(或者在那种情况下,固定)点运算。但是,我无法通过以下方式检测潜在的溢出: typedef boost::multiprecisi
如果我有: 字符缓冲区[16]; 如何将其原始字节转换为: boost::multiprecision::uint128_t ? 我尝试进行标准的 C 风格转换: uint128_t myInt =
我正在使用 boost::multiprecision 来获得固定但任意精度的整数。我打算使用 number> .第一个明显的问题是: 此数据类型是否具有给定精度的任何无符号整数的标准位模式?我听说有
我使用 boost::multiprecision::uint128_t 类型来对 128 位值执行按位运算。但是,我无法将 128 位值写入二进制文件。特别是需要用零填充值。 例如,如果 uint1
http://www.boost.org/doc/libs/1_53_0/libs/multiprecision/doc/html/index.html 我刚刚开始探索这个图书馆。似乎没有办法将 cp
我读过 boost::multiprecision documentation : Depending upon the number type, precision may be arbitrari
如何将字符串转换为“boost::multiprecision::cpp_int”? 此外,我有一个 .txt 文件,其中包含 100 个数字,每个数字 50 个数字,我使用 ifstream 将它们
我需要以任意精度获取一个值的散列值(来自 Boost.Multiprecision);我用 cpp_int后端。我想出了以下代码: boost::multiprecision::cpp_int x0
考虑以下使用 boost 创建多精度 float “a”的代码。 如何使用boost库调用三角函数?比如我希望计算sin(a)。 #include #include "boost/multiprec
我正在尝试转换 boost::multiprecision::cpp_dec_float_x到 boost::multiprecision::uintx_t .所以基本上是 boost bigreal
我尝试创建一个派生自 boost::multiprecision::mpz_int 的类并让它继承基类构造函数: #include using namespace boost::multipreci
我需要从 boost::multiprecision::int128_t 转换至 double . 对于较小的整数,我使用: template flt_t as_flt() const { ret
我正在尝试使用Ramanujan's公式为浮点后的任意位数计算我的大学项目之一的pi。对于这项工作,我使用的boost::multiprecision库只是我已经安装在计算机上的mpfr和mpir的包
关于 this answer ,有一个使用 boost::multiprecision 和 boost::random 的最小示例。 当我使用种子时,我正在努力解决这个例子: #include #i
我有一些模板代码,编译器可以尾调用优化大多数数据类型,但不能优化其他数据类型。代码实现pow() template void powRecurse(T& x, U& y, T& acc) { i
我需要两个函数: std::vector bigint_to_bytes(cpp_int a); cpp_int bytes_to_bigint(std::vector const& a); goog
我是一名优秀的程序员,十分优秀!