Ignore:
Timestamp:
Nov 2, 2012, 11:45:02 AM (8 years ago)
Author:
sam
Message:

math: reimplement min(), max(), abs() and fmod() in the lol:: namespace to
avoid conflicts with the C++ stdlib.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lol/math/math.h

    r1888 r2056  
    1919#include <cmath>
    2020#include <cstdio>
     21#include <algorithm>
     22
    2123#include <stdint.h>
    2224
    2325namespace lol
    2426{
     27
     28/* This is OUR namespace. Don't let Windows headers fuck with it. */
     29#undef min
     30#undef max
     31
     32/* There are many reasons for wanting single-word type names, the most
     33 * important one being compilation speedups in our vector.h: we can hide
     34 * some global methods in namespaces that contain the name of the type,
     35 * but namespaces cannot have spaces in their names. */
     36typedef long double ldouble;
    2537
    2638/* Standard cmath functions */
     
    6577}
    6678
     79static inline float lerp(float const &a, float const &b, float const &x)
     80{
     81    return a + (b - a) * x;
     82}
     83
    6784static inline double lerp(double const &a, double const &b, double const &x)
    6885{
    6986    return a + (b - a) * x;
    7087}
    71 static inline float lerp(float const &a, float const &b, float const &x)
     88
     89static inline ldouble lerp(ldouble const &a, ldouble const &b, ldouble const &x)
    7290{
    7391    return a + (b - a) * x;
     
    86104double lol_atan2(double, double);
    87105
     106/* C++ doesn't define abs() and fmod() for all types; we add these for
     107 * convenience to avoid adding complexity to vector.h. */
     108static inline int8_t abs(int8_t x) { return std::abs(x); }
     109static inline uint8_t abs(uint8_t x) { return x; }
     110static inline int16_t abs(int16_t x) { return std::abs(x); }
     111static inline uint16_t abs(uint16_t x) { return x; }
     112static inline int32_t abs(int32_t x) { return std::abs(x); }
     113static inline uint32_t abs(uint32_t x) { return x; }
     114static inline int64_t abs(int64_t x) { return std::abs(x); }
     115static inline uint64_t abs(uint64_t x) { return x; }
     116static inline float abs(float x) { return std::abs(x); }
     117static inline double abs(double x) { return std::abs(x); }
     118static inline ldouble abs(ldouble x) { return std::abs(x); }
     119
     120static inline uint8_t fmod(uint8_t x, uint8_t y) { return x % y; }
     121static inline int8_t fmod(int8_t x, int8_t y) { return x % y; }
     122static inline uint16_t fmod(uint16_t x, uint16_t y) { return x % y; }
     123static inline int16_t fmod(int16_t x, int16_t y) { return x % y; }
     124static inline uint32_t fmod(uint32_t x, uint32_t y) { return x % y; }
     125static inline int32_t fmod(int32_t x, int32_t y) { return x % y; }
     126static inline uint64_t fmod(uint64_t x, uint64_t y) { return x % y; }
     127static inline int64_t fmod(int64_t x, int64_t y) { return x % y; }
     128static inline float fmod(float x, float y) { return std::fmod(x, y); }
     129static inline double fmod(double x, double y) { return std::fmod(x, y); }
     130static inline ldouble fmod(ldouble x, ldouble y) { return std::fmod(x, y); }
     131
     132static inline uint8_t min(uint8_t x, uint8_t y) { return std::min(x, y); }
     133static inline int8_t min(int8_t x, int8_t y) { return std::min(x, y); }
     134static inline uint16_t min(uint16_t x, uint16_t y) { return std::min(x, y); }
     135static inline int16_t min(int16_t x, int16_t y) { return std::min(x, y); }
     136static inline uint32_t min(uint32_t x, uint32_t y) { return std::min(x, y); }
     137static inline int32_t min(int32_t x, int32_t y) { return std::min(x, y); }
     138static inline uint64_t min(uint64_t x, uint64_t y) { return std::min(x, y); }
     139static inline int64_t min(int64_t x, int64_t y) { return std::min(x, y); }
     140static inline float min(float x, float y) { return std::min(x, y); }
     141static inline double min(double x, double y) { return std::min(x, y); }
     142static inline ldouble min(ldouble x, ldouble y) { return std::min(x, y); }
     143
     144static inline uint8_t max(uint8_t x, uint8_t y) { return std::max(x, y); }
     145static inline int8_t max(int8_t x, int8_t y) { return std::max(x, y); }
     146static inline uint16_t max(uint16_t x, uint16_t y) { return std::max(x, y); }
     147static inline int16_t max(int16_t x, int16_t y) { return std::max(x, y); }
     148static inline uint32_t max(uint32_t x, uint32_t y) { return std::max(x, y); }
     149static inline int32_t max(int32_t x, int32_t y) { return std::max(x, y); }
     150static inline uint64_t max(uint64_t x, uint64_t y) { return std::max(x, y); }
     151static inline int64_t max(int64_t x, int64_t y) { return std::max(x, y); }
     152static inline float max(float x, float y) { return std::max(x, y); }
     153static inline double max(double x, double y) { return std::max(x, y); }
     154static inline ldouble max(ldouble x, ldouble y) { return std::max(x, y); }
     155
    88156} /* namespace lol */
    89157
Note: See TracChangeset for help on using the changeset viewer.