如何浮点转换用C究竟做++?(双浮动或浮动双) [英] How is floating point conversion actually done in C++?(double to float or float to double)

查看:120
本文介绍了如何浮点转换用C究竟做++?(双浮动或浮动双)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我搜索关于这个话题,发现没有什么相关了。

So I've searched about this topic and found nothing really relevant about it.

我想看看身后这个简单的code装配:

I've tried to look at the assembly behind this simple code :

int main(int argc, char *argv[])
{
    double d = 1.0;
    float f = static_cast<float>(d);

    system("PAUSE");
    return 0;
}

这是(与Visual Studio 2012):

which is (with Visual Studio 2012) :

    15:     double d = 1.0;
000000013FD7C16D  movsd       xmm0,mmword ptr [__real@3ff0000000000000 (013FD91AB0h)]  
000000013FD7C175  movsd       mmword ptr [d],xmm0  
    16:     float f = static_cast<float>(d);
000000013FD7C17B  cvtsd2ss    xmm0,mmword ptr [d]  
000000013FD7C181  movss       dword ptr [f],xmm0

我不大会,舒适,但试图分析认为反正。
因此,前两行似乎对双precision值 3ff0000000000000 进入一个寄存器,然后寄存器的内容移动到D的内存ADRESS。

I'm not that comfortable with assembly but tried to analyze that anyway. So the first two lines seems to move the double-precision value 3ff0000000000000 into a register, and then move the content of the register to the memory adress of d.

然后,我只是不知道到底是什么做的下一行。在 cvtsd2ss 的操作显然是一个指令,该指令双转换precision浮点值单precision浮点值,但我找不到什么该指令实际执行。
(转换后的值被移动到F的内存空间)。

Then, I just don't know exactly what does the next lines. The cvtsd2ss operation is apparently an instruction that convert double precision floating point value to single precision floating point value but I couldn't find what this instruction actually does. (Then the converted value is moved to the memory space of f).

我的问题是,这是怎么转换实际上是由该指令做了什么?我知道C ++投将产生最接近的值中的其他类型,但除此之外,我不知道实际的操作思路进行...

So my question is, how is this conversion actually done by this instruction ? I know that the C++ cast will yield the closest value in the other type but apart from that, I have no idea about the actual operations performed...

推荐答案

cvtsd2ss 指令使用FPU的舍入模式进行转换。默认的舍入模式是舍到最近的偶数。

The cvtsd2ss instruction uses the FPU's rounding mode to do the conversion. The default rounding mode is round-to-nearest-even.

为了遵循算法,它有助于记住的信息在 IEEE 754-1985维基百科页面,尤其是图重新presenting布局

In order to follow the algorithm, it helps to keep in mind the information at the IEEE 754-1985 Wikipedia page, especially the diagrams representing the layout.

首先,目标的指数浮动的计算:在双击类型具有更宽的范围比浮动,那么结果可能是 0.0 (或规格化)一个非常小的,或为无限的价值非常大一倍。

First, the exponent of the target float is computed: the double type has a wider range than float, so the result may be 0.0f (or a denormal) for a very small double, or an infinite value for a very large double.

对于一个正常的的通常情况下双被转换为普通的浮动(大致,当偏指数在单precision重新presentation的8位psented的双击可重新$ p $),目的地有效数字的前23位开始时一样的最显著原来的号码的52位有效数字。

For the usual case of a normal double being converted to a normal float (roughly, when the unbiased exponent of the double can be represented in the 8 bits of a single-precision representation), the first 23 bits of the destination significand start out the same as the most significant of the original number's 52-bit significand.

再有就是四舍五入的问题:

Then there is the problem of rounding:


  • 如果左位上都低于 10..0 ,则目标尾数是保持原样。

  • if the left-over bits are below 10..0, then the target significand is left as-is.

如果左位上高于 10..0 ,则目标尾数递增。如果递增它使溢出(因为它已经是 1..1 ),那么进位传播到指数位。这产生的,因为细心的方式IEEE 754的布局被设计正确的结果。

If the left-over bits are above 10..0, then the target significand is incremented. If incrementing it makes it overflow (because it is already 1..1), then the carry is propagated into the exponent bits. This produces the correct result because of the careful way the IEEE 754 layout has been designed.

如果该位遗留下来的正是 10..0 ,那么双击正是中途之间有两个浮动秒。这两种选择中,一个与最后一个比特 0 (偶)被选中。

If the bits left over are exactly 10..0, then the double is exactly midway between two floats. Of these two choices, the one with the last bit 0 ("even") is chosen.

此步骤后,目标尾数对应于浮动最接近原始的双击

After this step, the target significand corresponds to the float nearest to the original double.

本的舍入模式只是简单。其中目标浮动是规格化的情况稍微更复杂(必须小心,以避免双圆)。

The directed rounding modes are only simpler. The case where the target float is a denormal is slightly more complicated (one must be careful to avoid "double-rounding").

这篇关于如何浮点转换用C究竟做++?(双浮动或浮动双)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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