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

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

math: define lol::pow to remove ambiguous calls.

  • Property svn:keywords set to Id
File size: 6.1 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 sin(double const &x) { return std::sin(x); }
37static inline double cos(double const &x) { return std::cos(x); }
38static inline double tan(double const &x) { return std::tan(x); }
39static inline float sin(float const &x) { return std::sin(x); }
40static inline float cos(float const &x) { return std::cos(x); }
41static inline float tan(float const &x) { return std::tan(x); }
42
43static inline double asin(double const &x) { return std::asin(x); }
44static inline double acos(double const &x) { return std::acos(x); }
45static inline double atan(double const &x) { return std::atan(x); }
46static inline float asin(float const &x) { return std::asin(x); }
47static inline float acos(float const &x) { return std::acos(x); }
48static inline float atan(float const &x) { return std::atan(x); }
49
50static inline double atan2(double const &y, double const &x)
51{
52    return std::atan2(y, x);
53}
54
55static inline float atan2(float const &y, float const &x)
56{
57    return std::atan2(y, x);
58}
59
60static inline double pow(double const &x, double const &y)
61{
62    return std::pow(x, y);
63}
64
65static inline float pow(float const &x, float const &y)
66{
67    return std::pow(x, y);
68}
69
70/* Our extensions */
71static inline void sincos(double const &x, double *s, double *c)
72{
73    *s = std::sin(x);
74    *c = std::cos(x);
75}
76
77static inline void sincos(float const &x, float *s, float *c)
78{
79    *s = std::sin(x);
80    *c = std::cos(x);
81}
82
83static inline float lerp(float const &a, float const &b, float const &x)
84{
85    return a + (b - a) * x;
86}
87
88static inline double lerp(double const &a, double const &b, double const &x)
89{
90    return a + (b - a) * x;
91}
92
93static inline ldouble lerp(ldouble const &a, ldouble const &b, ldouble const &x)
94{
95    return a + (b - a) * x;
96}
97
98
99/* These accelerated functions will be merged into the above, one day */
100double lol_sin(double);
101double lol_cos(double);
102double lol_tan(double);
103void lol_sincos(double, double*, double*);
104void lol_sincos(float, float*, float*);
105double lol_asin(double);
106double lol_acos(double);
107double lol_atan(double);
108double lol_atan2(double, double);
109
110/* C++ doesn't define abs() and fmod() for all types; we add these for
111 * convenience to avoid adding complexity to vector.h. */
112static inline int8_t abs(int8_t x) { return std::abs(x); }
113static inline uint8_t abs(uint8_t x) { return x; }
114static inline int16_t abs(int16_t x) { return std::abs(x); }
115static inline uint16_t abs(uint16_t x) { return x; }
116static inline int32_t abs(int32_t x) { return std::abs(x); }
117static inline uint32_t abs(uint32_t x) { return x; }
118#if defined __native_client__
119/* The pepper 19 toolchain doesn't provide abs() for long long int. */
120static inline int64_t abs(int64_t x) { return x > 0 ? x : -x; }
121#else
122static inline int64_t abs(int64_t x) { return std::abs(x); }
123#endif
124static inline uint64_t abs(uint64_t x) { return x; }
125static inline float abs(float x) { return std::abs(x); }
126static inline double abs(double x) { return std::abs(x); }
127static inline ldouble abs(ldouble x) { return std::abs(x); }
128
129static inline uint8_t fmod(uint8_t x, uint8_t y) { return x % y; }
130static inline int8_t fmod(int8_t x, int8_t y) { return x % y; }
131static inline uint16_t fmod(uint16_t x, uint16_t y) { return x % y; }
132static inline int16_t fmod(int16_t x, int16_t y) { return x % y; }
133static inline uint32_t fmod(uint32_t x, uint32_t y) { return x % y; }
134static inline int32_t fmod(int32_t x, int32_t y) { return x % y; }
135static inline uint64_t fmod(uint64_t x, uint64_t y) { return x % y; }
136static inline int64_t fmod(int64_t x, int64_t y) { return x % y; }
137static inline float fmod(float x, float y) { return std::fmod(x, y); }
138static inline double fmod(double x, double y) { return std::fmod(x, y); }
139static inline ldouble fmod(ldouble x, ldouble y) { return std::fmod(x, y); }
140
141static inline uint8_t min(uint8_t x, uint8_t y) { return std::min(x, y); }
142static inline int8_t min(int8_t x, int8_t y) { return std::min(x, y); }
143static inline uint16_t min(uint16_t x, uint16_t y) { return std::min(x, y); }
144static inline int16_t min(int16_t x, int16_t y) { return std::min(x, y); }
145static inline uint32_t min(uint32_t x, uint32_t y) { return std::min(x, y); }
146static inline int32_t min(int32_t x, int32_t y) { return std::min(x, y); }
147static inline uint64_t min(uint64_t x, uint64_t y) { return std::min(x, y); }
148static inline int64_t min(int64_t x, int64_t y) { return std::min(x, y); }
149static inline float min(float x, float y) { return std::min(x, y); }
150static inline double min(double x, double y) { return std::min(x, y); }
151static inline ldouble min(ldouble x, ldouble y) { return std::min(x, y); }
152
153static inline uint8_t max(uint8_t x, uint8_t y) { return std::max(x, y); }
154static inline int8_t max(int8_t x, int8_t y) { return std::max(x, y); }
155static inline uint16_t max(uint16_t x, uint16_t y) { return std::max(x, y); }
156static inline int16_t max(int16_t x, int16_t y) { return std::max(x, y); }
157static inline uint32_t max(uint32_t x, uint32_t y) { return std::max(x, y); }
158static inline int32_t max(int32_t x, int32_t y) { return std::max(x, y); }
159static inline uint64_t max(uint64_t x, uint64_t y) { return std::max(x, y); }
160static inline int64_t max(int64_t x, int64_t y) { return std::max(x, y); }
161static inline float max(float x, float y) { return std::max(x, y); }
162static inline double max(double x, double y) { return std::max(x, y); }
163static inline ldouble max(ldouble x, ldouble y) { return std::max(x, y); }
164
165} /* namespace lol */
166
167#endif // __LOL_MATH_MATH_H__
168
Note: See TracBrowser for help on using the repository browser.