From 4269c8cb07db4748b444e0f191901bef49cf33ec Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Sun, 16 Feb 2020 11:04:30 +0900 Subject: [PATCH] Rename the mod instruction to rem Because % really implements remainder rather than true modulo, and I plan on adding %% to implement true modulo. --- include/QF/pr_comp.h | 6 +++--- libs/gamecode/pr_exec.c | 10 +++++----- libs/gamecode/pr_opcode.c | 6 +++--- tools/qfcc/test/modulo.r | 20 ++++++++++++++++++-- 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/include/QF/pr_comp.h b/include/QF/pr_comp.h index 122de60b9..0fb09da41 100644 --- a/include/QF/pr_comp.h +++ b/include/QF/pr_comp.h @@ -181,8 +181,8 @@ typedef enum { OP_SHL_I, OP_SHR_I, - OP_MOD_F, - OP_MOD_I, + OP_REM_F, + OP_REM_I, OP_LOADB_F, OP_LOADB_V, @@ -371,7 +371,7 @@ typedef enum { OP_MUL_VD, OP_MUL_DV, OP_DIV_D, - OP_MOD_D, + OP_REM_D, OP_GE_D, OP_LE_D, OP_GT_D, diff --git a/libs/gamecode/pr_exec.c b/libs/gamecode/pr_exec.c index 3ea2c3bd5..8cb6ecbb8 100644 --- a/libs/gamecode/pr_exec.c +++ b/libs/gamecode/pr_exec.c @@ -318,8 +318,8 @@ signal_hook (int sig, void *data) else OPC.integer_var = 0x7fffffff; return 1; - case OP_MOD_I: - case OP_MOD_F: + case OP_REM_I: + case OP_REM_F: OPC.integer_var = 0x00000000; return 1; default: @@ -1429,17 +1429,17 @@ op_call: case OP_DIV_I: OPC.integer_var = OPA.integer_var / OPB.integer_var; break; - case OP_MOD_I: + case OP_REM_I: OPC.integer_var = OPA.integer_var % OPB.integer_var; break; - case OP_MOD_D: + case OP_REM_D: { double a = OPA_double_var; double b = OPB_double_var; OPC_double_var = a - b * trunc (a / b); } break; - case OP_MOD_F: + case OP_REM_F: { float a = OPA.float_var; float b = OPB.float_var; diff --git a/libs/gamecode/pr_opcode.c b/libs/gamecode/pr_opcode.c index f18fd2b42..01dd172ae 100644 --- a/libs/gamecode/pr_opcode.c +++ b/libs/gamecode/pr_opcode.c @@ -171,7 +171,7 @@ VISIBLE opcode_t pr_opcodes[] = { ev_double, ev_double, ev_double, PROG_VERSION, }, - {"%", "mod.d", OP_MOD_D, false, + {"%", "rem.d", OP_REM_D, false, ev_double, ev_double, ev_double, PROG_VERSION, }, @@ -1036,7 +1036,7 @@ VISIBLE opcode_t pr_opcodes[] = { ev_integer, ev_integer, ev_integer, PROG_VERSION, }, - {"%", "mod.i", OP_MOD_I, false, + {"%", "rem.i", OP_REM_I, false, ev_integer, ev_integer, ev_integer, PROG_VERSION, }, @@ -1049,7 +1049,7 @@ VISIBLE opcode_t pr_opcodes[] = { PROG_VERSION, }, - {"%", "mod.f", OP_MOD_F, false, + {"%", "rem.f", OP_REM_F, false, ev_float, ev_float, ev_float, PROG_VERSION, }, diff --git a/tools/qfcc/test/modulo.r b/tools/qfcc/test/modulo.r index 1e02fcaa0..81366c134 100644 --- a/tools/qfcc/test/modulo.r +++ b/tools/qfcc/test/modulo.r @@ -1,11 +1,19 @@ -#pragma traditional +void printf (string ftm, ...) = #0; -void (...) printf = #0; +float snafu (float a, float b) +{ + float c = a % b; + return c; +} + +#pragma traditional float foo (float a, float b) { float c = a % b; return c; } +#pragma advanced + float bar (float a, float b) { float c; @@ -15,11 +23,13 @@ float bar (float a, float b) return c; } +#pragma traditional float baz (float a, float b) { float c = (a + b) % (a - b); return c; } +#pragma advanced float test (string name, float (func)(float a, float b), float a, float b, float c) @@ -45,20 +55,26 @@ float main (void) res |= test ("foo", foo, 5, 3, 2); res |= test ("bar", bar, 5, 3, 2); res |= test ("baz", baz, 5, 3, 0); + res |= test ("snafu", snafu, 5, 3, 2); res |= test ("foo", foo, -5, 3, -2); res |= test ("bar", bar, -5, 3, -2); res |= test ("baz", baz, -5, 3, -2); + res |= test ("snafu", snafu, -5, 3, -2); res |= test ("foo", foo, 5, -3, 2); res |= test ("bar", bar, 5, -3, 2); res |= test ("baz", baz, 5, -3, 2); + res |= test ("snafu", snafu, 5, -3, 2); res |= test ("foo", foo, -5, -3, -2); res |= test ("bar", bar, -5, -3, -2); res |= test ("baz", baz, -5, -3, 0); + res |= test ("snafu", snafu, -5, -3, -2); res |= test ("foo", foo, 5, 3.5, 1.5); res |= test ("foo", foo, -5, 3.5, -1.5); + res |= test ("snafu", snafu, 5, 3.5, 1.5); + res |= test ("snafu", snafu, -5, 3.5, -1.5); return res; }