2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
21 /* Endian_SwapXX functions taken from SDL (SDL_endian.h) */
23 #ifndef __ENDIAN_SWAP_H__
24 #define __ENDIAN_SWAP_H__
26 /* Include config.h to define (or not) WORDS_BIGENDIAN
27 File created by configure */
28 #if defined(TARGET_POSIX)
33 #define __inline__ __inline
38 /* Set up for C function definitions, even when using C++ */
43 #if defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
44 static __inline__ uint16_t Endian_Swap16(uint16_t x)
48 __asm__("rlwimi %0,%2,8,16,23" : "=&r" (result) : "0" (x >> 8), "r" (x));
52 static __inline__ uint32_t Endian_Swap32(uint32_t x)
56 __asm__("rlwimi %0,%2,24,16,23" : "=&r" (result) : "0" (x>>24), "r" (x));
57 __asm__("rlwimi %0,%2,8,8,15" : "=&r" (result) : "0" (result), "r" (x));
58 __asm__("rlwimi %0,%2,24,0,7" : "=&r" (result) : "0" (result), "r" (x));
62 static __inline__ uint16_t Endian_Swap16(uint16_t x) {
63 return((x<<8)|(x>>8));
66 static __inline__ uint32_t Endian_Swap32(uint32_t x) {
67 return((x<<24)|((x<<8)&0x00FF0000)|((x>>8)&0x0000FF00)|(x>>24));
71 static __inline__ uint64_t Endian_Swap64(uint64_t x) {
74 /* Separate into high and low 32-bit values and swap them */
75 lo = (uint32_t)(x&0xFFFFFFFF);
77 hi = (uint32_t)(x&0xFFFFFFFF);
78 x = Endian_Swap32(lo);
80 x |= Endian_Swap32(hi);
85 void Endian_Swap16_buf(uint16_t *dst, uint16_t *src, int w);
87 #ifndef WORDS_BIGENDIAN
88 #define Endian_SwapLE16(X) (X)
89 #define Endian_SwapLE32(X) (X)
90 #define Endian_SwapLE64(X) (X)
91 #define Endian_SwapBE16(X) Endian_Swap16(X)
92 #define Endian_SwapBE32(X) Endian_Swap32(X)
93 #define Endian_SwapBE64(X) Endian_Swap64(X)
95 #define Endian_SwapLE16(X) Endian_Swap16(X)
96 #define Endian_SwapLE32(X) Endian_Swap32(X)
97 #define Endian_SwapLE64(X) Endian_Swap64(X)
98 #define Endian_SwapBE16(X) (X)
99 #define Endian_SwapBE32(X) (X)
100 #define Endian_SwapBE64(X) (X)
103 /* Ends C function definitions when using C++ */
108 #endif /* __ENDIAN_SWAP_H__ */