1 /*
2 	rua_math.c
3 
4 	Floating point math api for ruamoko
5 
6 	Copyright (C) 2003 Robert F Merrill
7 
8 	Author: Robert F Merrill
9 	Date: 2003-01-22
10 
11 	This program is free software; you can redistribute it and/or
12 	modify it under the terms of the GNU General Public License
13 	as published by the Free Software Foundation; either version 2
14 	of the License, or (at your option) any later version.
15 
16 	This program is distributed in the hope that it will be useful,
17 	but WITHOUT ANY WARRANTY; without even the implied warranty of
18 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 
20 	See the GNU General Public License for more details.
21 
22 	You should have received a copy of the GNU General Public License
23 	along with this program; if not, write to:
24 
25 		Free Software Foundation, Inc.
26 		59 Temple Place - Suite 330
27 		Boston, MA  02111-1307, USA
28 
29 */
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33 
34 #include <stdlib.h>
35 #include <math.h>
36 
37 #include "QF/cmd.h"
38 #include "QF/hash.h"
39 #include "QF/progs.h"
40 #include "QF/sys.h"
41 
42 #include "rua_internal.h"
43 
44 static void
bi_sin(progs_t * pr)45 bi_sin (progs_t *pr)
46 {
47 	R_FLOAT (pr) = sinf (P_FLOAT (pr, 0));
48 }
49 
50 static void
bi_cos(progs_t * pr)51 bi_cos (progs_t *pr)
52 {
53 	R_FLOAT (pr) = cosf (P_FLOAT (pr, 0));
54 }
55 
56 static void
bi_tan(progs_t * pr)57 bi_tan (progs_t *pr)
58 {
59 	R_FLOAT (pr) = tanf (P_FLOAT (pr, 0));
60 }
61 
62 static void
bi_asin(progs_t * pr)63 bi_asin (progs_t *pr)
64 {
65 	R_FLOAT (pr) = asinf (P_FLOAT (pr, 0));
66 }
67 
68 static void
bi_acos(progs_t * pr)69 bi_acos (progs_t *pr)
70 {
71 	R_FLOAT (pr) = acosf (P_FLOAT (pr, 0));
72 }
73 
74 static void
bi_atan(progs_t * pr)75 bi_atan (progs_t *pr)
76 {
77 	R_FLOAT (pr) = atanf (P_FLOAT (pr, 0));
78 }
79 
80 static void
bi_atan2(progs_t * pr)81 bi_atan2 (progs_t *pr)
82 {
83 	R_FLOAT (pr) = atan2f (P_FLOAT (pr, 0), P_FLOAT (pr, 1));
84 }
85 
86 static void
bi_log(progs_t * pr)87 bi_log (progs_t *pr)
88 {
89 	R_FLOAT (pr) = logf (P_FLOAT (pr, 0));
90 }
91 
92 static void
bi_log2(progs_t * pr)93 bi_log2 (progs_t *pr)
94 {
95 #ifdef HAVE_LOG2F
96 	R_FLOAT (pr) = log2f (P_FLOAT (pr, 0));
97 #else
98 	R_FLOAT (pr) = logf (P_FLOAT (pr, 0)) / M_LOG2E;
99 #endif
100 }
101 
102 static void
bi_log10(progs_t * pr)103 bi_log10 (progs_t *pr)
104 {
105 	R_FLOAT (pr) = log10f (P_FLOAT (pr, 0));
106 }
107 
108 static void
bi_pow(progs_t * pr)109 bi_pow (progs_t *pr)
110 {
111 	R_FLOAT (pr) = powf (P_FLOAT (pr, 0), P_FLOAT (pr, 1));
112 }
113 
114 static void
bi_sqrt(progs_t * pr)115 bi_sqrt (progs_t *pr)
116 {
117 	R_FLOAT (pr) = sqrtf (P_FLOAT (pr, 0));
118 }
119 
120 static void
bi_cbrt(progs_t * pr)121 bi_cbrt (progs_t *pr)
122 {
123 	R_FLOAT (pr) = cbrtf (P_FLOAT (pr, 0));
124 }
125 
126 static void
bi_hypot(progs_t * pr)127 bi_hypot (progs_t *pr)
128 {
129 	R_FLOAT (pr) = hypotf (P_FLOAT (pr, 0), P_FLOAT (pr, 1));
130 }
131 
132 static void
bi_sinh(progs_t * pr)133 bi_sinh (progs_t *pr)
134 {
135 	R_FLOAT (pr) = sinhf (P_FLOAT (pr, 0));
136 }
137 
138 static void
bi_cosh(progs_t * pr)139 bi_cosh (progs_t *pr)
140 {
141 	R_FLOAT (pr) = coshf (P_FLOAT (pr, 0));
142 }
143 
144 static void
bi_tanh(progs_t * pr)145 bi_tanh (progs_t *pr)
146 {
147 	R_FLOAT (pr) = tanhf (P_FLOAT (pr, 0));
148 }
149 
150 static void
bi_asinh(progs_t * pr)151 bi_asinh (progs_t *pr)
152 {
153 	double      y = P_FLOAT (pr, 0);
154 	R_FLOAT (pr) = logf (y + sqrtf (y * y + 1));
155 }
156 
157 static void
bi_acosh(progs_t * pr)158 bi_acosh (progs_t *pr)
159 {
160 	double      y = P_FLOAT (pr, 0);
161 	R_FLOAT (pr) = logf (y + sqrtf (y * y - 1));
162 }
163 
164 static void
bi_atanh(progs_t * pr)165 bi_atanh (progs_t *pr)
166 {
167 	double      y = P_FLOAT (pr, 0);
168 	R_FLOAT (pr) = logf ((1 + y) / (1 - y)) / 2;
169 }
170 
171 static builtin_t builtins[] = {
172 	{"sin",		bi_sin,		-1},
173 	{"cos",		bi_cos,		-1},
174 	{"tan",		bi_tan,		-1},
175 	{"asin",	bi_asin,	-1},
176 	{"acos",	bi_acos,	-1},
177 	{"atan",	bi_atan,	-1},
178 	{"atan2", 	bi_atan2, 	-1},
179 	{"log",		bi_log,		-1},
180 	{"log2",	bi_log2,	-1},
181 	{"log10",	bi_log10,	-1},
182 	{"pow",		bi_pow,		-1},
183 	{"sqrt",	bi_sqrt,	-1},
184 	{"cbrt",	bi_cbrt,	-1},
185 	{"hypot",	bi_hypot,	-1},
186 	{"sinh",	bi_sinh,	-1},
187 	{"cosh",	bi_cosh,	-1},
188 	{"tanh",	bi_tanh,	-1},
189 	{"asinh",	bi_asinh,	-1},
190 	{"acosh",	bi_acosh,	-1},
191 	{"atanh",	bi_atanh,	-1},
192 	{0}
193 };
194 
195 void
RUA_Math_Init(progs_t * pr,int secure)196 RUA_Math_Init (progs_t *pr, int secure)
197 {
198 	PR_RegisterBuiltins (pr, builtins);
199 }
200