float变量如何自动提升为double类型? [英] How is float variable auto-promoted to double type?

查看:515
本文介绍了float变量如何自动提升为double类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在C和Java中,float的基本表示形式是IEEE754-32,double是IEEE754-64.

I know in C and Java, float's underlying representation is IEEE754-32, double is IEEE754-64.

在表达式中,float将自动提升为double.又怎样? 以3.7f为例.这样的过程吗?

In expressions, float will be auto-promoted to double. So how? Take 3.7f for example. Is the process like this?

  1. 3.7f将使用IEEE754在内存中表示.它可以容纳4个字节.
  2. 在计算期间,可以将其加载到64位寄存器(或任何64位位置)中,从而将3.7f转换为IEEE754-64表示.

推荐答案

它非常依赖于实现.

例如,在x86平台上,FPU命令集包括用于以IEEE754 floatdouble格式(以及许多其他格式)加载/存储数据的命令.数据被加载到具有80位宽度的内部FPU寄存器中.因此,实际上在x86上,所有浮点计算都以80位浮点精度执行.也就是说,所有浮点数据实际上都提升为80位精度.这些寄存器中的数据表示方式完全无关紧要,因为无论如何您都无法直接观察它们.

For one example, on x86 platform the set of FPU commands includes commands for loading/storing data in IEEE754 float and double formats (as well as many other formats). The data is loaded into the internal FPU registers that have 80-bit width. So in reality on x86 all floating-point calculations are performed with 80-bit floating-point precision. i.e. all floating-point data is actually promoted to 80-bit precision. How is data represented inside those registers is completely irrelevant, since you cannot observe them directly anyway.

这意味着在x86平台上没有单步浮点到双精度转换之类的东西.每当需要进行这种转换时,它实际上都实现为两步转换:float-to-internal-fpu和internal-fpu-double.

This means that on x86 platform there's no such thing as a single-step float-to-double conversion. Whenever a need for such conversion arises, it is actually implemented as two-step conversion: float-to-internal-fpu and internal-fpu-to-double.

此BTW在x86 FPU计算模型和C/C ++计算模型之间造成了显着的语义差异.为了完全匹配语言模型,处理器必须强制降低中间浮点结果的精度,从而对性能产生负面影响.许多编译器为用户提供了控制FPU计算模型的选项,从而允许用户选择严格的C/C ++一致性,更好的性能或介于两者之间.

This BTW created a significant semantic difference between x86 FPU computation model and C/C++ computation models. In order to fully match the language model the processor has to forcefully reduce precision of intermediate floating-point results, thus negatively affecting performance. Many compilers provide user with options that control FPU computation model, allowing the user to opt for strict C/C++ conformance, better performance or something in between.

几年前,FPU单元是x86平台的可选组件.在无FPU平台上的浮点计算是在软件中执行的,方法是模拟FPU或生成完全没有任何FPU指令的代码.在这种实现中,事情可能会有所不同,例如,直接执行从IEEE754 float到IEEE754 double的软件转换.

Not so many years ago FPU unit was an optional component of x86 platform. Floating-point computations on FPU-less platforms were performed in software, either by emulating FPU or by generating code without any FPU instructions at all. In such implementations things could work differently, like, for example, perform software conversion from IEEE754 float to IEEE754 double directly.

这篇关于float变量如何自动提升为double类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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