From 13b608f40c890578a5805b5f1127306eb1d590b8 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Fri, 14 Feb 2020 13:54:26 +0900 Subject: [PATCH] Don't truncat float % float This allows full usage, eg, x % pi, but otherwise maintains compatibility with integer % --- libs/gamecode/pr_exec.c | 6 +++++- tools/qfcc/source/expr_binary.c | 26 ++++---------------------- 2 files changed, 9 insertions(+), 23 deletions(-) diff --git a/libs/gamecode/pr_exec.c b/libs/gamecode/pr_exec.c index 56d5b0401..27c3f9c50 100644 --- a/libs/gamecode/pr_exec.c +++ b/libs/gamecode/pr_exec.c @@ -1337,7 +1337,11 @@ op_call: OPC.integer_var = OPA.integer_var % OPB.integer_var; break; case OP_MOD_F: - OPC.float_var = (int) OPA.float_var % (int) OPB.float_var; + { + float a = OPA.float_var; + float b = OPB.float_var; + OPC.float_var = a - b * truncf (a / b); + } break; case OP_CONV_IF: OPC.float_var = OPA.integer_var; diff --git a/tools/qfcc/source/expr_binary.c b/tools/qfcc/source/expr_binary.c index 103cd1e7d..6dd7c1177 100644 --- a/tools/qfcc/source/expr_binary.c +++ b/tools/qfcc/source/expr_binary.c @@ -628,32 +628,14 @@ reimplement_binary_expr (int op, expr_t *e1, expr_t *e2) switch (op) { case '%': { - expr_t *tmp1, *tmp2, *tmp3, *tmp4, *t1, *t2; + expr_t *tmp1, *tmp2; e = new_block_expr (); - t1 = new_temp_def_expr (&type_float); - t2 = new_temp_def_expr (&type_float); tmp1 = new_temp_def_expr (&type_float); tmp2 = new_temp_def_expr (&type_float); - tmp3 = new_temp_def_expr (&type_float); - tmp4 = new_temp_def_expr (&type_float); - append_expr (e, assign_expr (t1, e1)); - e1 = binary_expr ('&', t1, t1); - append_expr (e, assign_expr (tmp1, e1)); - - append_expr (e, assign_expr (t2, e2)); - e2 = binary_expr ('&', t2, t2); - append_expr (e, assign_expr (tmp2, e2)); - - e1 = binary_expr ('/', tmp1, tmp2); - append_expr (e, assign_expr (tmp3, e1)); - - e2 = binary_expr ('&', tmp3, tmp3); - append_expr (e, assign_expr (tmp4, e2)); - - e1 = binary_expr ('*', tmp2, tmp4); - e2 = binary_expr ('-', tmp1, e1); - e->e.block.result = e2; + append_expr (e, assign_expr (tmp1, binary_expr ('/', e1, e2))); + append_expr (e, assign_expr (tmp2, binary_expr ('&', tmp1, tmp1))); + e->e.block.result = binary_expr ('-', e1, binary_expr ('*', e2, tmp2)); return e; } break;