FFmpeg
mem_internal.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2002 Fabrice Bellard
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 #ifndef AVUTIL_MEM_INTERNAL_H
22 #define AVUTIL_MEM_INTERNAL_H
23 
24 #include "config.h"
25 
26 #include <stdint.h>
27 
28 #include "attributes.h"
29 #include "macros.h"
30 
31 /**
32  * @def DECLARE_ALIGNED(n,t,v)
33  * Declare a variable that is aligned in memory.
34  *
35  * @code{.c}
36  * DECLARE_ALIGNED(16, uint16_t, aligned_int) = 42;
37  * DECLARE_ALIGNED(32, uint8_t, aligned_array)[128];
38  *
39  * // The default-alignment equivalent would be
40  * uint16_t aligned_int = 42;
41  * uint8_t aligned_array[128];
42  * @endcode
43  *
44  * @param n Minimum alignment in bytes
45  * @param t Type of the variable (or array element)
46  * @param v Name of the variable
47  */
48 
49 /**
50  * @def DECLARE_ASM_ALIGNED(n,t,v)
51  * Declare an aligned variable appropriate for use in inline assembly code.
52  *
53  * @code{.c}
54  * DECLARE_ASM_ALIGNED(16, uint64_t, pw_08) = UINT64_C(0x0008000800080008);
55  * @endcode
56  *
57  * @param n Minimum alignment in bytes
58  * @param t Type of the variable (or array element)
59  * @param v Name of the variable
60  */
61 
62 /**
63  * @def DECLARE_ASM_CONST(n,t,v)
64  * Declare a static constant aligned variable appropriate for use in inline
65  * assembly code.
66  *
67  * @code{.c}
68  * DECLARE_ASM_CONST(16, uint64_t, pw_08) = UINT64_C(0x0008000800080008);
69  * @endcode
70  *
71  * @param n Minimum alignment in bytes
72  * @param t Type of the variable (or array element)
73  * @param v Name of the variable
74  */
75 
76 #if defined(__INTEL_COMPILER) && __INTEL_COMPILER < 1110 || defined(__SUNPRO_C)
77  #define DECLARE_ALIGNED_T(n,t,v) t __attribute__ ((aligned (n))) v
78  #define DECLARE_ASM_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
79  #define DECLARE_ASM_CONST(n,t,v) const t __attribute__ ((aligned (n))) v
80 #elif defined(__DJGPP__)
81  #define DECLARE_ALIGNED_T(n,t,v) t __attribute__ ((aligned (FFMIN(n, 16)))) v
82  #define DECLARE_ASM_ALIGNED(n,t,v) t av_used __attribute__ ((aligned (FFMIN(n, 16)))) v
83  #define DECLARE_ASM_CONST(n,t,v) static const t av_used __attribute__ ((aligned (FFMIN(n, 16)))) v
84 #elif defined(__GNUC__) || defined(__clang__)
85  #define DECLARE_ALIGNED_T(n,t,v) t __attribute__ ((aligned (n))) v
86  #define DECLARE_ASM_ALIGNED(n,t,v) t av_used __attribute__ ((aligned (n))) v
87  #define DECLARE_ASM_CONST(n,t,v) static const t av_used __attribute__ ((aligned (n))) v
88 #elif defined(_MSC_VER)
89  #define DECLARE_ALIGNED_T(n,t,v) __declspec(align(n)) t v
90  #define DECLARE_ASM_ALIGNED(n,t,v) __declspec(align(n)) t v
91  #define DECLARE_ASM_CONST(n,t,v) __declspec(align(n)) static const t v
92 #else
93  #define DECLARE_ALIGNED_T(n,t,v) t v
94  #define DECLARE_ASM_ALIGNED(n,t,v) t v
95  #define DECLARE_ASM_CONST(n,t,v) static const t v
96 #endif
97 
98 #if HAVE_SIMD_ALIGN_64
99  #define ALIGN_64 64
100  #define ALIGN_32 32
101 #elif HAVE_SIMD_ALIGN_32
102  #define ALIGN_64 32
103  #define ALIGN_32 32
104 #else
105  #define ALIGN_64 16
106  #define ALIGN_32 16
107 #endif
108 
109 #define DECLARE_ALIGNED(n,t,v) DECLARE_ALIGNED_V(n,t,v)
110 
111 // Macro needs to be double-wrapped in order to expand
112 // possible other macros being passed for n.
113 #define DECLARE_ALIGNED_V(n,t,v) DECLARE_ALIGNED_##n(t,v)
114 
115 #define DECLARE_ALIGNED_4(t,v) DECLARE_ALIGNED_T( 4, t, v)
116 #define DECLARE_ALIGNED_8(t,v) DECLARE_ALIGNED_T( 8, t, v)
117 #define DECLARE_ALIGNED_16(t,v) DECLARE_ALIGNED_T( 16, t, v)
118 #define DECLARE_ALIGNED_32(t,v) DECLARE_ALIGNED_T(ALIGN_32, t, v)
119 #define DECLARE_ALIGNED_64(t,v) DECLARE_ALIGNED_T(ALIGN_64, t, v)
120 
121 // Some broken preprocessors need a second expansion
122 // to be forced to tokenize __VA_ARGS__
123 #define E1(x) x
124 
125 #define LOCAL_ALIGNED_A(a, t, v, s, o, ...) \
126  uint8_t la_##v[sizeof(t s o) + (a)]; \
127  t (*v) o = (void *)FFALIGN((uintptr_t)la_##v, a)
128 
129 #define LOCAL_ALIGNED_D(a, t, v, s, o, ...) \
130  DECLARE_ALIGNED(a, t, la_##v) s o; \
131  t (*v) o = la_##v
132 
133 #define LOCAL_ALIGNED(a, t, v, ...) LOCAL_ALIGNED_##a(t, v, __VA_ARGS__)
134 
135 #if HAVE_LOCAL_ALIGNED
136 # define LOCAL_ALIGNED_4(t, v, ...) E1(LOCAL_ALIGNED_D(4, t, v, __VA_ARGS__,,))
137 #else
138 # define LOCAL_ALIGNED_4(t, v, ...) E1(LOCAL_ALIGNED_A(4, t, v, __VA_ARGS__,,))
139 #endif
140 
141 #if HAVE_LOCAL_ALIGNED
142 # define LOCAL_ALIGNED_8(t, v, ...) E1(LOCAL_ALIGNED_D(8, t, v, __VA_ARGS__,,))
143 #else
144 # define LOCAL_ALIGNED_8(t, v, ...) E1(LOCAL_ALIGNED_A(8, t, v, __VA_ARGS__,,))
145 #endif
146 
147 #if HAVE_LOCAL_ALIGNED
148 # define LOCAL_ALIGNED_16(t, v, ...) E1(LOCAL_ALIGNED_D(16, t, v, __VA_ARGS__,,))
149 #else
150 # define LOCAL_ALIGNED_16(t, v, ...) E1(LOCAL_ALIGNED_A(16, t, v, __VA_ARGS__,,))
151 #endif
152 
153 #if HAVE_LOCAL_ALIGNED
154 # define LOCAL_ALIGNED_32(t, v, ...) E1(LOCAL_ALIGNED_D(32, t, v, __VA_ARGS__,,))
155 #else
156 # define LOCAL_ALIGNED_32(t, v, ...) E1(LOCAL_ALIGNED_A(32, t, v, __VA_ARGS__,,))
157 #endif
158 
159 #endif /* AVUTIL_MEM_INTERNAL_H */
macros.h
attributes.h