Are there unsigned equivalents of the x87 FILD and SSE CVTSI2SD instructions?

前端 未结 5 1358
离开以前
离开以前 2021-01-18 20:12

I want to implement the equivalent of C\'s uint-to-double cast in the GHC Haskell compiler. We already implement int-to-double

5条回答
  •  礼貌的吻别
    2021-01-18 20:53

    We already implement int-to-double using FILD ...
    Is there unsigned versions of these operations

    If you want exactly x87 FILD opcode to use, just shift uint64 to uint63 (div 2) and then mul it by 2 back, but already as double, so the x87 uint64-to-double conversion requires one FMUL execution in overhead.

    The example: 0xFFFFFFFFFFFFFFFFU -> +1.8446744073709551e+0019

    it was unable to post the code example in the strict form rules. I'll try later.

        //inline
        double    u64_to_d(unsigned _int64 v){
    
        //volatile double   res;
        volatile unsigned int tmp=2;
        _asm{
        fild  dword ptr tmp
        //v>>=1;
        shr   dword ptr v+4, 1
        rcr   dword ptr v, 1
        fild  qword ptr v
    
        //save lsb
        //mov   byte ptr tmp, 0  
        //rcl   byte ptr tmp, 1
    
        //res=tmp+res*2;
        fmulp st(1),st
        //fild  dword ptr tmp
        //faddp st(1),st 
    
        //fstp  qword ptr res
        }
    
        //return res;
        //fld  qword ptr res
    }

    VC produced x86 output

            //inline
            double    u64_to_d(unsigned _int64 v){
        55                   push        ebp  
        8B EC                mov         ebp,esp  
        81 EC 04 00 00 00    sub         esp,04h  
    
            //volatile double   res;
            volatile unsigned int tmp=2;
        C7 45 FC 02 00 00 00 mov         dword ptr [tmp], 2  
            _asm{
            fild  dword ptr tmp
        DB 45 FC             fild        dword ptr [tmp]  
            //v>>=1;
            shr   dword ptr v+4, 1
        D1 6D 0C             shr         dword ptr [ebp+0Ch],1  
            rcr   dword ptr v, 1
        D1 5D 08             rcr         dword ptr [v],1  
            fild  qword ptr v
        DF 6D 08             fild        qword ptr [v]  
    
            //save lsb
        //    mov   byte ptr [tmp], 0  
        //C6 45 FC 00        mov         byte ptr [tmp], 0
        //    rcl   byte ptr tmp, 1
        //D0 55 FC           rcl         byte ptr [tmp],1  
    
            //res=tmp+res*2;
            fmulp st(1),st
        DE C9                fmulp       st(1),st  
        //    fild  dword ptr tmp
        //DB 45 FC           fild        dword ptr [tmp]  
        //    faddp st(1),st 
        //DE C1              faddp       st(1),st  
    
    
            //fstp  qword ptr res
            //fstp        qword ptr [res]  
        }
    
            //return res;
            //fld         qword ptr [res]  
    
        8B E5                mov         esp,ebp  
        5D                   pop         ebp  
        C3                   ret  
    }

    i posted (probably i manually removed all incorrected ascii chars in text file).

提交回复
热议问题