source: trunk/src/lol/math/functions.h @ 2354

Last change on this file since 2354 was 2354, checked in by sam, 8 years ago

base: move headers around and promote gpu/ to lol/gpu/.

  • Property svn:keywords set to Id
File size: 6.0 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
5// Copyright: (c) 2012-2013 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
6//   This program is free software; you can redistribute it and/or
7//   modify it under the terms of the Do What The Fuck You Want To
8//   Public License, Version 2, as published by Sam Hocevar. See
9//   http://www.wtfpl.net/ for more details.
10//
11
12//
13// Various maths functions
14// -----------------------
15//
16
17#if !defined __LOL_MATH_FUNCTIONS_H__
18#define __LOL_MATH_FUNCTIONS_H__
19
20#include <cmath>
21#include <cstdio>
22#include <algorithm>
23
24#include <stdint.h>
25
26namespace 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 */
34static inline double sqrt(double const &x) { return std::sqrt(x); }
35static inline float sqrt(float const &x) { return std::sqrt(x); }
36
37static inline double exp(double const &x) { return std::exp(x); }
38static inline float exp(float const &x) { return std::exp(x); }
39
40static inline double sin(double const &x) { return std::sin(x); }
41static inline double cos(double const &x) { return std::cos(x); }
42static inline double tan(double const &x) { return std::tan(x); }
43static inline float sin(float const &x) { return std::sin(x); }
44static inline float cos(float const &x) { return std::cos(x); }
45static inline float tan(float const &x) { return std::tan(x); }
46
47static inline double asin(double const &x) { return std::asin(x); }
48static inline double acos(double const &x) { return std::acos(x); }
49static inline double atan(double const &x) { return std::atan(x); }
50static inline float asin(float const &x) { return std::asin(x); }
51static inline float acos(float const &x) { return std::acos(x); }
52static inline float atan(float const &x) { return std::atan(x); }
53
54static inline double atan2(double const &y, double const &x)
55{
56    return std::atan2(y, x);
57}
58
59static inline float atan2(float const &y, float const &x)
60{
61    return std::atan2(y, x);
62}
63
64static inline double pow(double const &x, double const &y)
65{
66    return std::pow(x, y);
67}
68
69static inline float pow(float const &x, float const &y)
70{
71    return std::pow(x, y);
72}
73
74/* Our extensions */
75static inline void sincos(double const &x, double *s, double *c)
76{
77    *s = std::sin(x);
78    *c = std::cos(x);
79}
80
81static inline void sincos(float const &x, float *s, float *c)
82{
83    *s = std::sin(x);
84    *c = std::cos(x);
85}
86
87static inline float lerp(float const &a, float const &b, float const &x)
88{
89    return a + (b - a) * x;
90}
91
92static inline double lerp(double const &a, double const &b, double const &x)
93{
94    return a + (b - a) * x;
95}
96
97static 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 */
104double lol_sin(double);
105double lol_cos(double);
106double lol_tan(double);
107void lol_sincos(double, double*, double*);
108void lol_sincos(float, float*, float*);
109double lol_asin(double);
110double lol_acos(double);
111double lol_atan(double);
112double 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. */
116static inline int8_t abs(int8_t x) { return std::abs(x); }
117static inline uint8_t abs(uint8_t x) { return x; }
118static inline int16_t abs(int16_t x) { return std::abs(x); }
119static inline uint16_t abs(uint16_t x) { return x; }
120static inline int32_t abs(int32_t x) { return std::abs(x); }
121static 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. */
124static inline int64_t abs(int64_t x) { return x > 0 ? x : -x; }
125#else
126static inline int64_t abs(int64_t x) { return std::abs(x); }
127#endif
128static inline uint64_t abs(uint64_t x) { return x; }
129static inline float abs(float x) { return std::abs(x); }
130static inline double abs(double x) { return std::abs(x); }
131static inline ldouble abs(ldouble x) { return std::abs(x); }
132
133static inline uint8_t fmod(uint8_t x, uint8_t y) { return x % y; }
134static inline int8_t fmod(int8_t x, int8_t y) { return x % y; }
135static inline uint16_t fmod(uint16_t x, uint16_t y) { return x % y; }
136static inline int16_t fmod(int16_t x, int16_t y) { return x % y; }
137static inline uint32_t fmod(uint32_t x, uint32_t y) { return x % y; }
138static inline int32_t fmod(int32_t x, int32_t y) { return x % y; }
139static inline uint64_t fmod(uint64_t x, uint64_t y) { return x % y; }
140static inline int64_t fmod(int64_t x, int64_t y) { return x % y; }
141static inline float fmod(float x, float y) { return std::fmod(x, y); }
142static inline double fmod(double x, double y) { return std::fmod(x, y); }
143static inline ldouble fmod(ldouble x, ldouble y) { return std::fmod(x, y); }
144
145static inline uint8_t fract(uint8_t x) { (void)x; return 0; }
146static inline int8_t fract(int8_t x) { (void)x; return 0; }
147static inline uint16_t fract(uint16_t x) { (void)x; return 0; }
148static inline int16_t fract(int16_t x) { (void)x; return 0; }
149static inline uint32_t fract(uint32_t x) { (void)x; return 0; }
150static inline int32_t fract(int32_t x) { (void)x; return 0; }
151static inline uint64_t fract(uint64_t x) { (void)x; return 0; }
152static inline int64_t fract(int64_t x) { (void)x; return 0; }
153static inline float fract(float x) { return x - std::floor(x); }
154static inline double fract(double x) { return x - std::floor(x); }
155static 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); }
161LOL_MIN_MAX_CLAMP(uint8_t)
162LOL_MIN_MAX_CLAMP(int8_t)
163LOL_MIN_MAX_CLAMP(uint16_t)
164LOL_MIN_MAX_CLAMP(int16_t)
165LOL_MIN_MAX_CLAMP(uint32_t)
166LOL_MIN_MAX_CLAMP(int32_t)
167LOL_MIN_MAX_CLAMP(uint64_t)
168LOL_MIN_MAX_CLAMP(int64_t)
169LOL_MIN_MAX_CLAMP(float)
170LOL_MIN_MAX_CLAMP(double)
171LOL_MIN_MAX_CLAMP(ldouble)
172#undef LOL_CLAMP
173
174} /* namespace lol */
175
176#endif // __LOL_MATH_FUNCTIONS_H__
177
Note: See TracBrowser for help on using the repository browser.