Subversion Repositories freemyipod

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 theseven 1
//
2
//
3
//    Copyright 2010 TheSeven
4
//
5
//
6
//    This file is part of emBIOS.
7
//
8
//    emBIOS is free software: you can redistribute it and/or
9
//    modify it under the terms of the GNU General Public License as
10
//    published by the Free Software Foundation, either version 2 of the
11
//    License, or (at your option) any later version.
12
//
13
//    emBIOS is distributed in the hope that it will be useful,
14
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
//    See the GNU General Public License for more details.
17
//
18
//    You should have received a copy of the GNU General Public License along
19
//    with emBIOS.  If not, see <http://www.gnu.org/licenses/>.
20
//
21
//
22
 
23
 
24
#ifndef __UTIL_H__
25
#define __UTIL_H__
26
 
27
 
28
#include "global.h"
29
 
30
 
31
void* memcpy(void* destination, const void* source, size_t num) ICODE_ATTR;
32
void* memset(void* ptr, int value, size_t num) ICODE_ATTR;
33
int memcmp(const void* ptr1, const void* ptr2, size_t num) ICODE_ATTR;
58 theseven 34
void* memchr(const void* ptr, int value, size_t num) ICODE_ATTR;
2 theseven 35
 
36
 
58 theseven 37
#ifndef NULL
38
#define NULL ((void*)0)
2 theseven 39
#endif
58 theseven 40
 
41
#ifndef MIN
42
#define MIN(a, b) (((a)<(b))?(a):(b))
43
#endif
44
 
45
#ifndef MAX
46
#define MAX(a, b) (((a)>(b))?(a):(b))
47
#endif
48
 
49
/* return number of elements in array a */
50
#define ARRAYLEN(a) (sizeof(a)/sizeof((a)[0]))
51
 
52
/* return p incremented by specified number of bytes */
53
#define SKIPBYTES(p, count) ((typeof (p))((char *)(p) + (count)))
54
 
55
#define P2_M1(p2)  ((1 << (p2))-1)
56
 
57
/* align up or down to nearest 2^p2 */
58
#define ALIGN_DOWN_P2(n, p2) ((n) & ~P2_M1(p2))
59
#define ALIGN_UP_P2(n, p2)   ALIGN_DOWN_P2((n) + P2_M1(p2),p2)
60
 
61
/* align up or down to nearest integer multiple of a */
62
#define ALIGN_DOWN(n, a)     ((n)/(a)*(a))
63
#define ALIGN_UP(n, a)       ALIGN_DOWN((n)+((a)-1),a)
64
 
65
/* align start and end of buffer to nearest integer multiple of a */
66
#define ALIGN_BUFFER(ptr,len,align) \
67
{\
68
    uintptr_t tmp_ptr1 = (uintptr_t)ptr; \
69
    uintptr_t tmp_ptr2 = tmp_ptr1 + len;\
70
    tmp_ptr1 = ALIGN_UP(tmp_ptr1,align); \
71
    tmp_ptr2 = ALIGN_DOWN(tmp_ptr2,align); \
72
    len = tmp_ptr2 - tmp_ptr1; \
73
    ptr = (typeof(ptr))tmp_ptr1; \
74
}
75
 
76
 
77
/* live endianness conversion */
78
#ifdef LITTLE_ENDIAN
79
#define letoh16(x) (x)
80
#define letoh32(x) (x)
81
#define htole16(x) (x)
82
#define htole32(x) (x)
83
#define betoh16(x) swap16(x)
84
#define betoh32(x) swap32(x)
85
#define htobe16(x) swap16(x)
86
#define htobe32(x) swap32(x)
87
#define swap_odd_even_be32(x) (x)
88
#define swap_odd_even_le32(x) swap_odd_even32(x)
89
#else
90
#define letoh16(x) swap16(x)
91
#define letoh32(x) swap32(x)
92
#define htole16(x) swap16(x)
93
#define htole32(x) swap32(x)
94
#define betoh16(x) (x)
95
#define betoh32(x) (x)
96
#define htobe16(x) (x)
97
#define htobe32(x) (x)
98
#define swap_odd_even_be32(x) swap_odd_even32(x)
99
#define swap_odd_even_le32(x) (x)
100
#endif
101
 
102
 
103
/* static endianness conversion */
104
#define SWAP_16(x) ((typeof(x))(unsigned short)(((unsigned short)(x) >> 8) | \
105
                                                ((unsigned short)(x) << 8)))
106
 
107
#define SWAP_32(x) ((typeof(x))(unsigned long)( ((unsigned long)(x) >> 24) | \
108
                                               (((unsigned long)(x) & 0xff0000ul) >> 8) | \
