make use of byte swap builtins of gcc if they are available
[platform/upstream/pulseaudio.git] / src / pulsecore / endianmacros.h
1 #ifndef fooendianmacroshfoo
2 #define fooendianmacroshfoo
3
4 /* $Id$ */
5
6 /***
7   This file is part of PulseAudio.
8
9   Copyright 2004-2006 Lennart Poettering
10   Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
11
12   PulseAudio is free software; you can redistribute it and/or modify
13   it under the terms of the GNU Lesser General Public License as published
14   by the Free Software Foundation; either version 2 of the License,
15   or (at your option) any later version.
16
17   PulseAudio is distributed in the hope that it will be useful, but
18   WITHOUT ANY WARRANTY; without even the implied warranty of
19   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20   General Public License for more details.
21
22   You should have received a copy of the GNU Lesser General Public License
23   along with PulseAudio; if not, write to the Free Software
24   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25   USA.
26 ***/
27
28 #include <inttypes.h>
29
30 #ifndef PACKAGE
31 #error "Please include config.h before including this file!"
32 #endif
33
34 #ifdef HAVE_BYTESWAP_H
35 #include <byteswap.h>
36 #endif
37
38 #ifdef HAVE_BYTESWAP_H
39 #define INT16_SWAP(x) ((int16_t) bswap_16((uint16_t) x))
40 #define UINT16_SWAP(x) ((uint16_t) bswap_16((uint16_t) x))
41 #define INT32_SWAP(x) ((int32_t) bswap_32((uint32_t) x))
42 #define UINT32_SWAP(x) ((uint32_t) bswap_32((uint32_t) x))
43 #else
44 #define INT16_SWAP(x) ( (int16_t) ( ((uint16_t) x >> 8) | ((uint16_t) x << 8) ) )
45 #define UINT16_SWAP(x) ( (uint16_t) ( ((uint16_t) x >> 8) | ((uint16_t) x << 8) ) )
46 #define INT32_SWAP(x) ( (int32_t) ( ((uint32_t) x >> 24) | ((uint32_t) x << 24) | (((uint32_t) x & 0xFF00) << 8) | ((((uint32_t) x) >> 8) & 0xFF00) ) )
47 #define UINT32_SWAP(x) ( (uint32_t) ( ((uint32_t) x >> 24) | ((uint32_t) x << 24) | (((uint32_t) x & 0xFF00) << 8) | ((((uint32_t) x) >> 8) & 0xFF00) ) )
48 #endif
49
50 #define MAYBE_INT32_SWAP(c,x) ((c) ? INT32_SWAP(x) : x)
51 #define MAYBE_UINT32_SWAP(c,x) ((c) ? UINT32_SWAP(x) : x)
52
53 #ifdef WORDS_BIGENDIAN
54  #define INT16_FROM_LE(x) INT16_SWAP(x)
55  #define INT16_FROM_BE(x) ((int16_t)(x))
56
57  #define INT16_TO_LE(x) INT16_SWAP(x)
58  #define INT16_TO_BE(x) ((int16_t)(x))
59
60  #define UINT16_FROM_LE(x) UINT16_SWAP(x)
61  #define UINT16_FROM_BE(x) ((uint16_t)(x))
62
63  #define INT32_FROM_LE(x) INT32_SWAP(x)
64  #define INT32_FROM_BE(x) ((int32_t)(x))
65
66  #define UINT32_FROM_LE(x) UINT32_SWAP(x)
67  #define UINT32_FROM_BE(x) ((uint32_t)(x))
68
69  #define UINT32_TO_LE(x) UINT32_SWAP(x)
70  #define UINT32_TO_BE(x) ((uint32_t)(x))
71 #else
72  #define INT16_FROM_LE(x) ((int16_t)(x))
73  #define INT16_FROM_BE(x) INT16_SWAP(x)
74
75  #define INT16_TO_LE(x) ((int16_t)(x))
76  #define INT16_TO_BE(x) INT16_SWAP(x)
77
78  #define UINT16_FROM_LE(x) ((uint16_t)(x))
79  #define UINT16_FROM_BE(x) UINT16_SWAP(x)
80
81  #define INT32_FROM_LE(x) ((int32_t)(x))
82  #define INT32_FROM_BE(x) INT32_SWAP(x)
83
84  #define UINT32_FROM_LE(x) ((uint32_t)(x))
85  #define UINT32_FROM_BE(x) UINT32_SWAP(x)
86
87  #define UINT32_TO_LE(x) ((uint32_t)(x))
88  #define UINT32_TO_BE(x) UINT32_SWAP(x)
89 #endif
90
91 #endif