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
|
/*!The Treasure Box Library
*
* TBox is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* TBox is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with TBox;
* If not, see <a href="http://www.gnu.org/licenses/"> http://www.gnu.org/licenses/</a>
*
* Copyright (C) 2009 - 2015, ruki All rights reserved.
*
* @author ruki
* @file utils.h
*
*/
#ifndef TB_PREFIX_UTILS_H
#define TB_PREFIX_UTILS_H
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "cpu.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* macros
*/
/// abs
#define tb_abs(x) ((x) > 0? (x) : -(x))
/// max
#define tb_max(x, y) (((x) > (y))? (x) : (y))
/// min
#define tb_min(x, y) (((x) < (y))? (x) : (y))
/// max3
#define tb_max3(x, y, z) (((x) > (y))? (((x) > (z))? (x) : (z)) : (((y) > (z))? (y) : (z)))
/// min3
#define tb_min3(x, y, z) (((x) < (y))? (((x) < (z))? (x) : (z)) : (((y) < (z))? (y) : (z)))
/// the number of entries in the array
#define tb_arrayn(x) ((sizeof((x)) / sizeof((x)[0])))
/// ispow2: 1, 2, 4, 8, 16, 32, ...
#define tb_ispow2(x) (!((x) & ((x) - 1)) && (x))
/// align2
#define tb_align2(x) (((x) + 1) >> 1 << 1)
/// align4
#define tb_align4(x) (((x) + 3) >> 2 << 2)
/// align8
#define tb_align8(x) (((x) + 7) >> 3 << 3)
/// align
#define tb_align(x, b) (((tb_size_t)(x) + ((tb_size_t)(b) - 1)) & ~((tb_size_t)(b) - 1))
/// align u32
#define tb_align_u32(x, b) (((tb_uint32_t)(x) + ((tb_uint32_t)(b) - 1)) & ~((tb_uint32_t)(b) - 1))
/// align u64
#define tb_align_u64(x, b) (((tb_uint64_t)(x) + ((tb_uint64_t)(b) - 1)) & ~((tb_uint64_t)(b) - 1))
/// align by pow2
#define tb_align_pow2(x) (((x) > 1)? (tb_ispow2(x)? (x) : ((tb_size_t)1 << (32 - tb_bits_cl0_u32_be((tb_uint32_t)(x))))) : 1)
/*! @def tb_align_cpu
*
* align by cpu bytes
*/
#if TB_CPU_BIT64
# define tb_align_cpu(x) tb_align8(x)
#else
# define tb_align_cpu(x) tb_align4(x)
#endif
/// offsetof
#if defined(TB_COMPILER_IS_GCC) \
&& TB_COMPILER_VERSION_BE(4, 1)
# define tb_offsetof(s, m) (tb_size_t)__builtin_offsetof(s, m)
#else
# define tb_offsetof(s, m) (tb_size_t)&(((s const*)0)->m)
#endif
/// container of
#define tb_container_of(s, m, p) ((s*)(((tb_byte_t*)(p)) - tb_offsetof(s, m)))
/// memsizeof
#define tb_memsizeof(s, m) sizeof(((s const*)0)->m)
/// memtailof
#define tb_memtailof(s, m) (tb_offsetof(s, m) + tb_memsizeof(s, m))
/// memdiffof: lm - rm
#define tb_memdiffof(s, lm, rm) (tb_memtailof(s, lm) - tb_memtailof(s, rm))
/// check the offset and size of member for struct or union
#define tb_memberof_eq(ls, lm, rs, rm) ((tb_offsetof(ls, lm) == tb_offsetof(rs, rm)) && (tb_memsizeof(ls, lm) == tb_memsizeof(rs, rm)))
/// pointer to bool
#define tb_p2b(x) ((tb_bool_t)(tb_size_t)(x))
/// pointer to u8
#define tb_p2u8(x) ((tb_uint8_t)(tb_size_t)(x))
/// pointer to u16
#define tb_p2u16(x) ((tb_uint16_t)(tb_size_t)(x))
/// pointer to u32
#define tb_p2u32(x) ((tb_uint32_t)(tb_size_t)(x))
/// pointer to u64
#define tb_p2u64(x) ((tb_uint64_t)(tb_size_t)(x))
/// pointer to s8
#define tb_p2s8(x) ((tb_sint8_t)(tb_long_t)(x))
/// pointer to s16
#define tb_p2s16(x) ((tb_sint16_t)(tb_long_t)(x))
/// pointer to s32
#define tb_p2s32(x) ((tb_sint32_t)(tb_long_t)(x))
/// pointer to s64
#define tb_p2s64(x) ((tb_sint64_t)(tb_long_t)(x))
/// bool to pointer
#define tb_b2p(x) ((tb_pointer_t)(tb_size_t)(x))
/// unsigned integer to pointer
#define tb_u2p(x) ((tb_pointer_t)(tb_size_t)(x))
/// integer to pointer
#define tb_i2p(x) ((tb_pointer_t)(tb_long_t)(x))
/// swap
#define tb_swap(t, l, r) do { t __p = (r); (r) = (l); (l) = __p; } while (0)
#endif
|