From: bones_was_here Date: Fri, 21 Jul 2023 08:34:28 +0000 (+1000) Subject: mod() builtin: match gmqcc and fteqw X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=3c88117e0e804df932c17735fe400ebe1eebf193;p=xonotic%2Fdarkplaces.git mod() builtin: match gmqcc and fteqw Also catch div0, he escaped again Signed-off-by: bones_was_here --- diff --git a/prvm_cmds.c b/prvm_cmds.c index fc39fa33..6535ea0a 100644 --- a/prvm_cmds.c +++ b/prvm_cmds.c @@ -3064,13 +3064,21 @@ float mod(float val, float m) */ void VM_modulo(prvm_prog_t *prog) { - prvm_int_t val, m; + vec_t val, m; + VM_SAFEPARMCOUNT(2, VM_modulo); - val = (prvm_int_t) PRVM_G_FLOAT(OFS_PARM0); - m = (prvm_int_t) PRVM_G_FLOAT(OFS_PARM1); + val = PRVM_G_FLOAT(OFS_PARM0); + m = PRVM_G_FLOAT(OFS_PARM1); - PRVM_G_FLOAT(OFS_RETURN) = (prvm_vec_t) (val % m); + // matches how gmqcc implements % when mod() builtin isn't defined, and FTEQW mod() + if (m) + PRVM_G_FLOAT(OFS_RETURN) = val - m * (prvm_int_t)(val / m); + else + { + VM_Warning(prog, "Attempted modulo of %f by zero\n", val); + PRVM_G_FLOAT(OFS_RETURN) = 0; + } } static void VM_Search_Init(prvm_prog_t *prog) diff --git a/prvm_execprogram.h b/prvm_execprogram.h index d9331426..99a89906 100644 --- a/prvm_execprogram.h +++ b/prvm_execprogram.h @@ -377,11 +377,8 @@ int i; } else { - if (developer.integer) - { - PRE_ERROR(); - VM_Warning(prog, "Attempted division by zero in %s\n", prog->name ); - } + PRE_ERROR(); + VM_Warning(prog, "Attempted division of %f by zero\n", OPA->_float); OPC->_float = 0.0f; } DISPATCH_OPCODE();