Pyrogenesis  trunk
lib.h File Reference
#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.


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

low-level aka "lib"

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

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.


  • 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 

are the given floats nearly "equal"?

whether the numbers are within "epsilon" of each other.


  • 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 

◆ IsSimilarMagnitude()

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

◆ u16_from_double()

u16 u16_from_double ( double  in)

convert double to u16; verifies number is in range.

◆ u16_from_larger()

template<typename T >
u16 u16_from_larger ( T  x)

◆ u32_from_larger()

template<typename T >
u32 u32_from_larger ( T  x)

◆ u32_from_u16()

u32 u32_from_u16 ( u16  hi,
u16  lo 

assemble u64 from u32

◆ u32_hi()

u16 u32_hi ( u32  x)

return lower 32-bits

◆ u32_lo()

u16 u32_lo ( u32  x)

return upper 16-bits

◆ u64_from_u32()

u64 u64_from_u32 ( u32  hi,
u32  lo 

return lower 16-bits

◆ u64_hi()

u32 u64_hi ( u64  x)

◆ u64_lo()

u32 u64_lo ( u64  x)

return upper 32-bits

◆ u8_from_double()

u8 u8_from_double ( double  in)

convert double to u8; verifies number is in range.

◆ u8_from_larger()

template<typename T >
u8 u8_from_larger ( T  x)

assemble u32 from u16