为什么我的处理器没有内置的BigInt支持? [英] Why doesn't my processor have built-in BigInt support?

查看:75
本文介绍了为什么我的处理器没有内置的BigInt支持?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,BigInts通常在大多数编程语言中以包含数字的数组的形式实现,例如:当将两个数字相加时,每个数字都会像我们在学校学到的那样一个接一个地添加,例如:

As far as I understood it, BigInts are usually implemented in most programming languages as arrays containing digits, where, eg.: when adding two of them, each digit is added one after another like we know it from school, e.g.:

 246
 816
 * *
----
1062

*表示存在溢出.我在学校是通过这种方式学习的,所有BigInt添加的功能我都实现了与上面的示例类似的工作.

Where * marks that there was an overflow. I learned it this way at school and all BigInt adding functions I've implemented work similar to the example above.

所以我们都知道我们的处理器只能本地管理从0到2^32/2^64的整数.

So we all know that our processors can only natively manage ints from 0 to 2^32 / 2^64.

这意味着大多数脚本语言为了具有高级功能并提供具有大整数的算术运算,必须实现/使用像上述那样将整数与整数一起使用的BigInt库. 但这当然意味着它们将比处理器慢得多.

That means that most scripting languages in order to be high-level and offer arithmetics with big integers, have to implement/use BigInt libraries that work with integers as arrays like above. But of course this means that they'll be far slower than the processor.

所以我问自己:

  • 为什么我的处理器没有内置的BigInt函数?

它可以像任何其他BigInt库一样工作,只是(很多)更快,并且级别更低:处理器从缓存/RAM中获取一位数字,将其添加,然后再次写回结果.

It would work like any other BigInt library, only (a lot) faster and at a lower level: Processor fetches one digit from the cache/RAM, adds it, and writes the result back again.

在我看来,这是个好主意,那么为什么没有这样的东西?

Seems like a fine idea to me, so why isn't there something like that?

推荐答案

存在太多问题,要求处理器处理大量的工作,而这不是它的工作.

There are simply too many issues that require the processor to deal with a ton of stuff which isn't its job.

假设处理器DID具有该功能.我们可以设计出一个系统,在该系统中我们知道给定的BigInt使用了多少字节-只需使用与大多数字符串库相同的原理并记录其长度即可.

Suppose that the processor DID have that feature. We can work out a system where we know how many bytes are used by a given BigInt - just use the same principle as most string libraries and record the length.

但是,如果BigInt操作的结果超出保留的空间量会发生什么?

But what would happen if the result of a BigInt operation exceeded the amount of space reserved?

有两种选择:

  1. 它将包裹在它确实有的空间内 或
  2. 它将使用更多的内存.
  1. It'll wrap around inside the space it does have or
  2. It'll use more memory.

问题是,如果执行了1),那么它就没用了-您必须事先知道需要多少空间,这就是您要使用BigInt的原因的一部分-因此,您不必受那些东西的限制.

The thing is, if it did 1), then it's useless - you'd have to know how much space was required beforehand, and that's part of the reason you'd want to use a BigInt - so you're not limited by those things.

如果执行2),则必须以某种方式分配该内存.跨操作系统的内存分配方式并不相同,但是即使分配了内存,仍然必须将所有指针更新为旧值.它怎么知道该值的指针是什么,以及与所涉及的内存地址包含相同值的简单整数值?

If it did 2), then it'll have to allocate that memory somehow. Memory allocation is not done in the same way across OSes, but even if it were, it would still have to update all pointers to the old value. How would it know what were pointers to the value, and what were simply integer values containing the same value as the memory address in question?

这篇关于为什么我的处理器没有内置的BigInt支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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