#include <cmath>
#include <limits>
#include <stdexcept>
Go to the source code of this file.
|
| template<typename T > |
| T | Clamp (T val, T min, T max) |
| | low-level aka "lib"
More...
|
| |
| template<typename T > |
| T | DivideRoundUp (T dividend, T divisor) |
| |
| bool | feq (double d1, double d2, double epsilon=0.00001) |
| | are the given floats nearly "equal"? More...
|
| |
| bool | feqf (float f1, float f2, float epsilon=0.001f) |
| |
| bool | IsSimilarMagnitude (double d1, double d2, const double relativeErrorTolerance=0.05) |
| |
| u32 | u64_hi (u64 x) |
| |
| u32 | u64_lo (u64 x) |
| | return upper 32-bits More...
|
| |
| u16 | u32_hi (u32 x) |
| | return lower 32-bits More...
|
| |
| u16 | u32_lo (u32 x) |
| | return upper 16-bits More...
|
| |
| u64 | u64_from_u32 (u32 hi, u32 lo) |
| | return lower 16-bits More...
|
| |
| u32 | u32_from_u16 (u16 hi, u16 lo) |
| | assemble u64 from u32 More...
|
| |
| template<typename T > |
| u8 | u8_from_larger (T x) |
| | assemble u32 from u16 More...
|
| |
| template<typename T > |
| u16 | u16_from_larger (T x) |
| |
| template<typename T > |
| u32 | u32_from_larger (T x) |
| |
| u8 | u8_from_double (double in) |
| | convert double to u8; verifies number is in range. More...
|
| |
| u16 | u16_from_double (double in) |
| | convert double to u16; verifies number is in range. More...
|
| |
template<typename T >
| T Clamp |
( |
T |
val, |
|
|
T |
min, |
|
|
T |
max |
|
) |
| |
low-level aka "lib"
this codebase was grown from modules shared between several projects, i.e. my personal library; hence the name "lib". it has been expanded to fit the needs of 0ad - in particular, resource loading.
owing to the dual-use situation, the 0ad coding conventions are not met; also, major changes are ill-advised because they may break other projects.
design goals
- fast and low-overhead, including startup time
- portable: must run on Win32, Mac OS X and Linux
- reusable across projects, i.e. no dependency on a central 'manager' that ties modules together.
scope
- POSIX definitions
- resource management
- debugging tools (including memory tracker)
- low-level helper functions, e.g. ADTs, endian conversion and timing
- platform-dependent system/feature detection
Definition at line 68 of file lib.h.
template<typename T >
| T DivideRoundUp |
( |
T |
dividend, |
|
|
T |
divisor |
|
) |
| |
Definition at line 75 of file lib.h.
| bool feq |
( |
double |
d1, |
|
|
double |
d2, |
|
|
double |
epsilon = 0.00001 |
|
) |
| |
|
inline |
are the given floats nearly "equal"?
- Returns
- whether the numbers are within "epsilon" of each other.
notes:
- the epsilon magic number varies with the magnitude of the inputs. we use a sane default, but don't use this routine for very large/small comparands.
- floating-point numbers don't magically lose precision. addition, subtraction and multiplication results are precise up to the mantissa's least-significant bit. only division, sqrt, sin/cos and other transcendental operations introduce error.
Definition at line 95 of file lib.h.
| bool feqf |
( |
float |
f1, |
|
|
float |
f2, |
|
|
float |
epsilon = 0.001f |
|
) |
| |
|
inline |
| bool IsSimilarMagnitude |
( |
double |
d1, |
|
|
double |
d2, |
|
|
const double |
relativeErrorTolerance = 0.05 |
|
) |
| |
|
inline |
| u16 u16_from_double |
( |
double |
in | ) |
|
convert double to u16; verifies number is in range.
Definition at line 97 of file lib.cpp.
template<typename T >
| u16 u16_from_larger |
( |
T |
x | ) |
|
template<typename T >
| u32 u32_from_larger |
( |
T |
x | ) |
|
assemble u64 from u32
Definition at line 73 of file lib.cpp.
return lower 32-bits
Definition at line 54 of file lib.cpp.
return upper 16-bits
Definition at line 59 of file lib.cpp.
return lower 16-bits
Definition at line 65 of file lib.cpp.
return upper 32-bits
Definition at line 49 of file lib.cpp.
| u8 u8_from_double |
( |
double |
in | ) |
|
convert double to u8; verifies number is in range.
Definition at line 83 of file lib.cpp.
assemble u32 from u16
Definition at line 138 of file lib.h.