improve event handling (still has problems though)
[pcsx_rearmed.git] / plugins / dfxvideo / swap.h
1 #include <stdint.h>
2
3 // byteswappings
4
5 #define SWAP16(x) ({ uint16_t y=(x); (((y)>>8 & 0xff) | ((y)<<8 & 0xff00)); })
6 #define SWAP32(x) ({ uint32_t y=(x); (((y)>>24 & 0xfful) | ((y)>>8 & 0xff00ul) | ((y)<<8 & 0xff0000ul) | ((y)<<24 & 0xff000000ul)); })
7
8 #ifdef __BIG_ENDIAN__
9
10 // big endian config
11 #define HOST2LE32(x) SWAP32(x)
12 #define HOST2BE32(x) (x)
13 #define LE2HOST32(x) SWAP32(x)
14 #define BE2HOST32(x) (x)
15
16 #define HOST2LE16(x) SWAP16(x)
17 #define HOST2BE16(x) (x)
18 #define LE2HOST16(x) SWAP16(x)
19 #define BE2HOST16(x) (x)
20
21 #else
22
23 // little endian config
24 #define HOST2LE32(x) (x)
25 #define HOST2BE32(x) SWAP32(x)
26 #define LE2HOST32(x) (x)
27 #define BE2HOST32(x) SWAP32(x)
28
29 #define HOST2LE16(x) (x)
30 #define HOST2BE16(x) SWAP16(x)
31 #define LE2HOST16(x) (x)
32 #define BE2HOST16(x) SWAP16(x)
33
34 #endif
35
36 #define GETLEs16(X) ((int16_t)GETLE16((uint16_t *)X))
37 #define GETLEs32(X) ((int16_t)GETLE32((uint16_t *)X))
38
39 #if defined(__PPC__) && defined(__BIG_ENDIAN__)
40
41 // GCC style
42 static __inline__ uint16_t GETLE16(uint16_t *ptr) {
43     uint16_t ret; __asm__ ("lhbrx %0, 0, %1" : "=r" (ret) : "r" (ptr));
44     return ret;
45 }
46 static __inline__ uint32_t GETLE32(uint32_t *ptr) {
47     uint32_t ret;
48     __asm__ ("lwbrx %0, 0, %1" : "=r" (ret) : "r" (ptr));
49     return ret;
50 }
51 static __inline__ uint32_t GETLE16D(uint32_t *ptr) {
52     uint32_t ret;
53     __asm__ ("lwbrx %0, 0, %1\n"
54              "rlwinm %0, %0, 16, 0, 31" : "=r" (ret) : "r" (ptr));
55     return ret;
56 }
57
58 static __inline__ void PUTLE16(uint16_t *ptr, uint16_t val) {
59     __asm__ ("sthbrx %0, 0, %1" : : "r" (val), "r" (ptr) : "memory");
60 }
61 static __inline__ void PUTLE32(uint32_t *ptr, uint32_t val) {
62     __asm__ ("stwbrx %0, 0, %1" : : "r" (val), "r" (ptr) : "memory");
63 }
64
65 #else
66 #define GETLE16(X) LE2HOST16(*(uint16_t *)X)
67 #define GETLE16D(X) ({uint32_t val = GETLE32(X); (val<<16 | val >> 16);})
68 #define PUTLE16(X, Y) do{*((uint16_t *)X)=HOST2LE16((uint16_t)Y);}while(0)
69 #ifdef __arm__
70 #define GETLE32(X) (*(uint16_t *)X|(((uint16_t *)X)[1]<<16))
71 #define PUTLE32(X, Y) do{*((uint16_t *)X)=(uint32_t)Y;((uint16_t *)X)[1]=(uint32_t)(Y)>>16;}while(0)
72 #else
73 #define GETLE32(X) LE2HOST32(*(uint32_t *)X)
74 #define PUTLE32(X, Y) do{*((uint32_t *)X)=HOST2LE16((uint32_t)Y);}while(0)
75 #endif
76 #endif