forked from xbmc/xbmc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EndianSwap.h
108 lines (91 loc) · 2.98 KB
/
EndianSwap.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* Copyright (C) 2005-2013 Team XBMC
* http://xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
/* Endian_SwapXX functions taken from SDL (SDL_endian.h) */
#ifndef __ENDIAN_SWAP_H__
#define __ENDIAN_SWAP_H__
/* Include config.h to define (or not) WORDS_BIGENDIAN
File created by configure */
#if defined(TARGET_POSIX)
#include "config.h"
#include <inttypes.h>
#endif
#ifdef TARGET_WINDOWS
#define __inline__ __inline
#include <stdint.h>
#endif
/* Set up for C function definitions, even when using C++ */
#ifdef __cplusplus
extern "C" {
#endif
#if defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
static __inline__ uint16_t Endian_Swap16(uint16_t x)
{
uint16_t result;
__asm__("rlwimi %0,%2,8,16,23" : "=&r" (result) : "0" (x >> 8), "r" (x));
return result;
}
static __inline__ uint32_t Endian_Swap32(uint32_t x)
{
uint32_t result;
__asm__("rlwimi %0,%2,24,16,23" : "=&r" (result) : "0" (x>>24), "r" (x));
__asm__("rlwimi %0,%2,8,8,15" : "=&r" (result) : "0" (result), "r" (x));
__asm__("rlwimi %0,%2,24,0,7" : "=&r" (result) : "0" (result), "r" (x));
return result;
}
#else
static __inline__ uint16_t Endian_Swap16(uint16_t x) {
return((x<<8)|(x>>8));
}
static __inline__ uint32_t Endian_Swap32(uint32_t x) {
return((x<<24)|((x<<8)&0x00FF0000)|((x>>8)&0x0000FF00)|(x>>24));
}
#endif
static __inline__ uint64_t Endian_Swap64(uint64_t x) {
uint32_t hi, lo;
/* Separate into high and low 32-bit values and swap them */
lo = (uint32_t)(x&0xFFFFFFFF);
x >>= 32;
hi = (uint32_t)(x&0xFFFFFFFF);
x = Endian_Swap32(lo);
x <<= 32;
x |= Endian_Swap32(hi);
return(x);
}
void Endian_Swap16_buf(uint16_t *dst, uint16_t *src, int w);
#ifndef WORDS_BIGENDIAN
#define Endian_SwapLE16(X) (X)
#define Endian_SwapLE32(X) (X)
#define Endian_SwapLE64(X) (X)
#define Endian_SwapBE16(X) Endian_Swap16(X)
#define Endian_SwapBE32(X) Endian_Swap32(X)
#define Endian_SwapBE64(X) Endian_Swap64(X)
#else
#define Endian_SwapLE16(X) Endian_Swap16(X)
#define Endian_SwapLE32(X) Endian_Swap32(X)
#define Endian_SwapLE64(X) Endian_Swap64(X)
#define Endian_SwapBE16(X) (X)
#define Endian_SwapBE32(X) (X)
#define Endian_SwapBE64(X) (X)
#endif
/* Ends C function definitions when using C++ */
#ifdef __cplusplus
}
#endif
#endif /* __ENDIAN_SWAP_H__ */