Rev 273 | Blame | Compare with Previous | Last modification | View Log | RSS feed
#ifndef CONVERSION_INCLUDED#define CONVERSION_INCLUDED#include <cmath>/*conversion from:int to doubleint (modulo 100000) to int (modulo 0xFFFF)double to int rounding is done "to nearest" - hence the assembler and not simple static cast*/namespace conversion{#ifdef _WIN64// Constants for conversionsconstexpr double MULTIPLIER_BOB_TO_BOD = 0.65536; // == 65535 / 100000constexpr double MULTIPLIER_BOD_TO_BOB = 0.65536; // == 65535 / 100000constexpr double MULTIPLIER_DOUBLE_TO_INT = 1.52587890625E-05; // == 1 / 65535// Divide and round a double valueinline double divideAndRound(double value) {return std::round(value / MULTIPLIER_DOUBLE_TO_INT);}// Convert double to int with roundinginline int double2int(double d) {return static_cast<int>(divideAndRound(d));}// Convert vertex from bob to bodinline int vertex_bob2bod(int i) {return static_cast<int>(i / MULTIPLIER_BOB_TO_BOD + 0.5);}// Convert vertex from bod to bobinline int vertex_bod2bob(int i) {return static_cast<int>(std::round(i * MULTIPLIER_BOD_TO_BOB));}/*static inline double __fastcall divideAndRound(double value) {const double MULTIPLIER = 1.52587890625E-05; // == 1 / 65535value /= MULTIPLIER;return std::round(value);}static inline int __stdcall double2int(double d){return static_cast<int>(divideAndRound(d));}static int __stdcall vertex_bob2bod(int i){// bod = i / 0.65535static double MULTIPLIER = 0.65536; // == 65535 / 100000return static_cast<int>(i / MULTIPLIER + 0.5f);}static inline int __stdcall vertex_bod2bob(int i){// bob = i * 0.65535static double MULTIPLIER = 0.65536; // == 65535 / 100000double result = MULTIPLIER * i;return static_cast<int>(std::round(result));}*/#else__declspec(naked)staticint __stdcall vertex_bob2bod(int i){// bod = i / 0.65535static double MULTIPLIER = 0.65536; // == 65535 / 100000__asm {fld MULTIPLIERfidivr dword ptr [esp + 4]fistp dword ptr [esp + 4]mov eax, dword ptr [esp + 4]ret 4}}/*inlineint __stdcall vertex_bob2bod(int i){static double MULTIPLIER = 0.65535; // == 65535 / 100000return (int)(i / MULTIPLIER);}*/__declspec(naked)inlineint __stdcall vertex_bod2bob(int i){// bob = i * 0.65535static double MULTIPLIER = 0.65536; // == 65535 / 100000__asm {fld MULTIPLIERfimul dword ptr [esp + 4]fistp dword ptr [esp + 4]mov eax, dword ptr [esp + 4]ret 4}}__declspec(naked)inlineint __stdcall double2int(double d){static const double MULTIPLIER=1.52587890625E-05; // == 1 / 65535// int = right / multiplier__asm{fld qword ptr [esp + 4]fdiv MULTIPLIERfistp dword ptr [esp + 4]mov eax, dword ptr [esp + 4]ret 8}}/*inlineint __stdcall double2int(double d){static const double MULTIPLIER=1.52587890625E-05; // == 1 / 65535return (int)(d / MULTIPLIER);}*/#endif} // namespace conversion#endif // !defined(CONVERSION_INCLUDED)