xine-lib: xine-lib-1.2.13/src/xine-engine/bswap.h Source File

xine-lib 1.2.13-20230125hg15249

bswap.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2000-2018 the xine project
3 *
4 * This file is part of xine, a free video player.
5 *
6 * xine is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * xine 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
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
19 */
20
21#ifndef __BSWAP_H__
22#define __BSWAP_H__
23
24#if defined(HAVE_CONFIG_H) && !defined(__XINE_LIB_CONFIG_H__)
25# error config.h not included
26#endif
27
28#define always_inline inline
29
30#include "ffmpeg_bswap.h"
31
32/* These are the Aligned variants */
33#define _X_ABE_16(x) (be2me_16(*(const uint16_t*)(x)))
34#define _X_ABE_32(x) (be2me_32(*(const uint32_t*)(x)))
35#define _X_ABE_64(x) (be2me_64(*(const uint64_t*)(x)))
36#define _X_ALE_16(x) (le2me_16(*(const uint16_t*)(x)))
37#define _X_ALE_32(x) (le2me_32(*(const uint32_t*)(x)))
38#define _X_ALE_64(x) (le2me_64(*(const uint64_t*)(x)))
39
40#define _X_BE_16(x) (((uint16_t)(((const uint8_t*)(x))[0]) << 8) | \
41 ((uint16_t)((const uint8_t*)(x))[1]))
42#define _X_BE_24(x) (((uint32_t)(((const uint8_t*)(x))[0]) << 16) | \
43 ((uint32_t)(((const uint8_t*)(x))[1]) << 8) | \
44 ((uint32_t)(((const uint8_t*)(x))[2])))
45#define _X_BE_32(x) (((uint32_t)(((const uint8_t*)(x))[0]) << 24) | \
46 ((uint32_t)(((const uint8_t*)(x))[1]) << 16) | \
47 ((uint32_t)(((const uint8_t*)(x))[2]) << 8) | \
48 ((uint32_t)((const uint8_t*)(x))[3]))
49#define _X_BE_64(x) (((uint64_t)(((const uint8_t*)(x))[0]) << 56) | \
50 ((uint64_t)(((const uint8_t*)(x))[1]) << 48) | \
51 ((uint64_t)(((const uint8_t*)(x))[2]) << 40) | \
52 ((uint64_t)(((const uint8_t*)(x))[3]) << 32) | \
53 ((uint64_t)(((const uint8_t*)(x))[4]) << 24) | \
54 ((uint64_t)(((const uint8_t*)(x))[5]) << 16) | \
55 ((uint64_t)(((const uint8_t*)(x))[6]) << 8) | \
56 ((uint64_t)((const uint8_t*)(x))[7]))
57
58#define _X_LE_16(x) (((uint16_t)(((const uint8_t*)(x))[1]) << 8) | \
59 ((uint16_t)((const uint8_t*)(x))[0]))
60#define _X_LE_24(x) (((uint32_t)(((const uint8_t*)(x))[2]) << 16) | \
61 ((uint32_t)(((const uint8_t*)(x))[1]) << 8) | \
62 ((uint32_t)(((const uint8_t*)(x))[0])))
63#define _X_LE_32(x) (((uint32_t)(((const uint8_t*)(x))[3]) << 24) | \
64 ((uint32_t)(((const uint8_t*)(x))[2]) << 16) | \
65 ((uint32_t)(((const uint8_t*)(x))[1]) << 8) | \
66 ((uint32_t)((const uint8_t*)(x))[0]))
67#define _X_LE_64(x) (((uint64_t)(((const uint8_t*)(x))[7]) << 56) | \
68 ((uint64_t)(((const uint8_t*)(x))[6]) << 48) | \
69 ((uint64_t)(((const uint8_t*)(x))[5]) << 40) | \
70 ((uint64_t)(((const uint8_t*)(x))[4]) << 32) | \
71 ((uint64_t)(((const uint8_t*)(x))[3]) << 24) | \
72 ((uint64_t)(((const uint8_t*)(x))[2]) << 16) | \
73 ((uint64_t)(((const uint8_t*)(x))[1]) << 8) | \
74 ((uint64_t)((const uint8_t*)(x))[0]))
75
76/* Tested with gcc 4.4 */
77#if defined(__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ > 3)) || defined(__clang__)
78# undef _X_BE_24
79# undef _X_BE_32
80# undef _X_BE_64
81# undef _X_LE_24
82# undef _X_LE_32
83# undef _X_LE_64
84# ifdef WORDS_BIGENDIAN
85# define _X_LE_24(x) ({ int32_t tempi; __builtin_memcpy (&tempi, (x), 3); (uint32_t)(__builtin_bswap32 (tempi)) & 0xffffff; })
86# define _X_LE_32(x) ({ int32_t tempi; __builtin_memcpy (&tempi, (x), 4); (uint32_t)(__builtin_bswap32 (tempi)); })
87# define _X_LE_64(x) ({ int64_t tempi; __builtin_memcpy (&tempi, (x), 8); (uint64_t)(__builtin_bswap64 (tempi)); })
88# define _X_BE_24(x) ({ uint32_t tempi; __builtin_memcpy (&tempi, (x), 3); tempi >> 8; })
89# define _X_BE_32(x) ({ uint32_t tempi; __builtin_memcpy (&tempi, (x), 4); tempi; })
90# define _X_BE_64(x) ({ uint64_t tempi; __builtin_memcpy (&tempi, (x), 8); tempi; })
91# else
92# define _X_BE_24(x) ({ int32_t tempi; __builtin_memcpy (&tempi, (x), 3); (uint32_t)(__builtin_bswap32 (tempi)) >> 8; })
93# define _X_BE_32(x) ({ int32_t tempi; __builtin_memcpy (&tempi, (x), 4); (uint32_t)(__builtin_bswap32 (tempi)); })
94# define _X_BE_64(x) ({ int64_t tempi; __builtin_memcpy (&tempi, (x), 8); (uint64_t)(__builtin_bswap64 (tempi)); })
95# define _X_LE_24(x) ({ uint32_t tempi; __builtin_memcpy (&tempi, (x), 3); tempi & 0xffffff; })
96# define _X_LE_32(x) ({ uint32_t tempi; __builtin_memcpy (&tempi, (x), 4); tempi; })
97# define _X_LE_64(x) ({ uint64_t tempi; __builtin_memcpy (&tempi, (x), 8); tempi; })
98# endif
99# if defined(ARCH_X86)
100 /* These machines have true (u)int16_t. */
101# undef _X_BE_16
102# undef _X_LE_16
103# define _X_BE_16(x) ({ \
104 uint16_t tempi; \
105 __builtin_memcpy (&tempi, (x), 2); \
106 __asm__ __volatile__ ( \
107 "rolw\t$8, %0" \
108 : "=r" (tempi) \
109 : "0" (tempi) \
110 : "cc" \
111 ); \
112 tempi; })
113# define _X_LE_16(x) ({ uint16_t tempi; __builtin_memcpy (&tempi, (x), 2); tempi; })
114# endif
115#endif
116
117#ifdef WORDS_BIGENDIAN
118#define _X_ME_16(x) _X_BE_16(x)
119#define _X_ME_32(x) _X_BE_32(x)
120#define _X_ME_64(x) _X_BE_64(x)
121#define _X_AME_16(x) _X_ABE_16(x)
122#define _X_AME_32(x) _X_ABE_32(x)
123#define _X_AME_64(x) _X_ABE_64(x)
124#else
125#define _X_ME_16(x) _X_LE_16(x)
126#define _X_ME_32(x) _X_LE_32(x)
127#define _X_ME_64(x) _X_LE_64(x)
128#define _X_AME_16(x) _X_ALE_16(x)
129#define _X_AME_32(x) _X_ALE_32(x)
130#define _X_AME_64(x) _X_ALE_64(x)
131#endif
132
133#define BE_FOURCC( ch0, ch1, ch2, ch3 ) \
134 ( (uint32_t)(unsigned char)(ch3) | \
135 ( (uint32_t)(unsigned char)(ch2) << 8 ) | \
136 ( (uint32_t)(unsigned char)(ch1) << 16 ) | \
137 ( (uint32_t)(unsigned char)(ch0) << 24 ) )
138
139#define LE_FOURCC( ch0, ch1, ch2, ch3 ) \
140 ( (uint32_t)(unsigned char)(ch0) | \
141 ( (uint32_t)(unsigned char)(ch1) << 8 ) | \
142 ( (uint32_t)(unsigned char)(ch2) << 16 ) | \
143 ( (uint32_t)(unsigned char)(ch3) << 24 ) )
144
145#ifdef WORDS_BIGENDIAN
146#define ME_FOURCC BE_FOURCC
147#else
148#define ME_FOURCC LE_FOURCC
149#endif
150
151#endif
ffmpeg_bswap.h