20 #ifndef UTIL_H_INCLUDED
21 #define UTIL_H_INCLUDED
44 inline int bounded_add(
int base,
int increment,
int max_sum,
int min_sum=0) {
46 return std::min(base+increment, std::max(base, max_sum));
48 return std::max(base+increment, std::min(base, min_sum));
53 return (num < 0) ? -(((-
num) + 50) / 100) : (num + 50) / 100;
61 if (base_damage==0)
return 0;
62 const int rounding = divisor / 2 - (bonus < divisor || divisor==1 ? 0 : 1);
63 return std::max<int>(1, (base_damage * bonus + rounding) / divisor);
69 return static_cast<int>(round(d));
71 return static_cast<int>((d >= 0.0)?
std::floor(d + 0.5) : std::ceil(d - 0.5));
77 return (d >= 0.0) ?
std::floor(d + 0.5) : std::ceil(d - 0.5);
82 const char *
what()
const throw()
84 return "bad_lexical_cast";
89 template<
typename To,
typename From>
93 std::stringstream str;
95 if(str << a && str >> res) {
102 template<
typename To,
typename From>
106 std::stringstream str;
108 if(str << a && str >> res) {
122 size_t lexical_cast_default<size_t, const std::string&>(
const std::string&
a,
size_t def);
134 long lexical_cast_default<long, const std::string&>(
const std::string&
a,
long def);
146 int lexical_cast_default<int, const std::string&>(
const std::string&
a,
int def);
158 double lexical_cast_default<double, const std::string&>(
const std::string&
a,
double def);
170 float lexical_cast_default<float, const std::string&>(
const std::string&
a,
float def);
175 template<
typename To,
typename From>
179 std::stringstream str;
181 if(str << a && str >> res) {
194 template<
typename Cmp>
195 bool in_ranges(
const Cmp
c,
const std::vector<std::pair<Cmp, Cmp> >&ranges) {
196 typename std::vector<std::pair<Cmp,Cmp> >::const_iterator
range,
197 range_end = ranges.end();
198 for (range = ranges.begin(); range != range_end; ++
range) {
222 return sizeof(T) * std::numeric_limits<unsigned char>::digits;
239 return sizeof(T) * std::numeric_limits<unsigned char>::digits;
270 #if defined(__GNUC__) || defined(__clang__)
272 unsigned char n, std::size_t
w) {
278 return static_cast<unsigned int>(__builtin_clz(n))
279 - static_cast<unsigned int>(
280 bit_width<unsigned int>() -
w);
283 unsigned short int n, std::size_t w) {
284 return static_cast<unsigned int>(__builtin_clz(n))
285 - static_cast<unsigned int>(
286 bit_width<unsigned int>() -
w);
289 unsigned int n, std::size_t w) {
290 return static_cast<unsigned int>(__builtin_clz(n))
291 - static_cast<unsigned int>(
292 bit_width<unsigned int>() -
w);
295 unsigned long int n, std::size_t w) {
296 return static_cast<unsigned int>(__builtin_clzl(n))
297 - static_cast<unsigned int>(
298 bit_width<unsigned long int>() -
w);
301 unsigned long long int n, std::size_t w) {
302 return static_cast<unsigned int>(__builtin_clzll(n))
303 - static_cast<unsigned int>(
304 bit_width<unsigned long long int>() -
w);
307 char n, std::size_t w) {
309 static_cast<unsigned char>(n), w);
312 signed char n, std::size_t w) {
314 static_cast<unsigned char>(n), w);
317 signed short int n, std::size_t w) {
319 static_cast<unsigned short int>(n), w);
322 signed int n, std::size_t w) {
324 static_cast<unsigned int>(n), w);
327 signed long int n, std::size_t w) {
329 static_cast<unsigned long int>(n), w);
332 signed long long int n, std::size_t w) {
334 static_cast<unsigned long long int>(n), w);
341 for (
unsigned int shift = 1; shift <
w; shift *= 2) {
344 return static_cast<unsigned int>(
w) -
count_ones(n);
370 #if defined(__GNUC__) || defined(__clang__)
377 return static_cast<unsigned int>(
bit_width(n));
418 return count_leading_zeros<N>(~n);
422 #define LIKELY(a) __builtin_expect((a),1) // Tells GCC to optimize code so that if is likely to happen
423 #define UNLIKELY(a) __builtin_expect((a),0) // Tells GCC to optimize code so that if is unlikely to happen
426 #define UNLIKELY(a) a
466 typename Container::const_iterator
end = container.end();
473 template<
typename Container>
475 static bool eval(
const Container & container,
476 const typename Container::key_type &
value)
478 return container.find(value) != container.end();
488 template<
typename Container,
typename Value>
500 # define fxp_base (1 << fxp_shift)
503 # define ftofxp(x) (fixed_t((x) * fxp_base))
506 # define fxpmult(x,y) (((x)*(y)) >> fxp_shift)
509 # define fxpdiv(x,y) (((x) << fxp_shift) / (y))
512 # define fxptoi(x) ( ((x)>0) ? ((x) >> fxp_shift) : (-((-(x)) >> fxp_shift)) )
516 # define ftofxp(x) (x)
517 # define fxpmult(x,y) ((x)*(y))
518 # define fxpdiv(x,y) (static_cast<float>(x) / static_cast<float>(y))
519 # define fxptoi(x) ( static_cast<int>(x) )
int lexical_cast_default< int, const char * >(const char *a, int def)
unsigned int count_leading_ones(N n)
Returns the quantity of leading 1 bits in n — i.e., the quantity of bits in n, minus the 1-based bit...
bool contains(const Container &container, const Value &value)
Returns true iff value is found in container.
int div100rounded(int num)
Guarantees portable results for division by 100; round towards 0.
A struct that exists to implement a generic wrapper for std::find.
double lexical_cast_default< double, const char * >(const char *a, double def)
int round_double(double d)
unique_ptr(const unique_ptr &)
GLdouble GLdouble GLdouble b
const char * what() const
bool chars_less_insensitive(char a, char b)
bool chars_equal_insensitive(char a, char b)
int floor(tfloat< T, S > lhs)
GLubyte GLubyte GLubyte GLubyte w
GLsizei const GLfloat * value
unsigned int count_leading_zeros(N n)
Returns the quantity of leading 0 bits in n — i.e., the quantity of bits in n, minus the 1-based bit...
GLboolean GLboolean GLboolean GLboolean a
unique_ptr & operator=(const unique_ptr &)
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
long lexical_cast_default< long, const char * >(const char *a, long def)
float lexical_cast_default< float, const char * >(const char *a, float def)
std::size_t bit_width()
Returns the size, in bits, of an instance of type T, providing a convenient and self-documenting name...
double round_portable(double d)
int round_damage(int base_damage, int bonus, int divisor)
round (base_damage * bonus / divisor) to the closest integer, but up or down towards base_damage ...
static bool eval(const Container &container, const typename Container::key_type &value)
static bool eval(const Container &container, const Value &value)
GLdouble GLdouble GLdouble r
To lexical_cast_in_range(From a, To def, To min, To max)
int bounded_add(int base, int increment, int max_sum, int min_sum=0)
Returns base + increment, but will not increase base above max_sum, nor decrease it below min_sum...
unsigned int count_ones(N n)
Returns the quantity of 1 bits in n — i.e., n’s population count.
size_t lexical_cast_default< size_t, const char * >(const char *a, size_t def)
bool find(E event, F functor)
Tests whether an event handler is available.
To lexical_cast_default(From a, To def=To())
Thrown when a lexical_cast fails.
GLsizei const GLcharARB ** string
unsigned int count_leading_zeros_impl(N n, std::size_t w)
bool in_ranges(const Cmp c, const std::vector< std::pair< Cmp, Cmp > > &ranges)