简体   繁体   中英

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 appreciated!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM