2010-06-03 166 views
4

從擴展精度浮點數(80位值,也稱爲「long」雙倍「在一些編譯器)到MSVC win32/win64雙倍(64位)?MSVC win32:將擴展精度浮點數(80位)轉換爲雙精度浮點數(64位)

MSVC目前(截至2010年)假設「long double」是「double」的同義詞。

我大概可以在inline asm中編寫fld/fstp彙編程序對,但內聯asm不適用於MSVC中的win64代碼。我是否需要將此彙編代碼移到單獨的.asm文件中?這真的是沒有好的解決方案嗎?

回答

4

只是做這在x86代碼...

.686P 
    .XMM 

_TEXT SEGMENT 

EXTRN __fltused:DWORD 

PUBLIC _cvt80to64 
PUBLIC _cvt64to80 

_cvt80to64 PROC 

    mov eax, dword ptr [esp+4] 
    fld TBYTE PTR [eax] 

    ret 0 
_cvt80to64 ENDP 


_cvt64to80 PROC 
    mov eax, DWORD PTR [esp+12] 
    fld QWORD PTR [esp+4] 
    fstp TBYTE PTR [eax] 
    ret 0 
_cvt64to80 ENDP 

ENDIF 

_TEXT ENDS 
    END 
4

如果您的編譯器/平臺本身不支持80位浮點值,則必須自行解碼該值。

假設80位浮點存儲一個字節的緩衝區內,坐落在一個特定的偏移量,你可以做這樣的:

float64 C_IOHandler::readFloat80(IColl<uint8> buffer, uint32 *ref_offset) 
{ 
    uint32 &offset = *ref_offset; 

    //80 bit floating point value according to the IEEE-754 specification and the Standard Apple Numeric Environment specification: 
    //1 bit sign, 15 bit exponent, 1 bit normalization indication, 63 bit mantissa 

    float64 sign; 
    if ((buffer[offset] & 0x80) == 0x00) 
     sign = 1; 
    else 
     sign = -1; 
    uint32 exponent = (((uint32)buffer[offset] & 0x7F) << 8) | (uint32)buffer[offset + 1]; 
    uint64 mantissa = readUInt64BE(buffer, offset + 2); 

    //If the highest bit of the mantissa is set, then this is a normalized number. 
    float64 normalizeCorrection; 
    if ((mantissa & 0x8000000000000000) != 0x00) 
     normalizeCorrection = 1; 
    else 
     normalizeCorrection = 0; 
    mantissa &= 0x7FFFFFFFFFFFFFFF; 

    offset += 10; 

    //value = (-1)^s * (normalizeCorrection + m/2^63) * 2^(e - 16383) 
    return (sign * (normalizeCorrection + (float64)mantissa/((uint64)1 << 63)) * g_Math->toPower(2, (int32)exponent - 16383)); 
} 

這是我做了,而且它編譯與克細++ 4.5.0。它當然不是一個非常快速的解決方案,但至少是一個功能強大的解決方案。這個代碼也應該可以移植到不同的平臺上,儘管我沒有嘗試過。

+0

此代碼假定數據爲大端格式。 – Matt 2012-04-28 05:41:07

0

玩過與給定答案,結束了這一點。

#include <cmath> 
#include <limits> 
#include <cassert> 

#ifndef _M_X64 

__inline __declspec(naked) double _cvt80to64(void*) { 
    __asm { 
    // PUBLIC _cvt80to64 PROC 

    mov eax, dword ptr [esp+4] 
    fld TBYTE PTR [eax] 

    ret 0 
    // _cvt80to64 ENDP 
    } 
} 

#endif 

#pragma pack(push) 
#pragma pack(2) 
typedef unsigned char tDouble80[10]; 
#pragma pack(pop) 


typedef struct { 
    unsigned __int64 mantissa:64; 
    unsigned int exponent:15; 
    unsigned int sign:1; 
} tDouble80Struct; 

