PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
types.h
1/*
2# _____ ___ ____ ___ ____
3# ____| | ____| | | |____|
4# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5#-----------------------------------------------------------------------
6# Copyright 2001-2022, ps2dev - http://www.ps2dev.org
7# Licenced under Academic Free License version 2.0
8# Review ps2sdk README & LICENSE files for further details.
9*/
10
11#ifndef __TYPES_H__
12#define __TYPES_H__
13
14#include <stdint.h>
15
16static inline uint32_t lw_le(uint32_t data)
17{
18 uint8_t *ptr;
19 uint32_t val;
20
21 ptr = (uint8_t*) &data;
22
23 val = ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
24
25 return val;
26}
27
28static inline uint16_t lh_le(uint16_t data)
29{
30 uint8_t *ptr;
31 uint16_t val;
32
33 ptr = (uint8_t*) &data;
34
35 val = ptr[0] | (ptr[1] << 8);
36
37 return val;
38}
39
40static inline uint32_t lw_be(uint32_t data)
41{
42 uint8_t *ptr;
43 uint32_t val;
44
45 ptr = (uint8_t*) &data;
46
47 val = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
48
49 return val;
50}
51
52static inline uint16_t lh_be(uint16_t data)
53{
54 uint8_t *ptr;
55 uint16_t val;
56
57 ptr = (uint8_t*) &data;
58
59 val = (ptr[0] << 16) | ptr[1];
60
61 return val;
62}
63
64#define LW_LE(x) (lw_le((x)))
65#define LW_BE(x) (lw_be((x)))
66#define LH_LE(x) (lh_le((x)))
67#define LH_BE(x) (lh_be((x)))
68
69#define LW(x) (LW_LE(x))
70#define LH(x) (LH_LE(x))
71
72
73static inline void sw_le(uint8_t *data, uint32_t val)
74{
75 uint8_t* ptr = (uint8_t*) data;
76
77 ptr[0] = (uint8_t) (val & 0xFF);
78 ptr[1] = (uint8_t) ((val >> 8) & 0xFF);
79 ptr[2] = (uint8_t) ((val >> 16) & 0xFF);
80 ptr[3] = (uint8_t) ((val >> 24) & 0xFF);
81}
82
83static inline void sh_le(uint8_t *data, uint16_t val)
84{
85 uint8_t *ptr = (uint8_t*) data;
86
87 ptr[0] = (uint8_t) (val & 0xFF);
88 ptr[1] = (uint8_t) ((val >> 8) & 0xFF);
89}
90
91static inline void sw_be(uint8_t *data, uint32_t val)
92{
93 uint8_t *ptr = (uint8_t*) data;
94
95 ptr[0] = (uint8_t) ((val >> 24) & 0xFF);
96 ptr[1] = (uint8_t) ((val >> 16) & 0xFF);
97 ptr[2] = (uint8_t) ((val >> 8) & 0xFF);
98 ptr[3] = (uint8_t) (val & 0xFF);
99}
100
101static inline void sh_be(uint8_t *data, uint16_t val)
102{
103 uint8_t* ptr = (uint8_t*) data;
104
105 ptr[0] = (uint8_t) ((val >> 8) & 0xFF);
106 ptr[1] = (uint8_t) (val & 0xFF);
107}
108
109#define SW_LE(x, v) (sw_le((uint8_t *)(x), (v)))
110#define SW_BE(x, v) (sw_be((uint8_t *)(x), (v)))
111#define SH_LE(x, v) (sh_le((uint8_t *)(x), (v)))
112#define SH_BE(x, v) (sh_be((uint8_t *)(x), (v)))
113
114#define SW(x, v) (SW_LE(x, v))
115#define SH(x, v) (SH_LE(x, v))
116
117#endif