Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
lib.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining
4 * a copy of this software and associated documentation files (the
5 * "Software"), to deal in the Software without restriction, including
6 * without limitation the rights to use, copy, modify, merge, publish,
7 * distribute, sublicense, and/or sell copies of the Software, and to
8 * permit persons to whom the Software is furnished to do so, subject to
9 * the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23/*
24 * various utility functions.
25 */
26
27/**
28
29low-level aka "lib"
30-------------------
31
32this codebase was grown from modules shared between several projects,
33i.e. my personal library; hence the name "lib". it has been expanded to
34fit the needs of 0ad - in particular, resource loading.
35
36owing to the dual-use situation, the 0ad coding conventions are not met;
37also, major changes are ill-advised because they may break other projects.
38
39
40design goals
41------------
42
43- fast and low-overhead, including startup time
44- portable: must run on Win32, Mac OS X and Linux
45- reusable across projects, i.e. no dependency on a
46 central 'manager' that ties modules together.
47
48
49scope
50-----
51
52- POSIX definitions
53- resource management
54- debugging tools (including memory tracker)
55- low-level helper functions, e.g. ADTs, endian conversion and timing
56- platform-dependent system/feature detection
57
58**/
59
60#ifndef INCLUDED_LIB
61#define INCLUDED_LIB
62
63#include "lib/debug.h"
64
65#include <cmath> // fabsf
66#include <limits> // numeric_limits
67#include <stdexcept> // out_of_range
68
69template<typename T>
70T DivideRoundUp(T dividend, T divisor)
71{
72 ASSERT(divisor != 0);
73 return (dividend + divisor-1) / divisor;
74}
75
76/**
77 * are the given floats nearly "equal"?
78 *
79 * @return whether the numbers are within "epsilon" of each other.
80 *
81 * notes:
82 * - the epsilon magic number varies with the magnitude of the inputs.
83 * we use a sane default, but don't use this routine for very
84 * large/small comparands.
85 * - floating-point numbers don't magically lose precision. addition,
86 * subtraction and multiplication results are precise up to the mantissa's
87 * least-significant bit. only division, sqrt, sin/cos and other
88 * transcendental operations introduce error.
89 **/
90inline bool feq(double d1, double d2, double epsilon = 0.00001)
91{
92 return fabs(d1 - d2) < epsilon;
93}
94
95inline bool feqf(float f1, float f2, float epsilon = 0.001f)
96{
97 return fabsf(f1 - f2) < epsilon;
98}
99
100inline bool IsSimilarMagnitude(double d1, double d2, const double relativeErrorTolerance = 0.05)
101{
102 const double relativeError = fabs(d1/d2 - 1.0);
103 if(relativeError > relativeErrorTolerance)
104 return false;
105 return true;
106}
107
108
109//-----------------------------------------------------------------------------
110// type conversion
111
112// note: these avoid a common mistake in using >> (ANSI requires
113// shift count be less than the bit width of the type).
114
115extern std::uint32_t u64_hi(std::uint64_t x); /// return upper 32-bits
116extern std::uint32_t u64_lo(std::uint64_t x); /// return lower 32-bits
117extern std::uint16_t u32_hi(std::uint32_t x); /// return upper 16-bits
118extern std::uint16_t u32_lo(std::uint32_t x); /// return lower 16-bits
119
120extern std::uint64_t u64_from_u32(std::uint32_t hi, std::uint32_t lo); /// assemble std::uint64_t from std::uint32_t
121extern std::uint32_t u32_from_u16(std::uint16_t hi, std::uint16_t lo); /// assemble std::uint32_t from std::uint16_t
122
123// safe downcasters: cast from any integral type to std::uint32_t or
124// std::uint16_t; issues warning if larger than would fit in the target type.
125//
126// these are generally useful but included here (instead of e.g. lib.h) for
127// several reasons:
128// - including implementation in lib.h doesn't work because the definition
129// of ENSURE in turn requires lib.h's STMT.
130// - separate compilation of templates via export isn't supported by
131// most compilers.
132
133template<typename T> std::uint8_t u8_from_larger(T x)
134{
135 const std::uint8_t max = std::numeric_limits<std::uint8_t>::max();
136 if(static_cast<std::uint64_t>(x) > static_cast<std::uint64_t>(max))
137 throw std::out_of_range("u8_from_larger");
138 return static_cast<std::uint8_t>(x & max);
139}
140
141template<typename T> std::uint16_t u16_from_larger(T x)
142{
143 const std::uint16_t max = std::numeric_limits<std::uint16_t>::max();
144 if(static_cast<std::uint64_t>(x) > static_cast<std::uint64_t>(max))
145 throw std::out_of_range("u16_from_larger");
146 return static_cast<std::uint16_t>(x & max);
147}
148
149template<typename T> std::uint32_t u32_from_larger(T x)
150{
151 const std::uint32_t max = std::numeric_limits<std::uint32_t>::max();
152 if(static_cast<std::uint64_t>(x) > static_cast<std::uint64_t>(max))
153 throw std::out_of_range("u32_from_larger");
154 return static_cast<std::uint32_t>(x & max);
155}
156
157/// convert double to std::uint8_t; verifies number is in range.
158extern std::uint8_t u8_from_double(double in);
159/// convert double to std::uint16_t; verifies number is in range.
160extern std::uint16_t u16_from_double(double in);
161
162#endif // #ifndef INCLUDED_LIB
#define ASSERT(expr)
same as ENSURE in debug mode, does nothing in release mode.
Definition: debug.h:250
std::uint32_t u32_from_larger(T x)
Definition: lib.h:149
std::uint32_t u32_from_u16(std::uint16_t hi, std::uint16_t lo)
assemble std::uint64_t from std::uint32_t
Definition: lib.cpp:68
std::uint16_t u32_hi(std::uint32_t x)
return lower 32-bits
Definition: lib.cpp:49
std::uint8_t u8_from_double(double in)
convert double to std::uint8_t; verifies number is in range.
Definition: lib.cpp:78
std::uint32_t u64_lo(std::uint64_t x)
return upper 32-bits
Definition: lib.cpp:44
bool feq(double d1, double d2, double epsilon=0.00001)
are the given floats nearly "equal"?
Definition: lib.h:90
std::uint16_t u16_from_larger(T x)
Definition: lib.h:141
bool feqf(float f1, float f2, float epsilon=0.001f)
Definition: lib.h:95
std::uint16_t u16_from_double(double in)
convert double to std::uint16_t; verifies number is in range.
Definition: lib.cpp:92
std::uint64_t u64_from_u32(std::uint32_t hi, std::uint32_t lo)
return lower 16-bits
Definition: lib.cpp:60
T DivideRoundUp(T dividend, T divisor)
Definition: lib.h:70
std::uint16_t u32_lo(std::uint32_t x)
return upper 16-bits
Definition: lib.cpp:54
std::uint8_t u8_from_larger(T x)
assemble std::uint32_t from std::uint16_t
Definition: lib.h:133
bool IsSimilarMagnitude(double d1, double d2, const double relativeErrorTolerance=0.05)
Definition: lib.h:100
std::uint32_t u64_hi(std::uint64_t x)
Definition: lib.cpp:39
#define T(string_literal)
Definition: secure_crt.cpp:81