Line data Source code
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 1984 : 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 1984 : 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 1984 : n = (n >> 1) | n;
50 1984 : n = (n >> 2) | n;
51 1984 : n = (n >> 4) | n;
52 1984 : n = (n >> 8) | n;
53 1984 : n = (n >> 16) | n;
54 : #if SIZEOF_VOID_P == 8
55 1984 : n = (n >> 32) | n;
56 : #endif
57 :
58 1984 : n++;
59 :
60 1984 : return n;
61 : }
62 :
63 74054 : static inline bool is_power_of_two(size_t n) {
64 74054 : 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 25089071 : static inline size_t fast_modulo(size_t n, size_t modul) {
73 : EXPENSIVE_ASSERT(modul > 0);
74 : EXPENSIVE_ASSERT(is_power_of_two(modul));
75 :
76 25089071 : 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 326 : static inline size_t divide_rounding_up(size_t a, size_t b) {
84 326 : 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 : */
|