forked from solidoss/solidframe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.hpp
248 lines (205 loc) · 5.61 KB
/
common.hpp
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
// solid/utility/common.hpp
//
// Copyright (c) 2007, 2008 Valentin Palade (vipalade @ gmail . com)
//
// This file is part of SolidFrame framework.
//
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.
//
#ifndef UTILITY_COMMON_HPP
#define UTILITY_COMMON_HPP
#include <limits>
#include "solid/system/common.hpp"
namespace solid{
template <typename T>
inline T tmax(const T &v1, const T &v2){
return (v1 < v2) ? v2 : v1;
}
template <typename T>
inline T tmin(const T &v1, const T &v2){
return (v1 > v2) ? v2 : v1;
}
//! A fast template inline function for exchanging values
template <typename T>
void exchange(T &a, T &b, T &tmp){
tmp = a;
a = b;
b = tmp;
}
//! A fast template inline function for exchanging values
template <typename T>
void exchange(T &a, T &b){
T tmp(a);
a = b;
b = tmp;
}
#if 0
bool overflow_safe_great(const uint32_t _u1, const uint32_t _u2){
if(_u1 > _u2){
return (_u1 - _u2) <= (uint32_t)(0xffffffff/2);
}else{
return (_u2 - _u1) > (uint32_t)(0xffffffff/2);
}
}
#endif
inline bool overflow_safe_less(const uint32_t &_u1, const uint32_t &_u2){
if(_u1 < _u2){
return (_u2 - _u1) <= (uint32_t)(0xffffffff/2);
}else{
return (_u1 - _u2) > (uint32_t)(0xffffffff/2);
}
}
inline bool overflow_safe_less(const uint64_t &_u1, const uint64_t &_u2){
if(_u1 < _u2){
return (_u2 - _u1) <= ((uint64_t)-1)/2;
}else{
return (_u1 - _u2) > ((uint64_t)-1)/2;
}
}
inline uint32_t overflow_safe_max(const uint32_t &_u1, const uint32_t &_u2){
if(overflow_safe_less(_u1, _u2)){
return _u2;
}else{
return _u1;
}
}
inline uint64_t overflow_safe_max(const uint64_t &_u1, const uint64_t &_u2){
if(overflow_safe_less(_u1, _u2)){
return _u2;
}else{
return _u1;
}
}
template <typename T>
inline T circular_distance(const T &_v, const T &_piv, const T& _max){
if(_v >= _piv){
return _v - _piv;
}else{
return _max - _piv + _v;
}
}
inline size_t padded_size(const size_t _sz, const size_t _pad){
const size_t pad = (_pad - (_sz % _pad)) % _pad;
return _sz + pad;
}
inline size_t fast_padded_size(const size_t _sz, const size_t _bitpad){
//return padded_size(_sz, 1<<_bitpad);
const size_t padv = 1<<_bitpad;
const size_t padmsk = padv - 1;
const size_t pad = (padv - (_sz & padmsk)) & padmsk;
return _sz + pad;
}
size_t bit_count(const uint8_t _v);
size_t bit_count(const uint16_t _v);
size_t bit_count(const uint32_t _v);
size_t bit_count(const uint64_t _v);
inline size_t leading_zero_count(uint64_t x){
x = x | (x >> 1);
x = x | (x >> 2);
x = x | (x >> 4);
x = x | (x >> 8);
x = x | (x >>16);
x = x | (x >>32);
return bit_count(~x);
}
inline size_t leading_zero_count(uint32_t x){
x = x | (x >> 1);
x = x | (x >> 2);
x = x | (x >> 4);
x = x | (x >> 8);
x = x | (x >>16);
return bit_count(~x);
}
inline size_t leading_zero_count(uint16_t x){
x = x | (x >> 1);
x = x | (x >> 2);
x = x | (x >> 4);
x = x | (x >> 8);
return bit_count(static_cast<uint16_t>(~x));
}
inline size_t leading_zero_count(uint8_t x){
x = x | (x >> 1);
x = x | (x >> 2);
x = x | (x >> 4);
return bit_count(static_cast<uint8_t>(~x));
}
inline void pack(uint32_t &_v, const uint16_t _v1, const uint16_t _v2){
_v = _v2;
_v <<= 16;
_v |= _v1;
}
inline uint32_t pack(const uint16_t _v1, const uint16_t _v2){
uint32_t v;
pack(v, _v1, _v2);
return v;
}
inline void unpack(uint16_t &_v1, uint16_t &_v2, const uint32_t _v){
_v1 = _v & 0xffffUL;
_v2 = (_v >> 16) & 0xffffUL;
}
extern const uint8_t reverted_chars[];
inline uint32_t bit_revert(const uint32_t _v){
uint32_t r = (((uint32_t)reverted_chars[_v & 0xff]) << 24);
r |= (((uint32_t)reverted_chars[(_v >> 8) & 0xff]) << 16);
r |= (((uint32_t)reverted_chars[(_v >> 16) & 0xff]) << 8);
r |= (((uint32_t)reverted_chars[(_v >> 24) & 0xff]) << 0);
return r;
}
inline uint64_t bit_revert(const uint64_t _v){
uint64_t r = (((uint64_t)reverted_chars[_v & 0xff]) << 56);
r |= (((uint64_t)reverted_chars[(_v >> 8) & 0xff]) << 48);
r |= (((uint64_t)reverted_chars[(_v >> 16) & 0xff]) << 40);
r |= (((uint64_t)reverted_chars[(_v >> 24) & 0xff]) << 32);
r |= (((uint64_t)reverted_chars[(_v >> 32) & 0xff]) << 24);
r |= (((uint64_t)reverted_chars[(_v >> 40) & 0xff]) << 16);
r |= (((uint64_t)reverted_chars[(_v >> 48) & 0xff]) << 8);
r |= (((uint64_t)reverted_chars[(_v >> 56) & 0xff]) << 0);
return r;
}
struct InvalidIndex{
template <typename SizeT>
operator SizeT ()const{
return std::numeric_limits<SizeT>::max();
}
};
struct InvalidSize{
template <typename SizeT>
operator SizeT ()const{
return std::numeric_limits<SizeT>::max();
}
};
template <typename SizeT>
bool operator==(SizeT const&_index, InvalidIndex const&_invalid){
return _index == static_cast<SizeT>(_invalid);
}
template <typename SizeT>
bool operator!=(SizeT const&_index, InvalidIndex const&_invalid){
return _index != static_cast<SizeT>(_invalid);
}
template <typename SizeT>
bool operator==(SizeT const&_index, InvalidSize const&_invalid){
return _index == static_cast<SizeT>(_invalid);
}
template <typename SizeT>
bool operator!=(SizeT const&_index, InvalidSize const&_invalid){
return _index != static_cast<SizeT>(_invalid);
}
template <typename SizeT>
inline bool is_invalid_index(SizeT const& _index){
return _index == InvalidIndex();
}
template <typename SizeT>
inline bool is_valid_index(SizeT const& _index){
return _index != InvalidIndex();
}
template <typename SizeT>
inline bool is_invalid_size(SizeT const& _index){
return _index == InvalidSize();
}
template <typename SizeT>
inline bool is_valid_size(SizeT const& _index){
return _index != InvalidSize();
}
}//namespace solid
#endif