109
                                               (((unsigned long)(x) & 0xff00ul) << 8) | \
110
                                                ((unsigned long)(x) << 24)))
111
 
112
#ifdef RLITTLE_ENDIAN
113
#define LE_TO_H16(x) (x)
114
#define LE_TO_H32(x) (x)
115
#define H_TO_LE16(x) (x)
116
#define H_TO_LE32(x) (x)
117
#define BE_TO_H16(x) SWAP_16(x)
118
#define BE_TO_H32(x) SWAP_32(x)
119
#define H_TO_BE16(x) SWAP_16(x)
120
#define H_TO_BE32(x) SWAP_32(x)
121
#else
122
#define LE_TO_H16(x) SWAP_16(x)
123
#define LE_TO_H32(x) SWAP_32(x)
124
#define H_TO_LE16(x) SWAP_16(x)
125
#define H_TO_LE32(x) SWAP_32(x)
126
#define BE_TO_H16(x) (x)
127
#define BE_TO_H32(x) (x)
128
#define H_TO_BE16(x) (x)
129
#define H_TO_BE32(x) (x)
130
#endif
131
 
132
/* Get the byte offset of a type's member */
133
#define OFFSETOF(type, membername) ((off_t)&((type *)0)->membername)
134
 
135
/* Get the type pointer from one of its members */
136
#define TYPE_FROM_MEMBER(type, memberptr, membername) \
137
    ((type *)((intptr_t)(memberptr) - OFFSETOF(type, membername)))
138
 
139
static inline uint16_t swap16(uint16_t value)
140
    /*
141
      result[15..8] = value[ 7..0];
142
      result[ 7..0] = value[15..8];
143
    */
144
{
145
    return (value >> 8) | (value << 8);
146
}
147
 
148
static inline uint32_t swap32(uint32_t value)
149
    /*
150
      result[31..24] = value[ 7.. 0];
151
      result[23..16] = value[15.. 8];
152
      result[15.. 8] = value[23..16];
153
      result[ 7.. 0] = value[31..24];
154
    */
155
{
156
    uint32_t hi = swap16(value >> 16);
157
    uint32_t lo = swap16(value & 0xffff);
158
    return (lo << 16) | hi;
159
}
160
 
161
static inline uint32_t swap_odd_even32(uint32_t value)
162
{
163
    /*
164
      result[31..24],[15.. 8] = value[23..16],[ 7.. 0]
165
      result[23..16],[ 7.. 0] = value[31..24],[15.. 8]
166
    */
167
    uint32_t t = value & 0xff00ff00;
168
    return (t >> 8) | ((t ^ value) << 8);
169
}
170
 
171
#ifndef BIT_N
172
#define BIT_N(n) (1U << (n))
173
#endif
174
 
175
#ifndef CACHEALIGN_SIZE /* could be elsewhere for a particular reason */
176
#ifdef CACHEALIGN_BITS
177
/* 2^CACHEALIGN_BITS = the byte size */
178
#define CACHEALIGN_SIZE (1u << CACHEALIGN_BITS)
179
#else
180
#define CACHEALIGN_SIZE 16  /* FIXME */
181
#endif
182
#endif /* CACHEALIGN_SIZE */
183
 
184
#define CACHEALIGN_ATTR __attribute__((aligned(CACHEALIGN_SIZE)))
185
/* Aligns x up to a CACHEALIGN_SIZE boundary */
186
#define CACHEALIGN_UP(x) \
187
    ((typeof (x))ALIGN_UP_P2((uintptr_t)(x), CACHEALIGN_BITS))
188
/* Aligns x down to a CACHEALIGN_SIZE boundary */
189
#define CACHEALIGN_DOWN(x) \
190
    ((typeof (x))ALIGN_DOWN_P2((uintptr_t)(x), CACHEALIGN_BITS))
191
/* Aligns at least to the greater of size x or CACHEALIGN_SIZE */
192
#define CACHEALIGN_AT_LEAST_ATTR(x) \
193
    __attribute__((aligned(CACHEALIGN_UP(x))))
194
/* Aligns a buffer pointer and size to proper boundaries */
195
#define CACHEALIGN_BUFFER(start, size) \
196
    ALIGN_BUFFER((start), (size), CACHEALIGN_SIZE)
197
 
198
/* Double-cast to avoid 'dereferencing type-punned pointer will
199
 * break strict aliasing rules' B.S. */
200
#define PUN_PTR(type, p) ((type)(intptr_t)(p))
201
 
202
 
203
#endif