Subversion Repositories freemyipod

Rev

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

Rev Author Line No. Line
646 theseven 1
//
2
//
3
//    Copyright 2010 TheSeven
4
//
5
//
6
//    This file is part of emCORE.
7
//
8
//    emCORE 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
//    emCORE 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 emCORE.  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
#ifndef NULL
32
#define NULL ((void*)0)
33
#endif
34
 
35
#ifndef MIN
36
#define MIN(a, b) (((a)<(b))?(a):(b))
37
#endif
38
 
39
#ifndef MAX
40
#define MAX(a, b) (((a)>(b))?(a):(b))
41
#endif
42
 
43
/* return number of elements in array a */
44
#define ARRAYLEN(a) (sizeof(a)/sizeof((a)[0]))
45
 
46
/* return p incremented by specified number of bytes */
47
#define SKIPBYTES(p, count) ((typeof (p))((char *)(p) + (count)))
48
 
49
#define BIT(x) (1 << (x))
50
#define BITRANGE(x, y) ((0xfffffffful >> (31 + (x) - (y))) << (x))
51
 
52
#define ERR_RC(val) (BIT(31) | (val))
53
#define IS_ERR(val) (val & BIT(31))
54
#define RET_ERR(val)                                           \
55
{                                                              \
56
    return ERR_RC(val);                                        \
57
}
58
#define RET_ERR_MTX(val, mutex)                                \
59
{                                                              \
60
    mutex_unlock(mutex);                                       \
61
    return ERR_RC(val);                                        \
62
}
654 theseven 63
#define RET_ERR_FREE(val, ptr)                                 \
64
{                                                              \
65
    free(ptr);                                                 \
66
    return ERR_RC(val);                                        \
67
}
646 theseven 68
#define PASS_RC(expr, bits, val)                               \
69
{                                                              \
70
    int PASS_RC_rc = (expr);                                   \
71
    if (IS_ERR(PASS_RC_rc))                                    \
72
        return ERR_RC((PASS_RC_rc << (bits)) | (val));         \
73
}
74
#define PASS_RC_MTX(expr, bits, val, mutex)                    \
75
{                                                              \
76
    int PASS_RC_MTX_rc = (expr);                               \
77
    if (IS_ERR(PASS_RC_MTX_rc))                                \
78
    {                                                          \
79
        mutex_unlock(mutex);                                   \
80
        return ERR_RC((PASS_RC_MTX_rc << (bits)) | (val));     \
81
    }                                                          \
82
}
654 theseven 83
#define PASS_RC_FREE(expr, bits, val, ptr)                     \
84
{                                                              \
85
    int PASS_RC_FREE_rc = (expr);                              \
86
    if (IS_ERR(PASS_RC_FREE_rc))                               \
87
    {                                                          \
88
        free(ptr);                                             \
89
        return ERR_RC((PASS_RC_FREE_rc << (bits)) | (val));    \
90
    }                                                          \
91
}
646 theseven 92
 
93
#define P2_M1(p2)  ((1 << (p2))-1)
94
 
95
/* align up or down to nearest 2^p2 */
96
#define ALIGN_DOWN_P2(n, p2) ((n) & ~P2_M1(p2))
97
#define ALIGN_UP_P2(n, p2)   ALIGN_DOWN_P2((n) + P2_M1(p2),p2)
98
 
99
/* align up or down to nearest integer multiple of a */
100
#define ALIGN_DOWN(n, a)     ((n)/(a)*(a))
101
#define ALIGN_UP(n, a)       ALIGN_DOWN((n)+((a)-1),a)
102
 
103
/* align start and end of buffer to nearest integer multiple of a */
104
#define ALIGN_BUFFER(ptr,len,align) \
105
{\
106
    uintptr_t tmp_ptr1 = (uintptr_t)ptr; \
107
    uintptr_t tmp_ptr2 = tmp_ptr1 + len;\
108
    tmp_ptr1 = ALIGN_UP(tmp_ptr1,align); \
109
    tmp_ptr2 = ALIGN_DOWN(tmp_ptr2,align); \
110
    len = tmp_ptr2 - tmp_ptr1; \
111
    ptr = (typeof(ptr))tmp_ptr1; \
112
}
113
 
114
 
115
/* live endianness conversion */
116
#ifdef LITTLE_ENDIAN
117
#define letoh16(x) (x)
118
#define letoh32(x) (x)
119
#define htole16(x) (x)
120
#define htole32(x) (x)
121
#define betoh16(x) swap16(x)
122
#define betoh32(x) swap32(x)
123
#define htobe16(x) swap16(x)
124
#define htobe32(x) swap32(x)
125
#define swap_odd_even_be32(x) (x)
126
#define swap_odd_even_le32(x) swap_odd_even32(x)
127
#else
128
#define letoh16(x) swap16(x)
129
#define letoh32(x) swap32(x)
130
#define htole16(x) swap16(x)
131
#define htole32(x) swap32(x)
132
#define betoh16(x) (x)
133
#define betoh32(x) (x)
134
#define htobe16(x) (x)
135
#define htobe32(x) (x)
136
#define swap_odd_even_be32(x) swap_odd_even32(x)
137
#define swap_odd_even_le32(x) (x)
138
#endif
139
 
