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
28 #include "config.h"
29
33
34 #if ARCH_ARM
36 #elif ARCH_AVR32
38 #elif ARCH_BFIN
40 #elif ARCH_MIPS
42 #elif ARCH_PPC
44 #elif ARCH_X86
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
60 return MUL64(a, b) >> 32;
61 }
62 #endif
63
64 #ifndef UMULH
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
96 {
97 #if 0
98 int t= (a-
b)&((a-b)>>31);
101 b-= (b-
c)&((b-c)>>31);
102 b+= (a-
b)&((a-b)>>31);
103
105 #else
106 if(a>b){
107 if(c>b){
110 }
111 }else{
112 if(b>c){
115 }
116 }
118 #endif
119 }
120 #endif
121
122 #ifndef sign_extend
124 {
125 unsigned shift = 8 *
sizeof(int) - bits;
126 union { unsigned u; int s; } v = { (unsigned) val << shift };
128 }
129 #endif
130
131 #ifndef zero_extend
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
199 {
201
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;
207 #endif
208 else {
210 unsigned int c = a >> (s + 2);
213 }
214
215 return b - (a < b *
b);
216 }
217
218 #endif /* AVCODEC_MATHOPS_H */