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
161Math_bits32real(vm_registers_t &r, vm_t &vm)161Math_bits32real(vm_registers_t &r, vm_t &vm)
162{162{
163 auto &fp = r.stack.peek_frame()->base<F_Math_bits32real>();163 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 };
165}170}
166 171
167void172void
168Math_bits64real(vm_registers_t &r, vm_t &vm)173Math_bits64real(vm_registers_t &r, vm_t &vm)
169{174{
170 auto &fp = r.stack.peek_frame()->base<F_Math_bits64real>();175 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;
172}183}
173 184
174void185void
@@ -252,7 +263,35 @@ void · 77 unmodified lines
252Math_export_real(vm_registers_t &r, vm_t &vm)263Math_export_real(vm_registers_t &r, vm_t &vm)
253{264{
254 auto &fp = r.stack.peek_frame()->base<F_Math_export_real>();265 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 }
256}295}
257 296
258void297void
@@ -382,11 +421,11 @@ Math_import_real(vm_registers_t &r, vm_t &vm) · 123 unmodified lines
382 for (int j = 1; j < 8; ++j)421 for (int j = 1; j < 8; ++j)
383 t = (t << 8) | buffer->at<byte_t>(j);422 t = (t << 8) | buffer->at<byte_t>(j);
384 423
385 auto r = real_t{};424 real_t x;
386 std::memcpy(&r, &t, sizeof(t));425 std::memcpy(&x, &t, sizeof(t));
387 static_assert(sizeof(t) == sizeof(r), "Real should be 64-bits");426 static_assert(sizeof(t) == sizeof(x), "Real should be 64-bits");
388 427
389 result->at<real_t>(i) = r;428 result->at<real_t>(i) = x;
390 }429 }
391}430}
392 431
@@ -499,14 +538,25 @@ void · 106 unmodified lines
499Math_realbits32(vm_registers_t &r, vm_t &vm)538Math_realbits32(vm_registers_t &r, vm_t &vm)
500{539{
501 auto &fp = r.stack.peek_frame()->base<F_Math_realbits32>();540 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;
503}547}
504 548
505void549void
506Math_realbits64(vm_registers_t &r, vm_t &vm)550Math_realbits64(vm_registers_t &r, vm_t &vm)
507{551{
508 auto &fp = r.stack.peek_frame()->base<F_Math_realbits64>();552 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;
510}560}
511 561
512void562void

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