1 #ifndef fooendianmacroshfoo
2 #define fooendianmacroshfoo
3
4 /***
5 This file is part of PulseAudio.
6
7 Copyright 2004-2006 Lennart Poettering
8 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9
10 PulseAudio is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published
12 by the Free Software Foundation; either version 2.1 of the License,
13 or (at your option) any later version.
14
15 PulseAudio is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
19
20 You should have received a copy of the GNU Lesser General Public License
21 along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
22 ***/
23
24 #include <inttypes.h>
25
26 #ifndef PACKAGE
27 #error "Please include config.h before including this file!"
28 #endif
29
30 #ifdef HAVE_BYTESWAP_H
31 #include <byteswap.h>
32 #endif
33
34 #ifdef HAVE_BYTESWAP_H
35 #define PA_INT16_SWAP(x) ((int16_t) bswap_16((uint16_t) x))
36 #define PA_UINT16_SWAP(x) ((uint16_t) bswap_16((uint16_t) x))
37 #define PA_INT32_SWAP(x) ((int32_t) bswap_32((uint32_t) x))
38 #define PA_UINT32_SWAP(x) ((uint32_t) bswap_32((uint32_t) x))
39 #else
40 #define PA_INT16_SWAP(x) ( (int16_t) ( ((uint16_t) (x) >> 8) | ((uint16_t) (x) << 8) ) )
41 #define PA_UINT16_SWAP(x) ( (uint16_t) ( ((uint16_t) (x) >> 8) | ((uint16_t) (x) << 8) ) )
42 #define PA_INT32_SWAP(x) ( (int32_t) ( ((uint32_t) (x) >> 24) | ((uint32_t) (x) << 24) | (((uint32_t) (x) & 0xFF00) << 8) | ((((uint32_t) (x)) >> 8) & 0xFF00) ) )
43 #define PA_UINT32_SWAP(x) ( (uint32_t) ( ((uint32_t) (x) >> 24) | ((uint32_t) (x) << 24) | (((uint32_t) (x) & 0xFF00) << 8) | ((((uint32_t) (x)) >> 8) & 0xFF00) ) )
44 #endif
45
PA_READ24BE(const uint8_t * p)46 static inline uint32_t PA_READ24BE(const uint8_t *p) {
47 return
48 ((uint32_t) p[0] << 16) |
49 ((uint32_t) p[1] << 8) |
50 ((uint32_t) p[2]);
51 }
52
PA_READ24LE(const uint8_t * p)53 static inline uint32_t PA_READ24LE(const uint8_t *p) {
54 return
55 ((uint32_t) p[2] << 16) |
56 ((uint32_t) p[1] << 8) |
57 ((uint32_t) p[0]);
58 }
59
PA_WRITE24BE(uint8_t * p,uint32_t u)60 static inline void PA_WRITE24BE(uint8_t *p, uint32_t u) {
61 p[0] = (uint8_t) (u >> 16);
62 p[1] = (uint8_t) (u >> 8);
63 p[2] = (uint8_t) u;
64 }
65
PA_WRITE24LE(uint8_t * p,uint32_t u)66 static inline void PA_WRITE24LE(uint8_t *p, uint32_t u) {
67 p[2] = (uint8_t) (u >> 16);
68 p[1] = (uint8_t) (u >> 8);
69 p[0] = (uint8_t) u;
70 }
71
PA_READ_FLOAT32RE(const void * p)72 static inline float PA_READ_FLOAT32RE(const void *p) {
73 union {
74 float f;
75 uint32_t u;
76 } t;
77
78 t.u = PA_UINT32_SWAP(*(uint32_t *) p);
79 return t.f;
80 }
81
PA_WRITE_FLOAT32RE(void * p,float x)82 static inline void PA_WRITE_FLOAT32RE(void *p, float x) {
83 union {
84 float f;
85 uint32_t u;
86 } t;
87
88 t.f = x;
89 *(uint32_t *) p = PA_UINT32_SWAP(t.u);
90 }
91
92 #define PA_MAYBE_INT16_SWAP(c,x) ((c) ? PA_INT16_SWAP(x) : (x))
93 #define PA_MAYBE_UINT16_SWAP(c,x) ((c) ? PA_UINT16_SWAP(x) : (x))
94
95 #define PA_MAYBE_INT32_SWAP(c,x) ((c) ? PA_INT32_SWAP(x) : (x))
96 #define PA_MAYBE_UINT32_SWAP(c,x) ((c) ? PA_UINT32_SWAP(x) : (x))
97
98 #ifdef WORDS_BIGENDIAN
99 #define PA_INT16_FROM_LE(x) PA_INT16_SWAP(x)
100 #define PA_INT16_FROM_BE(x) ((int16_t)(x))
101
102 #define PA_INT16_TO_LE(x) PA_INT16_SWAP(x)
103 #define PA_INT16_TO_BE(x) ((int16_t)(x))
104
105 #define PA_UINT16_FROM_LE(x) PA_UINT16_SWAP(x)
106 #define PA_UINT16_FROM_BE(x) ((uint16_t)(x))
107
108 #define PA_UINT16_TO_LE(x) PA_UINT16_SWAP(x)
109 #define PA_UINT16_TO_BE(x) ((uint16_t)(x))
110
111 #define PA_INT32_FROM_LE(x) PA_INT32_SWAP(x)
112 #define PA_INT32_FROM_BE(x) ((int32_t)(x))
113
114 #define PA_INT32_TO_LE(x) PA_INT32_SWAP(x)
115 #define PA_INT32_TO_BE(x) ((int32_t)(x))
116
117 #define PA_UINT32_FROM_LE(x) PA_UINT32_SWAP(x)
118 #define PA_UINT32_FROM_BE(x) ((uint32_t)(x))
119
120 #define PA_UINT32_TO_LE(x) PA_UINT32_SWAP(x)
121 #define PA_UINT32_TO_BE(x) ((uint32_t)(x))
122
123 #define PA_READ24NE(x) PA_READ24BE(x)
124 #define PA_WRITE24NE(x,y) PA_WRITE24BE((x),(y))
125
126 #define PA_READ24RE(x) PA_READ24LE(x)
127 #define PA_WRITE24RE(x,y) PA_WRITE24LE((x),(y))
128 #else
129 #define PA_INT16_FROM_LE(x) ((int16_t)(x))
130 #define PA_INT16_FROM_BE(x) PA_INT16_SWAP(x)
131
132 #define PA_INT16_TO_LE(x) ((int16_t)(x))
133 #define PA_INT16_TO_BE(x) PA_INT16_SWAP(x)
134
135 #define PA_UINT16_FROM_LE(x) ((uint16_t)(x))
136 #define PA_UINT16_FROM_BE(x) PA_UINT16_SWAP(x)
137
138 #define PA_UINT16_TO_LE(x) ((uint16_t)(x))
139 #define PA_UINT16_TO_BE(x) PA_UINT16_SWAP(x)
140
141 #define PA_INT32_FROM_LE(x) ((int32_t)(x))
142 #define PA_INT32_FROM_BE(x) PA_INT32_SWAP(x)
143
144 #define PA_INT32_TO_LE(x) ((int32_t)(x))
145 #define PA_INT32_TO_BE(x) PA_INT32_SWAP(x)
146
147 #define PA_UINT32_FROM_LE(x) ((uint32_t)(x))
148 #define PA_UINT32_FROM_BE(x) PA_UINT32_SWAP(x)
149
150 #define PA_UINT32_TO_LE(x) ((uint32_t)(x))
151 #define PA_UINT32_TO_BE(x) PA_UINT32_SWAP(x)
152
153 #define PA_READ24NE(x) PA_READ24LE(x)
154 #define PA_WRITE24NE(x,y) PA_WRITE24LE((x),(y))
155
156 #define PA_READ24RE(x) PA_READ24BE(x)
157 #define PA_WRITE24RE(x,y) PA_WRITE24BE((x),(y))
158 #endif
159
160 #endif
161