Fix comparing with zero (added tolerance epsilon), added timestamp

This commit is contained in:
Stefan Bühler 2009-06-26 23:58:27 +02:00
parent 67b60c19df
commit 04795f0a7a
4 changed files with 28 additions and 12 deletions

View File

@ -49,10 +49,6 @@ ovm_t* ovm_load(const gchar *filename) {
return ovm; return ovm;
} }
static const gchar* cmp_op_strings[] = {
"<", "<=", "==", ">=", ">"
};
void ovm_print_c(ovm_t *ovm, const gchar *filename) { void ovm_print_c(ovm_t *ovm, const gchar *filename) {
guint i; guint i;
instruction_t *oi = ovm->instructions; instruction_t *oi = ovm->instructions;
@ -81,7 +77,23 @@ void ovm_print_c(ovm_t *ovm, const gchar *filename) {
g_string_truncate(str, 0); g_string_truncate(str, 0);
break; break;
case SOP_CMPZ: case SOP_CMPZ:
g_string_printf(str, "\tovm_status = (v%u.d %s 0.0);\n", USED(instr_sop_r1(oi[i])), cmp_op_strings[instr_sop_cmp(oi[i])]); switch (instr_sop_cmp(oi[i])) {
case CMP_LTZ:
g_string_printf(str, "\tovm_status = (v%u.d < -eps);\n", USED(instr_sop_r1(oi[i])));
break;
case CMP_LEZ:
g_string_printf(str, "\tovm_status = (v%u.d <= eps);\n", USED(instr_sop_r1(oi[i])));
break;
case CMP_EQZ:
g_string_printf(str, "\tovm_status = (fabs(v%u.d) <= eps);\n", USED(instr_sop_r1(oi[i])));
break;
case CMP_GEZ:
g_string_printf(str, "\tovm_status = (v%u.d > eps);\n", USED(instr_sop_r1(oi[i])));
break;
case CMP_GTZ:
g_string_printf(str, "\tovm_status = (v%u.d >= -eps);\n", USED(instr_sop_r1(oi[i])));
break;
}
break; break;
case SOP_SQRT: case SOP_SQRT:
g_string_printf(str, "\tv%u.d = sqrt(v%u.d);\n", USED(i), USED(instr_sop_r1(oi[i]))); g_string_printf(str, "\tv%u.d = sqrt(v%u.d);\n", USED(i), USED(instr_sop_r1(oi[i])));
@ -108,7 +120,7 @@ void ovm_print_c(ovm_t *ovm, const gchar *filename) {
g_string_printf(str, "\tv%u.d = v%u.d * v%u.d;\n", USED(i), USED(instr_dop_r1(oi[i])), USED(instr_dop_r2(oi[i]))); g_string_printf(str, "\tv%u.d = v%u.d * v%u.d;\n", USED(i), USED(instr_dop_r1(oi[i])), USED(instr_dop_r2(oi[i])));
break; break;
case OP_DIV: case OP_DIV:
g_string_printf(str, "\tv%u.d = (v%u.d == 0.0) ? 0.0 : v%u.d / v%u.d;\n", USED(i), USED(instr_dop_r2(oi[i])), USED(instr_dop_r1(oi[i])), instr_dop_r2(oi[i])); g_string_printf(str, "\tv%u.d = (fabs(v%u.d) < eps) ? 0.0 : v%u.d / v%u.d;\n", USED(i), USED(instr_dop_r2(oi[i])), USED(instr_dop_r1(oi[i])), instr_dop_r2(oi[i]));
break; break;
case OP_OUT: case OP_OUT:
g_string_printf(str, "\tout[%u] = v%u.d;\n", instr_dop_r1(oi[i]), USED(instr_dop_r2(oi[i]))); g_string_printf(str, "\tout[%u] = v%u.d;\n", instr_dop_r1(oi[i]), USED(instr_dop_r2(oi[i])));
@ -132,7 +144,8 @@ void ovm_print_c(ovm_t *ovm, const gchar *filename) {
"\n" "\n"
"typedef union { gdouble d; guint64 i; } double_int;\n" "typedef union { gdouble d; guint64 i; } double_int;\n"
"\n" "\n"
"static gboolean ovm_status = FALSE;\n" "static gboolean ovm_status;\n"
"static const gdouble eps = 1e-300;\n"
"\n" "\n"
"static double_int v0" "static double_int v0"
)); ));
@ -150,6 +163,7 @@ void ovm_print_c(ovm_t *ovm, const gchar *filename) {
)); ));
for (i = 0; i < ovm->used; i++) { for (i = 0; i < ovm->used; i++) {
if (!usedv[i]) continue; if (!usedv[i]) continue;
/* g_string_printf(str, "\tv%u.d = %f;\n", i, vi[i]); */
g_string_printf(str, "\tv%u.i = G_GUINT64_CONSTANT(%" G_GUINT64_FORMAT "); /* %f */ \n", i, uvi[i], vi[i]); g_string_printf(str, "\tv%u.i = G_GUINT64_CONSTANT(%" G_GUINT64_FORMAT "); /* %f */ \n", i, uvi[i], vi[i]);
write(f, GSTR_LEN(str)); write(f, GSTR_LEN(str));
} }

View File

@ -11,6 +11,7 @@ void task_free(task_t *task) {
task_t* task_new(gdouble scenario, guint in_size, guint out_size) { task_t* task_new(gdouble scenario, guint in_size, guint out_size) {
task_t *task = g_slice_new(task_t); task_t *task = g_slice_new(task_t);
task->scenario = scenario; task->scenario = scenario;
task->timestamp = 0;
task->in_size = in_size; task->in_size = in_size;
task->out_size = out_size; task->out_size = out_size;
task->in = g_slice_alloc0(task->in_size * sizeof(gdouble)); task->in = g_slice_alloc0(task->in_size * sizeof(gdouble));

View File

@ -5,6 +5,7 @@
typedef struct { typedef struct {
gdouble scenario; gdouble scenario;
guint64 timestamp;
guint in_size, out_size; guint in_size, out_size;
gdouble *in, *out; gdouble *in, *out;
} task_t; } task_t;
@ -19,7 +20,7 @@ void ovm_step(gdouble scenario, gdouble *in, gdouble *out);
static inline gdouble task_run(task_t *task, task_app_t app) { static inline gdouble task_run(task_t *task, task_app_t app) {
ovm_init(); ovm_init();
while (task->out[0] == 0.0) { for ( ; task->out[0] == 0.0 && task->timestamp < 3000000; task->timestamp++) {
app(task); app(task);
ovm_step(task->scenario, task->in, task->out); ovm_step(task->scenario, task->in, task->out);
} }

View File

@ -3,9 +3,9 @@
static void debug(task_t * task) { static void debug(task_t * task) {
gdouble *o = task->out; gdouble *o = task->out;
g_debug( printf(
"Score: %5f Fuel: %5f x: %5f y: %5f r: %5f\n", "Step: %9"G_GUINT64_FORMAT" Score: %5f Fuel: %5f x: %5f y: %5f r: %5f\n",
o[0], o[1], o[2], o[3], o[4]); task->timestamp, o[0], o[1], o[2], o[3], o[4]);
} }
static void run(task_t *task) { static void run(task_t *task) {
@ -19,7 +19,7 @@ int main(int argc, char **argv) {
UNUSED(argc); UNUSED(argc);
UNUSED(argv); UNUSED(argv);
task = task_new(1001.0, 4, 5); task = task_new(1002.0, 4, 5);
score = task_run(task, run); score = task_run(task, run);
printf("Final Score: %f\n", score); printf("Final Score: %f\n", score);
return 0; return 0;