History

Implement built-in Math module functions needed for limbo compiler

arr committed 9 years ago

Commit 1a8be841 parent 84ac3311 file · +59 −9Browse files

Unified · Split

src/vm/math/Mathmod.cpp+59−9
@@ -161,14 +161,25 @@ void · 160 unmodified lines
161161 Math_bits32real(vm_registers_t &r, vm_t &vm)
162162 {
163163 auto &fp = r.stack.peek_frame()->base<F_Math_bits32real>();
164− throw vm_system_exception{ "Instruction not implemented" };
164+
165+ auto x = fp.b;
166+ float t;
167+ std::memcpy(&t, &x, sizeof(x));
168+
169+ *fp.ret = real_t{ t };
165170 }
166171
167172 void
168173 Math_bits64real(vm_registers_t &r, vm_t &vm)
169174 {
170175 auto &fp = r.stack.peek_frame()->base<F_Math_bits64real>();
171− throw vm_system_exception{ "Instruction not implemented" };
176+
177+ auto x = fp.b;
178+ real_t t;
179+ std::memcpy(&t, &x, sizeof(x));
180+ static_assert(sizeof(t) == sizeof(x), "Real should be 64-bits");
181+
182+ *fp.ret = t;
172183 }
173184
174185 void
@@ -252,7 +263,35 @@ void · 77 unmodified lines
252263 Math_export_real(vm_registers_t &r, vm_t &vm)
253264 {
254265 auto &fp = r.stack.peek_frame()->base<F_Math_export_real>();
255− throw vm_system_exception{ "Instruction not implemented" };
266+
267+ auto buffer = vm_alloc_t::from_allocation<vm_array_t>(fp.b);
268+ assert(buffer->get_element_type()->size_in_bytes == intrinsic_type_desc::type<byte_t>()->size_in_bytes);
269+
270+ auto values = vm_alloc_t::from_allocation<vm_array_t>(fp.x);
271+ assert(values->get_element_type()->size_in_bytes == intrinsic_type_desc::type<real_t>()->size_in_bytes);
272+ const auto values_len = values->get_length();
273+
274+ if (buffer->get_length() != (8 * values_len))
275+ throw vm_user_exception{ "Invalid buffer size based on conversion" };
276+
277+ // Insert as big endian
278+ for (int i = 0; i < values_len; ++i)
279+ {
280+ auto x = values->at<real_t>(i);
281+ uint64_t t;
282+ std::memcpy(&t, &x, sizeof(x));
283+ static_assert(sizeof(t) == sizeof(x), "Real should be 64-bits");
284+
285+ auto base = reinterpret_cast<byte_t *>(buffer->at(i * sizeof(x)));
286+ base[0] = static_cast<byte_t>(t >> 56);
287+ base[1] = static_cast<byte_t>(t >> 48);
288+ base[2] = static_cast<byte_t>(t >> 40);
289+ base[3] = static_cast<byte_t>(t >> 32);
290+ base[4] = static_cast<byte_t>(t >> 24);
291+ base[5] = static_cast<byte_t>(t >> 16);
292+ base[6] = static_cast<byte_t>(t >> 8);
293+ base[7] = static_cast<byte_t>(t);
294+ }
256295 }
257296
258297 void
@@ -382,11 +421,11 @@ Math_import_real(vm_registers_t &r, vm_t &vm) · 123 unmodified lines
382421 for (int j = 1; j < 8; ++j)
383422 t = (t << 8) | buffer->at<byte_t>(j);
384423
385− auto r = real_t{};
386− std::memcpy(&r, &t, sizeof(t));
387− static_assert(sizeof(t) == sizeof(r), "Real should be 64-bits");
424+ real_t x;
425+ std::memcpy(&x, &t, sizeof(t));
426+ static_assert(sizeof(t) == sizeof(x), "Real should be 64-bits");
388427
389− result->at<real_t>(i) = r;
428+ result->at<real_t>(i) = x;
390429 }
391430 }
392431
@@ -499,14 +538,25 @@ void · 106 unmodified lines
499538 Math_realbits32(vm_registers_t &r, vm_t &vm)
500539 {
501540 auto &fp = r.stack.peek_frame()->base<F_Math_realbits32>();
502− throw vm_system_exception{ "Instruction not implemented" };
541+
542+ auto x = static_cast<float>(fp.x);
543+ uint32_t t;
544+ std::memcpy(&t, &x, sizeof(x));
545+
546+ *fp.ret = t;
503547 }
504548
505549 void
506550 Math_realbits64(vm_registers_t &r, vm_t &vm)
507551 {
508552 auto &fp = r.stack.peek_frame()->base<F_Math_realbits64>();
509− throw vm_system_exception{ "Instruction not implemented" };
553+
554+ auto x = fp.x;
555+ uint64_t t;
556+ std::memcpy(&t, &x, sizeof(x));
557+ static_assert(sizeof(t) == sizeof(x), "Real should be 64-bits");
558+
559+ *fp.ret = t;
510560 }
511561
512562 void

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t