Changeset 2354 for trunk/src/lol/math/math.h
 Timestamp:
 Feb 5, 2013, 12:48:19 AM (8 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/src/lol/math/math.h
r2308 r2354 3 3 // 4 4 // Copyright: (c) 20102013 Sam Hocevar <sam@hocevar.net> 5 // Copyright: (c) 20122013 Benjamin "Touky" Huet <huet.benjamin@gmail.com>6 5 // This program is free software; you can redistribute it and/or 7 6 // modify it under the terms of the Do What The Fuck You Want To … … 10 9 // 11 10 12 //13 // Various maths functions14 // 15 //16 17 11 #if !defined __LOL_MATH_MATH_H__ 18 12 #define __LOL_MATH_MATH_H__ 19 13 20 #include <cmath> 21 #include <cstdio> 22 #include <algorithm> 23 24 #include <stdint.h> 25 26 namespace lol 27 { 28 29 /* This is OUR namespace. Don't let Windows headers fuck with it. */ 30 #undef min 31 #undef max 32 33 /* Standard cmath functions */ 34 static inline double sqrt(double const &x) { return std::sqrt(x); } 35 static inline float sqrt(float const &x) { return std::sqrt(x); } 36 37 static inline double exp(double const &x) { return std::exp(x); } 38 static inline float exp(float const &x) { return std::exp(x); } 39 40 static inline double sin(double const &x) { return std::sin(x); } 41 static inline double cos(double const &x) { return std::cos(x); } 42 static inline double tan(double const &x) { return std::tan(x); } 43 static inline float sin(float const &x) { return std::sin(x); } 44 static inline float cos(float const &x) { return std::cos(x); } 45 static inline float tan(float const &x) { return std::tan(x); } 46 47 static inline double asin(double const &x) { return std::asin(x); } 48 static inline double acos(double const &x) { return std::acos(x); } 49 static inline double atan(double const &x) { return std::atan(x); } 50 static inline float asin(float const &x) { return std::asin(x); } 51 static inline float acos(float const &x) { return std::acos(x); } 52 static inline float atan(float const &x) { return std::atan(x); } 53 54 static inline double atan2(double const &y, double const &x) 55 { 56 return std::atan2(y, x); 57 } 58 59 static inline float atan2(float const &y, float const &x) 60 { 61 return std::atan2(y, x); 62 } 63 64 static inline double pow(double const &x, double const &y) 65 { 66 return std::pow(x, y); 67 } 68 69 static inline float pow(float const &x, float const &y) 70 { 71 return std::pow(x, y); 72 } 73 74 /* Our extensions */ 75 static inline void sincos(double const &x, double *s, double *c) 76 { 77 *s = std::sin(x); 78 *c = std::cos(x); 79 } 80 81 static inline void sincos(float const &x, float *s, float *c) 82 { 83 *s = std::sin(x); 84 *c = std::cos(x); 85 } 86 87 static inline float lerp(float const &a, float const &b, float const &x) 88 { 89 return a + (b  a) * x; 90 } 91 92 static inline double lerp(double const &a, double const &b, double const &x) 93 { 94 return a + (b  a) * x; 95 } 96 97 static inline ldouble lerp(ldouble const &a, ldouble const &b, ldouble const &x) 98 { 99 return a + (b  a) * x; 100 } 101 102 103 /* These accelerated functions will be merged into the above, one day */ 104 double lol_sin(double); 105 double lol_cos(double); 106 double lol_tan(double); 107 void lol_sincos(double, double*, double*); 108 void lol_sincos(float, float*, float*); 109 double lol_asin(double); 110 double lol_acos(double); 111 double lol_atan(double); 112 double lol_atan2(double, double); 113 114 /* C++ doesn't define abs() and fmod() for all types; we add these for 115 * convenience to avoid adding complexity to vector.h. */ 116 static inline int8_t abs(int8_t x) { return std::abs(x); } 117 static inline uint8_t abs(uint8_t x) { return x; } 118 static inline int16_t abs(int16_t x) { return std::abs(x); } 119 static inline uint16_t abs(uint16_t x) { return x; } 120 static inline int32_t abs(int32_t x) { return std::abs(x); } 121 static inline uint32_t abs(uint32_t x) { return x; } 122 #if defined __native_client__ 123 /* The pepper 19 toolchain doesn't provide abs() for long long int. */ 124 static inline int64_t abs(int64_t x) { return x > 0 ? x : x; } 125 #else 126 static inline int64_t abs(int64_t x) { return std::abs(x); } 127 #endif 128 static inline uint64_t abs(uint64_t x) { return x; } 129 static inline float abs(float x) { return std::abs(x); } 130 static inline double abs(double x) { return std::abs(x); } 131 static inline ldouble abs(ldouble x) { return std::abs(x); } 132 133 static inline uint8_t fmod(uint8_t x, uint8_t y) { return x % y; } 134 static inline int8_t fmod(int8_t x, int8_t y) { return x % y; } 135 static inline uint16_t fmod(uint16_t x, uint16_t y) { return x % y; } 136 static inline int16_t fmod(int16_t x, int16_t y) { return x % y; } 137 static inline uint32_t fmod(uint32_t x, uint32_t y) { return x % y; } 138 static inline int32_t fmod(int32_t x, int32_t y) { return x % y; } 139 static inline uint64_t fmod(uint64_t x, uint64_t y) { return x % y; } 140 static inline int64_t fmod(int64_t x, int64_t y) { return x % y; } 141 static inline float fmod(float x, float y) { return std::fmod(x, y); } 142 static inline double fmod(double x, double y) { return std::fmod(x, y); } 143 static inline ldouble fmod(ldouble x, ldouble y) { return std::fmod(x, y); } 144 145 static inline uint8_t fract(uint8_t x) { (void)x; return 0; } 146 static inline int8_t fract(int8_t x) { (void)x; return 0; } 147 static inline uint16_t fract(uint16_t x) { (void)x; return 0; } 148 static inline int16_t fract(int16_t x) { (void)x; return 0; } 149 static inline uint32_t fract(uint32_t x) { (void)x; return 0; } 150 static inline int32_t fract(int32_t x) { (void)x; return 0; } 151 static inline uint64_t fract(uint64_t x) { (void)x; return 0; } 152 static inline int64_t fract(int64_t x) { (void)x; return 0; } 153 static inline float fract(float x) { return x  std::floor(x); } 154 static inline double fract(double x) { return x  std::floor(x); } 155 static inline ldouble fract(ldouble x) { return x  std::floor(x); } 156 157 #define LOL_MIN_MAX_CLAMP(T) \ 158 static inline T min(T x, T y) { return std::min(x, y); } \ 159 static inline T max(T x, T y) { return std::max(x, y); } \ 160 static inline T clamp(T x, T y, T z) { return min(max(x, y), z); } 161 LOL_MIN_MAX_CLAMP(uint8_t) 162 LOL_MIN_MAX_CLAMP(int8_t) 163 LOL_MIN_MAX_CLAMP(uint16_t) 164 LOL_MIN_MAX_CLAMP(int16_t) 165 LOL_MIN_MAX_CLAMP(uint32_t) 166 LOL_MIN_MAX_CLAMP(int32_t) 167 LOL_MIN_MAX_CLAMP(uint64_t) 168 LOL_MIN_MAX_CLAMP(int64_t) 169 LOL_MIN_MAX_CLAMP(float) 170 LOL_MIN_MAX_CLAMP(double) 171 LOL_MIN_MAX_CLAMP(ldouble) 172 #undef LOL_CLAMP 173 174 } /* namespace lol */ 14 #include <lol/math/functions.h> 15 #include <lol/math/half.h> 16 #include <lol/math/real.h> 17 #include <lol/math/vector.h> 18 #include <lol/math/geometry.h> 175 19 176 20 #endif // __LOL_MATH_MATH_H__
Note: See TracChangeset
for help on using the changeset viewer.