140
 
141
/* static endianness conversion */
142
#define SWAP_16(x) ((typeof(x))(unsigned short)(((unsigned short)(x) >> 8) | \
143
                                                ((unsigned short)(x) << 8)))
144
 
145
#define SWAP_32(x) ((typeof(x))(unsigned long)( ((unsigned long)(x) >> 24) | \
146
                                               (((unsigned long)(x) & 0xff0000ul) >> 8) | \
147
                                               (((unsigned long)(x) & 0xff00ul) << 8) | \
148
                                                ((unsigned long)(x) << 24)))
149
 
150
#ifdef RLITTLE_ENDIAN
151
#define LE_TO_H16(x) (x)
152
#define LE_TO_H32(x) (x)
153
#define H_TO_LE16(x) (x)
154
#define H_TO_LE32(x) (x)
155
#define BE_TO_H16(x) SWAP_16(x)
156
#define BE_TO_H32(x) SWAP_32(x)
157
#define H_TO_BE16(x) SWAP_16(x)
158
#define H_TO_BE32(x) SWAP_32(x)
159
#else
160
#define LE_TO_H16(x) SWAP_16(x)
161
#define LE_TO_H32(x) SWAP_32(x)
162
#define H_TO_LE16(x) SWAP_16(x)
163
#define H_TO_LE32(x) SWAP_32(x)
164
#define BE_TO_H16(x) (x)
165
#define BE_TO_H32(x) (x)
166
#define H_TO_BE16(x) (x)
167
#define H_TO_BE32(x) (x)
168
#endif
169
 
170
/* Get the byte offset of a type's member */
171
#define OFFSETOF(type, membername) ((off_t)&((type *)0)->membername)
172
 
173
/* Get the type pointer from one of its members */
174
#define TYPE_FROM_MEMBER(type, memberptr, membername) \
175
    ((type *)((intptr_t)(memberptr) - OFFSETOF(type, membername)))
176
 
177
static inline uint16_t swap16(uint16_t value)
178
    /*
179
      result[15..8] = value[ 7..0];
180
      result[ 7..0] = value[15..8];
181
    */
182
{
183
    return (value >> 8) | (value << 8);
184
}
185
 
186
static inline uint32_t swap32(uint32_t value)
187
    /*
188
      result[31..24] = value[ 7.. 0];
189
      result[23..16] = value[15.. 8];
190
      result[15.. 8] = value[23..16];
191
      result[ 7.. 0] = value[31..24];
192
    */
193
{
194
    uint32_t hi = swap16(value >> 16);
195
    uint32_t lo = swap16(value & 0xffff);
196
    return (lo << 16) | hi;
197
}
198
 
199
static inline uint32_t swap_odd_even32(uint32_t value)
200
{
201
    /*
202
      result[31..24],[15.. 8] = value[23..16],[ 7.. 0]
203
      result[23..16],[ 7.. 0] = value[31..24],[15.. 8]
204
    */
205
    uint32_t t = value & 0xff00ff00;
206
    return (t >> 8) | ((t ^ value) << 8);
207
}
208
 
209
#ifndef BIT_N
210
#define BIT_N(n) (1U << (n))
211
#endif
212
 
213
#ifndef CACHEALIGN_SIZE /* could be elsewhere for a particular reason */
214
#ifdef CACHEALIGN_BITS
215
/* 2^CACHEALIGN_BITS = the byte size */
216
#define CACHEALIGN_SIZE (1u << CACHEALIGN_BITS)
217
#else
218
#define CACHEALIGN_SIZE 16  /* FIXME */
219
#endif
220
#endif /* CACHEALIGN_SIZE */
221
 
222
#define CACHEALIGN_ATTR __attribute__((aligned(CACHEALIGN_SIZE)))
223
/* Aligns x up to a CACHEALIGN_SIZE boundary */
224
#define CACHEALIGN_UP(x) \
225
    ((typeof (x))ALIGN_UP_P2((uintptr_t)(x), CACHEALIGN_BITS))
226
/* Aligns x down to a CACHEALIGN_SIZE boundary */
227
#define CACHEALIGN_DOWN(x) \
228
    ((typeof (x))ALIGN_DOWN_P2((uintptr_t)(x), CACHEALIGN_BITS))
229
/* Aligns at least to the greater of size x or CACHEALIGN_SIZE */
230
#define CACHEALIGN_AT_LEAST_ATTR(x) \
231
    __attribute__((aligned(CACHEALIGN_UP(x))))
232
/* Aligns a buffer pointer and size to proper boundaries */
233
#define CACHEALIGN_BUFFER(start, size) \
234
    ALIGN_BUFFER((start), (size), CACHEALIGN_SIZE)
235
 
236
/* Double-cast to avoid 'dereferencing type-punned pointer will
237
 * break strict aliasing rules' B.S. */
238
#define PUN_PTR(type, p) ((type)(intptr_t)(p))
239
 
763 user890104 240
#define BCD2DEC(X) (((((X)>>4) & 0x0f) * 10) + ((X) & 0xf))
241
#define DEC2BCD(X) ((((X)/10)<<4) | ((X)%10))
646 theseven 242
 
763 user890104 243
 
646 theseven 244
#endif