pspsdk-1.0+beta2
types.h
Go to the documentation of this file.
1 /*
2  * PSP Software Development Kit - http://www.pspdev.org
3  * -----------------------------------------------------------------------
4  * Licensed under the BSD license, see LICENSE in PSPSDK root for details.
5  *
6  * types.h - Definition of basic cross platform types.
7  *
8  * Copyright (c) 2005 James Forshaw <tyranid@gmail.com>
9  *
10  * $Id: types.h 2333 2007-10-31 19:37:40Z tyranid $
11  */
12 
13 #ifndef __TYPES_H__
14 #define __TYPES_H__
15 
16 #include "config.h"
17 #ifdef HAVE_STDINT_H
18 #include <stdint.h>
19 #else
20 #ifdef HAVE_INTTYPES_H
21 #include <inttypes.h>
22 #else
23 #include <sys/types.h>
24 #define uint8_t u_int8_t
25 #define uint16_t u_int16_t
26 #define uint32_t u_int32_t
27 #define uint64_t u_int64_t
28 #endif
29 #endif
30 
31 /* Re-define some system types */
32 typedef uint8_t u8;
33 typedef uint16_t u16;
34 typedef uint32_t u32;
35 typedef uint64_t u64;
36 typedef int8_t s8;
37 typedef int16_t s16;
38 typedef int32_t s32;
39 typedef int64_t s64;
40 
41 #ifdef WORDS_BIGENDIAN
42 inline u32 lw_le(u32 data)
43 {
44  u8 *ptr;
45  u32 val;
46 
47  ptr = (u8*) &data;
48 
49  val = ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
50 
51  return val;
52 }
53 
54 inline u16 lh_le(u16 data)
55 {
56  u8 *ptr;
57  u16 val;
58 
59  ptr = (u8*) &data;
60 
61  val = ptr[0] | (ptr[1] << 8);
62 
63  return val;
64 }
65 
66 #define LW_LE(x) (lw_le((x)))
67 #define LW_BE(x) (x)
68 #define LH_LE(x) (lh_le((x)))
69 #define LH_BE(x) (x)
70 
71 #else
72 
73 inline u32 lw_be(u32 data)
74 {
75  u8 *ptr;
76  u32 val;
77 
78  ptr = (u8*) &data;
79 
80  val = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
81 
82  return val;
83 }
84 
85 inline u16 lh_be(u16 data)
86 {
87  u8 *ptr;
88  u16 val;
89 
90  ptr = (u8*) &data;
91 
92  val = (ptr[0] << 16) | ptr[1];
93 
94  return val;
95 }
96 
97 #define LW_LE(x) (x)
98 #define LW_BE(x) (lw_be((x)))
99 #define LH_LE(x) (x)
100 #define LH_BE(x) (lh_be((x)))
101 
102 #endif
103 
104 #define LW(x) (LW_LE(x))
105 #define LH(x) (LH_LE(x))
106 
107 
108 #ifdef WORDS_BIGENDIAN
109 inline void sw_le(u32 *data, u32 val)
110 {
111  u8* ptr = (u8*) data;
112 
113  ptr[0] = (u8) (val & 0xFF);
114  ptr[1] = (u8) ((val >> 8) & 0xFF);
115  ptr[2] = (u8) ((val >> 16) & 0xFF);
116  ptr[3] = (u8) ((val >> 24) & 0xFF);
117 }
118 
119 inline void sh_le(u16 *data, u16 val)
120 {
121  u8 *ptr = (u8*) data;
122 
123  ptr[0] = (u8) (val & 0xFF);
124  ptr[1] = (u8) ((val >> 8) & 0xFF);
125 }
126 
127 #define SW_LE(x, v) (sw_le((x), (v)))
128 #define SW_BE(x, v) (*(x) = (v))
129 #define SH_LE(x, v) (sh_le((x), (v)))
130 #define SH_BE(x, v) (*(x) = (v))
131 
132 #else
133 
134 inline void sw_be(u32 *data, u32 val)
135 {
136  u8 *ptr = (u8*) data;
137 
138  ptr[0] = (u8) ((val >> 24) & 0xFF);
139  ptr[1] = (u8) ((val >> 16) & 0xFF);
140  ptr[2] = (u8) ((val >> 8) & 0xFF);
141  ptr[3] = (u8) (val & 0xFF);
142 }
143 
144 inline void sh_be(u16 *data, u16 val)
145 {
146  u8* ptr = (u8*) data;
147 
148  ptr[0] = (u8) ((val >> 8) & 0xFF);
149  ptr[1] = (u8) (val & 0xFF);
150 }
151 
152 #define SW_LE(x, v) (*(x) = (v))
153 #define SW_BE(x, v) (sw_be((x), (v)))
154 #define SH_LE(x, v) (*(x) = (v))
155 #define SH_BE(x, v) (sh_be((x), (v)))
156 
157 #endif
158 
159 #define SW(x, v) (SW_LE(x, v))
160 #define SH(x, v) (SH_LE(x, v))
161 
162 #endif