为什么 C99 标准的作者不指定浮点类型大小的标准?
我注意到在Windows和Linux x86上,float是4字节类型,double是8,但long double在x86和x86_64上分别是12和16。 C99 有望通过特定的整体尺寸打破此类障碍。
最初的技术限制似乎是由于 x86 处理器无法处理超过 80 位浮点运算(加上 2 个字节进行四舍五入),但为什么与 int 类型相比标准不一致?为什么他们不至少进行 80 位标准化?
I noticed on Windows and Linux x86, float is a 4-byte type, double is 8, but long double is 12 and 16 on x86 and x86_64 respectively. C99 is supposed to be breaking such barriers with the specific integral sizes.
The initial technological limitation appears to be due to the x86 processor not being able to handle more than 80-bit floating point operations (plus 2 bytes to round it up) but why the inconsistency in the standard compared to int types? Why don't they go at least to 80-bit standardization?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(4)
C99 应该通过特定的整数大小来打破这些障碍。
不,那些固定宽度的(u)intN_t
类型完全是可选,因为并非所有处理器都使用 2 的幂的类型大小。C99 仅要求 (u)int_fastN _t
和 (u)int_leastN_t
待定义。这意味着为什么标准与 int 类型不一致的前提是完全错误的,因为 int 类型的大小没有一致性。
许多现代 DSP 对 24 位音频使用 24 位字。甚至还有 20 位 DSP,例如 Zoran ZR3800x 系列 或 < a href="https://www.analog.com/media/en/technical-documentation/data-sheets/ADAU1701.pdf" rel="nofollow noreferrer">28 位 DSP,例如 ADAU1701 允许在不削波的情况下转换 16/24 位音频。许多 32 位或 64 位架构还具有一些奇数大小的寄存器,以允许累积值而不会溢出,例如 TI C5500/C6000 与 40 位长
和 SHARC 带有 80 位累加器。 Motorola DSP5600x/3xx 系列 也有奇怪的大小:2 字节短, 3 字节 int,6 字节长。过去有很多架构具有其他字号,例如 12、18、 36 位、60 位...以及许多使用符号量补码的 CPU。请参阅标准委员会关心的奇异架构
C 被设计为灵活以支持各种类型这样的平台。指定固定大小,无论是整数还是浮点类型,都达不到这个目的。就像整数支持一样,硬件中的浮点支持也有很大差异。有不同的格式使用十进制、十六进制或可能的其他基数。每种格式都有不同的指数/尾数大小、不同的符号/指数/尾数位置,甚至有符号格式。例如,有些使用二进制补码作为尾数,而另一些使用二进制补码作为指数或整个浮点值。您可以在此处看到多种格式,但这显然不是所有存在的格式。例如上面的 SHARC 具有特殊的 40 位浮点格式。某些平台还使用 双精度算术 进行 长双精度
。另请参阅
这意味着您无法为所有平台标准化单一浮点格式,因为没有一个 -适合所有人的解决方案。如果您正在设计 DSP,那么显然您需要一种最适合您用途的格式,以便您可以搅动尽可能多的数据。当 40 位格式对您的应用程序具有足够的精度、更适合缓存并且需要的芯片尺寸小得多时,没有理由使用 IEEE-754 二进制 64。或者,如果您使用的是小型嵌入式系统,则 80 位 long double
通常是无用的,因为您甚至没有足够的 ROM 用于该 80 位 long double
库。这就是为什么某些平台将 long double
限制为 64 位,例如 double
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
C语言没有指定各种类型的实现,因此它可以在尽可能广泛的硬件上有效地实现。
这也扩展到整数类型 - C 标准整数类型具有最小范围(例如,
signed char
为 -127 到 127,short
和int
均为 -32,767 到 32,767,long
为 -2,147,483,647 到 2,147,483,647,long long
为 -9,223,372,036,854,775,807 到 9,223,372,036,854,775, 807)。对于几乎所有目的,这就是程序员需要知道的全部内容。C99 确实提供了“固定宽度”整数类型,例如
int32_t
- 但这些是可选 - 如果实现无法有效地提供此类类型,则它没有提供它。对于浮点类型,有等效的限制(例如,
double
必须具有至少 10 位十进制数字的精度)。The C language doesn't specify the implementation of various types, so that it can be efficiently implemented on as wide a variety of hardware as possible.
This extends to the integer types too - the C standard integral types have minimum ranges (eg.
signed char
is -127 to 127,short
andint
are both -32,767 to 32,767,long
is -2,147,483,647 to 2,147,483,647, andlong long
is -9,223,372,036,854,775,807 to 9,223,372,036,854,775,807). For almost all purposes, this is all that the programmer needs to know.C99 does provide "fixed-width" integer types, like
int32_t
- but these are optional - if the implementation can't provide such a type efficiently, it doesn't have to provide it.For floating point types, there are equivalent limits (eg
double
must have at least 10 decimal digits worth of precision).