1 /* Float arithmetic for the Small AMX engine
3 * Copyright (c) Artran, Inc. 1999
4 * Written by Greg Garner (gmg@artran.com)
5 * Portions Copyright (c) Carsten Haitzler, 2004 <raster@rasterman.com>
7 * This software is provided "as-is", without any express or implied warranty.
8 * In no event will the authors be held liable for any damages arising from
9 * the use of this software.
11 * Permission is granted to anyone to use this software for any purpose,
12 * including commercial applications, and to alter it and redistribute it
13 * freely, subject to the following restrictions:
15 * 1. The origin of this software must not be misrepresented; you must not
16 * claim that you wrote the original software. If you use this software in
17 * a product, an acknowledgment in the product documentation would be
18 * appreciated but is not required.
19 * 2. Altered source versions must be plainly marked as such, and must not be
20 * misrepresented as being the original software.
21 * 3. This notice may not be removed or altered from any source distribution.
25 * 2002-08-27: Basic conversion of source from C++ to C by Adam D. Moss
26 * <adam@gimp.org> <aspirin@icculus.org>
27 * 2003-08-29: Removal of the dynamic memory allocation and replacing two
28 * type conversion functions by macros, by Thiadmer Riemersma
29 * 2003-09-22: Moved the type conversion macros to AMX.H, and simplifications
30 * of some routines, by Thiadmer Riemersma
31 * 2003-11-24: A few more native functions (geometry), plus minor modifications,
32 * mostly to be compatible with dynamically loadable extension
33 * modules, by Thiadmer Riemersma
34 * 2004-03-20: Cleaned up and reduced size for Embryo, Modified to conform to
35 * E coding style. Added extra parameter checks.
36 * Carsten Haitzler, <raster@rasterman.com>
48 #include "embryo_private.h"
50 #define PI 3.1415926535897932384626433832795f
52 /* internally useful calls */
55 _embryo_fp_degrees_to_radians(float angle, int radix)
59 case 1: /* degrees, sexagesimal system (technically: degrees/minutes/seconds) */
60 return (angle * PI / 180.0f);
61 case 2: /* grades, centesimal system */
62 return (angle * PI / 200.0f);
63 default: /* assume already radian */
69 /* exported float api */
72 _embryo_fp(Embryo_Program *ep __UNUSED__, Embryo_Cell *params)
74 /* params[1] = long value to convert to a float */
77 if (params[0] != (1 * sizeof(Embryo_Cell))) return 0;
79 return EMBRYO_FLOAT_TO_CELL(f);
83 _embryo_fp_str(Embryo_Program *ep, Embryo_Cell *params)
85 /* params[1] = virtual string address to convert to a float */
91 if (params[0] != (1 * sizeof(Embryo_Cell))) return 0;
92 str = embryo_data_address_get(ep, params[1]);
93 len = embryo_data_string_length_get(ep, str);
94 if ((len == 0) || (len >= (int)sizeof(buf))) return 0;
95 embryo_data_string_get(ep, str, buf);
97 return EMBRYO_FLOAT_TO_CELL(f);
101 _embryo_fp_mul(Embryo_Program *ep __UNUSED__, Embryo_Cell *params)
103 /* params[1] = float operand 1 */
104 /* params[2] = float operand 2 */
107 if (params[0] != (2 * sizeof(Embryo_Cell))) return 0;
108 f = EMBRYO_CELL_TO_FLOAT(params[1]) * EMBRYO_CELL_TO_FLOAT(params[2]);
109 return EMBRYO_FLOAT_TO_CELL(f);
113 _embryo_fp_div(Embryo_Program *ep __UNUSED__, Embryo_Cell *params)
115 /* params[1] = float dividend (top) */
116 /* params[2] = float divisor (bottom) */
119 if (params[0] != (2 * sizeof(Embryo_Cell))) return 0;
120 f = EMBRYO_CELL_TO_FLOAT(params[1]) / EMBRYO_CELL_TO_FLOAT(params[2]);
121 return EMBRYO_FLOAT_TO_CELL(f);
125 _embryo_fp_add(Embryo_Program *ep __UNUSED__, Embryo_Cell *params)
127 /* params[1] = float operand 1 */
128 /* params[2] = float operand 2 */
131 if (params[0] != (2 * sizeof(Embryo_Cell))) return 0;
132 f = EMBRYO_CELL_TO_FLOAT(params[1]) + EMBRYO_CELL_TO_FLOAT(params[2]);
133 return EMBRYO_FLOAT_TO_CELL(f);
137 _embryo_fp_sub(Embryo_Program *ep __UNUSED__, Embryo_Cell *params)
139 /* params[1] = float operand 1 */
140 /* params[2] = float operand 2 */
143 if (params[0] != (2 * sizeof(Embryo_Cell))) return 0;
144 f = EMBRYO_CELL_TO_FLOAT(params[1]) - EMBRYO_CELL_TO_FLOAT(params[2]);
145 return EMBRYO_FLOAT_TO_CELL(f);
148 /* Return fractional part of float */
150 _embryo_fp_fract(Embryo_Program *ep __UNUSED__, Embryo_Cell *params)
152 /* params[1] = float operand */
155 if (params[0] != (1 * sizeof(Embryo_Cell))) return 0;
156 f = EMBRYO_CELL_TO_FLOAT(params[1]);
158 return EMBRYO_FLOAT_TO_CELL(f);
161 /* Return integer part of float, rounded */
163 _embryo_fp_round(Embryo_Program *ep __UNUSED__, Embryo_Cell *params)
165 /* params[1] = float operand */
166 /* params[2] = Type of rounding (cell) */
169 if (params[0] != (2 * sizeof(Embryo_Cell))) return 0;
170 f = EMBRYO_CELL_TO_FLOAT(params[1]);
173 case 1: /* round downwards (truncate) */
176 case 2: /* round upwards */
179 case 3: /* round towards zero */
180 if (f >= 0.0) f = (floorf(f));
183 default: /* standard, round to nearest */
184 f = (floorf(f + 0.5));
187 return (Embryo_Cell)f;
191 _embryo_fp_cmp(Embryo_Program *ep __UNUSED__, Embryo_Cell *params)
193 /* params[1] = float operand 1 */
194 /* params[2] = float operand 2 */
197 if (params[0] != (2 * sizeof(Embryo_Cell))) return 0;
198 f = EMBRYO_CELL_TO_FLOAT(params[1]);
199 ff = EMBRYO_CELL_TO_FLOAT(params[2]);
200 if (f == ff) return 0;
201 else if (f > ff) return 1;
206 _embryo_fp_sqroot(Embryo_Program *ep, Embryo_Cell *params)
208 /* params[1] = float operand */
211 if (params[0] != (1 * sizeof(Embryo_Cell))) return 0;
212 f = EMBRYO_CELL_TO_FLOAT(params[1]);
216 embryo_program_error_set(ep, EMBRYO_ERROR_DOMAIN);
219 return EMBRYO_FLOAT_TO_CELL(f);
223 _embryo_fp_power(Embryo_Program *ep __UNUSED__, Embryo_Cell *params)
225 /* params[1] = float operand 1 */
226 /* params[2] = float operand 2 */
229 if (params[0] != (2 * sizeof(Embryo_Cell))) return 0;
230 f = EMBRYO_CELL_TO_FLOAT(params[1]);
231 ff = EMBRYO_CELL_TO_FLOAT(params[2]);
233 return EMBRYO_FLOAT_TO_CELL(f);
237 _embryo_fp_log(Embryo_Program *ep, Embryo_Cell *params)
239 /* params[1] = float operand 1 (value) */
240 /* params[2] = float operand 2 (base) */
243 if (params[0] != (2 * sizeof(Embryo_Cell))) return 0;
244 f = EMBRYO_CELL_TO_FLOAT(params[1]);
245 ff = EMBRYO_CELL_TO_FLOAT(params[2]);
246 if ((f <= 0.0) || (ff <= 0.0))
248 embryo_program_error_set(ep, EMBRYO_ERROR_DOMAIN);
251 if (ff == 10.0) f = log10f(f);
252 else f = (logf(f) / logf(ff));
253 return EMBRYO_FLOAT_TO_CELL(f);
257 _embryo_fp_sin(Embryo_Program *ep __UNUSED__, Embryo_Cell *params)
259 /* params[1] = float operand 1 (angle) */
260 /* params[2] = float operand 2 (radix) */
263 if (params[0] != (2 * sizeof(Embryo_Cell))) return 0;
264 f = EMBRYO_CELL_TO_FLOAT(params[1]);
265 f = _embryo_fp_degrees_to_radians(f, params[2]);
267 return EMBRYO_FLOAT_TO_CELL(f);
271 _embryo_fp_cos(Embryo_Program *ep __UNUSED__, Embryo_Cell *params)
273 /* params[1] = float operand 1 (angle) */
274 /* params[2] = float operand 2 (radix) */
277 if (params[0] != (2 * sizeof(Embryo_Cell))) return 0;
278 f = EMBRYO_CELL_TO_FLOAT(params[1]);
279 f = _embryo_fp_degrees_to_radians(f, params[2]);
281 return EMBRYO_FLOAT_TO_CELL(f);
285 _embryo_fp_tan(Embryo_Program *ep __UNUSED__, Embryo_Cell *params)
287 /* params[1] = float operand 1 (angle) */
288 /* params[2] = float operand 2 (radix) */
291 if (params[0] != (2 * sizeof(Embryo_Cell))) return 0;
292 f = EMBRYO_CELL_TO_FLOAT(params[1]);
293 f = _embryo_fp_degrees_to_radians(f, params[2]);
295 return EMBRYO_FLOAT_TO_CELL(f);
299 _embryo_fp_abs(Embryo_Program *ep __UNUSED__, Embryo_Cell *params)
301 /* params[1] = float operand */
304 if (params[0] != (1 * sizeof(Embryo_Cell))) return 0;
305 f = EMBRYO_CELL_TO_FLOAT(params[1]);
306 f = (f >= 0) ? f : -f;
307 return EMBRYO_FLOAT_TO_CELL(f);
310 /* functions used by the rest of embryo */
313 _embryo_fp_init(Embryo_Program *ep)
315 embryo_program_native_call_add(ep, "float", _embryo_fp);
316 embryo_program_native_call_add(ep, "atof", _embryo_fp_str);
317 embryo_program_native_call_add(ep, "float_mul", _embryo_fp_mul);
318 embryo_program_native_call_add(ep, "float_div", _embryo_fp_div);
319 embryo_program_native_call_add(ep, "float_add", _embryo_fp_add);
320 embryo_program_native_call_add(ep, "float_sub", _embryo_fp_sub);
321 embryo_program_native_call_add(ep, "fract", _embryo_fp_fract);
322 embryo_program_native_call_add(ep, "round", _embryo_fp_round);
323 embryo_program_native_call_add(ep, "float_cmp", _embryo_fp_cmp);
324 embryo_program_native_call_add(ep, "sqrt", _embryo_fp_sqroot);
325 embryo_program_native_call_add(ep, "pow", _embryo_fp_power);
326 embryo_program_native_call_add(ep, "log", _embryo_fp_log);
327 embryo_program_native_call_add(ep, "sin", _embryo_fp_sin);
328 embryo_program_native_call_add(ep, "cos", _embryo_fp_cos);
329 embryo_program_native_call_add(ep, "tan", _embryo_fp_tan);
330 embryo_program_native_call_add(ep, "abs", _embryo_fp_abs);