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+
9+using namespace disvm;
10+using namespace disvm::runtime;
11+
12+namespace
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+
89+void
90+Mathmodinit(void)
91+{
92+ builtin::register_module_exports(Math_PATH, Mathmodlen, Mathmodtab);
93+}
94+
95+void
96+Math_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+
104+void
105+Math_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+
111+void
112+Math_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+
118+void
119+Math_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+
125+void
126+Math_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+
132+void
133+Math_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+
139+void
140+Math_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+
146+void
147+Math_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+
153+void
154+Math_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+
160+void
161+Math_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+
167+void
168+Math_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+
174+void
175+Math_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+
181+void
182+Math_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+
188+void
189+Math_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+
195+void
196+Math_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+
202+void
203+Math_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+
209+void
210+Math_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+
216+void
217+Math_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+
223+void
224+Math_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+
230+void
231+Math_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+
237+void
238+Math_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+
244+void
245+Math_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+
251+void
252+Math_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+
258+void
259+Math_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+
265+void
266+Math_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+
272+void
273+Math_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+
279+void
280+Math_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+
286+void
287+Math_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+
293+void
294+Math_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+
300+void
301+Math_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+
307+void
308+Math_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+
314+void
315+Math_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+
321+void
322+Math_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+
328+void
329+Math_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+
335+void
336+Math_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+
342+void
343+Math_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+
349+void
350+Math_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+
356+void
357+Math_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+
363+void
364+Math_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+
393+void
394+Math_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+
400+void
401+Math_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+
407+void
408+Math_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+
414+void
415+Math_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+
421+void
422+Math_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+
428+void
429+Math_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+
435+void
436+Math_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+
442+void
443+Math_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+
449+void
450+Math_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+
456+void
457+Math_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+
463+void
464+Math_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+
470+void
471+Math_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+
477+void
478+Math_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+
484+void
485+Math_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+
491+void
492+Math_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+
498+void
499+Math_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+
505+void
506+Math_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+
512+void
513+Math_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+
519+void
520+Math_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+
526+void
527+Math_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+
533+void
534+Math_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+
540+void
541+Math_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+
547+void
548+Math_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+
554+void
555+Math_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+
561+void
562+Math_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+
568+void
569+Math_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+
575+void
576+Math_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+
582+void
583+Math_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+
589+void
590+Math_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+}