summaryrefslogtreecommitdiff
path: root/cesar/lib/utils.h
blob: 306030ffff4a4c9fdfbb9e88588b0791427c323b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#ifndef lib_utils_h
#define lib_utils_h
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    lib/utils.h
 * \brief   Common utilities header.
 * \ingroup lib
 *
 * Define useful utilities used almost everywhere.
 *
 * Do not add or modify any macro unless you own the special skills needed to
 * prevent the creation of a mass destruction weapon from a nice looking
 * macro.
 */
#include "lib/preproc.h"

/** Count the number of element of a table. */
#define COUNT(table) (sizeof (table) / sizeof ((table)[0]))

/** Count the number of element of a table which is a member of a structure. */
#define COUNT_MEMBER(parent, table) (COUNT (((parent *) NULL)->table))

/** Return true iff \p a is before \p b modulo 2^32.  This is useful to compare two
 * values of a overflowing counter.  This only works iff \p a and \p b are
 * distant no more than 2^31. */
extern inline bool
less_mod2p32 (u32 a, u32 b)
{
    return (s32) (a - b) < 0;
}

/** Return true iff \p a is before or equal to \p b modulo 2^32.  This is
 * useful to compare two values of a overflowing counter.  This only works iff
 * \p a and \p b are distant no more than 2^31. */
extern inline bool
lesseq_mod2p32 (u32 a, u32 b)
{
    return (s32) (a - b) <= 0;
}

/** Return true iff \p a is before \p b modulo 2^16.  This is useful to compare two
 * values of a overflowing counter.  This only works iff \p a and \p b are
 * distant no more than 2^15. */
extern inline bool
less_mod2p16 (u16 a, u16 b)
{
    return (s16) (a - b) < 0;
}

/** Return true iff \p a is before or equal to \p b modulo 2^16.  This is
 * useful to compare two values of a overflowing counter.  This only works iff
 * \p a and \p b are distant no more than 2^15. */
extern inline bool
lesseq_mod2p16 (u16 a, u16 b)
{
    return (s16) (a - b) <= 0;
}

/** Return the absolute value. */
#define ABS(v) ({ typeof (v) _v = (v); _v > 0 ? _v : -_v; })

/** Return the maximum value. */
#define MAX(a, b) ({ typeof (a) _a = (a); typeof (b) _b = (b); \
                   _a > _b ? _a : _b; })

/** Return the minimum value. */
#define MIN(a, b) ({ typeof (a) _a = (a); typeof (b) _b = (b); \
                   _a < _b ? _a : _b; })

/**
 * Return the upper rounded integer value of a divided by b (a/b).
 * \param  a  the numerator
 * \param  b  the divisor
 *
 * \warning b is evaluated twice. You need to be careful if you do:
 * \code
 * uint b = 1;
 * uint a = CEIL_DIV (1, b++);
 * //   a = (1 + b++ - 1) / b++;
 * \endcode
 */
#define CEIL_DIV(a, b) ( ((a) + (b) - 1) / (b) )

/**
 * Return the nearest integer (away from 0) of a divided by b (a/b).
 * \param  a  the numerator (must be >= 0)
 * \param  b  the divisor (must be > 0)
 *
 * \warning b is evaluated twice.
 */
#define ROUND_DIV(a, b) ( ((a) + (b) / 2) / (b) )

/** Exchange two value. */
#define XCH(a, b) do { \
    typeof (b) _tmp = (a); \
    (a) = (b); \
    (b) = _tmp; \
} while (0)

/** Rotate the word to the right.
 * NB = 0 or size max of val will not work. */
#define ROR(val, nb) ({ \
    typeof (val) _val = (val); \
    typeof (nb) _nb = (nb); \
    typeof(_val) _tmp1 = (_val) >> (_nb); \
    typeof(_val) _tmp2 = (_val) << ((8*sizeof(_val)) - (_nb)); \
    _tmp2 | _tmp1; \
    })

/** Rotate the word to the left.
 * NB = 0 or size max of val will not work. */
#define ROL(val, nb) ({ \
    typeof (val) _val = (val); \
    typeof (nb) _nb = (nb); \
    typeof(_val) _tmp1 = (_val) << (_nb); \
    typeof(_val) _tmp2 = (_val) >> ((8*sizeof(_val)) - (_nb)); \
    _tmp2 | _tmp1; \
    })


