C 中的整数是假定由硬件规范处理还是由软件处理?

发布于 2024-07-29 16:26:56 字数 448 浏览 4 评论 0原文

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 技术交流群。

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

发布评论

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

评论(3

小ぇ时光︴ 2024-08-05 16:26:56

标准说

“普通”int 对象具有执行环境架构建议的自然大小(大到足以包含 INT_MIN范围内的任何值) INT_MAX 如标头 中所定义。

的描述限制 INT_MIN 最多为 -32767,INT_MAX 至少为 32767。因此,如果您的硬件有一个 12 位的字,您必须使用两个字来表示 int

The standard says

A "plain" int object has the natural size suggested by the architecture of the execution environment (large enough to contain any value in the range INT_MIN to INT_MAX as defined in the header <limits.h>).

And the description of <limits.h> constraints INT_MIN to be at most -32767 and INT_MAX to be at least 32767. So if your hardware has a word of 12 bits, you have to use two words for an int.

残疾 2024-08-05 16:26:56

是的,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 an int 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.

翻了热茶 2024-08-05 16:26:56

实现 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.

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