The Battle for Wesnoth  1.17.4+dev
Functions
math.hpp File Reference

General math utility functions. More...

#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <limits>
#include <vector>
#include <algorithm>
#include <cassert>
Include dependency graph for math.hpp:
This graph shows which files directly or indirectly include this file:

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 int32_t floating_to_fixed_point (double n)
 Converts a double to a fixed point. More...
 
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...
 

Detailed Description

General math utility functions.

Definition in file math.hpp.

Function Documentation

◆ bit_width() [1/2]

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:

sizeof(T) * std::numeric_limits<unsigned char>::digits

Template Parameters
TThe return value is the size, in bits, of an instance of this type.
Returns
the size, in bits, of an instance of type T.

Definition at line 106 of file math.hpp.

◆ bit_width() [2/2]

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:

sizeof(x) * std::numeric_limits<unsigned char>::digits

Template Parameters
TThe return value is the size, in bits, of the type of this object.
Returns
the size, in bits, of an instance of type T.

Definition at line 121 of file math.hpp.

◆ bounded_add()

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.

(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().

◆ count_leading_ones()

template<typename N >
constexpr unsigned int count_leading_ones ( 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.

Template Parameters
NThe 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.
Parameters
nAn integer upon which to operate.
Returns
the quantity of leading 1 bits in n, if N satisfies the above constraints.
See also
count_leading_zeros()

Definition at line 179 of file math.hpp.

Referenced by utf8::byte_size_from_utf8_first(), and ucs4_convert_impl::utf8_impl::byte_size_from_utf8_first().

◆ count_leading_zeros()

template<typename N >
constexpr std::enable_if_t<std::is_integral_v<N>, unsigned int> count_leading_zeros ( 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.

Template Parameters
NThe 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.
Parameters
nAn integer upon which to operate.
Returns
the quantity of leading 0 bits in n, if N satisfies the above constraints.
See also
count_leading_ones()

Definition at line 146 of file math.hpp.

References i, and n.

◆ div100rounded()

constexpr int div100rounded ( int  num)

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 campaign_controller::show_carryover_message().

◆ fixed_point_divide()

constexpr int32_t fixed_point_divide ( int  n1,
int  n2 
)
Parameters
n1The numerator, which gets bit shifted left.
n2The divisor.
Returns
n1 bit shifted left then divided by n1.

Definition at line 215 of file math.hpp.

Referenced by unit_drawer::draw_bar(), game_display::new_turn(), scale_surface(), and scale_surface_legacy().

◆ fixed_point_multiply()

constexpr unsigned fixed_point_multiply ( int32_t  n1,
int32_t  n2 
)
Parameters
n1The first number to multiply.
n2The second number to multiply.
Returns
The unsigned result of n1 * n2, then bitshifting the result to the right.

Definition at line 205 of file math.hpp.

Referenced by brighten_image(), unit_drawer::draw_bar(), image::o_modification::operator()(), and submerge_alpha().

◆ fixed_point_to_int()

constexpr int fixed_point_to_int ( int32_t  n)

If positive, just bit shift.

Else, make positive, bit shift, then make negative again.

Parameters
nThe number to bit shift right.
Returns
The result of the bit shift.

Definition at line 227 of file math.hpp.

Referenced by unit_drawer::draw_bar(), scale_surface(), and scale_surface_legacy().

◆ floating_to_fixed_point()

constexpr int32_t floating_to_fixed_point ( double  n)

◆ in_ranges()

template<typename Cmp >
bool in_ranges ( const Cmp  c,
const std::vector< std::pair< Cmp, Cmp >> &  ranges 
)

◆ is_even()

template<typename T >
constexpr bool is_even ( num)

◆ is_odd()

template<typename T >
constexpr bool is_odd ( num)

◆ modulo()

template<typename T >
constexpr T modulo ( num,
int  mod,
min = 0 
)
Returns
: the number n in [min, min+mod ) so that (n - num) is a multiple of mod.

Definition at line 62 of file math.hpp.

References n.

Referenced by tod_manager::calculate_time_index_at_turn(), play_controller::find_last_visible_team(), tod_manager::fix_time_index(), and play_controller::play_turn().

◆ round_damage()

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

Definition at line 80 of file math.hpp.

Referenced by attack_info(), battle_context_unit_stats::battle_context_unit_stats(), and battle_context_unit_stats::calc_blows().

◆ rounded_division()

int rounded_division ( int  a,
int  b 
)
inline