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

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

build: fix the WTFPL site URL in all code comments.

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