blob: daa5a6176b31e7f6d3c2605eae05c9547608f226
1 | /* |
2 | * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at> |
3 | * |
4 | * This file is part of FFmpeg. |
5 | * |
6 | * FFmpeg is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU Lesser General Public |
8 | * License as published by the Free Software Foundation; either |
9 | * version 2.1 of the License, or (at your option) any later version. |
10 | * |
11 | * FFmpeg is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | * Lesser General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU Lesser General Public |
17 | * License along with FFmpeg; if not, write to the Free Software |
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
19 | */ |
20 | |
21 | /** |
22 | * @file libavutil/internal.h |
23 | * common internal API header |
24 | */ |
25 | |
26 | #ifndef AVUTIL_INTERNAL_H |
27 | #define AVUTIL_INTERNAL_H |
28 | |
29 | #if !defined(DEBUG) && !defined(NDEBUG) |
30 | # define NDEBUG |
31 | #endif |
32 | |
33 | #include <limits.h> |
34 | #include "common.h" |
35 | |
36 | #ifndef attribute_align_arg |
37 | #if (!defined(__ICC) || __ICC > 1110) && AV_GCC_VERSION_AT_LEAST(4,2) |
38 | # define attribute_align_arg __attribute__((force_align_arg_pointer)) |
39 | #else |
40 | # define attribute_align_arg |
41 | #endif |
42 | #endif |
43 | |
44 | #ifndef attribute_used |
45 | #if AV_GCC_VERSION_AT_LEAST(3,1) |
46 | # define attribute_used __attribute__((used)) |
47 | #else |
48 | # define attribute_used |
49 | #endif |
50 | #endif |
51 | |
52 | #ifndef INT16_MIN |
53 | #define INT16_MIN (-0x7fff - 1) |
54 | #endif |
55 | |
56 | #ifndef INT16_MAX |
57 | #define INT16_MAX 0x7fff |
58 | #endif |
59 | |
60 | #ifndef INT32_MIN |
61 | #define INT32_MIN (-0x7fffffff - 1) |
62 | #endif |
63 | |
64 | #ifndef INT32_MAX |
65 | #define INT32_MAX 0x7fffffff |
66 | #endif |
67 | |
68 | #ifndef UINT32_MAX |
69 | #define UINT32_MAX 0xffffffff |
70 | #endif |
71 | |
72 | #ifndef INT64_MIN |
73 | #define INT64_MIN (-0x7fffffffffffffffLL - 1) |
74 | #endif |
75 | |
76 | #ifndef INT64_MAX |
77 | #define INT64_MAX INT64_C(9223372036854775807) |
78 | #endif |
79 | |
80 | #ifndef UINT64_MAX |
81 | #define UINT64_MAX UINT64_C(0xFFFFFFFFFFFFFFFF) |
82 | #endif |
83 | |
84 | #ifndef INT_BIT |
85 | # define INT_BIT (CHAR_BIT * sizeof(int)) |
86 | #endif |
87 | |
88 | #ifndef offsetof |
89 | # define offsetof(T, F) ((unsigned int)((char *)&((T *)0)->F)) |
90 | #endif |
91 | |
92 | /* Use to export labels from asm. */ |
93 | #define LABEL_MANGLE(a) EXTERN_PREFIX #a |
94 | |
95 | // Use rip-relative addressing if compiling PIC code on x86-64. |
96 | #if ARCH_X86_64 && defined(PIC) |
97 | # define LOCAL_MANGLE(a) #a "(%%rip)" |
98 | #else |
99 | # define LOCAL_MANGLE(a) #a |
100 | #endif |
101 | |
102 | #define MANGLE(a) EXTERN_PREFIX LOCAL_MANGLE(a) |
103 | |
104 | /* math */ |
105 | |
106 | extern const uint32_t ff_inverse[257]; |
107 | |
108 | #if CONFIG_FASTDIV |
109 | #define FASTDIV(a,b) ((uint32_t)((((uint64_t)a) * ff_inverse[b]) >> 32)) |
110 | #else |
111 | #define FASTDIV(a,b) ((a) / (b)) |
112 | #endif |
113 | |
114 | extern const uint8_t ff_sqrt_tab[256]; |
115 | |
116 | static inline av_const unsigned int ff_sqrt(unsigned int a) |
117 | { |
118 | unsigned int b; |
119 | |
120 | if (a < 255) { |
121 | return (ff_sqrt_tab[a + 1] - 1) >> 4; |
122 | } else if (a < (1 << 12)) { |
123 | b = ff_sqrt_tab[a >> 4] >> 2; |
124 | } |
125 | #if !CONFIG_SMALL |
126 | else if (a < (1 << 14)) { |
127 | b = ff_sqrt_tab[a >> 6] >> 1; |
128 | } else if (a < (1 << 16)) { |
129 | b = ff_sqrt_tab[a >> 8] ; |
130 | } |
131 | #endif |
132 | else { |
133 | int s = av_log2_16bit(a >> 16) >> 1; |
134 | unsigned int c = a >> (s + 2); |
135 | b = ff_sqrt_tab[c >> (s + 8)]; |
136 | b = FASTDIV(c, b) + (b << s); |
137 | } |
138 | |
139 | return b - (a < b * b); |
140 | } |
141 | |
142 | #if ARCH_X86 |
143 | #define MASK_ABS(mask, level)\ |
144 | __asm__ volatile(\ |
145 | "cltd \n\t"\ |
146 | "xorl %1, %0 \n\t"\ |
147 | "subl %1, %0 \n\t"\ |
148 | : "+a" (level), "=&d" (mask)\ |
149 | ); |
150 | #else |
151 | #define MASK_ABS(mask, level)\ |
152 | mask = level >> 31;\ |
153 | level = (level ^ mask) - mask; |
154 | #endif |
155 | |
156 | #if HAVE_CMOV |
157 | #define COPY3_IF_LT(x, y, a, b, c, d)\ |
158 | __asm__ volatile(\ |
159 | "cmpl %0, %3 \n\t"\ |
160 | "cmovl %3, %0 \n\t"\ |
161 | "cmovl %4, %1 \n\t"\ |
162 | "cmovl %5, %2 \n\t"\ |
163 | : "+&r" (x), "+&r" (a), "+r" (c)\ |
164 | : "r" (y), "r" (b), "r" (d)\ |
165 | ); |
166 | #else |
167 | #define COPY3_IF_LT(x, y, a, b, c, d)\ |
168 | if ((y) < (x)) {\ |
169 | (x) = (y);\ |
170 | (a) = (b);\ |
171 | (c) = (d);\ |
172 | } |
173 | #endif |
174 | |
175 | #define FF_ALLOC_OR_GOTO(ctx, p, size, label)\ |
176 | {\ |
177 | p = av_malloc(size);\ |
178 | if (p == NULL && (size) != 0) {\ |
179 | av_log(ctx, AV_LOG_ERROR, "Cannot allocate memory.\n");\ |
180 | goto label;\ |
181 | }\ |
182 | } |
183 | |
184 | #define FF_ALLOCZ_OR_GOTO(ctx, p, size, label)\ |
185 | {\ |
186 | p = av_mallocz(size);\ |
187 | if (p == NULL && (size) != 0) {\ |
188 | av_log(ctx, AV_LOG_ERROR, "Cannot allocate memory.\n");\ |
189 | goto label;\ |
190 | }\ |
191 | } |
192 | |
193 | #define NULL_IF_CONFIG_SMALL(x) x |
194 | |
195 | #endif /* AVUTIL_INTERNAL_H */ |
196 |