home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 275 / DPCS0111DVD.ISO / Toolkit / Audio-Visual / VirtualDub / Source / VirtualDub-1.9.10-src.7z / src / h / vd2 / system / math.h < prev    next >
Encoding:
C/C++ Source or Header  |  2009-09-14  |  6.4 KB  |  260 lines

  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.
  12. //
  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:
  16. //
  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.
  25.  
  26. #ifndef f_VD2_SYSTEM_MATH_H
  27. #define f_VD2_SYSTEM_MATH_H
  28.  
  29. #include <math.h>
  30. #include <vd2/system/vdtypes.h>
  31.  
  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;
  44. };
  45.  
  46. ///////////////////////////////////////////////////////////////////////////
  47. // Integer clamping functions
  48. //
  49. #ifdef _M_IX86
  50.     inline uint32 VDClampToUint32(sint64 v) {
  51.         union U {
  52.             __int64 v64;
  53.             struct {
  54.                 unsigned lo;
  55.                 int hi;
  56.             } v32;
  57.         };
  58.  
  59.         return ((U *)&v)->v32.hi ? ~(((U *)&v)->v32.hi >> 31) : ((U *)&v)->v32.lo;
  60.     }
  61. #else
  62.     inline uint32 VDClampToUint32(sint64 v) {
  63.         uint32 r = (uint32)v;
  64.         return r == v ? r : (uint32)~(sint32)(v>>63);
  65.     }
  66. #endif
  67.  
  68. inline sint32 VDClampToSint32(uint32 v) {
  69.     return (v | ((sint32)v >> 31)) & 0x7FFFFFFF;
  70. }
  71.  
  72. inline sint32 VDClampToSint32(sint64 v) {
  73.     sint32 r = (sint32)v;
  74.     return r == v ? r : (sint32)(v >> 63) ^ 0x7FFFFFFF;
  75. }
  76.  
  77. inline uint16 VDClampToUint16(uint32 v) {
  78.     if (v > 0xffff)
  79.         v = 0xffff;
  80.     return (uint16)v;
  81. }
  82.  
  83. ///////////////////////////////////////////////////////////////////////////
  84. // Absolute value functions
  85. inline sint64 VDAbs64(sint64 v) {
  86.     return v<0 ? -v : v;
  87. }
  88.  
  89. inline ptrdiff_t VDAbsPtrdiff(ptrdiff_t v) {
  90.     return v<0 ? -v : v;
  91. }
  92.  
  93. // Rounding functions
  94. //
  95. // Round a double to an int or a long.  Behavior is not specified at
  96. // int(y)+0.5, if x is NaN or Inf, or if x is out of range.
  97.  
  98. int VDRoundToInt(double x);
  99. long VDRoundToLong(double x);
  100. sint32 VDRoundToInt32(double x);
  101. sint64 VDRoundToInt64(double x);
  102.  
  103. inline sint32 VDRoundToIntFast(float x) {
  104.     union {
  105.         float f;
  106.         sint32 i;
  107.     } u = {x + 12582912.0f};        // 2^22+2^23
  108.  
  109.     return (sint32)u.i - 0x4B400000;
  110. }
  111.  
  112. inline sint32 VDRoundToIntFastFullRange(double x) {
  113.     union {
  114.         double f;
  115.         sint32 i[2];
  116.     } u = {x + 6755399441055744.0f};        // 2^51+2^52
  117.  
  118.     return (sint32)u.i[0];
  119. }
  120.  
  121. #ifdef _M_AMD64
  122.     inline sint32 VDFloorToInt(double x) {
  123.         return (sint32)floor(x);
  124.     }
  125.  
  126.     inline sint64 VDFloorToInt64(double x) {
  127.         return (sint64)floor(x);
  128.     }
  129. #else
  130.     #pragma warning(push)
  131.     #pragma warning(disable: 4035)        // warning C4035: 'VDFloorToInt' : no return value
  132.     inline sint32 VDFloorToInt(double x) {
  133.         sint32 temp;
  134.  
  135.         __asm {
  136.             fld x
  137.             fist temp
  138.             fild temp
  139.             mov eax, temp
  140.             fsub
  141.             fstp temp
  142.             cmp    temp, 80000001h
  143.             adc eax, -1
  144.         }
  145.     }
  146.     inline sint64 VDFloorToInt64(double x) {
  147.         sint64 temp;
  148.         sint32 temp2;
  149.  
  150.         __asm {
  151.             fld x
  152.             fld st(0)
  153.             fistp qword ptr temp
  154.             fild qword ptr temp
  155.             mov eax, dword ptr temp
  156.             mov edx, dword ptr temp+4
  157.             fsub
  158.             fstp dword ptr temp2
  159.             cmp    dword ptr temp2, 80000001h
  160.             adc eax, -1
  161.             adc edx, -1
  162.         }
  163.     }
  164.     #pragma warning(pop)
  165. #endif
  166.  
  167. #ifdef _M_AMD64
  168.     inline sint32 VDCeilToInt(double x) {
  169.         return (sint32)ceil(x);
  170.     }
  171.  
  172.     inline sint64 VDCeilToInt64(double x) {
  173.         return (sint64)ceil(x);
  174.     }
  175. #else
  176.     #pragma warning(push)
  177.     #pragma warning(disable: 4035)        // warning C4035: 'VDCeilToInt' : no return value
  178.     inline sint32 VDCeilToInt(double x) {
  179.         sint32 temp;
  180.  
  181.         __asm {
  182.             fld x
  183.             fist temp
  184.             fild temp
  185.             mov eax, temp
  186.             fsubr
  187.             fstp temp
  188.             cmp    temp, 80000001h
  189.             sbb eax, -1
  190.         }
  191.     }
  192.  
  193.     inline sint32 VDCeilToInt64(double x) {
  194.         sint64 temp;
  195.         sint32 temp2;
  196.  
  197.         __asm {
  198.             fld x
  199.             fld st(0)
  200.             fistp temp
  201.             fild temp
  202.             mov eax, dword ptr temp
  203.             mov edx, dword ptr temp+4
  204.             fsubr
  205.             fstp temp2
  206.             cmp    temp2, 80000001h
  207.             sbb eax, -1
  208.             sbb edx, -1
  209.         }
  210.     }
  211.     #pragma warning(pop)
  212. #endif
  213.  
  214. ///////////////////////////////////////////////////////////////////////////
  215. inline sint16 VDClampedRoundFixedToInt16Fast(float x) {
  216.     union {
  217.         float f;
  218.         sint32 i;
  219.     } u = {x + 384.0f};        // 2^7+2^8
  220.  
  221.     sint32 v = (sint32)u.i - 0x43BF8000;
  222.  
  223.     if ((uint32)v >= 0x10000)
  224.         v = ~v >> 31;
  225.  
  226.     return (sint16)(v - 0x8000);
  227. }
  228.  
  229. inline uint8 VDClampedRoundFixedToUint8Fast(float x) {
  230.     union {
  231.         float f;
  232.         sint32 i;
  233.     } u = {x * 255.0f + 12582912.0f};        // 2^22+2^23
  234.  
  235.     sint32 v = (sint32)u.i - 0x4B400000;
  236.  
  237.     if ((uint32)v >= 0xFF)
  238.         v = ~v >> 31;
  239.  
  240.     return (uint8)v;
  241. }
  242.  
  243. ///////////////////////////////////////////////////////////////////////////
  244.  
  245. #ifdef _M_IX86
  246.     sint64 __stdcall VDFractionScale64(uint64 a, uint32 b, uint32 c, uint32& remainder);
  247.     uint64 __stdcall VDUMulDiv64x32(uint64 a, uint32 b, uint32 c);
  248. #else
  249.     extern "C" sint64 VDFractionScale64(uint64 a, uint64 b, uint64 c, uint32& remainder);
  250.     extern "C" uint64 VDUMulDiv64x32(uint64 a, uint32 b, uint32 c);
  251. #endif
  252.  
  253. sint64 VDMulDiv64(sint64 a, sint64 b, sint64 c);
  254.  
  255. ///////////////////////////////////////////////////////////////////////////
  256.  
  257. bool VDVerifyFiniteFloats(const float *p, uint32 n);
  258.  
  259. #endif
  260.