PolyVox
0.2.1
Open source voxel management library
Main Page
Related Pages
Namespaces
Classes
Files
File List
File Members
PolyVoxCore
source
Impl
Utility.cpp
Go to the documentation of this file.
1
/*******************************************************************************
2
Copyright (c) 2005-2009 David Williams
3
4
This software is provided 'as-is', without any express or implied
5
warranty. In no event will the authors be held liable for any damages
6
arising from the use of this software.
7
8
Permission is granted to anyone to use this software for any purpose,
9
including commercial applications, and to alter it and redistribute it
10
freely, subject to the following restrictions:
11
12
1. The origin of this software must not be misrepresented; you must not
13
claim that you wrote the original software. If you use this software
14
in a product, an acknowledgment in the product documentation would be
15
appreciated but is not required.
16
17
2. Altered source versions must be plainly marked as such, and must not be
18
misrepresented as being the original software.
19
20
3. This notice may not be removed or altered from any source
21
distribution.
22
*******************************************************************************/
23
24
#include "
PolyVoxCore/Impl/Utility.h
"
25
26
#include <cassert>
27
#include <stdexcept>
28
29
namespace
PolyVox
30
{
31
//Note: this function only works for inputs which are a power of two and not zero
32
//If this is not the case then the output is undefined.
33
uint8_t
logBase2
(uint32_t uInput)
34
{
35
//Debug mode validation
36
assert(uInput != 0);
37
assert(
isPowerOf2
(uInput));
38
39
//Release mode validation
40
if
(uInput == 0)
41
{
42
throw
std::invalid_argument(
"Cannot compute the log of zero."
);
43
}
44
if
(!
isPowerOf2
(uInput))
45
{
46
throw
std::invalid_argument(
"Input must be a power of two in order to compute the log."
);
47
}
48
49
uint32_t uResult = 0;
50
while
( (uInput >> uResult) != 0)
51
{
52
++uResult;
53
}
54
return
static_cast<
uint8_t
>
(uResult-1);
55
}
56
57
58
bool
isPowerOf2
(uint32_t uInput)
59
{
60
if
(uInput == 0)
61
return
false
;
62
else
63
return
((uInput & (uInput-1)) == 0);
64
}
65
}
Generated by
1.8.1