]> code.delx.au - pulseaudio/blob - src/pulsecore/endianmacros.h
remap: Change remapping function argument type from void to int16_t / float as approp...
[pulseaudio] / src / pulsecore / endianmacros.h
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, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 USA.
24 ***/
25
26 #include <inttypes.h>
27
28 #ifndef PACKAGE
29 #error "Please include config.h before including this file!"
30 #endif
31
32 #ifdef HAVE_BYTESWAP_H
33 #include <byteswap.h>
34 #endif
35
36 #ifdef HAVE_BYTESWAP_H
37 #define PA_INT16_SWAP(x) ((int16_t) bswap_16((uint16_t) x))
38 #define PA_UINT16_SWAP(x) ((uint16_t) bswap_16((uint16_t) x))
39 #define PA_INT32_SWAP(x) ((int32_t) bswap_32((uint32_t) x))
40 #define PA_UINT32_SWAP(x) ((uint32_t) bswap_32((uint32_t) x))
41 #else
42 #define PA_INT16_SWAP(x) ( (int16_t) ( ((uint16_t) (x) >> 8) | ((uint16_t) (x) << 8) ) )
43 #define PA_UINT16_SWAP(x) ( (uint16_t) ( ((uint16_t) (x) >> 8) | ((uint16_t) (x) << 8) ) )
44 #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) ) )
45 #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) ) )
46 #endif
47
48 static inline uint32_t PA_READ24BE(const uint8_t *p) {
49 return
50 ((uint32_t) p[0] << 16) |
51 ((uint32_t) p[1] << 8) |
52 ((uint32_t) p[2]);
53 }
54
55 static inline uint32_t PA_READ24LE(const uint8_t *p) {
56 return
57 ((uint32_t) p[2] << 16) |
58 ((uint32_t) p[1] << 8) |
59 ((uint32_t) p[0]);
60 }
61
62 static inline void PA_WRITE24BE(uint8_t *p, uint32_t u) {
63 p[0] = (uint8_t) (u >> 16);
64 p[1] = (uint8_t) (u >> 8);
65 p[2] = (uint8_t) u;
66 }
67
68 static inline void PA_WRITE24LE(uint8_t *p, uint32_t u) {
69 p[2] = (uint8_t) (u >> 16);
70 p[1] = (uint8_t) (u >> 8);
71 p[0] = (uint8_t) u;
72 }
73
74 static inline float PA_FLOAT32_SWAP(float x) {
75 union {
76 float f;
77 uint32_t u;
78 } t;
79
80 t.f = x;
81 t.u = PA_UINT32_SWAP(t.u);
82 return t.f;
83 }
84
85 #define PA_MAYBE_INT16_SWAP(c,x) ((c) ? PA_INT32_SWAP(x) : (x))
86 #define PA_MAYBE_UINT16_SWAP(c,x) ((c) ? PA_UINT32_SWAP(x) : (x))
87
88 #define PA_MAYBE_INT32_SWAP(c,x) ((c) ? PA_INT32_SWAP(x) : (x))
89 #define PA_MAYBE_UINT32_SWAP(c,x) ((c) ? PA_UINT32_SWAP(x) : (x))
90
91 #define PA_MAYBE_FLOAT32_SWAP(c,x) ((c) ? PA_FLOAT32_SWAP(x) : (x))
92
93 #ifdef WORDS_BIGENDIAN
94 #define PA_INT16_FROM_LE(x) PA_INT16_SWAP(x)
95 #define PA_INT16_FROM_BE(x) ((int16_t)(x))
96
97 #define PA_INT16_TO_LE(x) PA_INT16_SWAP(x)
98 #define PA_INT16_TO_BE(x) ((int16_t)(x))
99
100 #define PA_UINT16_FROM_LE(x) PA_UINT16_SWAP(x)
101 #define PA_UINT16_FROM_BE(x) ((uint16_t)(x))
102
103 #define PA_UINT16_TO_LE(x) PA_UINT16_SWAP(x)
104 #define PA_UINT16_TO_BE(x) ((uint16_t)(x))
105
106 #define PA_INT32_FROM_LE(x) PA_INT32_SWAP(x)
107 #define PA_INT32_FROM_BE(x) ((int32_t)(x))
108
109 #define PA_INT32_TO_LE(x) PA_INT32_SWAP(x)
110 #define PA_INT32_TO_BE(x) ((int32_t)(x))
111
112 #define PA_UINT32_FROM_LE(x) PA_UINT32_SWAP(x)
113 #define PA_UINT32_FROM_BE(x) ((uint32_t)(x))
114
115 #define PA_UINT32_TO_LE(x) PA_UINT32_SWAP(x)
116 #define PA_UINT32_TO_BE(x) ((uint32_t)(x))
117
118 #define PA_FLOAT32_TO_LE(x) PA_FLOAT32_SWAP(x)
119 #define PA_FLOAT32_TO_BE(x) ((float) (x))
120
121 #define PA_READ24NE(x) PA_READ24BE(x)
122 #define PA_WRITE24NE(x,y) PA_WRITE24BE((x),(y))
123
124 #define PA_READ24RE(x) PA_READ24LE(x)
125 #define PA_WRITE24RE(x,y) PA_WRITE24LE((x),(y))
126 #else
127 #define PA_INT16_FROM_LE(x) ((int16_t)(x))
128 #define PA_INT16_FROM_BE(x) PA_INT16_SWAP(x)
129
130 #define PA_INT16_TO_LE(x) ((int16_t)(x))
131 #define PA_INT16_TO_BE(x) PA_INT16_SWAP(x)
132
133 #define PA_UINT16_FROM_LE(x) ((uint16_t)(x))
134 #define PA_UINT16_FROM_BE(x) PA_UINT16_SWAP(x)
135
136 #define PA_UINT16_TO_LE(x) ((uint16_t)(x))
137 #define PA_UINT16_TO_BE(x) PA_UINT16_SWAP(x)
138
139 #define PA_INT32_FROM_LE(x) ((int32_t)(x))
140 #define PA_INT32_FROM_BE(x) PA_INT32_SWAP(x)
141
142 #define PA_INT32_TO_LE(x) ((int32_t)(x))
143 #define PA_INT32_TO_BE(x) PA_INT32_SWAP(x)
144
145 #define PA_UINT32_FROM_LE(x) ((uint32_t)(x))
146 #define PA_UINT32_FROM_BE(x) PA_UINT32_SWAP(x)
147
148 #define PA_UINT32_TO_LE(x) ((uint32_t)(x))
149 #define PA_UINT32_TO_BE(x) PA_UINT32_SWAP(x)
150
151 #define PA_FLOAT32_TO_LE(x) ((float) (x))
152 #define PA_FLOAT32_TO_BE(x) PA_FLOAT32_SWAP(x)
153
154 #define PA_READ24NE(x) PA_READ24LE(x)
155 #define PA_WRITE24NE(x,y) PA_WRITE24LE((x),(y))
156
157 #define PA_READ24RE(x) PA_READ24BE(x)
158 #define PA_WRITE24RE(x,y) PA_WRITE24BE((x),(y))
159 #endif
160
161 #endif