History

Skeleton of the built-in Math module. - Only functions needed for the limbo compiler are implemented.

arr committed 9 years ago

Commit 99a0b811 parent 7838bae4 files · +1145 −0Browse files

Showing the changes to src/vm/math/Mathmod.cpp (1 of 4 files). Show all

Unified · Split

src/vm/math/Mathmod.cpp+594−0
@@ -0,0 +1,594 @@
1//
2// Dis VM
3// File: Mathmod.cpp
4// Author: arr
5//
6
7#include "Mathmod.h"
8
9using namespace disvm;
10using namespace disvm::runtime;
11
12namespace
13{
14 disvm::runtime::builtin::vm_runtab_t Mathmodtab[] = {
15 "FPcontrol",0x6584767b,Math_FPcontrol,40,0,{0},
16 "FPstatus",0x6584767b,Math_FPstatus,40,0,{0},
17 "acos",0xa1d1fe48,Math_acos,40,0,{0},
18 "acosh",0xa1d1fe48,Math_acosh,40,0,{0},
19 "asin",0xa1d1fe48,Math_asin,40,0,{0},
20 "asinh",0xa1d1fe48,Math_asinh,40,0,{0},
21 "atan",0xa1d1fe48,Math_atan,40,0,{0},
22 "atan2",0xf293f6cf,Math_atan2,48,0,{0},
23 "atanh",0xa1d1fe48,Math_atanh,40,0,{0},
24 "bits32real",0x40a58596,Math_bits32real,40,0,{0},
25 "bits64real",0x5463c72c,Math_bits64real,40,0,{0},
26 "cbrt",0xa1d1fe48,Math_cbrt,40,0,{0},
27 "ceil",0xa1d1fe48,Math_ceil,40,0,{0},
28 "copysign",0xf293f6cf,Math_copysign,48,0,{0},
29 "cos",0xa1d1fe48,Math_cos,40,0,{0},
30 "cosh",0xa1d1fe48,Math_cosh,40,0,{0},
31 "dot",0xfeca4db6,Math_dot,40,2,{0x0,0xc0,},
32 "erf",0xa1d1fe48,Math_erf,40,0,{0},
33 "erfc",0xa1d1fe48,Math_erfc,40,0,{0},
34 "exp",0xa1d1fe48,Math_exp,40,0,{0},
35 "expm1",0xa1d1fe48,Math_expm1,40,0,{0},
36 "export_int",0x3f1d94ee,Math_export_int,40,2,{0x0,0xc0,},
37 "export_real",0xd1fb0c8c,Math_export_real,40,2,{0x0,0xc0,},
38 "export_real32",0xd1fb0c8c,Math_export_real32,40,2,{0x0,0xc0,},
39 "fabs",0xa1d1fe48,Math_fabs,40,0,{0},
40 "fdim",0xf293f6cf,Math_fdim,48,0,{0},
41 "finite",0x7b2e5f52,Math_finite,40,0,{0},
42 "floor",0xa1d1fe48,Math_floor,40,0,{0},
43 "fmax",0xf293f6cf,Math_fmax,48,0,{0},
44 "fmin",0xf293f6cf,Math_fmin,48,0,{0},
45 "fmod",0xf293f6cf,Math_fmod,48,0,{0},
46 "gemm",0x53b8cd34,Math_gemm,96,3,{0x0,0x0,0xa2,},
47 "getFPcontrol",0x616977e8,Math_getFPcontrol,32,0,{0},
48 "getFPstatus",0x616977e8,Math_getFPstatus,32,0,{0},
49 "hypot",0xf293f6cf,Math_hypot,48,0,{0},
50 "iamax",0xa5fc2e4d,Math_iamax,40,2,{0x0,0x80,},
51 "ilogb",0x7b2e5f52,Math_ilogb,40,0,{0},
52 "import_int",0x3f1d94ee,Math_import_int,40,2,{0x0,0xc0,},
53 "import_real",0xd1fb0c8c,Math_import_real,40,2,{0x0,0xc0,},
54 "import_real32",0xd1fb0c8c,Math_import_real32,40,2,{0x0,0xc0,},
55 "isnan",0x7b2e5f52,Math_isnan,40,0,{0},
56 "j0",0xa1d1fe48,Math_j0,40,0,{0},
57 "j1",0xa1d1fe48,Math_j1,40,0,{0},
58 "jn",0xb61ffc5b,Math_jn,48,0,{0},
59 "lgamma",0x6e97c646,Math_lgamma,40,0,{0},
60 "log",0xa1d1fe48,Math_log,40,0,{0},
61 "log10",0xa1d1fe48,Math_log10,40,0,{0},
62 "log1p",0xa1d1fe48,Math_log1p,40,0,{0},
63 "modf",0x6e97c646,Math_modf,40,0,{0},
64 "nextafter",0xf293f6cf,Math_nextafter,48,0,{0},
65 "norm1",0x1629207e,Math_norm1,40,2,{0x0,0x80,},
66 "norm2",0x1629207e,Math_norm2,40,2,{0x0,0x80,},
67 "pow",0xf293f6cf,Math_pow,48,0,{0},
68 "pow10",0x40a58596,Math_pow10,40,0,{0},
69 "realbits32",0x7b2e5f52,Math_realbits32,40,0,{0},
70 "realbits64",0x9589d476,Math_realbits64,40,0,{0},
71 "remainder",0xf293f6cf,Math_remainder,48,0,{0},
72 "rint",0xa1d1fe48,Math_rint,40,0,{0},
73 "scalbn",0x64fa84ba,Math_scalbn,48,0,{0},
74 "sin",0xa1d1fe48,Math_sin,40,0,{0},
75 "sinh",0xa1d1fe48,Math_sinh,40,0,{0},
76 "sort",0xfc7c7b8,Math_sort,40,2,{0x0,0xc0,},
77 "sqrt",0xa1d1fe48,Math_sqrt,40,0,{0},
78 "tan",0xa1d1fe48,Math_tan,40,0,{0},
79 "tanh",0xa1d1fe48,Math_tanh,40,0,{0},
80 "y0",0xa1d1fe48,Math_y0,40,0,{0},
81 "y1",0xa1d1fe48,Math_y1,40,0,{0},
82 "yn",0xb61ffc5b,Math_yn,48,0,{0},
83 0
84 };
85
86 const word_t Mathmodlen = 68;
87}
88
89void
90Mathmodinit(void)
91{
92 builtin::register_module_exports(Math_PATH, Mathmodlen, Mathmodtab);
93}
94
95void
96Math_FPcontrol(vm_registers_t &r, vm_t &vm)
97{
98 auto &fp = r.stack.peek_frame()->base<F_Math_FPcontrol>();
99
100 // [TODO] Properly implment floating point control
101 *fp.ret = fp.mask;
102}
103
104void
105Math_FPstatus(vm_registers_t &r, vm_t &vm)
106{
107 auto &fp = r.stack.peek_frame()->base<F_Math_FPstatus>();
108 throw vm_system_exception{ "Instruction not implemented" };
109}
110
111void
112Math_acos(vm_registers_t &r, vm_t &vm)
113{
114 auto &fp = r.stack.peek_frame()->base<F_Math_acos>();
115 throw vm_system_exception{ "Instruction not implemented" };
116}
117
118void
119Math_acosh(vm_registers_t &r, vm_t &vm)
120{
121 auto &fp = r.stack.peek_frame()->base<F_Math_acosh>();
122 throw vm_system_exception{ "Instruction not implemented" };
123}
124
125void
126Math_asin(vm_registers_t &r, vm_t &vm)
127{
128 auto &fp = r.stack.peek_frame()->base<F_Math_asin>();
129 throw vm_system_exception{ "Instruction not implemented" };
130}
131
132void
133Math_asinh(vm_registers_t &r, vm_t &vm)
134{
135 auto &fp = r.stack.peek_frame()->base<F_Math_asinh>();
136 throw vm_system_exception{ "Instruction not implemented" };
137}
138
139void
140Math_atan(vm_registers_t &r, vm_t &vm)
141{
142 auto &fp = r.stack.peek_frame()->base<F_Math_atan>();
143 throw vm_system_exception{ "Instruction not implemented" };
144}
145
146void
147Math_atan2(vm_registers_t &r, vm_t &vm)
148{
149 auto &fp = r.stack.peek_frame()->base<F_Math_atan2>();
150 throw vm_system_exception{ "Instruction not implemented" };
151}
152
153void
154Math_atanh(vm_registers_t &r, vm_t &vm)
155{
156 auto &fp = r.stack.peek_frame()->base<F_Math_atanh>();
157 throw vm_system_exception{ "Instruction not implemented" };
158}
159
160void
161Math_bits32real(vm_registers_t &r, vm_t &vm)
162{
163 auto &fp = r.stack.peek_frame()->base<F_Math_bits32real>();
164 throw vm_system_exception{ "Instruction not implemented" };
165}
166
167void
168Math_bits64real(vm_registers_t &r, vm_t &vm)
169{
170 auto &fp = r.stack.peek_frame()->base<F_Math_bits64real>();
171 throw vm_system_exception{ "Instruction not implemented" };
172}
173
174void
175Math_cbrt(vm_registers_t &r, vm_t &vm)
176{
177 auto &fp = r.stack.peek_frame()->base<F_Math_cbrt>();
178 throw vm_system_exception{ "Instruction not implemented" };
179}
180
181void
182Math_ceil(vm_registers_t &r, vm_t &vm)
183{
184 auto &fp = r.stack.peek_frame()->base<F_Math_ceil>();
185 throw vm_system_exception{ "Instruction not implemented" };
186}
187
188void
189Math_copysign(vm_registers_t &r, vm_t &vm)
190{
191 auto &fp = r.stack.peek_frame()->base<F_Math_copysign>();
192 throw vm_system_exception{ "Instruction not implemented" };
193}
194
195void
196Math_cos(vm_registers_t &r, vm_t &vm)
197{
198 auto &fp = r.stack.peek_frame()->base<F_Math_cos>();
199 throw vm_system_exception{ "Instruction not implemented" };
200}
201
202void
203Math_cosh(vm_registers_t &r, vm_t &vm)
204{
205 auto &fp = r.stack.peek_frame()->base<F_Math_cosh>();
206 throw vm_system_exception{ "Instruction not implemented" };
207}
208
209void
210Math_dot(vm_registers_t &r, vm_t &vm)
211{
212 auto &fp = r.stack.peek_frame()->base<F_Math_dot>();
213 throw vm_system_exception{ "Instruction not implemented" };
214}
215
216void
217Math_erf(vm_registers_t &r, vm_t &vm)
218{
219 auto &fp = r.stack.peek_frame()->base<F_Math_erf>();
220 throw vm_system_exception{ "Instruction not implemented" };
221}
222
223void
224Math_erfc(vm_registers_t &r, vm_t &vm)
225{
226 auto &fp = r.stack.peek_frame()->base<F_Math_erfc>();
227 throw vm_system_exception{ "Instruction not implemented" };
228}
229
230void
231Math_exp(vm_registers_t &r, vm_t &vm)
232{
233 auto &fp = r.stack.peek_frame()->base<F_Math_exp>();
234 throw vm_system_exception{ "Instruction not implemented" };
235}
236
237void
238Math_expm1(vm_registers_t &r, vm_t &vm)
239{
240 auto &fp = r.stack.peek_frame()->base<F_Math_expm1>();
241 throw vm_system_exception{ "Instruction not implemented" };
242}
243
244void
245Math_export_int(vm_registers_t &r, vm_t &vm)
246{
247 auto &fp = r.stack.peek_frame()->base<F_Math_export_int>();
248 throw vm_system_exception{ "Instruction not implemented" };
249}
250
251void
252Math_export_real(vm_registers_t &r, vm_t &vm)
253{
254 auto &fp = r.stack.peek_frame()->base<F_Math_export_real>();
255 throw vm_system_exception{ "Instruction not implemented" };
256}
257
258void
259Math_export_real32(vm_registers_t &r, vm_t &vm)
260{
261 auto &fp = r.stack.peek_frame()->base<F_Math_export_real32>();
262 throw vm_system_exception{ "Instruction not implemented" };
263}
264
265void
266Math_fabs(vm_registers_t &r, vm_t &vm)
267{
268 auto &fp = r.stack.peek_frame()->base<F_Math_fabs>();
269 throw vm_system_exception{ "Instruction not implemented" };
270}
271
272void
273Math_fdim(vm_registers_t &r, vm_t &vm)
274{
275 auto &fp = r.stack.peek_frame()->base<F_Math_fdim>();
276 throw vm_system_exception{ "Instruction not implemented" };
277}
278
279void
280Math_finite(vm_registers_t &r, vm_t &vm)
281{
282 auto &fp = r.stack.peek_frame()->base<F_Math_finite>();
283 throw vm_system_exception{ "Instruction not implemented" };
284}
285
286void
287Math_floor(vm_registers_t &r, vm_t &vm)
288{
289 auto &fp = r.stack.peek_frame()->base<F_Math_floor>();
290 throw vm_system_exception{ "Instruction not implemented" };
291}
292
293void
294Math_fmax(vm_registers_t &r, vm_t &vm)
295{
296 auto &fp = r.stack.peek_frame()->base<F_Math_fmax>();
297 throw vm_system_exception{ "Instruction not implemented" };
298}
299
300void
301Math_fmin(vm_registers_t &r, vm_t &vm)
302{
303 auto &fp = r.stack.peek_frame()->base<F_Math_fmin>();
304 throw vm_system_exception{ "Instruction not implemented" };
305}
306
307void
308Math_fmod(vm_registers_t &r, vm_t &vm)
309{
310 auto &fp = r.stack.peek_frame()->base<F_Math_fmod>();
311 throw vm_system_exception{ "Instruction not implemented" };
312}
313
314void
315Math_gemm(vm_registers_t &r, vm_t &vm)
316{
317 auto &fp = r.stack.peek_frame()->base<F_Math_gemm>();
318 throw vm_system_exception{ "Instruction not implemented" };
319}
320
321void
322Math_getFPcontrol(vm_registers_t &r, vm_t &vm)
323{
324 auto &fp = r.stack.peek_frame()->base<F_Math_getFPcontrol>();
325 throw vm_system_exception{ "Instruction not implemented" };
326}
327
328void
329Math_getFPstatus(vm_registers_t &r, vm_t &vm)
330{
331 auto &fp = r.stack.peek_frame()->base<F_Math_getFPstatus>();
332 throw vm_system_exception{ "Instruction not implemented" };
333}
334
335void
336Math_hypot(vm_registers_t &r, vm_t &vm)
337{
338 auto &fp = r.stack.peek_frame()->base<F_Math_hypot>();
339 throw vm_system_exception{ "Instruction not implemented" };
340}
341
342void
343Math_iamax(vm_registers_t &r, vm_t &vm)
344{
345 auto &fp = r.stack.peek_frame()->base<F_Math_iamax>();
346 throw vm_system_exception{ "Instruction not implemented" };
347}
348
349void
350Math_ilogb(vm_registers_t &r, vm_t &vm)
351{
352 auto &fp = r.stack.peek_frame()->base<F_Math_ilogb>();
353 throw vm_system_exception{ "Instruction not implemented" };
354}
355
356void
357Math_import_int(vm_registers_t &r, vm_t &vm)
358{
359 auto &fp = r.stack.peek_frame()->base<F_Math_import_int>();
360 throw vm_system_exception{ "Instruction not implemented" };
361}
362
363void
364Math_import_real(vm_registers_t &r, vm_t &vm)
365{
366 auto &fp = r.stack.peek_frame()->base<F_Math_import_real>();
367
368 auto buffer = vm_alloc_t::from_allocation<vm_array_t>(fp.b);
369 assert(buffer->get_element_type()->size_in_bytes == intrinsic_type_desc::type<byte_t>()->size_in_bytes);
370
371 auto result = vm_alloc_t::from_allocation<vm_array_t>(fp.x);
372 assert(result->get_element_type()->size_in_bytes == intrinsic_type_desc::type<real_t>()->size_in_bytes);
373 const auto result_len = result->get_length();
374
375 if (buffer->get_length() != (8 * result_len))
376 throw vm_user_exception{ "Invalid buffer size based on conversion" };
377
378 // Process as big endian
379 for (int i = 0; i < result_len; ++i)
380 {
381 uint64_t t = buffer->at<byte_t>(0);
382 for (int j = 1; j < 8; ++j)
383 t = (t << 8) | buffer->at<byte_t>(j);
384
385 auto r = real_t{};
386 std::memcpy(&r, &t, sizeof(t));
387 static_assert(sizeof(t) == sizeof(r), "Real should be 64-bits");
388
389 result->at<real_t>(i) = r;
390 }
391}
392
393void
394Math_import_real32(vm_registers_t &r, vm_t &vm)
395{
396 auto &fp = r.stack.peek_frame()->base<F_Math_import_real32>();
397 throw vm_system_exception{ "Instruction not implemented" };
398}
399
400void
401Math_isnan(vm_registers_t &r, vm_t &vm)
402{
403 auto &fp = r.stack.peek_frame()->base<F_Math_isnan>();
404 *fp.ret = std::isnan(fp.x) ? 1 : 0;
405}
406
407void
408Math_j0(vm_registers_t &r, vm_t &vm)
409{
410 auto &fp = r.stack.peek_frame()->base<F_Math_j0>();
411 throw vm_system_exception{ "Instruction not implemented" };
412}
413
414void
415Math_j1(vm_registers_t &r, vm_t &vm)
416{
417 auto &fp = r.stack.peek_frame()->base<F_Math_j1>();
418 throw vm_system_exception{ "Instruction not implemented" };
419}
420
421void
422Math_jn(vm_registers_t &r, vm_t &vm)
423{
424 auto &fp = r.stack.peek_frame()->base<F_Math_jn>();
425 throw vm_system_exception{ "Instruction not implemented" };
426}
427
428void
429Math_lgamma(vm_registers_t &r, vm_t &vm)
430{
431 auto &fp = r.stack.peek_frame()->base<F_Math_lgamma>();
432 throw vm_system_exception{ "Instruction not implemented" };
433}
434
435void
436Math_log(vm_registers_t &r, vm_t &vm)
437{
438 auto &fp = r.stack.peek_frame()->base<F_Math_log>();
439 throw vm_system_exception{ "Instruction not implemented" };
440}
441
442void
443Math_log10(vm_registers_t &r, vm_t &vm)
444{
445 auto &fp = r.stack.peek_frame()->base<F_Math_log10>();
446 throw vm_system_exception{ "Instruction not implemented" };
447}
448
449void
450Math_log1p(vm_registers_t &r, vm_t &vm)
451{
452 auto &fp = r.stack.peek_frame()->base<F_Math_log1p>();
453 throw vm_system_exception{ "Instruction not implemented" };
454}
455
456void
457Math_modf(vm_registers_t &r, vm_t &vm)
458{
459 auto &fp = r.stack.peek_frame()->base<F_Math_modf>();
460 throw vm_system_exception{ "Instruction not implemented" };
461}
462
463void
464Math_nextafter(vm_registers_t &r, vm_t &vm)
465{
466 auto &fp = r.stack.peek_frame()->base<F_Math_nextafter>();
467 throw vm_system_exception{ "Instruction not implemented" };
468}
469
470void
471Math_norm1(vm_registers_t &r, vm_t &vm)
472{
473 auto &fp = r.stack.peek_frame()->base<F_Math_norm1>();
474 throw vm_system_exception{ "Instruction not implemented" };
475}
476
477void
478Math_norm2(vm_registers_t &r, vm_t &vm)
479{
480 auto &fp = r.stack.peek_frame()->base<F_Math_norm2>();
481 throw vm_system_exception{ "Instruction not implemented" };
482}
483
484void
485Math_pow(vm_registers_t &r, vm_t &vm)
486{
487 auto &fp = r.stack.peek_frame()->base<F_Math_pow>();
488 throw vm_system_exception{ "Instruction not implemented" };
489}
490
491void
492Math_pow10(vm_registers_t &r, vm_t &vm)
493{
494 auto &fp = r.stack.peek_frame()->base<F_Math_pow10>();
495 throw vm_system_exception{ "Instruction not implemented" };
496}
497
498void
499Math_realbits32(vm_registers_t &r, vm_t &vm)
500{
501 auto &fp = r.stack.peek_frame()->base<F_Math_realbits32>();
502 throw vm_system_exception{ "Instruction not implemented" };
503}
504
505void
506Math_realbits64(vm_registers_t &r, vm_t &vm)
507{
508 auto &fp = r.stack.peek_frame()->base<F_Math_realbits64>();
509 throw vm_system_exception{ "Instruction not implemented" };
510}
511
512void
513Math_remainder(vm_registers_t &r, vm_t &vm)
514{
515 auto &fp = r.stack.peek_frame()->base<F_Math_remainder>();
516 throw vm_system_exception{ "Instruction not implemented" };
517}
518
519void
520Math_rint(vm_registers_t &r, vm_t &vm)
521{
522 auto &fp = r.stack.peek_frame()->base<F_Math_rint>();
523 throw vm_system_exception{ "Instruction not implemented" };
524}
525
526void
527Math_scalbn(vm_registers_t &r, vm_t &vm)
528{
529 auto &fp = r.stack.peek_frame()->base<F_Math_scalbn>();
530 throw vm_system_exception{ "Instruction not implemented" };
531}
532
533void
534Math_sin(vm_registers_t &r, vm_t &vm)
535{
536 auto &fp = r.stack.peek_frame()->base<F_Math_sin>();
537 throw vm_system_exception{ "Instruction not implemented" };
538}
539
540void
541Math_sinh(vm_registers_t &r, vm_t &vm)
542{
543 auto &fp = r.stack.peek_frame()->base<F_Math_sinh>();
544 throw vm_system_exception{ "Instruction not implemented" };
545}
546
547void
548Math_sort(vm_registers_t &r, vm_t &vm)
549{
550 auto &fp = r.stack.peek_frame()->base<F_Math_sort>();
551 throw vm_system_exception{ "Instruction not implemented" };
552}
553
554void
555Math_sqrt(vm_registers_t &r, vm_t &vm)
556{
557 auto &fp = r.stack.peek_frame()->base<F_Math_sqrt>();
558 throw vm_system_exception{ "Instruction not implemented" };
559}
560
561void
562Math_tan(vm_registers_t &r, vm_t &vm)
563{
564 auto &fp = r.stack.peek_frame()->base<F_Math_tan>();
565 throw vm_system_exception{ "Instruction not implemented" };
566}
567
568void
569Math_tanh(vm_registers_t &r, vm_t &vm)
570{
571 auto &fp = r.stack.peek_frame()->base<F_Math_tanh>();
572 throw vm_system_exception{ "Instruction not implemented" };
573}
574
575void
576Math_y0(vm_registers_t &r, vm_t &vm)
577{
578 auto &fp = r.stack.peek_frame()->base<F_Math_y0>();
579 throw vm_system_exception{ "Instruction not implemented" };
580}
581
582void
583Math_y1(vm_registers_t &r, vm_t &vm)
584{
585 auto &fp = r.stack.peek_frame()->base<F_Math_y1>();
586 throw vm_system_exception{ "Instruction not implemented" };
587}
588
589void
590Math_yn(vm_registers_t &r, vm_t &vm)
591{
592 auto &fp = r.stack.peek_frame()->base<F_Math_yn>();
593 throw vm_system_exception{ "Instruction not implemented" };
594}