-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnative_math.c
129 lines (118 loc) · 4.55 KB
/
native_math.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
// NanoVM, a tiny java VM for the Atmel AVR family
// Copyright (C) 2005 by Till Harbaum <[email protected]>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//
// native_lcd.c, native driver for Hitachi HD44780 based text LCDs
//
#include "config.h"
#include "error.h"
#include "stack.h"
#include "native.h"
#include "native_math.h"
#include <math.h>
#include <stdlib.h>
#define NATIVE_METHOD_absF 1
#define NATIVE_METHOD_absI 2
#define NATIVE_METHOD_acos 3
#define NATIVE_METHOD_asin 4
#define NATIVE_METHOD_atan 5
#define NATIVE_METHOD_atan2 6
#define NATIVE_METHOD_ceil 7
#define NATIVE_METHOD_cos 8
#define NATIVE_METHOD_exp 9
#define NATIVE_METHOD_floor 10
#define NATIVE_METHOD_log 11
#define NATIVE_METHOD_maxF 12
#define NATIVE_METHOD_maxI 13
#define NATIVE_METHOD_minI 14
#define NATIVE_METHOD_minF 15
#define NATIVE_METHOD_pow 16
#define NATIVE_METHOD_random 17
#define NATIVE_METHOD_rint 18
#define NATIVE_METHOD_round 19
#define NATIVE_METHOD_sin 20
#define NATIVE_METHOD_sqrt 21
#define NATIVE_METHOD_tan 22
#define NATIVE_METHOD_toDegrees 23
#define NATIVE_METHOD_toRadians 24
void native_math_init(void) {
}
void native_math_invoke(uint8_t mref) {
if(mref == NATIVE_METHOD_absF) {
stack_push(nvm_float2stack(fabs(stack_pop_float())));
} else if(mref == NATIVE_METHOD_absI) {
stack_push(nvm_int2stack(abs(stack_pop_int())));
} else if(mref == NATIVE_METHOD_acos) {
stack_push(nvm_float2stack(acos(stack_pop_float())));
} else if(mref == NATIVE_METHOD_asin) {
stack_push(nvm_float2stack(asin(stack_pop_float())));
} else if(mref == NATIVE_METHOD_atan) {
stack_push(nvm_float2stack(atan(stack_pop_float())));
} else if(mref == NATIVE_METHOD_atan2) {
nvm_float_t a = stack_pop_float();
nvm_float_t b = stack_pop_float();
stack_push(nvm_float2stack(atan2(a,b)));
} else if(mref == NATIVE_METHOD_ceil) {
stack_push(nvm_float2stack(ceil(stack_pop_float())));
} else if(mref == NATIVE_METHOD_cos) {
stack_push(nvm_float2stack(cos(stack_pop_float())));
} else if(mref == NATIVE_METHOD_exp) {
stack_push(nvm_float2stack(exp(stack_pop_float())));
} else if(mref == NATIVE_METHOD_floor) {
stack_push(nvm_float2stack(floor(stack_pop_float())));
} else if(mref == NATIVE_METHOD_log) {
stack_push(nvm_float2stack(log(stack_pop_float())));
} else if(mref == NATIVE_METHOD_maxF) {
nvm_float_t a=stack_pop_float();
nvm_float_t b=stack_pop_float();
stack_push(nvm_float2stack(a>b?a:b));
} else if(mref == NATIVE_METHOD_maxI) {
nvm_int_t a=stack_pop_int();
nvm_int_t b=stack_pop_int();
stack_push(nvm_int2stack(a>b?a:b));
} else if(mref == NATIVE_METHOD_minI) {
nvm_int_t a=stack_pop_int();
nvm_int_t b=stack_pop_int();
stack_push(nvm_int2stack(a<b?a:b));
} else if(mref == NATIVE_METHOD_minF) {
nvm_float_t a=stack_pop_float();
nvm_float_t b=stack_pop_float();
stack_push(nvm_float2stack(a<b?a:b));
} else if(mref == NATIVE_METHOD_pow) {
nvm_float_t a = stack_pop_float();
nvm_float_t b = stack_pop_float();
stack_push(nvm_float2stack(pow(a,b)));
} else if(mref == NATIVE_METHOD_random) {
stack_push(nvm_float2stack((nvm_float_t)rand()/RAND_MAX));
} else if(mref == NATIVE_METHOD_rint) {
stack_push(nvm_float2stack((int)(stack_pop_float())));
} else if(mref == NATIVE_METHOD_round) {
stack_push(nvm_int2stack((int)(stack_pop_float())));
} else if(mref == NATIVE_METHOD_sin) {
stack_push(nvm_float2stack(sin(stack_pop_float())));
} else if(mref == NATIVE_METHOD_sqrt) {
stack_push(nvm_float2stack(sqrt(stack_pop_float())));
} else if(mref == NATIVE_METHOD_tan) {
stack_push(nvm_float2stack(tan(stack_pop_float())));
} else if(mref == NATIVE_METHOD_toDegrees) {
stack_push(nvm_float2stack(180/M_PI*stack_pop_float()));
} else if(mref == NATIVE_METHOD_toRadians) {
stack_push(nvm_float2stack(M_PI/180*stack_pop_float()));
} else
error(ERROR_NATIVE_UNKNOWN_METHOD);
}