Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/math/math_funcs_binary.h
45997 views
1
/**************************************************************************/
2
/* math_funcs_binary.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "core/typedefs.h"
34
35
namespace Math {
36
37
/* Functions to handle powers of 2 and shifting. */
38
39
// Returns `true` if a positive integer is a power of 2, `false` otherwise.
40
template <typename T>
41
constexpr bool is_power_of_2(const T x) {
42
return x && ((x & (x - 1)) == 0);
43
}
44
45
// Function to find the next power of 2 to an integer.
46
constexpr uint64_t next_power_of_2(uint64_t p_number) {
47
if (p_number == 0) {
48
return 0;
49
}
50
51
--p_number;
52
p_number |= p_number >> 1;
53
p_number |= p_number >> 2;
54
p_number |= p_number >> 4;
55
p_number |= p_number >> 8;
56
p_number |= p_number >> 16;
57
p_number |= p_number >> 32;
58
59
return ++p_number;
60
}
61
62
constexpr uint32_t next_power_of_2(uint32_t p_number) {
63
if (p_number == 0) {
64
return 0;
65
}
66
67
--p_number;
68
p_number |= p_number >> 1;
69
p_number |= p_number >> 2;
70
p_number |= p_number >> 4;
71
p_number |= p_number >> 8;
72
p_number |= p_number >> 16;
73
74
return ++p_number;
75
}
76
77
// Function to find the previous power of 2 to an integer.
78
constexpr uint64_t previous_power_of_2(uint64_t p_number) {
79
p_number |= p_number >> 1;
80
p_number |= p_number >> 2;
81
p_number |= p_number >> 4;
82
p_number |= p_number >> 8;
83
p_number |= p_number >> 16;
84
p_number |= p_number >> 32;
85
return p_number - (p_number >> 1);
86
}
87
88
constexpr uint32_t previous_power_of_2(uint32_t p_number) {
89
p_number |= p_number >> 1;
90
p_number |= p_number >> 2;
91
p_number |= p_number >> 4;
92
p_number |= p_number >> 8;
93
p_number |= p_number >> 16;
94
return p_number - (p_number >> 1);
95
}
96
97
// Function to find the closest power of 2 to an integer.
98
constexpr uint64_t closest_power_of_2(uint64_t p_number) {
99
uint64_t nx = next_power_of_2(p_number);
100
uint64_t px = previous_power_of_2(p_number);
101
return (nx - p_number) > (p_number - px) ? px : nx;
102
}
103
104
constexpr uint32_t closest_power_of_2(uint32_t p_number) {
105
uint32_t nx = next_power_of_2(p_number);
106
uint32_t px = previous_power_of_2(p_number);
107
return (nx - p_number) > (p_number - px) ? px : nx;
108
}
109
110
// Get a shift value from a power of 2.
111
constexpr int32_t get_shift_from_power_of_2(uint64_t p_bits) {
112
for (uint64_t i = 0; i < (uint64_t)64; i++) {
113
if (p_bits == (uint64_t)((uint64_t)1 << i)) {
114
return i;
115
}
116
}
117
118
return -1;
119
}
120
121
constexpr int32_t get_shift_from_power_of_2(uint32_t p_bits) {
122
for (uint32_t i = 0; i < (uint32_t)32; i++) {
123
if (p_bits == (uint32_t)((uint32_t)1 << i)) {
124
return i;
125
}
126
}
127
128
return -1;
129
}
130
131
template <typename T>
132
constexpr T nearest_power_of_2_templated(T p_number) {
133
--p_number;
134
135
// The number of operations on x is the base two logarithm
136
// of the number of bits in the type. Add three to account
137
// for sizeof(T) being in bytes.
138
constexpr size_t shift_steps = get_shift_from_power_of_2((uint64_t)sizeof(T)) + 3;
139
140
// If the compiler is smart, it unrolls this loop.
141
// If it's dumb, this is a bit slow.
142
for (size_t i = 0; i < shift_steps; i++) {
143
p_number |= p_number >> (1 << i);
144
}
145
146
return ++p_number;
147
}
148
149
// Function to find the nearest (bigger) power of 2 to an integer.
150
constexpr uint64_t nearest_shift(uint64_t p_number) {
151
uint64_t i = 63;
152
do {
153
i--;
154
if (p_number & ((uint64_t)1 << i)) {
155
return i + (uint64_t)1;
156
}
157
} while (i != 0);
158
159
return 0;
160
}
161
162
constexpr uint32_t nearest_shift(uint32_t p_number) {
163
uint32_t i = 31;
164
do {
165
i--;
166
if (p_number & ((uint32_t)1 << i)) {
167
return i + (uint32_t)1;
168
}
169
} while (i != 0);
170
171
return 0;
172
}
173
174
// constexpr function to find the floored log2 of a number
175
template <typename T>
176
constexpr T floor_log2(T x) {
177
return x < 2 ? x : 1 + floor_log2(x >> 1);
178
}
179
180
// Get the number of bits needed to represent the number.
181
// IE, if you pass in 8, you will get 4.
182
// If you want to know how many bits are needed to store 8 values however, pass in (8 - 1).
183
template <typename T>
184
constexpr T get_num_bits(T x) {
185
return floor_log2(x);
186
}
187
188
} //namespace Math
189
190