1
0

ifunctions.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*!
  2. ************************************************************************
  3. * \file
  4. * ifunctions.h
  5. *
  6. * \brief
  7. * define some inline functions that are used within the encoder.
  8. *
  9. * \author
  10. * Main contributors (see contributors.h for copyright, address and affiliation details)
  11. * - Karsten Sühring <[email protected]>
  12. * - Alexis Tourapis <[email protected]>
  13. *
  14. ************************************************************************
  15. */
  16. #ifndef _IFUNCTIONS_H_
  17. #define _IFUNCTIONS_H_
  18. # if !defined(WIN32) && (__STDC_VERSION__ < 199901L)
  19. #define static
  20. #define inline
  21. #endif
  22. #include <math.h>
  23. #include <limits.h>
  24. static inline short smin(short a, short b)
  25. {
  26. return (short) (((a) < (b)) ? (a) : (b));
  27. }
  28. static inline short smax(short a, short b)
  29. {
  30. return (short) (((a) > (b)) ? (a) : (b));
  31. }
  32. static inline int imin(int a, int b)
  33. {/*
  34. int retu;
  35. _asm
  36. {
  37. mov eax, a
  38. mov edx, b
  39. cmp edx, eax
  40. cmovle eax, edx
  41. mov retu, eax
  42. }
  43. return retu;*/
  44. return ((a) < (b)) ? (a) : (b);
  45. }
  46. static inline int imax(int a, int b)
  47. {
  48. return ((a) > (b)) ? (a) : (b);
  49. }
  50. static inline double dmin(double a, double b)
  51. {
  52. return ((a) < (b)) ? (a) : (b);
  53. }
  54. static inline double dmax(double a, double b)
  55. {
  56. return ((a) > (b)) ? (a) : (b);
  57. }
  58. static inline int64 i64min(int64 a, int64 b)
  59. {
  60. return ((a) < (b)) ? (a) : (b);
  61. }
  62. static inline int64 i64max(int64 a, int64 b)
  63. {
  64. return ((a) > (b)) ? (a) : (b);
  65. }
  66. static inline short sabs(short x)
  67. {
  68. static const short SHORT_BITS = (sizeof(short) * CHAR_BIT) - 1;
  69. short y = (short) (x >> SHORT_BITS);
  70. return (short) ((x ^ y) - y);
  71. }
  72. static inline int iabs(int x)
  73. {
  74. static const int INT_BITS = (sizeof(int) * CHAR_BIT) - 1;
  75. int y = x >> INT_BITS;
  76. return (x ^ y) - y;
  77. }
  78. static inline double dabs(double x)
  79. {
  80. return ((x) < 0) ? -(x) : (x);
  81. }
  82. static inline int64 i64abs(int64 x)
  83. {
  84. static const int64 INT64_BITS = (sizeof(int64) * CHAR_BIT) - 1;
  85. int64 y = x >> INT64_BITS;
  86. return (x ^ y) - y;
  87. }
  88. static inline double dabs2(double x)
  89. {
  90. return (x) * (x);
  91. }
  92. static inline int iabs2(int x)
  93. {
  94. return (x) * (x);
  95. }
  96. static inline int64 i64abs2(int64 x)
  97. {
  98. return (x) * (x);
  99. }
  100. static inline int isign(int x)
  101. {
  102. return ( (x > 0) - (x < 0));
  103. }
  104. static inline int isignab(int a, int b)
  105. {
  106. return ((b) < 0) ? -iabs(a) : iabs(a);
  107. }
  108. static inline int rshift_rnd(int x, int a)
  109. {
  110. return (a > 0) ? ((x + (1 << (a-1) )) >> a) : (x << (-a));
  111. }
  112. static inline int rshift_rnd_pos(int x, int a)
  113. {
  114. return (x + (1 << (a-1) )) >> a;
  115. }
  116. // flip a before calling
  117. static inline int rshift_rnd_nonpos(int x, int a)
  118. {
  119. return (x << a);
  120. }
  121. static inline int rshift_rnd_sign(int x, int a)
  122. {
  123. return (x > 0) ? ( ( x + (1 << (a-1)) ) >> a ) : (-( ( iabs(x) + (1 << (a-1)) ) >> a ));
  124. }
  125. static inline unsigned int rshift_rnd_us(unsigned int x, unsigned int a)
  126. {
  127. return (a > 0) ? ((x + (1 << (a-1))) >> a) : x;
  128. }
  129. static inline int rshift_rnd_sf(int x, int a)
  130. {
  131. return ((x + (1 << (a-1) )) >> a);
  132. }
  133. static inline unsigned int rshift_rnd_us_sf(unsigned int x, unsigned int a)
  134. {
  135. return ((x + (1 << (a-1))) >> a);
  136. }
  137. static inline int iClip1(int high, int x)
  138. {
  139. if (x < 0)
  140. return 0;
  141. if (x > high)
  142. return high;
  143. return x;
  144. /* old:
  145. x = imax(x, 0);
  146. x = imin(x, high);
  147. return x;*/
  148. }
  149. static inline int iClip3(int low, int high, int x)
  150. {
  151. if (x < low)
  152. return low;
  153. if (x > high)
  154. return high;
  155. return x;
  156. /* old:
  157. x = imax(x, low);
  158. x = imin(x, high);
  159. return x;*/
  160. }
  161. static inline short sClip3(short low, short high, short x)
  162. {
  163. x = smax(x, low);
  164. x = smin(x, high);
  165. return x;
  166. }
  167. static inline double dClip3(double low, double high, double x)
  168. {
  169. x = dmax(x, low);
  170. x = dmin(x, high);
  171. return x;
  172. }
  173. static inline int weighted_cost(int factor, int bits)
  174. {
  175. return (((factor)*(bits))>>LAMBDA_ACCURACY_BITS);
  176. }
  177. static inline int RSD(int x)
  178. {
  179. return ((x&2)?(x|1):(x&(~1)));
  180. }
  181. static inline int power2(int x)
  182. {
  183. return 1 << (x);
  184. }
  185. static inline int float2int (float x)
  186. {
  187. return (int)((x < 0) ? (x - 0.5f) : (x + 0.5f));
  188. }
  189. #if ZEROSNR
  190. static inline float psnr(int max_sample_sq, int samples, float sse_distortion )
  191. {
  192. return (float) (10.0 * log10(max_sample_sq * (double) ((double) samples / (sse_distortion < 1.0 ? 1.0 : sse_distortion))));
  193. }
  194. #else
  195. static inline float psnr(int max_sample_sq, int samples, float sse_distortion )
  196. {
  197. return (float) (sse_distortion == 0.0 ? 0.0 : (10.0 * log10(max_sample_sq * (double) ((double) samples / sse_distortion))));
  198. }
  199. #endif
  200. # if !defined(WIN32) && (__STDC_VERSION__ < 199901L)
  201. #undef static
  202. #undef inline
  203. #endif
  204. #endif