Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
lib.h File Reference
#include "lib/debug.h"
#include <cmath>
#include <limits>
#include <stdexcept>
Include dependency graph for lib.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

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)
 
std::uint32_t u64_hi (std::uint64_t x)
 
std::uint32_t u64_lo (std::uint64_t x)
 return upper 32-bits More...
 
std::uint16_t u32_hi (std::uint32_t x)
 return lower 32-bits More...
 
std::uint16_t u32_lo (std::uint32_t x)
 return upper 16-bits More...
 
std::uint64_t u64_from_u32 (std::uint32_t hi, std::uint32_t lo)
 return lower 16-bits More...
 
std::uint32_t u32_from_u16 (std::uint16_t hi, std::uint16_t lo)
 assemble std::uint64_t from std::uint32_t More...
 
template<typename T >
std::uint8_t u8_from_larger (T x)
 assemble std::uint32_t from std::uint16_t More...
 
template<typename T >
std::uint16_t u16_from_larger (T x)
 
template<typename T >
std::uint32_t u32_from_larger (T x)
 
std::uint8_t u8_from_double (double in)
 convert double to std::uint8_t; verifies number is in range. More...
 
std::uint16_t u16_from_double (double in)
 convert double to std::uint16_t; verifies number is in range. More...
 

Function Documentation

◆ DivideRoundUp()

template<typename T >
T DivideRoundUp ( T  dividend,
T  divisor 
)

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

◆ feq()

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.

◆ feqf()

bool feqf ( float  f1,
float  f2,
float  epsilon = 0.001f 
)
inline

◆ IsSimilarMagnitude()

bool IsSimilarMagnitude ( double  d1,
double  d2,
const double  relativeErrorTolerance = 0.05 
)
inline

◆ u16_from_double()

std::uint16_t u16_from_double ( double  in)

convert double to std::uint16_t; verifies number is in range.

◆ u16_from_larger()

template<typename T >
std::uint16_t u16_from_larger ( T  x)

◆ u32_from_larger()

template<typename T >
std::uint32_t u32_from_larger ( T  x)

◆ u32_from_u16()

std::uint32_t u32_from_u16 ( std::uint16_t  hi,
std::uint16_t  lo 
)

assemble std::uint64_t from std::uint32_t

◆ u32_hi()

std::uint16_t u32_hi ( std::uint32_t  x)

return lower 32-bits

◆ u32_lo()

std::uint16_t u32_lo ( std::uint32_t  x)

return upper 16-bits

◆ u64_from_u32()

std::uint64_t u64_from_u32 ( std::uint32_t  hi,
std::uint32_t  lo 
)

return lower 16-bits

◆ u64_hi()

std::uint32_t u64_hi ( std::uint64_t  x)

◆ u64_lo()

std::uint32_t u64_lo ( std::uint64_t  x)

return upper 32-bits

◆ u8_from_double()

std::uint8_t u8_from_double ( double  in)

convert double to std::uint8_t; verifies number is in range.

◆ u8_from_larger()

template<typename T >
std::uint8_t u8_from_larger ( T  x)

assemble std::uint32_t from std::uint16_t