1 // VirtualDub - Video processing and capture application
2 // System library component
3 // Copyright (C) 1998-2004 Avery Lee, All Rights Reserved.
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):
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
26 #ifndef f_VD2_SYSTEM_MATH_H
27 #define f_VD2_SYSTEM_MATH_H
30 #include <vd2/system/vdtypes.h>
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
50 inline uint32
VDClampToUint32(uint64 v
) {
51 return v
>= 0x100000000UL
? 0xFFFFFFFFUL
: (uint32
)v
;
54 inline uint32
VDClampToUint32(sint64 v
) {
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
;
70 inline uint32
VDClampToUint32(sint64 v
) {
72 return r
== v
? r
: (uint32
)~(sint32
)(v
>>63);
76 inline sint32
VDClampToSint32(uint32 v
) {
77 return (v
| ((sint32
)v
>> 31)) & 0x7FFFFFFF;
80 inline sint32
VDClampToSint32(sint64 v
) {
82 return r
== v
? r
: (sint32
)(v
>> 63) ^ 0x7FFFFFFF;
85 inline uint16
VDClampToUint16(uint32 v
) {
91 ///////////////////////////////////////////////////////////////////////////
92 // Absolute value functions
93 inline sint64
VDAbs64(sint64 v
) {
97 inline ptrdiff_t VDAbsPtrdiff(ptrdiff_t 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
) {
115 } u
= {x
+ 12582912.0f
}; // 2^22+2^23
117 return (sint32
)u
.i
- 0x4B400000;
120 inline sint32
VDRoundToIntFastFullRange(double x
) {
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
);
138 #pragma warning(push)
139 #pragma warning(disable: 4035) // warning C4035: 'VDFloorToInt' : no return value
140 inline sint32
VDFloorToInt(double x
) {
154 inline sint64
VDFloorToInt64(double x
) {
163 mov eax
, dword ptr temp
164 mov edx
, dword ptr temp
+4
167 cmp dword ptr temp2
, 80000001h
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
);
184 #pragma warning(push)
185 #pragma warning(disable: 4035) // warning C4035: 'VDCeilToInt' : no return value
186 inline sint32
VDCeilToInt(double x
) {
201 inline sint32
VDCeilToInt64(double x
) {
210 mov eax
, dword ptr temp
211 mov edx
, dword ptr temp
+4
222 ///////////////////////////////////////////////////////////////////////////
223 /// Convert a value from [-~1..1] to [-32768, 32767] with clamping.
224 inline sint16
VDClampedRoundFixedToInt16Fast(float x
) {
228 } u
= {x
* 65535.0f
+ 12582912.0f
}; // 2^22+2^23
230 sint32 v
= (sint32
)u
.i
- 0x4B3F8000;
232 if ((uint32
)v
>= 0x10000)
235 return (sint16
)(v
- 0x8000);
238 /// Convert a value from [0..1] to [0..255] with clamping.
239 inline uint8
VDClampedRoundFixedToUint8Fast(float x
) {
243 } u
= {x
* 255.0f
+ 12582912.0f
}; // 2^22+2^23
245 sint32 v
= (sint32
)u
.i
- 0x4B400000;
247 if ((uint32
)v
>= 0x100)
253 ///////////////////////////////////////////////////////////////////////////
256 sint64 __stdcall
VDFractionScale64(uint64 a
, uint32 b
, uint32 c
, uint32
& remainder
);
257 uint64 __stdcall
VDUMulDiv64x32(uint64 a
, uint32 b
, uint32 c
);
259 extern "C" sint64
VDFractionScale64(uint64 a
, uint64 b
, uint64 c
, uint32
& remainder
);
260 extern "C" uint64
VDUMulDiv64x32(uint64 a
, uint32 b
, uint32 c
);
263 sint64
VDMulDiv64(sint64 a
, sint64 b
, sint64 c
);
265 ///////////////////////////////////////////////////////////////////////////
267 bool VDVerifyFiniteFloats(const float *p
, uint32 n
);