C 中的整数是假定由硬件规范处理还是由软件处理?
C 中的整数是假定由硬件规范处理还是由软件处理?
对于整数,我指的是原始“int”。
其基本思想是,如果 C 中的整数不依赖于硬件,那么让 gcc 实现不同的整数处理程序会违反标准。 这样您就可以拥有传统的 32 位 int 处理程序,然后您还可以拥有 256 位整数甚至动态大小的 int 的处理程序。
我确实知道 long 和 long long,但我不确定它们是否独立于提供的硬件,并且我只想指定构建工具链时要使用的“int”处理程序的大小/类型。
我也理解这样做的危险(因为用 32 位整数编译器构建 256 位整数会很糟糕!),但对于那些需要 gmp 库之类的代码,我认为这会使代码可读性更好。 兼容性是相同的,但依赖性将取决于编译器而不是代码本身。
疯狂的想法,我知道......但回到最初的问题:
C 中的整数是假定由硬件规范处理还是由软件处理?
Are integers in C assumed to be handled by a hardware spec or handled in software?
By integer, I am referring to the primitive "int"
The underlying idea being that if integers in C are not hardware dependent would it be a violation of standard to have gcc implement different integer handlers. This way you could have your traditional 32 bit int handler, and then you could also have handlers for 256 bit integers, or maybe even dynamic sized ints.
I do know about long and long long, but I'm not sure if those are independent of that hardware provided, and I'd like to just specify the size/type of "int" handler to use when building a toolchain.
I also understand the dangers of doing this (because building building 256 bit integers with a 32 bit integer compiler would be bad!), but for those bits of code that require something like a gmp library, I think it would make code readability much better. Compatibility would be the same but the dependency would be on the compiler instead of the code itself.
Crazy idea, I know... but back to the original question:
Are integers in C assumed to be handled by a hardware spec or handled in software?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
标准说
的描述限制INT_MIN
最多为 -32767,INT_MAX
至少为 32767。因此,如果您的硬件有一个 12 位的字,您必须使用两个字来表示int
。The standard says
And the description of
<limits.h>
constraintsINT_MIN
to be at most -32767 andINT_MAX
to be at least 32767. So if your hardware has a word of 12 bits, you have to use two words for anint
.是的,int 将以本机大小进行处理。 因此,
sizeof(int)
可能会根据您编译和运行的系统提供不同的值。int
的所有数学运算都将由 CPU 的本机指令处理 - 比在软件中执行要快得多。 如果您需要 int256,则需要自己编写。 我确信那里有任意大小的整数库。Yes, int is going to be handled in the native size. So
sizeof(int)
might give you a different value depending on what system you compile and run on. All the math for anint
is going to be handled by the CPU's native instructions--much faster than doing it in software. If you need an int256, you will need to write that yourself. And I'm sure there are arbitrary-sized integer libraries out there.实现 256 位 int 的 AC 实现将完全符合 C 标准(但请注意,它也必须使 long int 和 long long int 至少那么长)。
然而,这样的编译器生成的代码通常不与同一平台上的其他编译器生成的代码链接兼容,这是它在实践中没有这样做的原因之一。
A C implementation which implemented 256 bit ints would be perfectly compliant with the C standard (but note that it has to make long int and long long int at least that long too).
However code produced by such a compiler would generally not be link-compatible with code produced by other compilers on the same platform, which is one reason it's not done in practice.