Changeset 2056


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.

Location:
trunk
Files:
5 edited

Legend:

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

    r2054 r2056  
    121121static inline half min(half a, half b) { return a < b ? a : b; }
    122122static inline half max(half a, half b) { return a > b ? a : b; }
    123 static inline half fmod(half a, half b) { return (half)fmod((float)a, (float)b); }
     123static inline half fmod(half a, half b)
     124{
     125    using std::fmod;
     126    return (half)fmod((float)a, (float)b);
     127}
    124128static inline half abs(half a) { return half::makebits(a.bits & 0x7fffu); }
    125129
  • 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
  • trunk/src/lol/math/vector.h

    r2054 r2056  
    1919#include <stdint.h>
    2020#include <ostream>
    21 #include <algorithm>
    2221
    2322#include "lol/math/math.h"
     
    2726namespace lol
    2827{
    29 
    30 /* This is OUR namespace. Don't let Windows headers fuck with it. */
    31 #undef min
    32 #undef max
    3328
    3429/* Some compilers do not support const members in anonymous unions. So
     
    4035#endif
    4136
    42 /* Hack for compilation speedups: we can hide some of our global methods in
    43  * namespaces. We therefore want "long_double" to be a single-word name */
    44 typedef long double long_double;
    45 
    4637#define DECLARE_VECTOR_TYPEDEFS(tname, suffix) \
    4738    template <typename T> struct tname; \
     
    4940    typedef tname<float> suffix; \
    5041    typedef tname<double> d##suffix; \
    51     typedef tname<long_double> f128##suffix; \
     42    typedef tname<ldouble> f128##suffix; \
    5243    typedef tname<int8_t> i8##suffix; \
    5344    typedef tname<uint8_t> u8##suffix; \
     
    10741065    inline tname<type> op(tname<type> const &a, tname<type> const &b) \
    10751066    { \
    1076         using std::op; \
     1067        using lol::op; \
    10771068        tname<type> ret; \
    10781069        for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \
     
    10841075    inline tname<type> op(tname<type> const &a, type const &b) \
    10851076    { \
    1086         using std::op; \
     1077        using lol::op; \
    10871078        tname<type> ret; \
    10881079        for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \
     
    10941085    inline tname<type> op(type const &a, tname<type> const &b) \
    10951086    { \
    1096         using std::op; \
     1087        using lol::op; \
    10971088        tname<type> ret; \
    10981089        for (size_t n = 0; n < sizeof(b) / sizeof(type); n++) \
     
    12281219    inline tname<type> abs(tname<type> const &a) \
    12291220    { \
    1230         using std::abs; \
    12311221        tname<type> ret; \
    12321222        for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \
    1233             ret[n] = abs(a[n]); \
     1223            ret[n] = lol::abs(a[n]); \
    12341224        return ret; \
    12351225    }
     
    13611351DECLARE_ALL_VECTOR_OPS(float)
    13621352DECLARE_ALL_VECTOR_OPS(double)
    1363 DECLARE_ALL_VECTOR_OPS(long_double)
     1353DECLARE_ALL_VECTOR_OPS(ldouble)
    13641354DECLARE_ALL_VECTOR_OPS(int8_t)
    13651355DECLARE_ALL_VECTOR_OPS(uint8_t)
     
    14021392DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, float)
    14031393DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, double)
    1404 DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, long_double)
     1394DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, ldouble)
    14051395
    14061396DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, int16_t)
     
    14121402DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, float)
    14131403DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, double)
    1414 DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, long_double)
     1404DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, ldouble)
    14151405
    14161406DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, uint16_t)
     
    14211411DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, float)
    14221412DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, double)
    1423 DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, long_double)
     1413DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, ldouble)
    14241414
    14251415DECLARE_ALL_VECTOR_COERCE_OPS(uint16_t, int32_t)
     
    14291419DECLARE_ALL_VECTOR_COERCE_OPS(uint16_t, float)
    14301420DECLARE_ALL_VECTOR_COERCE_OPS(uint16_t, double)
    1431 DECLARE_ALL_VECTOR_COERCE_OPS(uint16_t, long_double)
     1421DECLARE_ALL_VECTOR_COERCE_OPS(uint16_t, ldouble)
    14321422
    14331423DECLARE_ALL_VECTOR_COERCE_OPS(int32_t, uint32_t)
     
    14361426DECLARE_ALL_VECTOR_COERCE_OPS(int32_t, float)
    14371427DECLARE_ALL_VECTOR_COERCE_OPS(int32_t, double)
    1438 DECLARE_ALL_VECTOR_COERCE_OPS(int32_t, long_double)
     1428DECLARE_ALL_VECTOR_COERCE_OPS(int32_t, ldouble)
    14391429
    14401430DECLARE_ALL_VECTOR_COERCE_OPS(uint32_t, int64_t)
     
    14421432DECLARE_ALL_VECTOR_COERCE_OPS(uint32_t, float)
    14431433DECLARE_ALL_VECTOR_COERCE_OPS(uint32_t, double)
    1444 DECLARE_ALL_VECTOR_COERCE_OPS(uint32_t, long_double)
     1434DECLARE_ALL_VECTOR_COERCE_OPS(uint32_t, ldouble)
    14451435
    14461436DECLARE_ALL_VECTOR_COERCE_OPS(int64_t, uint64_t)
    14471437DECLARE_ALL_VECTOR_COERCE_OPS(int64_t, float)
    14481438DECLARE_ALL_VECTOR_COERCE_OPS(int64_t, double)
    1449 DECLARE_ALL_VECTOR_COERCE_OPS(int64_t, long_double)
     1439DECLARE_ALL_VECTOR_COERCE_OPS(int64_t, ldouble)
    14501440
    14511441DECLARE_ALL_VECTOR_COERCE_OPS(uint64_t, float)
    14521442DECLARE_ALL_VECTOR_COERCE_OPS(uint64_t, double)
    1453 DECLARE_ALL_VECTOR_COERCE_OPS(uint64_t, long_double)
     1443DECLARE_ALL_VECTOR_COERCE_OPS(uint64_t, ldouble)
    14541444
    14551445DECLARE_ALL_VECTOR_COERCE_OPS(float, double)
    1456 DECLARE_ALL_VECTOR_COERCE_OPS(float, long_double)
    1457 
    1458 DECLARE_ALL_VECTOR_COERCE_OPS(double, long_double)
     1446DECLARE_ALL_VECTOR_COERCE_OPS(float, ldouble)
     1447
     1448DECLARE_ALL_VECTOR_COERCE_OPS(double, ldouble)
    14591449
    14601450/* FIXME: vectors of "half" are deactivated for now, because they
     
    14751465DECLARE_ALL_VECTOR_COERCE_OPS(half, float)
    14761466DECLARE_ALL_VECTOR_COERCE_OPS(half, double)
    1477 DECLARE_ALL_VECTOR_COERCE_OPS(half, long_double)
     1467DECLARE_ALL_VECTOR_COERCE_OPS(half, ldouble)
    14781468#endif
    14791469
     
    14941484DECLARE_ALL_VECTOR_COERCE_OPS(float, real)
    14951485DECLARE_ALL_VECTOR_COERCE_OPS(double, real)
    1496 DECLARE_ALL_VECTOR_COERCE_OPS(long_double, real)
     1486DECLARE_ALL_VECTOR_COERCE_OPS(ldouble, real)
    14971487#endif
    14981488
     
    15191509    ACTIVATE_COERCE_NAMESPACES_INNER(tlow, float) \
    15201510    ACTIVATE_COERCE_NAMESPACES_INNER(tlow, double) \
    1521     ACTIVATE_COERCE_NAMESPACES_INNER(tlow, long_double) \
     1511    ACTIVATE_COERCE_NAMESPACES_INNER(tlow, ldouble) \
    15221512    ACTIVATE_COERCE_NAMESPACES_INNER(tlow, real)
    15231513
     
    15331523ACTIVATE_COERCE_NAMESPACES(float)
    15341524ACTIVATE_COERCE_NAMESPACES(double)
    1535 ACTIVATE_COERCE_NAMESPACES(long_double)
     1525ACTIVATE_COERCE_NAMESPACES(ldouble)
    15361526ACTIVATE_COERCE_NAMESPACES(real)
    15371527
  • trunk/test/math/poly.cpp

    r1893 r2056  
    124124        union { float f; uint32_t x; } s1 = { sinf(adjustf(u.f, 0)) };
    125125        union { float f; uint32_t x; } s2 = { floatsin(adjustf(u.f, 0)) };
    126         int e = abs((int)(s1.x - s2.x));
     126        int e = lol::abs((int)(s1.x - s2.x));
    127127        switch (e)
    128128        {
     
    133133printf("sinf: ");
    134134inspect(sinf(u.f));
    135             if (fabs((double)s1.f - (double)s2.f) > 1e-10 * fabs(s1.f))
     135            if (lol::abs((double)s1.f - (double)s2.f) > 1e-10 * lol::abs(s1.f))
    136136                printf("%15.13g %08x: %15.13g %15.13g %08x %08x\n", u.f, u.x, s1.f, s2.f, s1.x, s2.x);
    137137        case 0:
  • trunk/tutorial/11_fractal.cpp

    r1992 r2056  
    216216        {
    217217            m_zoom_speed *= std::pow(2.0, -seconds * 5.0);
    218             if (abs(m_zoom_speed) < 1e-5 || m_drag)
     218            if (lol::abs(m_zoom_speed) < 1e-5 || m_drag)
    219219                m_zoom_speed = 0.0;
    220220        }
Note: See TracChangeset for help on using the changeset viewer.