xine-lib  1.2.9
bswap.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2000-2017 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 #include "config.h"
25 
26 #define always_inline inline
27 
28 #include "ffmpeg_bswap.h"
29 
30 /* These are the Aligned variants */
31 #define _X_ABE_16(x) (be2me_16(*(const uint16_t*)(x)))
32 #define _X_ABE_32(x) (be2me_32(*(const uint32_t*)(x)))
33 #define _X_ABE_64(x) (be2me_64(*(const uint64_t*)(x)))
34 #define _X_ALE_16(x) (le2me_16(*(const uint16_t*)(x)))
35 #define _X_ALE_32(x) (le2me_32(*(const uint32_t*)(x)))
36 #define _X_ALE_64(x) (le2me_64(*(const uint64_t*)(x)))
37 
38 #define _X_BE_16(x) (((uint16_t)(((const uint8_t*)(x))[0]) << 8) | \
39  ((uint16_t)((const uint8_t*)(x))[1]))
40 #define _X_BE_24(x) (((uint32_t)(((const uint8_t*)(x))[0]) << 16) | \
41  ((uint32_t)(((const uint8_t*)(x))[1]) << 8) | \
42  ((uint32_t)(((const uint8_t*)(x))[2])))
43 #define _X_BE_32(x) (((uint32_t)(((const uint8_t*)(x))[0]) << 24) | \
44  ((uint32_t)(((const uint8_t*)(x))[1]) << 16) | \
45  ((uint32_t)(((const uint8_t*)(x))[2]) << 8) | \
46  ((uint32_t)((const uint8_t*)(x))[3]))
47 #define _X_BE_64(x) (((uint64_t)(((const uint8_t*)(x))[0]) << 56) | \
48  ((uint64_t)(((const uint8_t*)(x))[1]) << 48) | \
49  ((uint64_t)(((const uint8_t*)(x))[2]) << 40) | \
50  ((uint64_t)(((const uint8_t*)(x))[3]) << 32) | \
51  ((uint64_t)(((const uint8_t*)(x))[4]) << 24) | \
52  ((uint64_t)(((const uint8_t*)(x))[5]) << 16) | \
53  ((uint64_t)(((const uint8_t*)(x))[6]) << 8) | \
54  ((uint64_t)((const uint8_t*)(x))[7]))
55 
56 #define _X_LE_16(x) (((uint16_t)(((const uint8_t*)(x))[1]) << 8) | \
57  ((uint16_t)((const uint8_t*)(x))[0]))
58 #define _X_LE_24(x) (((uint32_t)(((const uint8_t*)(x))[2]) << 16) | \
59  ((uint32_t)(((const uint8_t*)(x))[1]) << 8) | \
60  ((uint32_t)(((const uint8_t*)(x))[0])))
61 #define _X_LE_32(x) (((uint32_t)(((const uint8_t*)(x))[3]) << 24) | \
62  ((uint32_t)(((const uint8_t*)(x))[2]) << 16) | \
63  ((uint32_t)(((const uint8_t*)(x))[1]) << 8) | \
64  ((uint32_t)((const uint8_t*)(x))[0]))
65 #define _X_LE_64(x) (((uint64_t)(((const uint8_t*)(x))[7]) << 56) | \
66  ((uint64_t)(((const uint8_t*)(x))[6]) << 48) | \
67  ((uint64_t)(((const uint8_t*)(x))[5]) << 40) | \
68  ((uint64_t)(((const uint8_t*)(x))[4]) << 32) | \
69  ((uint64_t)(((const uint8_t*)(x))[3]) << 24) | \
70  ((uint64_t)(((const uint8_t*)(x))[2]) << 16) | \
71  ((uint64_t)(((const uint8_t*)(x))[1]) << 8) | \
72  ((uint64_t)((const uint8_t*)(x))[0]))
73 
74 /* Tested with gcc 4.4 */
75 #if defined(__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ > 3)) || defined(__clang__)
76 # undef _X_BE_24
77 # undef _X_BE_32
78 # undef _X_BE_64
79 # undef _X_LE_24
80 # undef _X_LE_32
81 # undef _X_LE_64
82 # ifdef WORDS_BIGENDIAN
83 # define _X_LE_24(x) ({ int32_t tempi; __builtin_memcpy (&tempi, (x), 3); (uint32_t)(__builtin_bswap32 (tempi)) & 0xffffff; })
84 # define _X_LE_32(x) ({ int32_t tempi; __builtin_memcpy (&tempi, (x), 4); (uint32_t)(__builtin_bswap32 (tempi)); })
85 # define _X_LE_64(x) ({ int64_t tempi; __builtin_memcpy (&tempi, (x), 8); (uint64_t)(__builtin_bswap64 (tempi)); })
86 # define _X_BE_24(x) ({ uint32_t tempi; __builtin_memcpy (&tempi, (x), 3); tempi >> 8; })
87 # define _X_BE_32(x) ({ uint32_t tempi; __builtin_memcpy (&tempi, (x), 4); tempi; })
88 # define _X_BE_64(x) ({ uint64_t tempi; __builtin_memcpy (&tempi, (x), 8); tempi; })
89 # else
90 # define _X_BE_24(x) ({ int32_t tempi; __builtin_memcpy (&tempi, (x), 3); (uint32_t)(__builtin_bswap32 (tempi)) >> 8; })
91 # define _X_BE_32(x) ({ int32_t tempi; __builtin_memcpy (&tempi, (x), 4); (uint32_t)(__builtin_bswap32 (tempi)); })
92 # define _X_BE_64(x) ({ int64_t tempi; __builtin_memcpy (&tempi, (x), 8); (uint64_t)(__builtin_bswap64 (tempi)); })
93 # define _X_LE_24(x) ({ uint32_t tempi; __builtin_memcpy (&tempi, (x), 3); tempi & 0xffffff; })
94 # define _X_LE_32(x) ({ uint32_t tempi; __builtin_memcpy (&tempi, (x), 4); tempi; })
95 # define _X_LE_64(x) ({ uint64_t tempi; __builtin_memcpy (&tempi, (x), 8); tempi; })
96 # endif
97 #endif
98 
99 #ifdef WORDS_BIGENDIAN
100 #define _X_ME_16(x) _X_BE_16(x)
101 #define _X_ME_32(x) _X_BE_32(x)
102 #define _X_ME_64(x) _X_BE_64(x)
103 #define _X_AME_16(x) _X_ABE_16(x)
104 #define _X_AME_32(x) _X_ABE_32(x)
105 #define _X_AME_64(x) _X_ABE_64(x)
106 #else
107 #define _X_ME_16(x) _X_LE_16(x)
108 #define _X_ME_32(x) _X_LE_32(x)
109 #define _X_ME_64(x) _X_LE_64(x)
110 #define _X_AME_16(x) _X_ALE_16(x)
111 #define _X_AME_32(x) _X_ALE_32(x)
112 #define _X_AME_64(x) _X_ALE_64(x)
113 #endif
114 
115 #define BE_FOURCC( ch0, ch1, ch2, ch3 ) \
116  ( (uint32_t)(unsigned char)(ch3) | \
117  ( (uint32_t)(unsigned char)(ch2) << 8 ) | \
118  ( (uint32_t)(unsigned char)(ch1) << 16 ) | \
119  ( (uint32_t)(unsigned char)(ch0) << 24 ) )
120 
121 #define LE_FOURCC( ch0, ch1, ch2, ch3 ) \
122  ( (uint32_t)(unsigned char)(ch0) | \
123  ( (uint32_t)(unsigned char)(ch1) << 8 ) | \
124  ( (uint32_t)(unsigned char)(ch2) << 16 ) | \
125  ( (uint32_t)(unsigned char)(ch3) << 24 ) )
126 
127 #ifdef WORDS_BIGENDIAN
128 #define ME_FOURCC BE_FOURCC
129 #else
130 #define ME_FOURCC LE_FOURCC
131 #endif
132 
133 #endif