Merge pull request #4676 from jmarshallnz/dont_set_scraper_on_tvshow_on_nfo
[vuplus_xbmc] / xbmc / utils / EndianSwap.h
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
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)
8  *  any later version.
9  *
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.
14  *
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/>.
18  *
19  */
20
21  /* Endian_SwapXX functions taken from SDL (SDL_endian.h) */
22
23 #ifndef __ENDIAN_SWAP_H__
24 #define __ENDIAN_SWAP_H__
25
26 /* Include config.h to define (or not) WORDS_BIGENDIAN
27    File created by configure */
28 #if defined(TARGET_POSIX)
29 #include "config.h"
30 #include <inttypes.h>
31 #endif
32 #ifdef TARGET_WINDOWS
33 #define __inline__ __inline
34 #include <stdint.h>
35 #endif
36
37
38 /* Set up for C function definitions, even when using C++ */
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43 #if defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
44 static __inline__ uint16_t Endian_Swap16(uint16_t x)
45 {
46         uint16_t result;
47
48         __asm__("rlwimi %0,%2,8,16,23" : "=&r" (result) : "0" (x >> 8), "r" (x));
49         return result;
50 }
51
52 static __inline__ uint32_t Endian_Swap32(uint32_t x)
53 {
54         uint32_t result;
55
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));
59         return result;
60 }
61 #else
62 static __inline__ uint16_t Endian_Swap16(uint16_t x) {
63         return((x<<8)|(x>>8));
64 }
65
66 static __inline__ uint32_t Endian_Swap32(uint32_t x) {
67         return((x<<24)|((x<<8)&0x00FF0000)|((x>>8)&0x0000FF00)|(x>>24));
68 }
69 #endif
70
71 static __inline__ uint64_t Endian_Swap64(uint64_t x) {
72         uint32_t hi, lo;
73
74         /* Separate into high and low 32-bit values and swap them */
75         lo = (uint32_t)(x&0xFFFFFFFF);
76         x >>= 32;
77         hi = (uint32_t)(x&0xFFFFFFFF);
78         x = Endian_Swap32(lo);
79         x <<= 32;
80         x |= Endian_Swap32(hi);
81         return(x);
82
83 }
84
85 void Endian_Swap16_buf(uint16_t *dst, uint16_t *src, int w);
86
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)
94 #else
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)
101 #endif
102
103 /* Ends C function definitions when using C++ */
104 #ifdef __cplusplus
105 }
106 #endif
107
108 #endif  /* __ENDIAN_SWAP_H__ */