Visual Studio 2012 Support
[xy_vsfilter.git] / src / thirdparty / VirtualDub / h / vd2 / system / math.h
blob221035bb8e0daf44f352266d8a4731620ea20897
1 // VirtualDub - Video processing and capture application
2 // System library component
3 // Copyright (C) 1998-2004 Avery Lee, All Rights Reserved.
4 //
5 // Beginning with 1.6.0, the VirtualDub system library is licensed
6 // differently than the remainder of VirtualDub. This particular file is
7 // thus licensed as follows (the "zlib" license):
8 //
9 // This software is provided 'as-is', without any express or implied
10 // warranty. In no event will the authors be held liable for any
11 // damages arising from the use of this software.
13 // Permission is granted to anyone to use this software for any purpose,
14 // including commercial applications, and to alter it and redistribute it
15 // freely, subject to the following restrictions:
17 // 1. The origin of this software must not be misrepresented; you must
18 // not claim that you wrote the original software. If you use this
19 // software in a product, an acknowledgment in the product
20 // documentation would be appreciated but is not required.
21 // 2. Altered source versions must be plainly marked as such, and must
22 // not be misrepresented as being the original software.
23 // 3. This notice may not be removed or altered from any source
24 // distribution.
26 #ifndef f_VD2_SYSTEM_MATH_H
27 #define f_VD2_SYSTEM_MATH_H
29 #include <math.h>
30 #include <vd2/system/vdtypes.h>
32 // Constants
33 namespace nsVDMath {
34 static const float kfPi = 3.1415926535897932384626433832795f;
35 static const double krPi = 3.1415926535897932384626433832795;
36 static const float kfTwoPi = 6.283185307179586476925286766559f;
37 static const double krTwoPi = 6.283185307179586476925286766559;
38 static const float kfLn2 = 0.69314718055994530941723212145818f;
39 static const double krLn2 = 0.69314718055994530941723212145818;
40 static const float kfLn10 = 2.3025850929940456840179914546844f;
41 static const double krLn10 = 2.3025850929940456840179914546844;
42 static const float kfOneOverLn10 = 0.43429448190325182765112891891661f;
43 static const double krOneOverLn10 = 0.43429448190325182765112891891661;
46 ///////////////////////////////////////////////////////////////////////////
47 // Integer clamping functions
49 #ifdef _M_IX86
50 inline uint32 VDClampToUint32(uint64 v) {
51 return v >= 0x100000000UL ? 0xFFFFFFFFUL : (uint32)v;
54 inline uint32 VDClampToUint32(sint64 v) {
55 union U {
56 __int64 v64;
57 struct {
58 unsigned lo;
59 int hi;
60 } v32;
63 return ((U *)&v)->v32.hi ? ~(((U *)&v)->v32.hi >> 31) : ((U *)&v)->v32.lo;
66 inline uint32 VDClampToUint32(sint32 v) {
67 return v < 0 ? 0 : (uint32)v;
69 #else
70 inline uint32 VDClampToUint32(sint64 v) {
71 uint32 r = (uint32)v;
72 return r == v ? r : (uint32)~(sint32)(v>>63);
74 #endif
76 inline sint32 VDClampToSint32(uint32 v) {
77 return (v | ((sint32)v >> 31)) & 0x7FFFFFFF;
80 inline sint32 VDClampToSint32(sint64 v) {
81 sint32 r = (sint32)v;
82 return r == v ? r : (sint32)(v >> 63) ^ 0x7FFFFFFF;
85 inline uint16 VDClampToUint16(uint32 v) {
86 if (v > 0xffff)
87 v = 0xffff;
88 return (uint16)v;
91 ///////////////////////////////////////////////////////////////////////////
92 // Absolute value functions
93 inline sint64 VDAbs64(sint64 v) {
94 return v<0 ? -v : v;
97 inline ptrdiff_t VDAbsPtrdiff(ptrdiff_t v) {
98 return v<0 ? -v : v;
101 // Rounding functions
103 // Round a double to an int or a long. Behavior is not specified at
104 // int(y)+0.5, if x is NaN or Inf, or if x is out of range.
106 int VDRoundToInt(double x);
107 long VDRoundToLong(double x);
108 sint32 VDRoundToInt32(double x);
109 sint64 VDRoundToInt64(double x);
111 inline sint32 VDRoundToIntFast(float x) {
112 union {
113 float f;
114 sint32 i;
115 } u = {x + 12582912.0f}; // 2^22+2^23
117 return (sint32)u.i - 0x4B400000;
120 inline sint32 VDRoundToIntFastFullRange(double x) {
121 union {
122 double f;
123 sint32 i[2];
124 } u = {x + 6755399441055744.0f}; // 2^51+2^52
126 return (sint32)u.i[0];
129 #if !defined(VD_CPU_X86) || !defined(VD_COMPILER_MSVC)
130 inline sint32 VDFloorToInt(double x) {
131 return (sint32)floor(x);
134 inline sint64 VDFloorToInt64(double x) {
135 return (sint64)floor(x);
137 #else
138 #pragma warning(push)
139 #pragma warning(disable: 4035) // warning C4035: 'VDFloorToInt' : no return value
140 inline sint32 VDFloorToInt(double x) {
141 sint32 temp;
143 __asm {
144 fld x
145 fist temp
146 fild temp
147 mov eax, temp
148 fsub
149 fstp temp
150 cmp temp, 80000001h
151 adc eax, -1
154 inline sint64 VDFloorToInt64(double x) {
155 sint64 temp;
156 sint32 temp2;
158 __asm {
159 fld x
160 fld st(0)
161 fistp qword ptr temp
162 fild qword ptr temp
163 mov eax, dword ptr temp
164 mov edx, dword ptr temp+4
165 fsub
166 fstp dword ptr temp2
167 cmp dword ptr temp2, 80000001h
168 adc eax, -1
169 adc edx, -1
172 #pragma warning(pop)
173 #endif
175 #if !defined(VD_CPU_X86) || !defined(VD_COMPILER_MSVC)
176 inline sint32 VDCeilToInt(double x) {
177 return (sint32)ceil(x);
180 inline sint64 VDCeilToInt64(double x) {
181 return (sint64)ceil(x);
183 #else
184 #pragma warning(push)
185 #pragma warning(disable: 4035) // warning C4035: 'VDCeilToInt' : no return value
186 inline sint32 VDCeilToInt(double x) {
187 sint32 temp;
189 __asm {
190 fld x
191 fist temp
192 fild temp
193 mov eax, temp
194 fsubr
195 fstp temp
196 cmp temp, 80000001h
197 sbb eax, -1
201 inline sint32 VDCeilToInt64(double x) {
202 sint64 temp;
203 sint32 temp2;
205 __asm {
206 fld x
207 fld st(0)
208 fistp temp
209 fild temp
210 mov eax, dword ptr temp
211 mov edx, dword ptr temp+4
212 fsubr
213 fstp temp2
214 cmp temp2, 80000001h
215 sbb eax, -1
216 sbb edx, -1
219 #pragma warning(pop)
220 #endif
222 ///////////////////////////////////////////////////////////////////////////
223 /// Convert a value from [-~1..1] to [-32768, 32767] with clamping.
224 inline sint16 VDClampedRoundFixedToInt16Fast(float x) {
225 union {
226 float f;
227 sint32 i;
228 } u = {x * 65535.0f + 12582912.0f}; // 2^22+2^23
230 sint32 v = (sint32)u.i - 0x4B3F8000;
232 if ((uint32)v >= 0x10000)
233 v = ~v >> 31;
235 return (sint16)(v - 0x8000);
238 /// Convert a value from [0..1] to [0..255] with clamping.
239 inline uint8 VDClampedRoundFixedToUint8Fast(float x) {
240 union {
241 float f;
242 sint32 i;
243 } u = {x * 255.0f + 12582912.0f}; // 2^22+2^23
245 sint32 v = (sint32)u.i - 0x4B400000;
247 if ((uint32)v >= 0x100)
248 v = ~v >> 31;
250 return (uint8)v;
253 ///////////////////////////////////////////////////////////////////////////
255 #ifdef _M_IX86
256 sint64 __stdcall VDFractionScale64(uint64 a, uint32 b, uint32 c, uint32& remainder);
257 uint64 __stdcall VDUMulDiv64x32(uint64 a, uint32 b, uint32 c);
258 #else
259 extern "C" sint64 VDFractionScale64(uint64 a, uint64 b, uint64 c, uint32& remainder);
260 extern "C" uint64 VDUMulDiv64x32(uint64 a, uint32 b, uint32 c);
261 #endif
263 sint64 VDMulDiv64(sint64 a, sint64 b, sint64 c);
265 ///////////////////////////////////////////////////////////////////////////
267 bool VDVerifyFiniteFloats(const float *p, uint32 n);
269 #endif