00001 00017 #ifndef _IFUNCTIONS_H_ 00018 #define _IFUNCTIONS_H_ 00019 00020 # if !defined(WIN32) && (__STDC_VERSION__ < 199901L) 00021 #define static 00022 #define inline 00023 #endif 00024 #include <math.h> 00025 #include <limits.h> 00026 00027 00028 static inline short smin(short a, short b) 00029 { 00030 return (short) (((a) < (b)) ? (a) : (b)); 00031 } 00032 00033 static inline short smax(short a, short b) 00034 { 00035 return (short) (((a) > (b)) ? (a) : (b)); 00036 } 00037 00038 static inline int imin(int a, int b) 00039 { 00040 return ((a) < (b)) ? (a) : (b); 00041 } 00042 00043 static inline int imax(int a, int b) 00044 { 00045 return ((a) > (b)) ? (a) : (b); 00046 } 00047 00048 static inline double dmin(double a, double b) 00049 { 00050 return ((a) < (b)) ? (a) : (b); 00051 } 00052 00053 static inline double dmax(double a, double b) 00054 { 00055 return ((a) > (b)) ? (a) : (b); 00056 } 00057 00058 static inline int64 i64min(int64 a, int64 b) 00059 { 00060 return ((a) < (b)) ? (a) : (b); 00061 } 00062 00063 static inline int64 i64max(int64 a, int64 b) 00064 { 00065 return ((a) > (b)) ? (a) : (b); 00066 } 00067 00068 00069 static inline short sabs(short x) 00070 { 00071 static const short SHORT_BITS = (sizeof(short) * CHAR_BIT) - 1; 00072 short y = (short) (x >> SHORT_BITS); 00073 return (short) ((x ^ y) - y); 00074 } 00075 00076 static inline int iabs(int x) 00077 { 00078 static const int INT_BITS = (sizeof(int) * CHAR_BIT) - 1; 00079 int y = x >> INT_BITS; 00080 return (x ^ y) - y; 00081 } 00082 00083 static inline double dabs(double x) 00084 { 00085 return ((x) < 0) ? -(x) : (x); 00086 } 00087 00088 static inline int64 i64abs(int64 x) 00089 { 00090 static const int64 INT64_BITS = (sizeof(int64) * CHAR_BIT) - 1; 00091 int64 y = x >> INT64_BITS; 00092 return (x ^ y) - y; 00093 } 00094 00095 static inline double dabs2(double x) 00096 { 00097 return (x) * (x); 00098 } 00099 00100 static inline int iabs2(int x) 00101 { 00102 return (x) * (x); 00103 } 00104 00105 static inline int64 i64abs2(int64 x) 00106 { 00107 return (x) * (x); 00108 } 00109 00110 static inline int isign(int x) 00111 { 00112 return ( (x > 0) - (x < 0)); 00113 } 00114 00115 static inline int isignab(int a, int b) 00116 { 00117 return ((b) < 0) ? -iabs(a) : iabs(a); 00118 } 00119 00120 static inline int rshift_rnd(int x, int a) 00121 { 00122 return (a > 0) ? ((x + (1 << (a-1) )) >> a) : (x << (-a)); 00123 } 00124 00125 static inline int rshift_rnd_sign(int x, int a) 00126 { 00127 return (x > 0) ? ( ( x + (1 << (a-1)) ) >> a ) : (-( ( iabs(x) + (1 << (a-1)) ) >> a )); 00128 } 00129 00130 static inline unsigned int rshift_rnd_us(unsigned int x, unsigned int a) 00131 { 00132 return (a > 0) ? ((x + (1 << (a-1))) >> a) : x; 00133 } 00134 00135 static inline int rshift_rnd_sf(int x, int a) 00136 { 00137 return ((x + (1 << (a-1) )) >> a); 00138 } 00139 00140 static inline unsigned int rshift_rnd_us_sf(unsigned int x, unsigned int a) 00141 { 00142 return ((x + (1 << (a-1))) >> a); 00143 } 00144 00145 static inline int iClip1(int high, int x) 00146 { 00147 x = imax(x, 0); 00148 x = imin(x, high); 00149 00150 return x; 00151 } 00152 00153 static inline int iClip3(int low, int high, int x) 00154 { 00155 x = imax(x, low); 00156 x = imin(x, high); 00157 00158 return x; 00159 } 00160 00161 static inline short sClip3(short low, short high, short x) 00162 { 00163 x = smax(x, low); 00164 x = smin(x, high); 00165 00166 return x; 00167 } 00168 00169 static inline double dClip3(double low, double high, double x) 00170 { 00171 x = dmax(x, low); 00172 x = dmin(x, high); 00173 00174 return x; 00175 } 00176 00177 static inline int weighted_cost(int factor, int bits) 00178 { 00179 return (((factor)*(bits))>>LAMBDA_ACCURACY_BITS); 00180 } 00181 00182 static inline int RSD(int x) 00183 { 00184 return ((x&2)?(x|1):(x&(~1))); 00185 } 00186 00187 static inline int power2(int x) 00188 { 00189 return 1 << (x); 00190 } 00191 00192 static inline int float2int (float x) 00193 { 00194 return (int)((x < 0) ? (x - 0.5f) : (x + 0.5f)); 00195 } 00196 00197 static inline int get_bit(int64 x,int n) 00198 { 00199 return (int)(((x >> n) & 1)); 00200 } 00201 00202 #if ZEROSNR 00203 static inline float psnr(int max_sample_sq, int samples, float sse_distortion ) 00204 { 00205 return (float) (10.0 * log10(max_sample_sq * (double) ((double) samples / (sse_distortion < 1.0 ? 1.0 : sse_distortion)))); 00206 } 00207 #else 00208 static inline float psnr(int max_sample_sq, int samples, float sse_distortion ) 00209 { 00210 return (float) (sse_distortion == 0.0 ? 0.0 : (10.0 * log10(max_sample_sq * (double) ((double) samples / sse_distortion)))); 00211 } 00212 #endif 00213 00214 00215 # if !defined(WIN32) && (__STDC_VERSION__ < 199901L) 00216 #undef static 00217 #undef inline 00218 #endif 00219 00220 #endif 00221