2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #ifndef AVUTIL_INTREADWRITE_H
20 #define AVUTIL_INTREADWRITE_H
23 #include "libavutil/avconfig.h"
24 #include "attributes.h"
34 } av_alias av_alias64;
41 } av_alias av_alias32;
46 } av_alias av_alias16;
49 * Arch-specific headers can provide any combination of
50 * AV_[RW][BLN](16|24|32|48|64) and AV_(COPY|SWAP|ZERO)(64|128) macros.
51 * Preprocessor symbols must be defined, even if these are implemented
52 * as inline functions.
54 * R/W means read/write, B/L/N means big/little/native endianness.
55 * The following macros require aligned access, compared to their
56 * unaligned variants: AV_(COPY|SWAP|ZERO)(64|128), AV_[RW]N[8-64]A.
57 * Incorrect usage may range from abysmal performance to crash
58 * depending on the platform.
60 * The unaligned variants are AV_[RW][BLN][8-64] and AV_COPY*U.
63 #ifdef HAVE_AV_CONFIG_H
68 # include "arm/intreadwrite.h"
70 # include "avr32/intreadwrite.h"
72 # include "mips/intreadwrite.h"
74 # include "ppc/intreadwrite.h"
76 # include "tomi/intreadwrite.h"
78 # include "x86/intreadwrite.h"
81 #endif /* HAVE_AV_CONFIG_H */
84 * Map AV_RNXX <-> AV_R[BL]XX for all variants provided by per-arch headers.
89 # if defined(AV_RN16) && !defined(AV_RB16)
90 # define AV_RB16(p) AV_RN16(p)
91 # elif !defined(AV_RN16) && defined(AV_RB16)
92 # define AV_RN16(p) AV_RB16(p)
95 # if defined(AV_WN16) && !defined(AV_WB16)
96 # define AV_WB16(p, v) AV_WN16(p, v)
97 # elif !defined(AV_WN16) && defined(AV_WB16)
98 # define AV_WN16(p, v) AV_WB16(p, v)
101 # if defined(AV_RN24) && !defined(AV_RB24)
102 # define AV_RB24(p) AV_RN24(p)
103 # elif !defined(AV_RN24) && defined(AV_RB24)
104 # define AV_RN24(p) AV_RB24(p)
107 # if defined(AV_WN24) && !defined(AV_WB24)
108 # define AV_WB24(p, v) AV_WN24(p, v)
109 # elif !defined(AV_WN24) && defined(AV_WB24)
110 # define AV_WN24(p, v) AV_WB24(p, v)
113 # if defined(AV_RN32) && !defined(AV_RB32)
114 # define AV_RB32(p) AV_RN32(p)
115 # elif !defined(AV_RN32) && defined(AV_RB32)
116 # define AV_RN32(p) AV_RB32(p)
119 # if defined(AV_WN32) && !defined(AV_WB32)
120 # define AV_WB32(p, v) AV_WN32(p, v)
121 # elif !defined(AV_WN32) && defined(AV_WB32)
122 # define AV_WN32(p, v) AV_WB32(p, v)
125 # if defined(AV_RN48) && !defined(AV_RB48)
126 # define AV_RB48(p) AV_RN48(p)
127 # elif !defined(AV_RN48) && defined(AV_RB48)
128 # define AV_RN48(p) AV_RB48(p)
131 # if defined(AV_WN48) && !defined(AV_WB48)
132 # define AV_WB48(p, v) AV_WN48(p, v)
133 # elif !defined(AV_WN48) && defined(AV_WB48)
134 # define AV_WN48(p, v) AV_WB48(p, v)
137 # if defined(AV_RN64) && !defined(AV_RB64)
138 # define AV_RB64(p) AV_RN64(p)
139 # elif !defined(AV_RN64) && defined(AV_RB64)
140 # define AV_RN64(p) AV_RB64(p)
143 # if defined(AV_WN64) && !defined(AV_WB64)
144 # define AV_WB64(p, v) AV_WN64(p, v)
145 # elif !defined(AV_WN64) && defined(AV_WB64)
146 # define AV_WN64(p, v) AV_WB64(p, v)
149 #else /* AV_HAVE_BIGENDIAN */
151 # if defined(AV_RN16) && !defined(AV_RL16)
152 # define AV_RL16(p) AV_RN16(p)
153 # elif !defined(AV_RN16) && defined(AV_RL16)
154 # define AV_RN16(p) AV_RL16(p)
157 # if defined(AV_WN16) && !defined(AV_WL16)
158 # define AV_WL16(p, v) AV_WN16(p, v)
159 # elif !defined(AV_WN16) && defined(AV_WL16)
160 # define AV_WN16(p, v) AV_WL16(p, v)
163 # if defined(AV_RN24) && !defined(AV_RL24)
164 # define AV_RL24(p) AV_RN24(p)
165 # elif !defined(AV_RN24) && defined(AV_RL24)
166 # define AV_RN24(p) AV_RL24(p)
169 # if defined(AV_WN24) && !defined(AV_WL24)
170 # define AV_WL24(p, v) AV_WN24(p, v)
171 # elif !defined(AV_WN24) && defined(AV_WL24)
172 # define AV_WN24(p, v) AV_WL24(p, v)
175 # if defined(AV_RN32) && !defined(AV_RL32)
176 # define AV_RL32(p) AV_RN32(p)
177 # elif !defined(AV_RN32) && defined(AV_RL32)
178 # define AV_RN32(p) AV_RL32(p)
181 # if defined(AV_WN32) && !defined(AV_WL32)
182 # define AV_WL32(p, v) AV_WN32(p, v)
183 # elif !defined(AV_WN32) && defined(AV_WL32)
184 # define AV_WN32(p, v) AV_WL32(p, v)
187 # if defined(AV_RN48) && !defined(AV_RL48)
188 # define AV_RL48(p) AV_RN48(p)
189 # elif !defined(AV_RN48) && defined(AV_RL48)
190 # define AV_RN48(p) AV_RL48(p)
193 # if defined(AV_WN48) && !defined(AV_WL48)
194 # define AV_WL48(p, v) AV_WN48(p, v)
195 # elif !defined(AV_WN48) && defined(AV_WL48)
196 # define AV_WN48(p, v) AV_WL48(p, v)
199 # if defined(AV_RN64) && !defined(AV_RL64)
200 # define AV_RL64(p) AV_RN64(p)
201 # elif !defined(AV_RN64) && defined(AV_RL64)
202 # define AV_RN64(p) AV_RL64(p)
205 # if defined(AV_WN64) && !defined(AV_WL64)
206 # define AV_WL64(p, v) AV_WN64(p, v)
207 # elif !defined(AV_WN64) && defined(AV_WL64)
208 # define AV_WN64(p, v) AV_WL64(p, v)
211 #endif /* !AV_HAVE_BIGENDIAN */
214 * Define AV_[RW]N helper macros to simplify definitions not provided
215 * by per-arch headers.
218 #if defined(__GNUC__) && !defined(__TI_COMPILER_VERSION__)
220 union unaligned_64 { uint64_t l; } __attribute__((packed)) av_alias;
221 union unaligned_32 { uint32_t l; } __attribute__((packed)) av_alias;
222 union unaligned_16 { uint16_t l; } __attribute__((packed)) av_alias;
224 # define AV_RN(s, p) (((const union unaligned_##s *) (p))->l)
225 # define AV_WN(s, p, v) ((((union unaligned_##s *) (p))->l) = (v))
227 #elif defined(__DECC)
229 # define AV_RN(s, p) (*((const __unaligned uint##s##_t*)(p)))
230 # define AV_WN(s, p, v) (*((__unaligned uint##s##_t*)(p)) = (v))
232 #elif AV_HAVE_FAST_UNALIGNED
234 # define AV_RN(s, p) (((const av_alias##s*)(p))->u##s)
235 # define AV_WN(s, p, v) (((av_alias##s*)(p))->u##s = (v))
240 # define AV_RB16(x) \
241 ((((const uint8_t*)(x))[0] << 8) | \
242 ((const uint8_t*)(x))[1])
245 # define AV_WB16(p, darg) do { \
246 unsigned d = (darg); \
247 ((uint8_t*)(p))[1] = (d); \
248 ((uint8_t*)(p))[0] = (d)>>8; \
253 # define AV_RL16(x) \
254 ((((const uint8_t*)(x))[1] << 8) | \
255 ((const uint8_t*)(x))[0])
258 # define AV_WL16(p, darg) do { \
259 unsigned d = (darg); \
260 ((uint8_t*)(p))[0] = (d); \
261 ((uint8_t*)(p))[1] = (d)>>8; \
266 # define AV_RB32(x) \
267 (((uint32_t)((const uint8_t*)(x))[0] << 24) | \
268 (((const uint8_t*)(x))[1] << 16) | \
269 (((const uint8_t*)(x))[2] << 8) | \
270 ((const uint8_t*)(x))[3])
273 # define AV_WB32(p, darg) do { \
274 unsigned d = (darg); \
275 ((uint8_t*)(p))[3] = (d); \
276 ((uint8_t*)(p))[2] = (d)>>8; \
277 ((uint8_t*)(p))[1] = (d)>>16; \
278 ((uint8_t*)(p))[0] = (d)>>24; \
283 # define AV_RL32(x) \
284 (((uint32_t)((const uint8_t*)(x))[3] << 24) | \
285 (((const uint8_t*)(x))[2] << 16) | \
286 (((const uint8_t*)(x))[1] << 8) | \
287 ((const uint8_t*)(x))[0])
290 # define AV_WL32(p, darg) do { \
291 unsigned d = (darg); \
292 ((uint8_t*)(p))[0] = (d); \
293 ((uint8_t*)(p))[1] = (d)>>8; \
294 ((uint8_t*)(p))[2] = (d)>>16; \
295 ((uint8_t*)(p))[3] = (d)>>24; \
300 # define AV_RB64(x) \
301 (((uint64_t)((const uint8_t*)(x))[0] << 56) | \
302 ((uint64_t)((const uint8_t*)(x))[1] << 48) | \
303 ((uint64_t)((const uint8_t*)(x))[2] << 40) | \
304 ((uint64_t)((const uint8_t*)(x))[3] << 32) | \
305 ((uint64_t)((const uint8_t*)(x))[4] << 24) | \
306 ((uint64_t)((const uint8_t*)(x))[5] << 16) | \
307 ((uint64_t)((const uint8_t*)(x))[6] << 8) | \
308 (uint64_t)((const uint8_t*)(x))[7])
311 # define AV_WB64(p, darg) do { \
312 uint64_t d = (darg); \
313 ((uint8_t*)(p))[7] = (d); \
314 ((uint8_t*)(p))[6] = (d)>>8; \
315 ((uint8_t*)(p))[5] = (d)>>16; \
316 ((uint8_t*)(p))[4] = (d)>>24; \
317 ((uint8_t*)(p))[3] = (d)>>32; \
318 ((uint8_t*)(p))[2] = (d)>>40; \
319 ((uint8_t*)(p))[1] = (d)>>48; \
320 ((uint8_t*)(p))[0] = (d)>>56; \
325 # define AV_RL64(x) \
326 (((uint64_t)((const uint8_t*)(x))[7] << 56) | \
327 ((uint64_t)((const uint8_t*)(x))[6] << 48) | \
328 ((uint64_t)((const uint8_t*)(x))[5] << 40) | \
329 ((uint64_t)((const uint8_t*)(x))[4] << 32) | \
330 ((uint64_t)((const uint8_t*)(x))[3] << 24) | \
331 ((uint64_t)((const uint8_t*)(x))[2] << 16) | \
332 ((uint64_t)((const uint8_t*)(x))[1] << 8) | \
333 (uint64_t)((const uint8_t*)(x))[0])
336 # define AV_WL64(p, darg) do { \
337 uint64_t d = (darg); \
338 ((uint8_t*)(p))[0] = (d); \
339 ((uint8_t*)(p))[1] = (d)>>8; \
340 ((uint8_t*)(p))[2] = (d)>>16; \
341 ((uint8_t*)(p))[3] = (d)>>24; \
342 ((uint8_t*)(p))[4] = (d)>>32; \
343 ((uint8_t*)(p))[5] = (d)>>40; \
344 ((uint8_t*)(p))[6] = (d)>>48; \
345 ((uint8_t*)(p))[7] = (d)>>56; \
349 #if AV_HAVE_BIGENDIAN
350 # define AV_RN(s, p) AV_RB##s(p)
351 # define AV_WN(s, p, v) AV_WB##s(p, v)
353 # define AV_RN(s, p) AV_RL##s(p)
354 # define AV_WN(s, p, v) AV_WL##s(p, v)
357 #endif /* HAVE_FAST_UNALIGNED */
360 # define AV_RN16(p) AV_RN(16, p)
364 # define AV_RN32(p) AV_RN(32, p)
368 # define AV_RN64(p) AV_RN(64, p)
372 # define AV_WN16(p, v) AV_WN(16, p, v)
376 # define AV_WN32(p, v) AV_WN(32, p, v)
380 # define AV_WN64(p, v) AV_WN(64, p, v)
383 #if AV_HAVE_BIGENDIAN
384 # define AV_RB(s, p) AV_RN##s(p)
385 # define AV_WB(s, p, v) AV_WN##s(p, v)
386 # define AV_RL(s, p) av_bswap##s(AV_RN##s(p))
387 # define AV_WL(s, p, v) AV_WN##s(p, av_bswap##s(v))
389 # define AV_RB(s, p) av_bswap##s(AV_RN##s(p))
390 # define AV_WB(s, p, v) AV_WN##s(p, av_bswap##s(v))
391 # define AV_RL(s, p) AV_RN##s(p)
392 # define AV_WL(s, p, v) AV_WN##s(p, v)
395 #define AV_RB8(x) (((const uint8_t*)(x))[0])
396 #define AV_WB8(p, d) do { ((uint8_t*)(p))[0] = (d); } while(0)
398 #define AV_RL8(x) AV_RB8(x)
399 #define AV_WL8(p, d) AV_WB8(p, d)
402 # define AV_RB16(p) AV_RB(16, p)
405 # define AV_WB16(p, v) AV_WB(16, p, v)
409 # define AV_RL16(p) AV_RL(16, p)
412 # define AV_WL16(p, v) AV_WL(16, p, v)
416 # define AV_RB32(p) AV_RB(32, p)
419 # define AV_WB32(p, v) AV_WB(32, p, v)
423 # define AV_RL32(p) AV_RL(32, p)
426 # define AV_WL32(p, v) AV_WL(32, p, v)
430 # define AV_RB64(p) AV_RB(64, p)
433 # define AV_WB64(p, v) AV_WB(64, p, v)
437 # define AV_RL64(p) AV_RL(64, p)
440 # define AV_WL64(p, v) AV_WL(64, p, v)
444 # define AV_RB24(x) \
445 ((((const uint8_t*)(x))[0] << 16) | \
446 (((const uint8_t*)(x))[1] << 8) | \
447 ((const uint8_t*)(x))[2])
450 # define AV_WB24(p, d) do { \
451 ((uint8_t*)(p))[2] = (d); \
452 ((uint8_t*)(p))[1] = (d)>>8; \
453 ((uint8_t*)(p))[0] = (d)>>16; \
458 # define AV_RL24(x) \
459 ((((const uint8_t*)(x))[2] << 16) | \
460 (((const uint8_t*)(x))[1] << 8) | \
461 ((const uint8_t*)(x))[0])
464 # define AV_WL24(p, d) do { \
465 ((uint8_t*)(p))[0] = (d); \
466 ((uint8_t*)(p))[1] = (d)>>8; \
467 ((uint8_t*)(p))[2] = (d)>>16; \
472 # define AV_RB48(x) \
473 (((uint64_t)((const uint8_t*)(x))[0] << 40) | \
474 ((uint64_t)((const uint8_t*)(x))[1] << 32) | \
475 ((uint64_t)((const uint8_t*)(x))[2] << 24) | \
476 ((uint64_t)((const uint8_t*)(x))[3] << 16) | \
477 ((uint64_t)((const uint8_t*)(x))[4] << 8) | \
478 (uint64_t)((const uint8_t*)(x))[5])
481 # define AV_WB48(p, darg) do { \
482 uint64_t d = (darg); \
483 ((uint8_t*)(p))[5] = (d); \
484 ((uint8_t*)(p))[4] = (d)>>8; \
485 ((uint8_t*)(p))[3] = (d)>>16; \
486 ((uint8_t*)(p))[2] = (d)>>24; \
487 ((uint8_t*)(p))[1] = (d)>>32; \
488 ((uint8_t*)(p))[0] = (d)>>40; \
493 # define AV_RL48(x) \
494 (((uint64_t)((const uint8_t*)(x))[5] << 40) | \
495 ((uint64_t)((const uint8_t*)(x))[4] << 32) | \
496 ((uint64_t)((const uint8_t*)(x))[3] << 24) | \
497 ((uint64_t)((const uint8_t*)(x))[2] << 16) | \
498 ((uint64_t)((const uint8_t*)(x))[1] << 8) | \
499 (uint64_t)((const uint8_t*)(x))[0])
502 # define AV_WL48(p, darg) do { \
503 uint64_t d = (darg); \
504 ((uint8_t*)(p))[0] = (d); \
505 ((uint8_t*)(p))[1] = (d)>>8; \
506 ((uint8_t*)(p))[2] = (d)>>16; \
507 ((uint8_t*)(p))[3] = (d)>>24; \
508 ((uint8_t*)(p))[4] = (d)>>32; \
509 ((uint8_t*)(p))[5] = (d)>>40; \
514 * The AV_[RW]NA macros access naturally aligned data
515 * in a type-safe way.
518 #define AV_RNA(s, p) (((const av_alias##s*)(p))->u##s)
519 #define AV_WNA(s, p, v) (((av_alias##s*)(p))->u##s = (v))
522 # define AV_RN16A(p) AV_RNA(16, p)
526 # define AV_RN32A(p) AV_RNA(32, p)
530 # define AV_RN64A(p) AV_RNA(64, p)
534 # define AV_WN16A(p, v) AV_WNA(16, p, v)
538 # define AV_WN32A(p, v) AV_WNA(32, p, v)
542 # define AV_WN64A(p, v) AV_WNA(64, p, v)
546 * The AV_COPYxxU macros are suitable for copying data to/from unaligned
550 #define AV_COPYU(n, d, s) AV_WN##n(d, AV_RN##n(s));
553 # define AV_COPY16U(d, s) AV_COPYU(16, d, s)
557 # define AV_COPY32U(d, s) AV_COPYU(32, d, s)
561 # define AV_COPY64U(d, s) AV_COPYU(64, d, s)
565 # define AV_COPY128U(d, s) \
568 AV_COPY64U((char *)(d) + 8, (const char *)(s) + 8); \
572 /* Parameters for AV_COPY*, AV_SWAP*, AV_ZERO* must be
573 * naturally aligned. They may be implemented using MMX,
574 * so emms_c() must be called before using any float code
578 #define AV_COPY(n, d, s) \
579 (((av_alias##n*)(d))->u##n = ((const av_alias##n*)(s))->u##n)
582 # define AV_COPY16(d, s) AV_COPY(16, d, s)
586 # define AV_COPY32(d, s) AV_COPY(32, d, s)
590 # define AV_COPY64(d, s) AV_COPY(64, d, s)
594 # define AV_COPY128(d, s) \
597 AV_COPY64((char*)(d)+8, (char*)(s)+8); \
601 #define AV_SWAP(n, a, b) FFSWAP(av_alias##n, *(av_alias##n*)(a), *(av_alias##n*)(b))
604 # define AV_SWAP64(a, b) AV_SWAP(64, a, b)
607 #define AV_ZERO(n, d) (((av_alias##n*)(d))->u##n = 0)
610 # define AV_ZERO16(d) AV_ZERO(16, d)
614 # define AV_ZERO32(d) AV_ZERO(32, d)
618 # define AV_ZERO64(d) AV_ZERO(64, d)
622 # define AV_ZERO128(d) \
625 AV_ZERO64((char*)(d)+8); \
629 #endif /* AVUTIL_INTREADWRITE_H */