Subversion Repositories spk

Rev

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

Rev Author Line No. Line
114 cycrow 1
#ifndef CONVERSION_INCLUDED
2
#define CONVERSION_INCLUDED
3
 
273 cycrow 4
#include <cmath>
114 cycrow 5
/*
6
 
7
	conversion from:
8
		int to double 
9
		int (modulo 100000) to int (modulo 0xFFFF)
10
	double to int rounding is done "to nearest" - hence the assembler and not simple static cast
11
 
12
*/
13
 
14
namespace conversion
15
{
273 cycrow 16
#ifdef _WIN64
114 cycrow 17
 
273 cycrow 18
	static inline double __fastcall divideAndRound(double value) {
19
		const double MULTIPLIER = 1.52587890625E-05; // == 1 / 65535
20
 
21
		value /= MULTIPLIER;
22
		return std::round(value);
23
	}
24
 
25
	static inline int __stdcall double2int(double d)
26
	{
27
		return static_cast<int>(divideAndRound(d));
28
	}
29
 
30
	static int __stdcall vertex_bob2bod(int i)
31
	{
32
		// bod = i / 0.65535
33
		static double MULTIPLIER = 0.65536; // == 65535 / 100000
34
		return static_cast<int>(i / MULTIPLIER + 0.5f);
35
	}
36
 
37
	static inline int __stdcall vertex_bod2bob(int i)
38
	{
39
		// bob = i * 0.65535
40
		static double MULTIPLIER = 0.65536; // == 65535 / 100000
41
 
42
		double result = MULTIPLIER * i;
43
		return static_cast<int>(std::round(result));
44
	}
45
 
46
#else
47
 
114 cycrow 48
__declspec(naked)
49
static 
50
int __stdcall vertex_bob2bod(int i)
51
{
52
	// bod = i / 0.65535
53
	static double MULTIPLIER = 0.65536; // == 65535 / 100000
54
	__asm {
55
		fld MULTIPLIER
56
		fidivr dword ptr [esp + 4]
57
		fistp dword ptr [esp + 4]
58
		mov eax, dword ptr [esp + 4]
59
		ret 4
60
	}
61
}
62
 
63
/*inline 
64
int __stdcall vertex_bob2bod(int i)
65
{
66
	static double MULTIPLIER = 0.65535; // == 65535 / 100000 
67
	return (int)(i / MULTIPLIER);
68
}*/
69
 
70
 
71
__declspec(naked)
72
inline 
73
int __stdcall vertex_bod2bob(int i)
74
{
75
	// bob = i * 0.65535
76
	static double MULTIPLIER = 0.65536; // == 65535 / 100000
77
	__asm {
78
		fld MULTIPLIER
79
		fimul dword ptr [esp + 4]
80
		fistp dword ptr [esp + 4]
81
		mov eax, dword ptr [esp + 4]
82
		ret 4
83
	}
84
}
85
 
86
__declspec(naked)
87
inline 
88
int __stdcall double2int(double d)
89
{
90
	static const double MULTIPLIER=1.52587890625E-05; // == 1 / 65535
91
 
92
	// int = right / multiplier 
93
	__asm{
94
		fld qword ptr [esp + 4]
95
		fdiv MULTIPLIER
96
		fistp dword ptr [esp + 4]
97
		mov eax, dword ptr [esp + 4]
98
		ret 8
99
	}
100
}
101
/*
102
inline 
103
int __stdcall double2int(double d)
104
{
105
	static const double MULTIPLIER=1.52587890625E-05; // == 1 / 65535
106
 
107
	return (int)(d / MULTIPLIER);
108
 
109
}*/
273 cycrow 110
#endif
114 cycrow 111
 
112
} // namespace conversion
113
 
114
#endif // !defined(CONVERSION_INCLUDED)