-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmath_native.c
More file actions
411 lines (340 loc) · 12.3 KB
/
math_native.c
File metadata and controls
411 lines (340 loc) · 12.3 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// --------------------------------------------------
// Project: ProX Programming Language (ProXPL)
// Author: ProgrammerKR
// Created: 2025-12-16
// Copyright © 2025. ProXentix India Pvt. Ltd. All rights reserved.
// --------------------------------------------------
/*
* ProXPL Standard Library - Math Module
* Native C implementation of mathematical functions
*/
// CRITICAL FIX: This must be defined BEFORE <math.h> for Windows to see M_E, M_PI
#define _USE_MATH_DEFINES
#include "common.h"
#include "vm.h"
#include "value.h"
#include "object.h"
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
extern VM vm;
// Helper to define native function in a module
static void defineModuleFn(ObjModule* module, const char* name, NativeFn function) {
ObjString* nameObj = copyString(name, (int)strlen(name));
push(&vm, OBJ_VAL(nameObj));
push(&vm, OBJ_VAL(newNative(function)));
tableSet(&module->exports, nameObj, peek(&vm, 0));
pop(&vm);
pop(&vm);
}
// abs(x) - Absolute value
static Value native_abs(int argCount, Value* args) {
if (argCount < 1) return NUMBER_VAL(0);
if (IS_NUMBER(args[0])) {
return NUMBER_VAL(fabs(AS_NUMBER(args[0])));
}
return args[0];
}
// ceil(x) - Ceiling function
static Value native_ceil(int argCount, Value* args) {
if (argCount < 1 || !IS_NUMBER(args[0])) return NUMBER_VAL(0);
return NUMBER_VAL(ceil(AS_NUMBER(args[0])));
}
// floor(x) - Floor function
static Value native_floor(int argCount, Value* args) {
if (argCount < 1 || !IS_NUMBER(args[0])) return NUMBER_VAL(0);
return NUMBER_VAL(floor(AS_NUMBER(args[0])));
}
// round(x, decimals) - Round to n decimal places
static Value native_round(int argCount, Value* args) {
if (argCount < 1 || !IS_NUMBER(args[0])) return NUMBER_VAL(0);
double value = AS_NUMBER(args[0]);
int decimals = 0;
if (argCount >= 2 && IS_NUMBER(args[1])) {
decimals = (int)AS_NUMBER(args[1]);
}
double multiplier = pow(10.0, decimals);
return NUMBER_VAL(round(value * multiplier) / multiplier);
}
// max(...) - Maximum value
static Value native_max(int argCount, Value* args) {
if (argCount == 0) return NIL_VAL;
double maxVal = IS_NUMBER(args[0]) ? AS_NUMBER(args[0]) : 0;
for (int i = 1; i < argCount; i++) {
if (IS_NUMBER(args[i])) {
double val = AS_NUMBER(args[i]);
if (val > maxVal) maxVal = val;
}
}
return NUMBER_VAL(maxVal);
}
// min(...) - Minimum value
static Value native_min(int argCount, Value* args) {
if (argCount == 0) return NIL_VAL;
double minVal = IS_NUMBER(args[0]) ? AS_NUMBER(args[0]) : 0;
for (int i = 1; i < argCount; i++) {
if (IS_NUMBER(args[i])) {
double val = AS_NUMBER(args[i]);
if (val < minVal) minVal = val;
}
}
return NUMBER_VAL(minVal);
}
// pow(base, exp) - Power function
static Value native_pow(int argCount, Value* args) {
if (argCount < 2 || !IS_NUMBER(args[0]) || !IS_NUMBER(args[1])) {
return NUMBER_VAL(0);
}
return NUMBER_VAL(pow(AS_NUMBER(args[0]), AS_NUMBER(args[1])));
}
// sqrt(x) - Square root
static Value native_sqrt(int argCount, Value* args) {
if (argCount < 1 || !IS_NUMBER(args[0])) return NUMBER_VAL(0);
return NUMBER_VAL(sqrt(AS_NUMBER(args[0])));
}
// Trigonometric functions
static Value native_sin(int argCount, Value* args) {
if (argCount < 1 || !IS_NUMBER(args[0])) return NUMBER_VAL(0);
return NUMBER_VAL(sin(AS_NUMBER(args[0])));
}
static Value native_cos(int argCount, Value* args) {
if (argCount < 1 || !IS_NUMBER(args[0])) return NUMBER_VAL(0);
return NUMBER_VAL(cos(AS_NUMBER(args[0])));
}
static Value native_tan(int argCount, Value* args) {
if (argCount < 1 || !IS_NUMBER(args[0])) return NUMBER_VAL(0);
return NUMBER_VAL(tan(AS_NUMBER(args[0])));
}
static Value native_asin(int argCount, Value* args) {
if (argCount < 1 || !IS_NUMBER(args[0])) return NUMBER_VAL(0);
return NUMBER_VAL(asin(AS_NUMBER(args[0])));
}
static Value native_acos(int argCount, Value* args) {
if (argCount < 1 || !IS_NUMBER(args[0])) return NUMBER_VAL(0);
return NUMBER_VAL(acos(AS_NUMBER(args[0])));
}
static Value native_atan(int argCount, Value* args) {
if (argCount < 1 || !IS_NUMBER(args[0])) return NUMBER_VAL(0);
return NUMBER_VAL(atan(AS_NUMBER(args[0])));
}
// log(x, base) - Logarithm
static Value native_log(int argCount, Value* args) {
if (argCount < 1 || !IS_NUMBER(args[0])) return NUMBER_VAL(0);
double x = AS_NUMBER(args[0]);
double base = M_E;
if (argCount >= 2 && IS_NUMBER(args[1])) {
base = AS_NUMBER(args[1]);
}
if (base == M_E) {
return NUMBER_VAL(log(x));
} else {
return NUMBER_VAL(log(x) / log(base));
}
}
// exp(x) - Exponential function
static Value native_exp(int argCount, Value* args) {
if (argCount < 1 || !IS_NUMBER(args[0])) return NUMBER_VAL(0);
return NUMBER_VAL(exp(AS_NUMBER(args[0])));
}
// random() - Random number [0, 1)
static Value native_random(int argCount, Value* args) {
// If no arguments, return float [0.0, 1.0) - Standard behavior
if (argCount == 0) {
double random_part = (double)rand() / ((double)RAND_MAX + 1.0);
return NUMBER_VAL(random_part);
}
// If arguments are provided, behave like randint (User request: "no decimals")
// random(max) -> [0, max]
// random(min, max) -> [min, max]
double min = 0.0;
double max = 1.0;
if (argCount == 1) {
if (!IS_NUMBER(args[0])) return NUMBER_VAL(0);
max = AS_NUMBER(args[0]);
} else if (argCount >= 2) {
if (!IS_NUMBER(args[0]) || !IS_NUMBER(args[1])) return NUMBER_VAL(0);
min = AS_NUMBER(args[0]);
max = AS_NUMBER(args[1]);
}
if (min > max) {
double temp = min;
min = max;
max = temp;
}
// Return integer
long long min_int = (long long)min;
long long max_int = (long long)max;
long long range = max_int - min_int + 1;
// Safety check for range 0 or negative (though swapped above)
if (range <= 0) return NUMBER_VAL(min_int);
return NUMBER_VAL((double)(min_int + (rand() % range)));
}
// randint(min, max) - Random integer in [min, max]
static Value native_randint(int argCount, Value* args) {
if (argCount < 2 || !IS_NUMBER(args[0]) || !IS_NUMBER(args[1])) {
return NUMBER_VAL(0);
}
int min = (int)AS_NUMBER(args[0]);
int max = (int)AS_NUMBER(args[1]);
if (min > max) {
int temp = min;
min = max;
max = temp;
}
int range = max - min + 1;
return NUMBER_VAL(min + (rand() % range));
}
// seed(val)
static Value native_seed(int argCount, Value* args) {
if (argCount > 0 && IS_NUMBER(args[0])) {
srand((unsigned int)AS_NUMBER(args[0]));
} else {
srand((unsigned int)time(NULL));
}
return NIL_VAL;
}
// --------------------------------------------------
// Tensor Functions (Activation & Utilities)
// --------------------------------------------------
// sigmoid(tensor)
static Value native_sigmoid(int argCount, Value* args) {
if (argCount < 1 || !IS_TENSOR(args[0])) {
// runtimeError not easily accessible here without passing vm explicitly?
// We use global vm extern
// Ideally we should raise error.
return NIL_VAL;
}
ObjTensor* t = AS_TENSOR(args[0]);
ObjTensor* res = newTensor(t->dimCount, t->dims, NULL);
push(&vm, OBJ_VAL(res));
for(int i=0; i<t->size; i++) {
res->data[i] = 1.0 / (1.0 + exp(-t->data[i]));
}
return pop(&vm);
}
// relu(tensor)
static Value native_relu(int argCount, Value* args) {
if (argCount < 1 || !IS_TENSOR(args[0])) return NIL_VAL;
ObjTensor* t = AS_TENSOR(args[0]);
ObjTensor* res = newTensor(t->dimCount, t->dims, NULL);
push(&vm, OBJ_VAL(res));
for(int i=0; i<t->size; i++) {
double val = t->data[i];
res->data[i] = (val > 0) ? val : 0;
}
return pop(&vm);
}
// tanh(tensor)
static Value native_tanh(int argCount, Value* args) {
if (argCount < 1) return NIL_VAL;
// Support Scalar tanh too
if (IS_NUMBER(args[0])) {
return NUMBER_VAL(tanh(AS_NUMBER(args[0])));
}
if (!IS_TENSOR(args[0])) return NIL_VAL;
ObjTensor* t = AS_TENSOR(args[0]);
ObjTensor* res = newTensor(t->dimCount, t->dims, NULL);
push(&vm, OBJ_VAL(res));
for(int i=0; i<t->size; i++) {
res->data[i] = tanh(t->data[i]);
}
return pop(&vm);
}
// transpose(tensor)
static Value native_transpose(int argCount, Value* args) {
if (argCount < 1 || !IS_TENSOR(args[0])) return NIL_VAL;
ObjTensor* t = AS_TENSOR(args[0]);
if (t->dimCount != 2) {
// Fallback: Return copy if 1D? Or error?
// For now return copy to be safe.
ObjTensor* res = newTensor(t->dimCount, t->dims, NULL);
push(&vm, OBJ_VAL(res));
memcpy(res->data, t->data, t->size * sizeof(double));
return pop(&vm);
}
// Swap dimensions
int dims[2];
dims[0] = t->dims[1];
dims[1] = t->dims[0];
ObjTensor* res = newTensor(2, dims, NULL);
push(&vm, OBJ_VAL(res));
int rows = t->dims[0];
int cols = t->dims[1];
// Transpose data
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
// src[i, j] -> dst[j, i]
// src flat: i * cols + j
// dst flat: j * rows + i
res->data[j * rows + i] = t->data[i * cols + j];
}
}
return pop(&vm);
}
// Create std.native.math module
ObjModule* create_std_math_module() {
// Initialize random seed once if needed, or rely on user seeding.
// We can seed on module creation.
static int seeded = 0;
if (!seeded) {
srand((unsigned int)time(NULL));
seeded = 1;
}
ObjString* name = copyString("std.native.math", 15);
push(&vm, OBJ_VAL(name));
ObjModule* module = newModule(name);
push(&vm, OBJ_VAL(module));
defineModuleFn(module, "abs", native_abs);
defineModuleFn(module, "ceil", native_ceil);
defineModuleFn(module, "floor", native_floor);
defineModuleFn(module, "round", native_round);
defineModuleFn(module, "max", native_max);
defineModuleFn(module, "min", native_min);
defineModuleFn(module, "pow", native_pow);
defineModuleFn(module, "sqrt", native_sqrt);
defineModuleFn(module, "sin", native_sin);
defineModuleFn(module, "cos", native_cos);
defineModuleFn(module, "tan", native_tan);
defineModuleFn(module, "asin", native_asin);
defineModuleFn(module, "acos", native_acos);
defineModuleFn(module, "atan", native_atan);
defineModuleFn(module, "log", native_log);
defineModuleFn(module, "exp", native_exp);
defineModuleFn(module, "random", native_random);
defineModuleFn(module, "randint", native_randint);
defineModuleFn(module, "seed", native_seed);
defineModuleFn(module, "sigmoid", native_sigmoid);
defineModuleFn(module, "relu", native_relu);
defineModuleFn(module, "tanh", native_tanh);
defineModuleFn(module, "transpose", native_transpose);
pop(&vm); // module
pop(&vm); // name
return module;
}
// Register math functions as globals (for benchmarks/ease of use)
void register_math_globals(VM* pVM) {
defineNative(pVM, "abs", native_abs);
defineNative(pVM, "ceil", native_ceil);
defineNative(pVM, "floor", native_floor);
defineNative(pVM, "round", native_round);
defineNative(pVM, "max", native_max);
defineNative(pVM, "min", native_min);
defineNative(pVM, "pow", native_pow);
defineNative(pVM, "sqrt", native_sqrt);
defineNative(pVM, "sin", native_sin);
defineNative(pVM, "cos", native_cos);
defineNative(pVM, "tan", native_tan);
defineNative(pVM, "asin", native_asin);
defineNative(pVM, "acos", native_acos);
defineNative(pVM, "atan", native_atan);
defineNative(pVM, "log", native_log);
defineNative(pVM, "exp", native_exp);
defineNative(pVM, "random", native_random);
defineNative(pVM, "randint", native_randint);
defineNative(pVM, "seed", native_seed);
defineNative(pVM, "sigmoid", native_sigmoid);
defineNative(pVM, "relu", native_relu);
defineNative(pVM, "tanh", native_tanh);
defineNative(pVM, "transpose", native_transpose);
}