Libav
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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; 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 #define MAX_NEG_CROP 1024
31 
32 extern const uint32_t ff_inverse[257];
33 extern const uint8_t ff_reverse[256];
34 extern const uint8_t ff_sqrt_tab[256];
35 extern const uint8_t ff_crop_tab[256 + 2 * MAX_NEG_CROP];
36 extern const uint8_t ff_zigzag_direct[64];
37 extern const uint8_t ff_zigzag_scan[16];
38 
39 #if ARCH_ARM
40 # include "arm/mathops.h"
41 #elif ARCH_AVR32
42 # include "avr32/mathops.h"
43 #elif ARCH_BFIN
44 # include "bfin/mathops.h"
45 #elif ARCH_MIPS
46 # include "mips/mathops.h"
47 #elif ARCH_PPC
48 # include "ppc/mathops.h"
49 #elif ARCH_X86
50 # include "x86/mathops.h"
51 #endif
52 
53 /* generic implementation */
54 
55 #ifndef MUL64
56 # define MUL64(a,b) ((int64_t)(a) * (int64_t)(b))
57 #endif
58 
59 #ifndef MULL
60 # define MULL(a,b,s) (MUL64(a, b) >> (s))
61 #endif
62 
63 #ifndef MULH
64 static av_always_inline int MULH(int a, int b){
65  return MUL64(a, b) >> 32;
66 }
67 #endif
68 
69 #ifndef UMULH
70 static av_always_inline unsigned UMULH(unsigned a, unsigned b){
71  return ((uint64_t)(a) * (uint64_t)(b))>>32;
72 }
73 #endif
74 
75 #ifndef MAC64
76 # define MAC64(d, a, b) ((d) += MUL64(a, b))
77 #endif
78 
79 #ifndef MLS64
80 # define MLS64(d, a, b) ((d) -= MUL64(a, b))
81 #endif
82 
83 /* signed 16x16 -> 32 multiply add accumulate */
84 #ifndef MAC16
85 # define MAC16(rt, ra, rb) rt += (ra) * (rb)
86 #endif
87 
88 /* signed 16x16 -> 32 multiply */
89 #ifndef MUL16
90 # define MUL16(ra, rb) ((ra) * (rb))
91 #endif
92 
93 #ifndef MLS16
94 # define MLS16(rt, ra, rb) ((rt) -= (ra) * (rb))
95 #endif
96 
97 /* median of 3 */
98 #ifndef mid_pred
99 #define mid_pred mid_pred
100 static inline av_const int mid_pred(int a, int b, int c)
101 {
102  if(a>b){
103  if(c>b){
104  if(c>a) b=a;
105  else b=c;
106  }
107  }else{
108  if(b>c){
109  if(c>a) b=c;
110  else b=a;
111  }
112  }
113  return b;
114 }
115 #endif
116 
117 #ifndef sign_extend
118 static inline av_const int sign_extend(int val, unsigned bits)
119 {
120  unsigned shift = 8 * sizeof(int) - bits;
121  union { unsigned u; int s; } v = { (unsigned) val << shift };
122  return v.s >> shift;
123 }
124 #endif
125 
126 #ifndef zero_extend
127 static inline av_const unsigned zero_extend(unsigned val, unsigned bits)
128 {
129  return (val << ((8 * sizeof(int)) - bits)) >> ((8 * sizeof(int)) - bits);
130 }
131 #endif
132 
133 #ifndef COPY3_IF_LT
134 #define COPY3_IF_LT(x, y, a, b, c, d)\
135 if ((y) < (x)) {\
136  (x) = (y);\
137  (a) = (b);\
138  (c) = (d);\
139 }
140 #endif
141 
142 #ifndef MASK_ABS
143 #define MASK_ABS(mask, level) do { \
144  mask = level >> 31; \
145  level = (level ^ mask) - mask; \
146  } while (0)
147 #endif
148 
149 #ifndef NEG_SSR32
150 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
151 #endif
152 
153 #ifndef NEG_USR32
154 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
155 #endif
156 
157 #if HAVE_BIGENDIAN
158 # ifndef PACK_2U8
159 # define PACK_2U8(a,b) (((a) << 8) | (b))
160 # endif
161 # ifndef PACK_4U8
162 # define PACK_4U8(a,b,c,d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
163 # endif
164 # ifndef PACK_2U16
165 # define PACK_2U16(a,b) (((a) << 16) | (b))
166 # endif
167 #else
168 # ifndef PACK_2U8
169 # define PACK_2U8(a,b) (((b) << 8) | (a))
170 # endif
171 # ifndef PACK_4U2
172 # define PACK_4U8(a,b,c,d) (((d) << 24) | ((c) << 16) | ((b) << 8) | (a))
173 # endif
174 # ifndef PACK_2U16
175 # define PACK_2U16(a,b) (((b) << 16) | (a))
176 # endif
177 #endif
178 
179 #ifndef PACK_2S8
180 # define PACK_2S8(a,b) PACK_2U8((a)&255, (b)&255)
181 #endif
182 #ifndef PACK_4S8
183 # define PACK_4S8(a,b,c,d) PACK_4U8((a)&255, (b)&255, (c)&255, (d)&255)
184 #endif
185 #ifndef PACK_2S16
186 # define PACK_2S16(a,b) PACK_2U16((a)&0xffff, (b)&0xffff)
187 #endif
188 
189 #ifndef FASTDIV
190 # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a) * ff_inverse[b]) >> 32))
191 #endif /* FASTDIV */
192 
193 static inline av_const unsigned int ff_sqrt(unsigned int a)
194 {
195  unsigned int b;
196 
197  if (a < 255) return (ff_sqrt_tab[a + 1] - 1) >> 4;
198  else if (a < (1 << 12)) b = ff_sqrt_tab[a >> 4] >> 2;
199 #if !CONFIG_SMALL
200  else if (a < (1 << 14)) b = ff_sqrt_tab[a >> 6] >> 1;
201  else if (a < (1 << 16)) b = ff_sqrt_tab[a >> 8] ;
202 #endif
203  else {
204  int s = av_log2_16bit(a >> 16) >> 1;
205  unsigned int c = a >> (s + 2);
206  b = ff_sqrt_tab[c >> (s + 8)];
207  b = FASTDIV(c,b) + (b << s);
208  }
209 
210  return b - (a < b * b);
211 }
212 
213 static inline int8_t ff_u8_to_s8(uint8_t a)
214 {
215  union {
216  uint8_t u8;
217  int8_t s8;
218  } b;
219  b.u8 = a;
220  return b.s8;
221 }
222 
223 #endif /* AVCODEC_MATHOPS_H */
#define MUL64(a, b)
Definition: mathops.h:56
#define av_const
Definition: attributes.h:60
const uint8_t ff_zigzag_scan[16]
Definition: mathtables.c:126
#define MAX_NEG_CROP
Definition: mathops.h:30
static int8_t ff_u8_to_s8(uint8_t a)
Definition: mathops.h:213
static av_const unsigned zero_extend(unsigned val, unsigned bits)
Definition: mathops.h:127
const uint32_t ff_inverse[257]
Definition: mathtables.c:25
uint8_t bits
Definition: crc.c:252
uint8_t
#define b
Definition: input.c:52
const uint8_t ff_sqrt_tab[256]
Definition: mathtables.c:61
static av_always_inline unsigned UMULH(unsigned a, unsigned b)
Definition: mathops.h:70
static av_const unsigned int ff_sqrt(unsigned int a)
Definition: mathops.h:193
#define FASTDIV(a, b)
Definition: mathops.h:190
const uint8_t ff_crop_tab[256+2 *MAX_NEG_CROP]
Definition: mathtables.c:94
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:115
#define mid_pred
Definition: mathops.h:99
#define u(width,...)
static av_always_inline int MULH(int a, int b)
Definition: mathops.h:64
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:118
common internal and external API header
#define av_log2_16bit
Definition: intmath.h:86
#define av_always_inline
Definition: attributes.h:40
const uint8_t ff_reverse[256]
Definition: mathtables.c:72