Skip to content

Commit 91185d1

Browse files
committed
added more native functions to math.h; untested
1 parent 56eb2d1 commit 91185d1

File tree

3 files changed

+142
-13
lines changed

3 files changed

+142
-13
lines changed

src/math.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,19 @@
22
#include "native_functions.h"
33
#include "gctypes.h"
44

5-
float (*recipSqrt)(float x) = RECIP_SQRT_FPTR;
6-
float (*fabs)(float x) = ABS_VAL_FPTR;
5+
/*************************** built in functions **************************/
6+
float (*recipSqrt)(float x) = RECIP_SQRT_FPTR;
7+
float (*fabs)(float x) = FABS_FPTR;
8+
float (*exp)(float x) = EXP_FPTR;
9+
float (*log)(float x) = LOG_FPTR;
10+
float (*native_sin)(float x) = SIN_FPTR;
11+
float (*native_cos)(float x) = COS_FPTR;
12+
float (*native_tan)(float x) = TAN_FPTR;
13+
float (*native_acos)(float x) = ACOS_FPTR;
14+
float (*native_asin)(float x) = ASIN_FPTR;
15+
float (*native_atan)(float x) = ATAN_FPTR;
16+
float (*native_atan2)(float y, float x) = ATAN2_FPTR;
17+
/*************************************************************************/
718

819
// see http://stackoverflow.com/a/6437076
920
float floor(float x)
@@ -74,19 +85,16 @@ float fpow(float base, u8 exp)
7485
return result;
7586
}
7687

77-
static float (*native_sin)(float) = SINE_FPTR;
7888
float sin(float x)
7989
{
8090
return native_sin(DEG_TO_RAD(x));
8191
}
8292

83-
static float (*native_cosine)(float) = COSINE_FPTR;
8493
float cos(float x)
8594
{
8695
return native_cosine(DEG_TO_RAD(x));
8796
}
8897

89-
static float (*native_tangent)(float) = TANGENT_FPTR;
9098
float tan(float x)
9199
{
92100
return native_tangent(DEG_TO_RAD(x));

src/math.h

Lines changed: 106 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,22 @@
1616
#define M_2PI 6.28318530718f
1717

1818
/** approximate value of pi / 2 */
19-
#define M_HALF_PI 1.57079632679f
19+
#define M_PI_OVER_2 1.57079632679f
20+
#define M_HALF_PI M_PI_OVER_2 //TODO: REMOVE
2021

21-
/** approximate value of pi / 180 */
22+
/** approximate value of pi / 3 */
23+
#define M_PI_OVER_3 1.047197551f
24+
25+
/** approximate value of pi / 4 */
26+
#define M_PI_OVER_4 0.785398163f
27+
28+
/** approximate value of pi / 6 */
29+
#define M_PI_OVER_6 0.523598776f
30+
31+
/** degree to radian conversion */
2232
#define DEG_TO_RAD(x) ((x) * 0.01745329251f)
2333

24-
/** approximate value of 180 / pi */
34+
/** radian to degree conversion */
2535
#define RAD_TO_DEG(x) ((x) * 57.2957795131f)
2636

2737
/** sign of value **/
@@ -40,13 +50,106 @@ typedef struct
4050
/** @cond */
4151
extern float (*fabs)(float x);
4252
extern float (*recipSqrt)(float x);
53+
extern float (*exp)(float x);
54+
extern float (*log)(float x);
55+
56+
extern float (*native_sin)(float x);
57+
extern float (*native_cos)(float x);
58+
extern float (*native_tan)(float x);
59+
extern float (*native_acos)(float x);
60+
extern float (*native_asin)(float x);
61+
extern float (*native_atan)(float x);
62+
extern float (*native_atan2)(float y, float x);
4363

4464
__inline__ __attribute__((always_inline)) float sqrt(float x)
4565
{return x == 0 ? 0 : 1 / recipSqrt(x);}
4666

4767
#if 0
4868
/// @endcond
4969

70+
/**
71+
* @brief native function for @c sin
72+
*
73+
* @param x -
74+
*
75+
* @return
76+
*/
77+
float native_sin(float x);
78+
79+
/**
80+
* @brief native function for @c cos
81+
*
82+
* @param x -
83+
*
84+
* @return
85+
*/
86+
float native_cos(float x);
87+
88+
/**
89+
* @brief native function for @c tan
90+
*
91+
* @param x -
92+
*
93+
* @return
94+
*/
95+
float native_tan(float x);
96+
97+
/**
98+
* @brief native function for @c asin
99+
*
100+
* @param x -
101+
*
102+
* @return
103+
*/
104+
float native_asin(float x);
105+
106+
107+
/**
108+
* @brief native function for @c acos
109+
*
110+
* @param x -
111+
*
112+
* @return
113+
*/
114+
float native_acos(float x);
115+
116+
/**
117+
* @brief native function for @c atan
118+
*
119+
* @param x -
120+
*
121+
* @return
122+
*/
123+
float native_atan(float x);
124+
125+
/**
126+
* @brief native function for @c atan2
127+
*
128+
* @param y -
129+
* @param x -
130+
*
131+
* @return
132+
*/
133+
float native_atan2(float y, float x);
134+
135+
/**
136+
* @brief @c e to the power @p x
137+
*
138+
* @param x - floating-point value
139+
*
140+
* @return @c e raised the power @p x
141+
*/
142+
float exp(float x);
143+
144+
/**
145+
* @brief natural log
146+
*
147+
* @param x - floating-point value, must be positive
148+
*
149+
* @return the natural log of @p x
150+
*/
151+
float log(float x);
152+
50153
/**
51154
* @brief Absolute value of @p x.
52155
*

src/native_functions.h

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,34 @@
7878
/************************** math.h ****************************/
7979

8080
/** float sin(float x) */
81-
#define SINE_FPTR VOID_PTR(0x80326220)
81+
#define SIN_FPTR VOID_PTR(0x80326220)
8282

83-
/** float cosine(float x) */
84-
#define COSINE_FPTR VOID_PTR(0x80326200)
83+
/** float cos(float x) */
84+
#define COS_FPTR VOID_PTR(0x80326200)
8585

8686
/** float tan(float x) */
87-
#define TANGENT_FPTR VOID_PTR(0x803261bc)
87+
#define TAN_FPTR VOID_PTR(0x803261bc)
88+
89+
/** float acos(float x) */
90+
#define ACOS_FPTR VOID_PTR(0x80022d1c)
91+
92+
/** float asin(float x) */
93+
#define ASIN_FPTR VOID_PTR(0x80022dbc)
94+
95+
/** float atan(float x) */
96+
#define ATAN_FPTR VOID_PTR(0x80022e68)
97+
98+
/** float atan2(float y, float x) */
99+
#define ATAN2_FPTR VOID_PTR(0x80022c30)
100+
101+
/** float exp(float x) */
102+
#define EXP_FPTR VOID_PTR(0x8000ce50)
103+
104+
/** float log(float x) */
105+
#define LOG_FPTR VOID_PTR(0x803265a8)
88106

89107
/** float fabs(float x) */
90-
#define ABS_VAL_FPTR VOID_PTR(0x803261b4)
108+
#define FABS_FPTR VOID_PTR(0x803261b4)
91109

92110
/** float recipSqrt(float x) */
93111
#define RECIP_SQRT_FPTR VOID_PTR(0x80022df8);

0 commit comments

Comments
 (0)