FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
mathops.h
Go to the documentation of this file.
1 /*
2  * simple math operations
3  * Copyright (c) 2001, 2002 Fabrice Bellard
4  * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at> et al
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 #ifndef AVCODEC_MATHOPS_H
23 #define AVCODEC_MATHOPS_H
24 
25 #include <stdint.h>
26 
27 #include "libavutil/common.h"
28 #include "config.h"
29 
30 extern const uint32_t ff_inverse[257];
31 extern const uint8_t ff_reverse[256];
32 extern const uint8_t ff_sqrt_tab[256];
33 
34 #if ARCH_ARM
35 # include "arm/mathops.h"
36 #elif ARCH_AVR32
37 # include "avr32/mathops.h"
38 #elif ARCH_BFIN
39 # include "bfin/mathops.h"
40 #elif ARCH_MIPS
41 # include "mips/mathops.h"
42 #elif ARCH_PPC
43 # include "ppc/mathops.h"
44 #elif ARCH_X86
45 # include "x86/mathops.h"
46 #endif
47 
48 /* generic implementation */
49 
50 #ifndef MUL64
51 # define MUL64(a,b) ((int64_t)(a) * (int64_t)(b))
52 #endif
53 
54 #ifndef MULL
55 # define MULL(a,b,s) (MUL64(a, b) >> (s))
56 #endif
57 
58 #ifndef MULH
59 static av_always_inline int MULH(int a, int b){
60  return MUL64(a, b) >> 32;
61 }
62 #endif
63 
64 #ifndef UMULH
65 static av_always_inline unsigned UMULH(unsigned a, unsigned b){
66  return ((uint64_t)(a) * (uint64_t)(b))>>32;
67 }
68 #endif
69 
70 #ifndef MAC64
71 # define MAC64(d, a, b) ((d) += MUL64(a, b))
72 #endif
73 
74 #ifndef MLS64
75 # define MLS64(d, a, b) ((d) -= MUL64(a, b))
76 #endif
77 
78 /* signed 16x16 -> 32 multiply add accumulate */
79 #ifndef MAC16
80 # define MAC16(rt, ra, rb) rt += (ra) * (rb)
81 #endif
82 
83 /* signed 16x16 -> 32 multiply */
84 #ifndef MUL16
85 # define MUL16(ra, rb) ((ra) * (rb))
86 #endif
87 
88 #ifndef MLS16
89 # define MLS16(rt, ra, rb) ((rt) -= (ra) * (rb))
90 #endif
91 
92 /* median of 3 */
93 #ifndef mid_pred
94 #define mid_pred mid_pred
95 static inline av_const int mid_pred(int a, int b, int c)
96 {
97 #if 0
98  int t= (a-b)&((a-b)>>31);
99  a-=t;
100  b+=t;
101  b-= (b-c)&((b-c)>>31);
102  b+= (a-b)&((a-b)>>31);
103 
104  return b;
105 #else
106  if(a>b){
107  if(c>b){
108  if(c>a) b=a;
109  else b=c;
110  }
111  }else{
112  if(b>c){
113  if(c>a) b=c;
114  else b=a;
115  }
116  }
117  return b;
118 #endif
119 }
120 #endif
121 
122 #ifndef sign_extend
123 static inline av_const int sign_extend(int val, unsigned bits)
124 {
125  unsigned shift = 8 * sizeof(int) - bits;
126  union { unsigned u; int s; } v = { (unsigned) val << shift };
127  return v.s >> shift;
128 }
129 #endif
130 
131 #ifndef zero_extend
132 static inline av_const unsigned zero_extend(unsigned val, unsigned bits)
133 {
134  return (val << ((8 * sizeof(int)) - bits)) >> ((8 * sizeof(int)) - bits);
135 }
136 #endif
137 
138 #ifndef COPY3_IF_LT
139 #define COPY3_IF_LT(x, y, a, b, c, d)\
140 if ((y) < (x)) {\
141  (x) = (y);\
142  (a) = (b);\
143  (c) = (d);\
144 }
145 #endif
146 
147 #ifndef MASK_ABS
148 #define MASK_ABS(mask, level) do { \
149  mask = level >> 31; \
150  level = (level ^ mask) - mask; \
151  } while (0)
152 #endif
153 
154 #ifndef NEG_SSR32
155 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
156 #endif
157 
158 #ifndef NEG_USR32
159 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
160 #endif
161 
162 #if HAVE_BIGENDIAN
163 # ifndef PACK_2U8
164 # define PACK_2U8(a,b) (((a) << 8) | (b))
165 # endif
166 # ifndef PACK_4U8
167 # define PACK_4U8(a,b,c,d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
168 # endif
169 # ifndef PACK_2U16
170 # define PACK_2U16(a,b) (((a) << 16) | (b))
171 # endif
172 #else
173 # ifndef PACK_2U8
174 # define PACK_2U8(a,b) (((b) << 8) | (a))
175 # endif
176 # ifndef PACK_4U2
177 # define PACK_4U8(a,b,c,d) (((d) << 24) | ((c) << 16) | ((b) << 8) | (a))
178 # endif
179 # ifndef PACK_2U16
180 # define PACK_2U16(a,b) (((b) << 16) | (a))
181 # endif
182 #endif
183 
184 #ifndef PACK_2S8
185 # define PACK_2S8(a,b) PACK_2U8((a)&255, (b)&255)
186 #endif
187 #ifndef PACK_4S8
188 # define PACK_4S8(a,b,c,d) PACK_4U8((a)&255, (b)&255, (c)&255, (d)&255)
189 #endif
190 #ifndef PACK_2S16
191 # define PACK_2S16(a,b) PACK_2U16((a)&0xffff, (b)&0xffff)
192 #endif
193 
194 #ifndef FASTDIV
195 # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a) * ff_inverse[b]) >> 32))
196 #endif /* FASTDIV */
197 
198 static inline av_const unsigned int ff_sqrt(unsigned int a)
199 {
200  unsigned int b;
201 
202  if (a < 255) return (ff_sqrt_tab[a + 1] - 1) >> 4;
203  else if (a < (1 << 12)) b = ff_sqrt_tab[a >> 4] >> 2;
204 #if !CONFIG_SMALL
205  else if (a < (1 << 14)) b = ff_sqrt_tab[a >> 6] >> 1;
206  else if (a < (1 << 16)) b = ff_sqrt_tab[a >> 8] ;
207 #endif
208  else {
209  int s = av_log2_16bit(a >> 16) >> 1;
210  unsigned int c = a >> (s + 2);
211  b = ff_sqrt_tab[c >> (s + 8)];
212  b = FASTDIV(c,b) + (b << s);
213  }
214 
215  return b - (a < b * b);
216 }
217 
218 #endif /* AVCODEC_MATHOPS_H */