/** Return a bit mask composed of a number of LSB ones.
 * \param  b  number of one bits, 1 to 32
 *
 * - BITS_ONES (0) => error
 * - BITS_ONES (1) => 0x00000001
 * - BITS_ONES (15) => 0x00007fff
 * - BITS_ONES (32) => 0xffffffff
 */
#define BITS_ONES(b) ((1u << ((b) - 1) << 1) - 1)

/** Return a bit mask composed of a number of LSB ones, corresponding to the
 * given bit field.
 * \param  f  bit field define
 *
 * The bit field is a preprocessor symbol composed of MSB and LSB separated by
 * a comma.
 */
#define BF_ONES(f) BF_ONES_ (f)
#define BF_ONES_(m, l) BITS_ONES ((m) - (l) + 1)

/** Return a bit mask composed of a number of shifted ones.
 * \param  b  number of one bits, 1 to 32
 * \param  s  shift, 0 to 31
 *
 * - BITS_MASK (0, 0) => error
 * - BITS_MASK (1, 14) => 0x00004000
 * - BITS_MASK (15, 7) => 0x003fff80
 * - BITS_MASK (32, 0) => 0xffffffff
 */
#define BITS_MASK(b, s) (BITS_ONES (b) << (s))

/** Return a bit mask composed of a number of shifted ones, corresponding to
 * the given bit field.
 * \param  f  bit field define
 *
 * \see BF_ONES.
 */
#define BF_MASK(f) BF_MASK_ (f)
#define BF_MASK_(m, l) BITS_MASK ((m) - (l) + 1, (l))

/** Return true if the given value is small enough to fit in the given bit
 * field.
 * \param  f  bit field define
 * \param  v  value to check
 *
 * \see BF_ONES.
 */
#define BF_CHECK(f, v) BF_CHECK_ (f, (v))
#define BF_CHECK_(m, l, v) !((v) & ~BF_ONES_ ((m), (l)))

/** Shift the given value to OR it at the right position in the given bit
 * field.
 * \param  f  bit field define
 * \param  v  value to shift
 *
 * The value is not masked, it should be masked or checked before.
 * \see BF_ONES.
 */
#define BF_SHIFT(f, v) BF_SHIFT_ (f, (v))
#define BF_SHIFT_(m, l, v) ((v) << (l))

/** Extract the given bit field.
 * \param  f  bit field define
 * \param  v  value to extract from
 *
 * \warning v MUST be unsigned
 *
 * \see BF_ONES.
 */
#define BF_GET(f, v) BF_GET_ (f, (v))
#define BF_GET_(m, l, v) ((v) << (31 - (m)) >> (31 - (m) + (l)))

/** Set the given bit field, without changing other bits.
 * \param  regv  register value
 * \param  f  bit field define
 * \param  v  value to set
 *
 * The value is not masked, it should be masked or checked before.
 * \see BF_ONES.
 */
#define BF_SET(regv, f, v) \
    (((regv) & ~BF_MASK_ (f)) | BF_SHIFT_ (f, (v)))

/** Pack different bit fields together.
 * \param  reg  register name (prefix)
 * \param  fv  pair of (field, value)
 *
 * Example:
 * \code
 * #define MYREG__A 7, 0
 * #define MYREG__B 23, 12
 * BF_FILL (MYREG, (A, 0x42), (B, 0x123)) => 0x00123042
 * \endcode
 */
