]> code.delx.au - pulseaudio/blob - src/pulsecore/endianmacros.h
Merge commit 'flameeyes/autoconf-2.62'
[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 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 float PA_FLOAT32_SWAP(float x) {
49 union {
50 float f;
51 uint32_t u;
52 } t;
53
54 t.f = x;
55 t.u = PA_UINT32_SWAP(t.u);
56 return t.f;
57 }
58
59 #define PA_MAYBE_INT16_SWAP(c,x) ((c) ? PA_INT32_SWAP(x) : x)
60 #define PA_MAYBE_UINT16_SWAP(c,x) ((c) ? PA_UINT32_SWAP(x) : x)
61
62 #define PA_MAYBE_INT32_SWAP(c,x) ((c) ? PA_INT32_SWAP(x) : x)
63 #define PA_MAYBE_UINT32_SWAP(c,x) ((c) ? PA_UINT32_SWAP(x) : x)
64
65 #define PA_MAYBE_FLOAT32_SWAP(c,x) ((c) ? PA_FLOAT32_SWAP(x) : x)
66
67 #ifdef WORDS_BIGENDIAN
68 #define PA_INT16_FROM_LE(x) PA_INT16_SWAP(x)
69 #define PA_INT16_FROM_BE(x) ((int16_t)(x))
70
71 #define PA_INT16_TO_LE(x) PA_INT16_SWAP(x)
72 #define PA_INT16_TO_BE(x) ((int16_t)(x))
73
74 #define PA_UINT16_FROM_LE(x) PA_UINT16_SWAP(x)
75 #define PA_UINT16_FROM_BE(x) ((uint16_t)(x))
76
77 #define PA_UINT16_TO_LE(x) PA_UINT16_SWAP(x)
78 #define PA_UINT16_TO_BE(x) ((uint16_t)(x))
79
80 #define PA_INT32_FROM_LE(x) PA_INT32_SWAP(x)
81 #define PA_INT32_FROM_BE(x) ((int32_t)(x))
82
83 #define PA_INT32_TO_LE(x) PA_INT32_SWAP(x)
84 #define PA_INT32_TO_BE(x) ((int32_t)(x))
85
86 #define PA_UINT32_FROM_LE(x) PA_UINT32_SWAP(x)
87 #define PA_UINT32_FROM_BE(x) ((uint32_t)(x))
88
89 #define PA_UINT32_TO_LE(x) PA_UINT32_SWAP(x)
90 #define PA_UINT32_TO_BE(x) ((uint32_t)(x))
91
92 #define PA_FLOAT32_TO_LE(x) PA_FLOAT32_SWAP(x)
93 #define PA_FLOAT32_TO_BE(x) ((float) (x))
94 #else
95 #define PA_INT16_FROM_LE(x) ((int16_t)(x))
96 #define PA_INT16_FROM_BE(x) PA_INT16_SWAP(x)
97
98 #define PA_INT16_TO_LE(x) ((int16_t)(x))
99 #define PA_INT16_TO_BE(x) PA_INT16_SWAP(x)
100
101 #define PA_UINT16_FROM_LE(x) ((uint16_t)(x))
102 #define PA_UINT16_FROM_BE(x) PA_UINT16_SWAP(x)
103
104 #define PA_UINT16_TO_LE(x) ((uint16_t)(x))
105 #define PA_UINT16_TO_BE(x) PA_UINT16_SWAP(x)
106
107 #define PA_INT32_FROM_LE(x) ((int32_t)(x))
108 #define PA_INT32_FROM_BE(x) PA_INT32_SWAP(x)
109
110 #define PA_INT32_TO_LE(x) ((int32_t)(x))
111 #define PA_INT32_TO_BE(x) PA_INT32_SWAP(x)
112
113 #define PA_UINT32_FROM_LE(x) ((uint32_t)(x))
114 #define PA_UINT32_FROM_BE(x) PA_UINT32_SWAP(x)
115
116 #define PA_UINT32_TO_LE(x) ((uint32_t)(x))
117 #define PA_UINT32_TO_BE(x) PA_UINT32_SWAP(x)
118
119 #define PA_FLOAT32_TO_LE(x) ((float) (x))
120 #define PA_FLOAT32_TO_BE(x) PA_FLOAT32_SWAP(x)
121 #endif
122
123 #endif