Implement FMOD function for fixed-point modulo

Fixes #1021
This commit is contained in:
Rangi
2022-08-27 12:40:55 -04:00
committed by Eldred Habert
parent 1a1f1365e6
commit 425339ccf6
6 changed files with 19 additions and 0 deletions

View File

@@ -20,6 +20,7 @@ int32_t fix_ACos(int32_t i);
int32_t fix_ATan(int32_t i); int32_t fix_ATan(int32_t i);
int32_t fix_ATan2(int32_t i, int32_t j); int32_t fix_ATan2(int32_t i, int32_t j);
int32_t fix_Mul(int32_t i, int32_t j); int32_t fix_Mul(int32_t i, int32_t j);
int32_t fix_Mod(int32_t i, int32_t j);
int32_t fix_Div(int32_t i, int32_t j); int32_t fix_Div(int32_t i, int32_t j);
int32_t fix_Pow(int32_t i, int32_t j); int32_t fix_Pow(int32_t i, int32_t j);
int32_t fix_Log(int32_t i, int32_t j); int32_t fix_Log(int32_t i, int32_t j);

View File

@@ -319,6 +319,7 @@ delim $$
.It Sy Name Ta Sy Operation .It Sy Name Ta Sy Operation
.It Fn DIV x y Ta $x \[di] y$ .It Fn DIV x y Ta $x \[di] y$
.It Fn MUL x y Ta $x \[mu] y$ .It Fn MUL x y Ta $x \[mu] y$
.It Fn FMOD x y Ta $x % y$
.It Fn POW x y Ta $x$ to the $y$ power .It Fn POW x y Ta $x$ to the $y$ power
.It Fn LOG x y Ta Logarithm of $x$ to the base $y$ .It Fn LOG x y Ta Logarithm of $x$ to the base $y$
.It Fn ROUND x Ta Round $x$ to the nearest integer .It Fn ROUND x Ta Round $x$ to the nearest integer

View File

@@ -119,6 +119,14 @@ int32_t fix_Div(int32_t i, int32_t j)
return double2fix(fix2double(i) / fix2double(j)); return double2fix(fix2double(i) / fix2double(j));
} }
/*
* Modulo
*/
int32_t fix_Mod(int32_t i, int32_t j)
{
return double2fix(fmod(fix2double(i), fix2double(j)));
}
/* /*
* Power * Power
*/ */

View File

@@ -181,6 +181,7 @@ static struct KeywordMapping {
{"FLOOR", T_OP_FLOOR}, {"FLOOR", T_OP_FLOOR},
{"DIV", T_OP_FDIV}, {"DIV", T_OP_FDIV},
{"MUL", T_OP_FMUL}, {"MUL", T_OP_FMUL},
{"FMOD", T_OP_FMOD},
{"POW", T_OP_POW}, {"POW", T_OP_POW},
{"LOG", T_OP_LOG}, {"LOG", T_OP_LOG},
{"SIN", T_OP_SIN}, {"SIN", T_OP_SIN},

View File

@@ -558,6 +558,7 @@ enum {
%token T_OP_ASIN "ASIN" T_OP_ACOS "ACOS" T_OP_ATAN "ATAN" T_OP_ATAN2 "ATAN2" %token T_OP_ASIN "ASIN" T_OP_ACOS "ACOS" T_OP_ATAN "ATAN" T_OP_ATAN2 "ATAN2"
%token T_OP_FDIV "FDIV" %token T_OP_FDIV "FDIV"
%token T_OP_FMUL "FMUL" %token T_OP_FMUL "FMUL"
%token T_OP_FMOD "FMOD"
%token T_OP_POW "POW" %token T_OP_POW "POW"
%token T_OP_LOG "LOG" %token T_OP_LOG "LOG"
%token T_OP_ROUND "ROUND" %token T_OP_ROUND "ROUND"
@@ -1488,6 +1489,9 @@ relocexpr_no_str : scoped_anon_id { rpn_Symbol(&$$, $1); }
| T_OP_FMUL T_LPAREN const T_COMMA const T_RPAREN { | T_OP_FMUL T_LPAREN const T_COMMA const T_RPAREN {
rpn_Number(&$$, fix_Mul($3, $5)); rpn_Number(&$$, fix_Mul($3, $5));
} }
| T_OP_FMOD T_LPAREN const T_COMMA const T_RPAREN {
rpn_Number(&$$, fix_Mod($3, $5));
}
| T_OP_POW T_LPAREN const T_COMMA const T_RPAREN { | T_OP_POW T_LPAREN const T_COMMA const T_RPAREN {
rpn_Number(&$$, fix_Pow($3, $5)); rpn_Number(&$$, fix_Pow($3, $5));
} }

View File

@@ -23,6 +23,10 @@ ENDM
assert MUL(10.0, 0.5) == 5.0 assert MUL(10.0, 0.5) == 5.0
assert MUL(10.0, 0.0) == 0.0 assert MUL(10.0, 0.0) == 0.0
assert FMOD(5.0, 2.0) == 1.0
assert FMOD(-5.0, 2.0) == -1.0
assert FMOD(-5.0, 0.0) == $8000_0000
assert POW(10.0, 2.0) == 100.0 assert POW(10.0, 2.0) == 100.0
assert POW(100.0, 0.5) == 10.0 assert POW(100.0, 0.5) == 10.0