General math utility functions. More...
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <limits>
#include <vector>
#include <algorithm>
#include <cassert>
Go to the source code of this file.
Functions | |
template<typename T > | |
constexpr bool | is_even (T num) |
template<typename T > | |
constexpr bool | is_odd (T num) |
constexpr int | div100rounded (int num) |
Guarantees portable results for division by 100; round half up, to the nearest integer. More... | |
constexpr int | bounded_add (int base, int increment, int max_sum, int min_sum=0) |
Returns base + increment, but will not increase base above max_sum, nor decrease it below min_sum. More... | |
template<typename T > | |
constexpr T | modulo (T num, int mod, T min=0) |
constexpr int | round_damage (int base_damage, int bonus, int divisor) |
round (base_damage * bonus / divisor) to the closest integer, but up or down towards base_damage More... | |
template<typename Cmp > | |
bool | in_ranges (const Cmp c, const std::vector< std::pair< Cmp, Cmp >> &ranges) |
template<typename T > | |
constexpr std::size_t | bit_width () |
Returns the size, in bits, of an instance of type T , providing a convenient and self-documenting name for the underlying expression: More... | |
template<typename T > | |
constexpr std::size_t | bit_width (const T &) |
Returns the size, in bits, of x , providing a convenient and self-documenting name for the underlying expression: More... | |
template<typename N > | |
constexpr std::enable_if_t< std::is_integral_v< N >, unsigned int > | count_leading_zeros (N n) |
Returns the quantity of leading 0 bits in n — i.e., the quantity of bits in n , minus the 1-based bit index of the most significant 1 bit in n , or minus 0 if n is 0. More... | |
template<typename N > | |
constexpr unsigned int | count_leading_ones (N n) |
Returns the quantity of leading 1 bits in n — i.e., the quantity of bits in n , minus the 1-based bit index of the most significant 0 bit in n , or minus 0 if n contains no 0 bits. More... | |
int | rounded_division (int a, int b) |
constexpr unsigned | fixed_point_multiply (int32_t n1, int32_t n2) |
constexpr int32_t | fixed_point_divide (int n1, int n2) |
constexpr int | fixed_point_to_int (int32_t n) |
If positive, just bit shift. More... | |
General math utility functions.
Definition in file math.hpp.
|
constexpr |
Returns the size, in bits, of an instance of type T
, providing a convenient and self-documenting name for the underlying expression:
sizeof(T) * std::numeric_limits<unsigned char>::digits
T | The return value is the size, in bits, of an instance of this type. |
T
.
|
constexpr |
Returns the size, in bits, of x
, providing a convenient and self-documenting name for the underlying expression:
sizeof(x) * std::numeric_limits<unsigned char>::digits
T | The return value is the size, in bits, of the type of this object. |
T
.
|
constexpr |
Returns base + increment, but will not increase base above max_sum, nor decrease it below min_sum.
(If base is already below the applicable limit, base will be returned.)
Definition at line 48 of file math.hpp.
Referenced by tod_manager::get_illuminated_time_of_day(), and terrain_type::light_bonus().
|
constexpr |
Returns the quantity of leading 1
bits in n
— i.e., the quantity of bits in n
, minus the 1-based bit index of the most significant 0
bit in n
, or minus 0 if n
contains no 0
bits.
N | The type of n . This should be a fundamental integer type that (a) is not wider than unsigned long long int , and (b) is no greater than INT_MAX bits in width; if N does not satisfy these constraints, the return value is undefined. |
n | An integer upon which to operate. |
1
bits in n
, if N
satisfies the above constraints.Definition at line 179 of file math.hpp.
References n.
Referenced by ucs4_convert_impl::utf8_impl::byte_size_from_utf8_first(), and utf8::byte_size_from_utf8_first().
|
constexpr |
Returns the quantity of leading 0
bits in n
— i.e., the quantity of bits in n
, minus the 1-based bit index of the most significant 1
bit in n
, or minus 0 if n
is 0.
N | The type of n . This should be a fundamental integer type that (a) is not wider than unsigned long long int (the GCC count-leading-zeros built-in functions are defined for unsigned int , unsigned long int , and unsigned long long int ), and (b) is no greater than INT_MAX bits in width (the GCC built-in functions return instances of type int ); if N does not satisfy these constraints, the return value is undefined. |
n | An integer upon which to operate. |
0
bits in n
, if N
satisfies the above constraints.
|
constexpr |
Guarantees portable results for division by 100; round half up, to the nearest integer.
Definition at line 39 of file math.hpp.
Referenced by utils::apply_modifier(), and carryover_show_gold().
|
constexpr |
n1 | The numerator, which gets bit shifted left. |
n2 | The divisor. |
Definition at line 207 of file math.hpp.
Referenced by scale_surface(), and scale_surface_legacy().
|
constexpr |
n1 | The first number to multiply. |
n2 | The second number to multiply. |
Definition at line 197 of file math.hpp.
Referenced by brighten_image().
|
constexpr |
If positive, just bit shift.
Else, make positive, bit shift, then make negative again.
n | The number to bit shift right. |
Definition at line 219 of file math.hpp.
References n.
Referenced by scale_surface(), and scale_surface_legacy().
bool in_ranges | ( | const Cmp | c, |
const std::vector< std::pair< Cmp, Cmp >> & | ranges | ||
) |
Definition at line 87 of file math.hpp.
References c.
Referenced by check_side_number(), game_events::builtin_conditions::have_unit(), and terrain_filter::match_internal().
|
constexpr |
Definition at line 33 of file math.hpp.
Referenced by actions::shroud_clearer::clear_loc(), default_map_generator_job::default_generate_map(), is_odd(), and is_vertically_higher_than().
|
constexpr |
Definition at line 36 of file math.hpp.
References is_even().
Referenced by actions::shroud_clearer::clear_loc(), display::draw_minimap_units(), default_map_generator::generate_map(), display::get_location_y(), intf_terrain_mask(), version_info::is_dev_version(), is_vertically_higher_than(), image::prep_minimap_for_rendering(), rotate_180_surface(), editor::map_fragment::rotate_60_ccw(), and editor::map_fragment::rotate_60_cw().
|
constexpr |
Definition at line 62 of file math.hpp.
References n.
Referenced by tod_manager::calculate_time_index_at_turn(), playsingle_controller::find_viewing_side(), tod_manager::fix_time_index(), and playsingle_controller::skip_empty_sides().
|
constexpr |
round (base_damage * bonus / divisor) to the closest integer, but up or down towards base_damage
Definition at line 80 of file math.hpp.
Referenced by attack_info(), and battle_context_unit_stats::battle_context_unit_stats().
|
inline |
Definition at line 186 of file math.hpp.
References b.
Referenced by gui2::slider_base::set_slider_position(), gui2::slider::set_value(), and gui2::slider_base::update_slider_position().