PolyVox
0.3.0-dev
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/ErrorHandling.h
"
25
#include "
PolyVoxCore/Impl/Utility.h
"
26
27
namespace
PolyVox
28
{
29
//Note: this function only works for inputs which are a power of two and not zero
30
//If this is not the case then the output is undefined.
31
uint8_t
logBase2
(
uint32_t
uInput)
32
{
33
//Debug mode validation
34
POLYVOX_ASSERT
(uInput != 0,
"Cannot compute the log of zero."
);
35
POLYVOX_ASSERT
(
isPowerOf2
(uInput),
"Input must be a power of two in order to compute the log."
);
36
37
//Release mode validation
38
if
(uInput == 0)
39
{
40
POLYVOX_THROW
(std::invalid_argument,
"Cannot compute the log of zero."
);
41
}
42
if
(!
isPowerOf2
(uInput))
43
{
44
POLYVOX_THROW
(std::invalid_argument,
"Input must be a power of two in order to compute the log."
);
45
}
46
47
uint32_t
uResult = 0;
48
while
( (uInput >> uResult) != 0)
49
{
50
++uResult;
51
}
52
return
static_cast<
uint8_t
>
(uResult-1);
53
}
54
55
56
bool
isPowerOf2
(
uint32_t
uInput)
57
{
58
if
(uInput == 0)
59
return
false
;
60
else
61
return
((uInput & (uInput-1)) == 0);
62
}
63
}
Generated by
1.8.2