固定大小的整数与 gmp ......? [英] Fixed size integers with gmp...?

查看:68
本文介绍了固定大小的整数与 gmp ......?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试在 MINGW 下安装 GMP 库好几天了.我已经在 linux64 位环境下将 __uint128_t 与 gcc 一起使用了几个星期,然后在 GMP 和 mingw(32 位版本)下移植了相同的程序.我使用 mpz_class 整数而不是 __uint128_t.然后我开始了我的新程序,然后……!使用 __uint128_t 和 64 位需要 16 分钟才能完成,使用 GMP 和 MINGW 需要 91 小时!!!

I've been trying for days to install GMP library under MINGW. I have been using for weeks __uint128_t with gcc under a linux64 bit environment, then ported the same program under GMP and mingw (32 bit version). I used mpz_class integers instead of __uint128_t. Then I started my new program and...! With __uint128_t and 64 bit it takes 16 minutes to complete, with GMP and MINGW it takes 91 HOURS!!!

我应该怎么做才能加快速度?在 32 位环境下有没有更快的方法来做 128 位整数数学?我不需要超过 128 位,所以有什么方法可以告诉 GMP好吧,我只需要 128 位,保持精度固定但请更快"?

What should I do to speed up things a bit? Is there any faster way to do 128 bit integer math under a 32 bit environment? I don't need more than 128 bit, so is there any way to tell GMP "ok, I just need 128 bits, keep presicion fixed but please GO FASTER"?

推荐答案

不,当您使用 mpz_t 时,您不能将 GMP 限制为固定长度的整数.mpz_t 是一个结构体,其长度为四肢数组(已分配;已使用)和指向实际值的指针,该指针存储为整数数组(四肢;int32 或 int64 数组).当它变大时,GMP 准备扩展任何值的长度.

No, when you are using an mpz_t, you can't limit GMP to fixed-len integers. mpz_t is a struct with lenght of limbs array (allocated; used) and a pointer to actual value which is stored as array of ints (limbs; array of int32 or int64). GMP is ready to expand length of any value when it will grow to big.

您可以在初始化时为每个 mpz_t 分配 128 位,使用 mpz_init2:

You can allocate 128 bits for every mpz_t at init, using mpz_init2:

 mpz_init2(mpz_t*, bit_number);

但是由此带来的加速很小,仍然存在数据间接和长度处理.

But the speedup from this is small, there is still data-indirection and length-handling.

您可以直接使用肢体,切换到mpn_ 低级函数:

You can use limbs directly with switching to mpn_ low-level functions:

http://www.gnu.org/software/gmp/manual/html_node/Low-level-Functions.html#Low-level%20Functions

不会有指向四肢的指针(这对缓存有好处),没有简单的输入/输出代码;并且没有自动肢体大小处理(也不是自动扩展;也不是分配).你应该自己做所有的存储;甚至可能有些进位必须手动处理,但是会有GMP的快速*/%操作;您可以使用 mpz_t t;t._mp_size = t._mp_alloc=limb_number;t._mp_d=pointer_to_limb_array.

There will be no pointer to limbs (this is good for cache), no easy input/output code; and no automatic limbs size handling (nor auto-expand; nor allocation). You should do all storage by yourself; may be even some carry must be handled manually, but there will be GMP's fast *, / and % operations; you can reconstruct mpz_t for easy input/output with mpz_t t;t._mp_size = t._mp_alloc=limb_number;t._mp_d=pointer_to_limb_array.

此外,如果您要切换到 64 位 mingw,则可以使用 uint128_t.

Also, you just can use uint128_t if you will switch to 64-bit mingw.

这篇关于固定大小的整数与 gmp ......?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