Skip to content

Commit

Permalink
better floats
Browse files Browse the repository at this point in the history
  • Loading branch information
ShawSumma committed Dec 18, 2023
1 parent c56fd27 commit 82d297d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
17 changes: 10 additions & 7 deletions vm/backend/tb.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ size_t vm_tb_ptr_len = 0;
void **vm_tb_ptr_globals = NULL;
size_t vm_tb_ptr_alloc = 0;

// void GC_add_roots(void *, void *);

TB_Node *vm_tb_ptr_name(vm_tb_state_t *state, const char *name, void *value) {
if (value != NULL) {
if (vm_tb_ptr_len + 1 >= vm_tb_ptr_alloc) {
vm_tb_ptr_alloc = (vm_tb_ptr_len + 1) * 2;
vm_tb_ptr_globals = vm_realloc(vm_tb_ptr_globals, sizeof(void *) * vm_tb_ptr_alloc);
}
vm_tb_ptr_globals[vm_tb_ptr_len++] = value;
}
// if (value != NULL) {
// if (vm_tb_ptr_len + 1 >= vm_tb_ptr_alloc) {
// vm_tb_ptr_alloc = (vm_tb_ptr_len + 1) * 2;
// vm_tb_ptr_globals = vm_realloc(vm_tb_ptr_globals, sizeof(void *) * vm_tb_ptr_alloc);
// GC_add_roots(vm_tb_ptr_globals, &vm_tb_ptr_globals[vm_tb_ptr_alloc]);
// }
// vm_tb_ptr_globals[vm_tb_ptr_len++] = value;
// }
return tb_inst_uint(state->fun, TB_TYPE_PTR, (uint64_t)value);
}

Expand Down
1 change: 1 addition & 0 deletions vm/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#define VM_USE_DUMP 1
#define VM_NO_TAILCALL 1
#define VM_FORMAT_FLOAT "%.14g"

struct vm_config_t;
typedef struct vm_config_t vm_config_t;
Expand Down
10 changes: 5 additions & 5 deletions vm/std/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ void vm_io_print_lit(vm_io_buffer_t *out, vm_std_value_t value) {
break;
}
case VM_TAG_F32: {
vm_io_buffer_format(out, "%f", value.value.f32);
vm_io_buffer_format(out, VM_FORMAT_FLOAT, value.value.f32);
break;
}
case VM_TAG_F64: {
vm_io_buffer_format(out, "%f", value.value.f64);
vm_io_buffer_format(out, VM_FORMAT_FLOAT, value.value.f64);
break;
}
case VM_TAG_FFI: {
Expand Down Expand Up @@ -172,12 +172,12 @@ void vm_io_debug(vm_io_buffer_t *out, size_t indent, const char *prefix, vm_std_
}
case VM_TAG_F32: {
vm_indent(out, indent, prefix);
vm_io_buffer_format(out, "%f\n", value.value.f32);
vm_io_buffer_format(out, VM_FORMAT_FLOAT "\n", value.value.f32);
break;
}
case VM_TAG_F64: {
vm_indent(out, indent, prefix);
vm_io_buffer_format(out, "%f\n", value.value.f64);
vm_io_buffer_format(out, VM_FORMAT_FLOAT "\n", value.value.f64);
break;
}
case VM_TAG_STR: {
Expand Down Expand Up @@ -285,7 +285,7 @@ void vm_io_debug(vm_io_buffer_t *out, size_t indent, const char *prefix, vm_std_
.value = p.val_val,
};
char buf[64];
snprintf(buf, 63, "%f = ", p.key_val.f64);
snprintf(buf, 63, VM_FORMAT_FLOAT " = ", p.key_val.f64);
vm_io_debug(out, indent + 1, buf, val, &next);
break;
}
Expand Down
4 changes: 2 additions & 2 deletions vm/std/std.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,11 @@ void vm_value_buffer_tostring(vm_io_buffer_t *buf, vm_std_value_t value) {
break;
}
case VM_TAG_F32: {
vm_io_buffer_format(buf, "%f", value.value.f32);
vm_io_buffer_format(buf, VM_FORMAT_FLOAT, value.value.f32);
break;
}
case VM_TAG_F64: {
vm_io_buffer_format(buf, "%f", value.value.f64);
vm_io_buffer_format(buf, VM_FORMAT_FLOAT, value.value.f64);
break;
}
case VM_TAG_STR: {
Expand Down

0 comments on commit 82d297d

Please sign in to comment.