GMP 算术运算
众所周知,GMP 是处理大型整数的最流行的工具...我有两个关于 GMP 的问题:
- GMP 库中如何进行内部计算? 假设一个整数为两个字节,另一个整数为三个字节,那么内部对这些原始位执行的操作是什么!?
- GMP的运行速度怎么比其他通用库更高啊!?
提前致谢。对我来说了解这些事情对我的项目非常重要。
As we know GMP is the most popular tool for handling large intergers... I have two questions regarding GMP:
- How are internal calculations done in GMP library?
Suppose one integer of two bytes, and another of three bytes, what are the operations performed internally on those raw bits!?? - How is performance speed higher for GMP than other general libraries!?
Thanks in advance. For me to know these about these things is much important for my project.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此页面 (http://gmplib.org/manual/Algorithms.html) 描述了算法GMP 用于其运营。
此外,GMP 库是开源的,因此您可以自行下载和查找。
This page (http://gmplib.org/manual/Algorithms.html) describes the algorithms GMP uses for its operations.
Also, the GMP library is open source so you can download and look for yourself.
GMP 使用本机整数算术来执行所有多精度算术运算。浮点运算太混乱而无法完全信任,这就是为什么 GNU MPFR 依赖 GMP 来进行多精度浮点运算。原因是 IEEE 浮点标准 754/854/等。太麻烦了,从来没有完全实现过,而且不同平台的实现差异很大。
所有 GMP 整数和有理函数都依赖于
mpn_...
函数,这些函数是自然数的低级运算。例如,mpz_...
函数使用符号幅度表示并处理符号,然后使用mpn_...
函数进行幅度运算。基本的 GMP 数字类型是mp_limb_t
类型,在gmp.h
中定义,所有 GMP 操作都在该类型上完成,它是一个unsigned int
、unsigned long int
、unsigned long long int
,具体取决于平台。在几乎所有平台上,所提供的示例(具有 2 和 3 个字节的整数)都适合unsigned int
类型,因此 GMP 将在单次操作中使用本机整数运算来执行最基本的算术。当您计算的数字大于肢体时,GMP 将分配多个肢体来进行多精度计算并适当处理进位(例如,请参见此 书)。请参阅此 GMP 手册条目,了解库如何在肢体上单独运行。这就是 GMP 被广泛使用的原因,它既准确又快速,同时还具有广泛的便携性。 GMP 的 mpn/ 目录包含基本算术运算的特定每处理器实现,以及针对每个最常用处理器的高度优化代码:x86、ARM、CRAY、IA64、mips、powerpcs、sparcs 等每个基本操作都以机器代码中的最佳方式实现,并且 GMP 附带的 Autotools(Autoconf 和 Automake)将知道针对您编译 GMP 的特定目标将哪一个链接到更高级别的函数。请注意,您还可以在编译之前将 GMP 调整到您的计算机,以便它可以针对您可能计算的许多不同整数大小使用最佳算法(请参阅此处)。
GMP uses native integer arithmetic in order to perform all multi-precision arithmetic operations. Floating-point arithmetic is too messy to be fully trusted, that's why GNU MPFR relies on GMP to do multi-precision floating-point operations. The reason is IEEE floating-point standards 754/854/etc. are way too cumbersome, never have been fully implemented, and implementations vary wildly from platform to platform.
All GMP integer and rational functions rely on
mpn_...
functions, which are low-level operations on natural numbers.mpz_...
functions for instance use sign-magnitude representation and handle sign and then usempn_...
functions for magnitude ops. The fundamental GMP numeric type is themp_limb_t
type, defined ingmp.h
, on which all GMP operations are done, is either aunsigned int
,unsigned long int
,unsigned long long int
, depending on the platform. On virtually all platforms, the example provided, integers with 2 and 3 bytes, will fit anunsigned int
type and so GMP will use the native integer operations on a single shot to do most basic arithmetic. When the numbers you compute with are larger than a limb, GMP will allocate more than one limb to do multi-precision calculations and handle the carries appropriately (see for example this book). See this GMP manual entry to see how the library operates separately on limbs.This is the reason why GMP is widely used, it's both accurate and fast, while widely portable. The GMP's
mpn/
directory contains specific per-processor implementations for the basic arithmetic operations, highly optimized code for each of the most used processors: x86, ARMs, CRAYs, IA64, mips, powerpcs, sparcs, etc. Each fundamental operation is implemented in the best possible way in machine code, and the Autotools (Autoconf and Automake) provided with GMP will know which one to link to higher level functions for the specific target you compile GMP. Note that you can also tune GMP to your machine before compiling so it may use the best algorithms for the many different integer sizes you may compute with (see here).