source: trunk/src/lol/math/math.h @ 2192

Last change on this file since 2192 was 2192, checked in by sam, 7 years ago

math: implement fract() for vectors and scalar types.

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