inline double convertDouble80(const tDouble80& val) 
{ 
    assert(10 == sizeof(tDouble80)); 

    const tDouble80Struct* valStruct = reinterpret_cast<const tDouble80Struct*>(&val); 

    const unsigned int mask_exponent = (1 << 15) - 1; 
    const unsigned __int64 mantissa_high_highestbit = unsigned __int64(1) << 63; 
    const unsigned __int64 mask_mantissa = (unsigned __int64(1) << 63) - 1; 

    if (mask_exponent == valStruct->exponent) { 

    if(0 == valStruct->mantissa) { 
     return (0 != valStruct->sign) ? -std::numeric_limits<double>::infinity() : std::numeric_limits<double>::infinity(); 
    } 

    // highest mantissa bit set means quiet NaN 
    return (0 != (mantissa_high_highestbit & valStruct->mantissa)) ? std::numeric_limits<double>::quiet_NaN() : std::numeric_limits<double>::signaling_NaN(); 
    } 

    // 80 bit floating point value according to the IEEE-754 specification and 
    // the Standard Apple Numeric Environment specification: 
    // 1 bit sign, 15 bit exponent, 1 bit normalization indication, 63 bit mantissa 

    const double sign(valStruct->sign ? -1 : 1); 


    //If the highest bit of the mantissa is set, then this is a normalized number. 
    unsigned __int64 mantissa = valStruct->mantissa; 
    double normalizeCorrection = (mantissa & mantissa_high_highestbit) != 0 ? 1 : 0; 
    mantissa &= mask_mantissa; 

    //value = (-1)^s * (normalizeCorrection + m/2^63) * 2^(e - 16383) 
    return (sign * (normalizeCorrection + double(mantissa)/mantissa_high_highestbit) * pow(2.0, int(valStruct->exponent) - 16383)); 
} 
1

我剛寫了這一個。它使用位操作從IEEE擴展精確數構造一個IEEE雙數。它需要10個字節的擴展精度數以小尾數格式:

typedef unsigned long long uint64; 

double makeDoubleFromExtended(const unsigned char x[10]) 
{ 
    int exponent = (((x[9] << 8) | x[8]) & 0x7FFF); 
    uint64 mantissa = 
     ((uint64)x[7] << 56) | ((uint64)x[6] << 48) | ((uint64)x[5] << 40) | ((uint64)x[4] << 32) | 
     ((uint64)x[3] << 24) | ((uint64)x[2] << 16) | ((uint64)x[1] << 8) | (uint64)x[0]; 
    unsigned char d[8] = {0}; 
    double result; 

    d[7] = x[9] & 0x80; /* Set sign. */ 

    if ((exponent == 0x7FFF) || (exponent == 0)) 
    { 
     /* Infinite, NaN or denormal */ 
     if (exponent == 0x7FFF) 
     { 
      /* Infinite or NaN */ 
      d[7] |= 0x7F; 
      d[6] = 0xF0; 
     } 
     else 
     { 
      /* Otherwise it's denormal. It cannot be represented as double. Translate as singed zero. */ 
      memcpy(&result, d, 8); 
      return result; 
     } 
    } 
    else 
    { 
     /* Normal number. */ 
     exponent = exponent - 0x3FFF + 0x03FF; /*< exponent for double precision. */ 

     if (exponent <= -52) /*< Too small to represent. Translate as (signed) zero. */ 
     { 
      memcpy(&result, d, 8); 
      return result; 
     } 
     else if (exponent < 0) 
     { 
      /* Denormal, exponent bits are already zero here. */ 
     } 
     else if (exponent >= 0x7FF) /*< Too large to represent. Translate as infinite. */ 
     { 
      d[7] |= 0x7F; 
      d[6] = 0xF0; 
      memset(d, 0x00, 6); 
      memcpy(&result, d, 8); 
      return result; 
     } 
     else 
     { 
      /* Representable number */ 
      d[7] |= (exponent & 0x7F0) >> 4; 
      d[6] |= (exponent & 0xF) << 4; 
     } 
    } 
    /* Translate mantissa. */ 

    mantissa >>= 11; 

    if (exponent < 0) 
    { 
     /* Denormal, further shifting is required here. */ 
     mantissa >>= (-exponent + 1); 
    } 

    d[0] = mantissa & 0xFF; 
    d[1] = (mantissa >> 8) & 0xFF; 
    d[2] = (mantissa >> 16) & 0xFF; 
    d[3] = (mantissa >> 24) & 0xFF; 
    d[4] = (mantissa >> 32) & 0xFF; 
    d[5] = (mantissa >> 40) & 0xFF; 
    d[6] |= (mantissa >> 48) & 0x0F; 

    memcpy(&result, d, 8); 

    printf("Result: 0x%016llx", *(uint64*)(&result)); 

    return result; 
} 
+0

我認爲'if(exponent <= 0)'的處理意味着可以表示爲binary64 subnormals的數字最終表示爲'0.0'。 – 2013-09-17 16:57:56

+0

修正了它。確切的'exponent == 0'的情況下的確可以產生一個denormal double。 – Calmarius 2013-09-17 20:08:05

+0

這仍然不是很正確:有些情況下,當'exponent'介於-52和0之間(在代碼的那一點)時,必須做的事情是**大致**到使隱含位顯式化,通過'-exponent'移動要使用的有效數字,並將'exponent'設置爲零。 OP最終使用'FSTP',所以它不是非常重要,但是你需要在你的模擬器中爲'FSTP'執行它。:) – 2013-09-18 13:33:02