Subversion Repositories spk

Rev

Rev 273 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#ifndef CONVERSION_INCLUDED
#define CONVERSION_INCLUDED

/*

        conversion from:
                int to double 
                int (modulo 100000) to int (modulo 0xFFFF)
        double to int rounding is done "to nearest" - hence the assembler and not simple static cast

*/

namespace conversion
{

__declspec(naked)
static 
int __stdcall vertex_bob2bod(int i)
{
        // bod = i / 0.65535
        static double MULTIPLIER = 0.65536; // == 65535 / 100000
        __asm {
                fld MULTIPLIER
                fidivr dword ptr [esp + 4]
                fistp dword ptr [esp + 4]
                mov eax, dword ptr [esp + 4]
                ret 4
        }
}

/*inline 
int __stdcall vertex_bob2bod(int i)
{
        static double MULTIPLIER = 0.65535; // == 65535 / 100000 
        return (int)(i / MULTIPLIER);
}*/


__declspec(naked)
inline 
int __stdcall vertex_bod2bob(int i)
{
        // bob = i * 0.65535
        static double MULTIPLIER = 0.65536; // == 65535 / 100000
        __asm {
                fld MULTIPLIER
                fimul dword ptr [esp + 4]
                fistp dword ptr [esp + 4]
                mov eax, dword ptr [esp + 4]
                ret 4
        }
}

__declspec(naked)
inline 
int __stdcall double2int(double d)
{
        static const double MULTIPLIER=1.52587890625E-05; // == 1 / 65535
        
        // int = right / multiplier 
        __asm{
                fld qword ptr [esp + 4]
                fdiv MULTIPLIER
                fistp dword ptr [esp + 4]
                mov eax, dword ptr [esp + 4]
                ret 8
        }
}
/*
inline 
int __stdcall double2int(double d)
{
        static const double MULTIPLIER=1.52587890625E-05; // == 1 / 65535
        
        return (int)(d / MULTIPLIER);
        
}*/

} // namespace conversion

#endif // !defined(CONVERSION_INCLUDED)