CACAO
util.hpp
Go to the documentation of this file.
1 /* src/toolbox/util.hpp - contains some utility functions
2 
3  Copyright (C) 1996-2013
4  CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 
6  This file is part of CACAO.
7 
8  This program 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, or (at
11  your option) any later version.
12 
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  02110-1301, USA.
22 
23 */
24 
25 
26 #ifndef UTIL_HPP_
27 #define UTIL_HPP_ 1
28 
29 #include <config.h>
30 #include <cstdarg>
31 
32 #include "toolbox/assert.hpp"
33 
34 /* function prototypes ********************************************************/
35 
36 int get_variable_message_length(const char *fmt, va_list ap);
37 
38 /**
39  * find the smallest power of two >= n
40  */
41 static inline size_t next_power_of_two(size_t n) {
42  // the 'n--' and 'n++' make sure that a power of two is mapped to itself
43 
44  n--;
45 
46  // this must be repeated log2(bits in size_t) times
47  // i.e. for 5 times for 32 bit, 6 times for 64 bit
48 
49  n = (n >> 1) | n;
50  n = (n >> 2) | n;
51  n = (n >> 4) | n;
52  n = (n >> 8) | n;
53  n = (n >> 16) | n;
54 #if SIZEOF_VOID_P == 8
55  n = (n >> 32) | n;
56 #endif
57 
58  n++;
59 
60  return n;
61 }
62 
63 static inline bool is_power_of_two(size_t n) {
64  return (n & (n - 1)) == 0;
65 }
66 
67 /**
68  * fast computation of n % m.
69  *
70  * m MUST be a power of two and greater than zero
71  */
72 static inline size_t fast_modulo(size_t n, size_t modul) {
73  EXPENSIVE_ASSERT(modul > 0);
75 
76  return n & (modul - 1);
77 }
78 
79 /**
80  * Perform unsigned integer division.
81  * But instead of rounding the result down, round it up.
82  */
83 static inline size_t divide_rounding_up(size_t a, size_t b) {
84  return (a + b - 1) / b;
85 }
86 
87 
88 #endif // UTIL_HPP_
89 
90 
91 /*
92  * These are local overrides for various environment variables in Emacs.
93  * Please do not remove this and leave it at the end of the file, where
94  * Emacs will automagically detect them.
95  * ---------------------------------------------------------------------
96  * Local variables:
97  * mode: c++
98  * indent-tabs-mode: t
99  * c-basic-offset: 4
100  * tab-width: 4
101  * End:
102  * vim:noexpandtab:sw=4:ts=4:
103  */
static bool is_power_of_two(size_t n)
Definition: util.hpp:63
static size_t fast_modulo(size_t n, size_t modul)
fast computation of n % m.
Definition: util.hpp:72
#define EXPENSIVE_ASSERT(EXPR)
An assertion that performs computations too expensive even for a normal debug build.
Definition: assert.hpp:90
int get_variable_message_length(const char *fmt, va_list ap)
Definition: util.cpp:45
Additional assertion macros.
static size_t next_power_of_two(size_t n)
find the smallest power of two &gt;= n
Definition: util.hpp:41
static size_t divide_rounding_up(size_t a, size_t b)
Perform unsigned integer division.
Definition: util.hpp:83