#define BF_FILL(reg, fv...) \
    (0 PREPROC_FOR_EACH_PARAM (BF_FILL_FIELD_, reg ## __, fv))
#define BF_FILL_FIELD_(reg, fv) \
    BF_FILL_FIELD__ (reg, PREPROC_UNPACK (fv))
#define BF_FILL_FIELD__(reg, fv) \
    BF_FILL_FIELD___ (reg, fv)
#define BF_FILL_FIELD___(reg, f, v) \
    | BF_SHIFT (reg ## f, v)

/** Pack different bit masks together.
 * \param  reg  register name (prefix)
 * \param  f  list of fields
 *
 * Example:
 * \code
 * #define MYREG__A 7, 0
 * #define MYREG__B 23, 12
 * BF_MASKS (MYREG, A, B) => 0x00fff0ff
 * \endcode
 */
#define BF_MASKS(reg, f...) \
    (0 PREPROC_FOR_EACH_PARAM (BF_MASKS_FIELD_, reg ## __, f))
#define BF_MASKS_FIELD_(reg, f) \
    | BF_MASK (PASTE_EXPAND (reg, f))

/** Update several bit fields, without changing other bits.
 * \param  regv  register value
 * \param  reg  register name (prefix)
 * \param  fv  pair of (field, value)
 */
#define BF_UPDATE(regv, reg, fv...) \
    (((regv) & ~(0 PREPROC_FOR_EACH_PARAM (BF_UPDATE_MASK_, reg ## __, fv))) \
      | (0 PREPROC_FOR_EACH_PARAM (BF_FILL_FIELD_, reg ## __, fv)))
#define BF_UPDATE_MASK_(reg, fv) \
    BF_UPDATE_MASK__ (reg, PREPROC_UNPACK (fv))
#define BF_UPDATE_MASK__(reg, fv) \
    BF_UPDATE_MASK___ (reg, fv)
#define BF_UPDATE_MASK___(reg, f, v) \
    | BF_MASK (reg ## f)

/** Return the number of one bits. */
#define BITS_ONES_COUNT(x) ({ \
    typeof (x) _x = (x); \
    uint c = 0; \
    while (_x) \
    { \
        _x = _x & (_x - 1); \
        c++; \
    } \
    c; \
})

/** Return the offset of the given \p member in the given \p parent struct. */
#define OFFSET_OF(parent, member) ((uint) &((parent *) NULL)->member)

/** When \p member is a member of \p parent struct and \p p a pointer to this
 * member, return a pointer to the containing struct. */
#define PARENT_OF(parent, member, p) \
    ((parent *) ((u8 *) (p) - OFFSET_OF (parent, member)))

/** Return the parent or NULL if p pointer is null. */
#define PARENT_OF_OR_NULL(parent, member, p) \
    ({ typeof (p) _p = (p); _p ? PARENT_OF (parent, member, _p) : NULL; })

/** Cast to the corresponding volatile type. */
#define VOLATILE(v) ((volatile typeof (*(v)) *) (v))

/** Stop the compiler to reorder instructions across this barrier.  You may
 * consider using volatile instead. */
#define REORDER_BARRIER() __asm__ __volatile__ ("" : : : "memory")

/** Reverse bitfields if needed to have the first field using the least
 * significant bit. */
#define BITFIELDS_WORD(args...) BITFIELDS_WORD_ (args)

#define BITFIELDS_WORD_ECHO(x) x
#if DEFS_REVERSE_BITFIELDS
#  define BITFIELDS_WORD_(args...) \
    PREPROC_FOR_EACH (BITFIELDS_WORD_ECHO, PREPROC_REVERSE (args))
#else
#  define BITFIELDS_WORD_(args...) \
    PREPROC_FOR_EACH (BITFIELDS_WORD_ECHO, args)
#endif

/** Define a fixed point number from a double, with 32 bits precision. */
#define CONST_UF32(d) ((u32) ((1ull << 32) * (d) + 0.5))

/** Convert a size in bytes to the size in bits. */
#define BYTES_SIZE_TO_BITS(val) ((val) * 8)

/**
 * Generic dichotomy search.
 * \param  min  minimum index
 * \param  max  maximum index (past the end)
 * \param  index  index unsigned integer variable
 * \param  less  comparison expression
 *
 * This will expand to a code block which make a dichotomy search in any
 * indexable object.
 *
 * In the following example, the variable \c i gets the found index.  It may
 * index a value greater than the looked up value or be equal to max if all
 * elements are smaller than the looked up element.
 *
 * \code
 * extern int table[256];
 * uint i;
 * int looked_up;
 * DICHOTOMY_SEARCH (0, COUNT (table), i, looked_up < table[i]);
 * \endcode
 */
#define DICHOTOMY_SEARCH(min, max, index, less) \
    do { \
        uint a_, b_; \
        a_ = (min); \
        b_ = (max); \
        while (a_ != b_) \
        { \
            index = (a_ + b_) / 2; \
            if (less) \
            { \
                b_ = index; \
            } \
            else \
            { \
                a_ = index + 1; \
            } \
        } \
        index = a_; \
    } while (0)

BEGIN_DECLS

/** Compare floating point numbers, return true if they are almost equal.
 * \param  a  first number to compare
 * \param  b  second number to compare
 * \param  max_ulps  maximum error as unit in the last place
 */
bool
almost_eqf (float a, float b, int max_ulps);

END_DECLS

#endif /* lib_utils_h */