将 uint64 转换为 GMP/MPIR 数字

发布于 2024-11-18 21:12:03 字数 241 浏览 12 评论 0 原文

我在 Windows (MSVC 2010) 上使用 MPIR 2.4.0,并且尝试将无符号 64 位整数添加到 mpz_t 数字。然而MPIR/GMP似乎不支持64位整数和mpz_t之间的直接转换。这是否意味着我必须将 uint64 转换为字符串并通过 mpz_init_set_str 读取它? 这既不是很吸引人,也不是看起来很快——两次转换都是免费的。

我错过了什么或者这里使用的技巧/技巧是什么?

干杯,

菲利普

I am using MPIR 2.4.0 on Windows (MSVC 2010) and I was trying to add an unsigned 64bit integer to a mpz_t number. However it seems that MPIR/GMP does not support direct conversion between 64bit Integers and mpz_t. Does this mean I have to convert my uint64 to a string and read this via mpz_init_set_str?
Neither is this very appealing nor does it look very quick - two conversion for nothing.

Did I miss something or what is the trick/hack to use here?

Cheers,

Philipp

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

笑梦风尘 2024-11-25 21:12:03

正如 Banthar 建议的那样,使用 mpz_import,但我建议以下内容不依赖于平台字节序:

mpz_import(b, 1, 1, sizeof(a), 0, 0, &a);

As suggested by Banthar use mpz_import, but I'd suggest the following which does not rely on the platform endianness:

mpz_import(b, 1, 1, sizeof(a), 0, 0, &a);
清晨说晚安 2024-11-25 21:12:03

使用mpz_import

void mpz_set_ull( mpz_t rop, unsigned long long op )
{
   mpz_import(rop, 1, 1, sizeof(op), 0, 0, &op);
}

编辑:代码根据弗兰克的评论。

Use mpz_import:

void mpz_set_ull( mpz_t rop, unsigned long long op )
{
   mpz_import(rop, 1, 1, sizeof(op), 0, 0, &op);
}

EDIT: Code updated according to Frank's comment.

等风来 2024-11-25 21:12:03

是的,如果您使用的平台 (Windows) 不使用 LP64 模型,则没有函数可以将 64 位整数分配给 mpz_t。您可以单独分配 64 位整数的高半部分和低半部分,然后将它们加在一起,而不是遍历字符串。仍然不是很干净,但几乎可以肯定更快。

编辑:请参阅 Banthar's 答案以获得更好的解决方法。

Yes, if you're on a platform (Windows) that doesn't use the LP64 model, then there is no function to assign a 64-bit integer to a mpz_t. Instead of going through a string, you could separately assign the high and low half of the 64-bit integer and then add them together. Still not very clean, but almost certainly faster.

Edit: see Banthar's answer for a much better workaround.

话少心凉 2024-11-25 21:12:03

在 MPIR 3 上运行一些测试后,似乎 mpz_import 由于某种原因在 64 位值上速度较慢,下面的方法更丑陋,但 IME 表现更好(hi 是 UInt64 的较高 32 位,lo 是较低 32 位)

 mpz_set_ui(dest, hi);
 mpz_mul_2exp(dest, dest, 32);
 mpz_add_ui(dest, lo);

如果 hi 部分为空,当然你可以走捷径,值得进行测试。

如果它是负的 Int64(有符号),则获取 Abs,执行上述操作,然后对 mp_size 字段求反。

After running some tests on MPIR 3, it appears mpz_import is slower for some reason on 64 bit values, the approach below is uglier, but behaves better IME (hi being higher 32bits and lo being lower 32bits of the UInt64)

 mpz_set_ui(dest, hi);
 mpz_mul_2exp(dest, dest, 32);
 mpz_add_ui(dest, lo);

If the hi part is null, you can shortcut of course, it can be worth making the test.

If it is a negative Int64 (signed), then get the Abs, do the above and then negate the mp_size field.

节枝 2024-11-25 21:12:03

MPIR 2.4 引入了对 intmax_t 和 uintmax_t 的支持。请参阅 mpz_set_ux()/sx() 和 mpz_get_ux()/sx()。这些函数在 GMP 中不存在,但在 MPIR 2.4.0 手册中记录。

MPIR 2.4 introduced support for intmax_t and uintmax_t. Please see mpz_set_ux()/sx() and mpz_get_ux()/sx(). These functions don't exist in GMP but are documented in the MPIR 2.4.0 manual.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文