开发者

Converting 32-bit float to IEEE 80-bit

I'm trying to convert a 32-bit float into an extended precision 80-bit float. I'm using MSVC x86. I tried the following inline ASM code:

void Convert32To80(float *value, void *outValue)
{
    __asm
    {
        fld float ptr [value];
        fstp tbyte ptr [outValue];
    }
}

Here, void *outValue is a buffer that is large enough to hold 10 bytes. This looks right to me, but it's crashing when it's run.

Any help is appreci开发者_如何学编程ated!


OK, this should do it:

void Convert32To80(float *value, void *outValue)
{
    __asm
    {
        mov eax,dword ptr [value] 
        fld dword ptr [eax] 
        mov ecx,dword ptr [outValue] 
        fstp tbyte ptr [ecx] 
    }
}

All I did was wrote some C code to do the same, but for a float to double conversion, looked at the dissasembly and then modified as necessary.

Note that I am no expert with MSVC and I'm not 100% sure that I can use the EAX and ECX registers like that without saving/restoring them. Others may know more and offer corrections.


Updated Note for posterity: Apparently, MSVC 2010 has no type for 80bit floating point types, so the obvious solution in C or C++ code along the lines of

float inValue = 666.666f;
long double outValue = (long double)inValue;

does NOT work, and you are actually forced to directly use assembly language.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