From 0774f5eb9d07a83600b10282a02a07e24bb9eb01 Mon Sep 17 00:00:00 2001 From: Rangi Date: Sun, 28 Feb 2021 16:30:48 -0500 Subject: [PATCH] Rename math.c/mymath.h to fixpoint.c/.h This also changes the functions' prefix from "math_" to "fix_". --- Makefile | 2 +- include/asm/fixpoint.h | 31 ++++++++ include/asm/mymath.h | 31 -------- src/CMakeLists.txt | 2 +- src/asm/fixpoint.c | 170 +++++++++++++++++++++++++++++++++++++++++ src/asm/math.c | 170 ----------------------------------------- src/asm/parser.y | 32 ++++---- src/asm/symbol.c | 4 +- 8 files changed, 221 insertions(+), 221 deletions(-) create mode 100644 include/asm/fixpoint.h delete mode 100644 include/asm/mymath.h create mode 100644 src/asm/fixpoint.c delete mode 100644 src/asm/math.c diff --git a/Makefile b/Makefile index 3bad5317..b6929666 100644 --- a/Makefile +++ b/Makefile @@ -54,12 +54,12 @@ all: rgbasm rgblink rgbfix rgbgfx rgbasm_obj := \ src/asm/charmap.o \ + src/asm/fixpoint.o \ src/asm/format.o \ src/asm/fstack.o \ src/asm/lexer.o \ src/asm/macro.o \ src/asm/main.o \ - src/asm/math.o \ src/asm/parser.o \ src/asm/opt.o \ src/asm/output.o \ diff --git a/include/asm/fixpoint.h b/include/asm/fixpoint.h new file mode 100644 index 00000000..b80c5964 --- /dev/null +++ b/include/asm/fixpoint.h @@ -0,0 +1,31 @@ +/* + * This file is part of RGBDS. + * + * Copyright (c) 1997-2021, Carsten Sorensen and RGBDS contributors. + * + * SPDX-License-Identifier: MIT + */ + +#ifndef RGBDS_ASM_FIXPOINT_H +#define RGBDS_ASM_FIXPOINT_H + +#include + +int32_t fix_Callback_PI(void); +void fix_Print(int32_t i); +int32_t fix_Sin(int32_t i); +int32_t fix_Cos(int32_t i); +int32_t fix_Tan(int32_t i); +int32_t fix_ASin(int32_t i); +int32_t fix_ACos(int32_t i); +int32_t fix_ATan(int32_t i); +int32_t fix_ATan2(int32_t i, int32_t j); +int32_t fix_Mul(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_Log(int32_t i, int32_t j); +int32_t fix_Round(int32_t i); +int32_t fix_Ceil(int32_t i); +int32_t fix_Floor(int32_t i); + +#endif /* RGBDS_ASM_FIXPOINT_H */ diff --git a/include/asm/mymath.h b/include/asm/mymath.h deleted file mode 100644 index f7c11aa6..00000000 --- a/include/asm/mymath.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * This file is part of RGBDS. - * - * Copyright (c) 1997-2018, Carsten Sorensen and RGBDS contributors. - * - * SPDX-License-Identifier: MIT - */ - -#ifndef RGBDS_ASM_MATH_H -#define RGBDS_ASM_MATH_H - -#include - -int32_t math_Callback_PI(void); -void math_Print(int32_t i); -int32_t math_Sin(int32_t i); -int32_t math_Cos(int32_t i); -int32_t math_Tan(int32_t i); -int32_t math_ASin(int32_t i); -int32_t math_ACos(int32_t i); -int32_t math_ATan(int32_t i); -int32_t math_ATan2(int32_t i, int32_t j); -int32_t math_Mul(int32_t i, int32_t j); -int32_t math_Div(int32_t i, int32_t j); -int32_t math_Pow(int32_t i, int32_t j); -int32_t math_Log(int32_t i, int32_t j); -int32_t math_Round(int32_t i); -int32_t math_Ceil(int32_t i); -int32_t math_Floor(int32_t i); - -#endif /* RGBDS_ASM_MATH_H */ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8a8293c2..88c6e867 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -44,12 +44,12 @@ BISON_TARGET(PARSER "asm/parser.y" set(rgbasm_src "${BISON_PARSER_OUTPUT_SOURCE}" "asm/charmap.c" + "asm/fixpoint.c" "asm/format.c" "asm/fstack.c" "asm/lexer.c" "asm/macro.c" "asm/main.c" - "asm/math.c" "asm/opt.c" "asm/output.c" "asm/rpn.c" diff --git a/src/asm/fixpoint.c b/src/asm/fixpoint.c new file mode 100644 index 00000000..9e872cc1 --- /dev/null +++ b/src/asm/fixpoint.c @@ -0,0 +1,170 @@ +/* + * This file is part of RGBDS. + * + * Copyright (c) 1997-2021, Carsten Sorensen and RGBDS contributors. + * + * SPDX-License-Identifier: MIT + */ + +/* + * Fixed-point math routines + */ + +#include +#include +#include +#include + +#include "asm/fixpoint.h" +#include "asm/symbol.h" +#include "asm/warning.h" + +#define fix2double(i) ((double)((i) / 65536.0)) +#define double2fix(d) ((int32_t)round((d) * 65536.0)) + +// pi radians == 32768 fixed-point "degrees" +#define fdeg2rad(f) ((f) * (M_PI / 32768.0)) +#define rad2fdeg(r) ((r) * (32768.0 / M_PI)) + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +/* + * Return the _PI symbol value + */ +int32_t fix_Callback_PI(void) +{ + warning(WARNING_OBSOLETE, "`_PI` is deprecated; use 3.14159\n"); + + return double2fix(M_PI); +} + +/* + * Print a fixed point value + */ +void fix_Print(int32_t i) +{ + uint32_t u = i; + const char *sign = ""; + + if (i < 0) { + u = -u; + sign = "-"; + } + + printf("%s%" PRIu32 ".%05" PRIu32, sign, u >> 16, + ((uint32_t)(fix2double(u) * 100000 + 0.5)) % 100000); +} + +/* + * Calculate sine + */ +int32_t fix_Sin(int32_t i) +{ + return double2fix(sin(fdeg2rad(fix2double(i)))); +} + +/* + * Calculate cosine + */ +int32_t fix_Cos(int32_t i) +{ + return double2fix(cos(fdeg2rad(fix2double(i)))); +} + +/* + * Calculate tangent + */ +int32_t fix_Tan(int32_t i) +{ + return double2fix(tan(fdeg2rad(fix2double(i)))); +} + +/* + * Calculate arcsine + */ +int32_t fix_ASin(int32_t i) +{ + return double2fix(rad2fdeg(asin(fix2double(i)))); +} + +/* + * Calculate arccosine + */ +int32_t fix_ACos(int32_t i) +{ + return double2fix(rad2fdeg(acos(fix2double(i)))); +} + +/* + * Calculate arctangent + */ +int32_t fix_ATan(int32_t i) +{ + return double2fix(rad2fdeg(atan(fix2double(i)))); +} + +/* + * Calculate atan2 + */ +int32_t fix_ATan2(int32_t i, int32_t j) +{ + return double2fix(rad2fdeg(atan2(fix2double(i), fix2double(j)))); +} + +/* + * Multiplication + */ +int32_t fix_Mul(int32_t i, int32_t j) +{ + return double2fix(fix2double(i) * fix2double(j)); +} + +/* + * Division + */ +int32_t fix_Div(int32_t i, int32_t j) +{ + return double2fix(fix2double(i) / fix2double(j)); +} + +/* + * Power + */ +int32_t fix_Pow(int32_t i, int32_t j) +{ + return double2fix(pow(fix2double(i), fix2double(j))); +} + +/* + * Logarithm + */ +int32_t fix_Log(int32_t i, int32_t j) +{ + return double2fix(log(fix2double(i)) / log(fix2double(j))); +} + +/* + * Round + */ +int32_t fix_Round(int32_t i) +{ + return double2fix(round(fix2double(i))); +} + +/* + * Ceil + */ +int32_t fix_Ceil(int32_t i) +{ + return double2fix(ceil(fix2double(i))); +} + +/* + * Floor + */ +int32_t fix_Floor(int32_t i) +{ + return double2fix(floor(fix2double(i))); +} diff --git a/src/asm/math.c b/src/asm/math.c deleted file mode 100644 index 7e291a18..00000000 --- a/src/asm/math.c +++ /dev/null @@ -1,170 +0,0 @@ -/* - * This file is part of RGBDS. - * - * Copyright (c) 1997-2018, Carsten Sorensen and RGBDS contributors. - * - * SPDX-License-Identifier: MIT - */ - -/* - * Fixedpoint math routines - */ - -#include -#include -#include -#include - -#include "asm/mymath.h" -#include "asm/symbol.h" -#include "asm/warning.h" - -#define fx2double(i) ((double)((i) / 65536.0)) -#define double2fx(d) ((int32_t)round((d) * 65536.0)) - -// pi radians == 32768 fixed-point "degrees" -#define fdeg2rad(f) ((f) * (M_PI / 32768.0)) -#define rad2fdeg(r) ((r) * (32768.0 / M_PI)) - -#ifndef M_PI -#define M_PI 3.14159265358979323846 -#endif - -/* - * Return the _PI symbol value - */ -int32_t math_Callback_PI(void) -{ - warning(WARNING_OBSOLETE, "`_PI` is deprecated; use 3.14159\n"); - - return double2fx(M_PI); -} - -/* - * Print a fixed point value - */ -void math_Print(int32_t i) -{ - uint32_t u = i; - const char *sign = ""; - - if (i < 0) { - u = -u; - sign = "-"; - } - - printf("%s%" PRIu32 ".%05" PRIu32, sign, u >> 16, - ((uint32_t)(fx2double(u) * 100000 + 0.5)) % 100000); -} - -/* - * Calculate sine - */ -int32_t math_Sin(int32_t i) -{ - return double2fx(sin(fdeg2rad(fx2double(i)))); -} - -/* - * Calculate cosine - */ -int32_t math_Cos(int32_t i) -{ - return double2fx(cos(fdeg2rad(fx2double(i)))); -} - -/* - * Calculate tangent - */ -int32_t math_Tan(int32_t i) -{ - return double2fx(tan(fdeg2rad(fx2double(i)))); -} - -/* - * Calculate arcsine - */ -int32_t math_ASin(int32_t i) -{ - return double2fx(rad2fdeg(asin(fx2double(i)))); -} - -/* - * Calculate arccosine - */ -int32_t math_ACos(int32_t i) -{ - return double2fx(rad2fdeg(acos(fx2double(i)))); -} - -/* - * Calculate arctangent - */ -int32_t math_ATan(int32_t i) -{ - return double2fx(rad2fdeg(atan(fx2double(i)))); -} - -/* - * Calculate atan2 - */ -int32_t math_ATan2(int32_t i, int32_t j) -{ - return double2fx(rad2fdeg(atan2(fx2double(i), fx2double(j)))); -} - -/* - * Multiplication - */ -int32_t math_Mul(int32_t i, int32_t j) -{ - return double2fx(fx2double(i) * fx2double(j)); -} - -/* - * Division - */ -int32_t math_Div(int32_t i, int32_t j) -{ - return double2fx(fx2double(i) / fx2double(j)); -} - -/* - * Power - */ -int32_t math_Pow(int32_t i, int32_t j) -{ - return double2fx(pow(fx2double(i), fx2double(j))); -} - -/* - * Logarithm - */ -int32_t math_Log(int32_t i, int32_t j) -{ - return double2fx(log(fx2double(i)) / log(fx2double(j))); -} - -/* - * Round - */ -int32_t math_Round(int32_t i) -{ - return double2fx(round(fx2double(i))); -} - -/* - * Ceil - */ -int32_t math_Ceil(int32_t i) -{ - return double2fx(ceil(fx2double(i))); -} - -/* - * Floor - */ -int32_t math_Floor(int32_t i) -{ - return double2fx(floor(fx2double(i))); -} diff --git a/src/asm/parser.y b/src/asm/parser.y index 841cb55a..4eb0ae7b 100644 --- a/src/asm/parser.y +++ b/src/asm/parser.y @@ -17,12 +17,12 @@ #include #include "asm/charmap.h" +#include "asm/fixpoint.h" #include "asm/format.h" #include "asm/fstack.h" #include "asm/lexer.h" #include "asm/macro.h" #include "asm/main.h" -#include "asm/mymath.h" #include "asm/opt.h" #include "asm/output.h" #include "asm/rpn.h" @@ -1148,7 +1148,7 @@ printi : T_POP_PRINTI const { printf : T_POP_PRINTF const { warning(WARNING_OBSOLETE, "`PRINTF` is deprecated; use `PRINT` with `STRFMT`\n"); - math_Print($2); + fix_Print($2); } ; @@ -1363,46 +1363,46 @@ relocexpr_no_str : scoped_anon_id { rpn_Symbol(&$$, $1); } lexer_ToggleStringExpansion(true); } | T_OP_ROUND T_LPAREN const T_RPAREN { - rpn_Number(&$$, math_Round($3)); + rpn_Number(&$$, fix_Round($3)); } | T_OP_CEIL T_LPAREN const T_RPAREN { - rpn_Number(&$$, math_Ceil($3)); + rpn_Number(&$$, fix_Ceil($3)); } | T_OP_FLOOR T_LPAREN const T_RPAREN { - rpn_Number(&$$, math_Floor($3)); + rpn_Number(&$$, fix_Floor($3)); } | T_OP_FDIV T_LPAREN const T_COMMA const T_RPAREN { - rpn_Number(&$$, math_Div($3, $5)); + rpn_Number(&$$, fix_Div($3, $5)); } | T_OP_FMUL T_LPAREN const T_COMMA const T_RPAREN { - rpn_Number(&$$, math_Mul($3, $5)); + rpn_Number(&$$, fix_Mul($3, $5)); } | T_OP_POW T_LPAREN const T_COMMA const T_RPAREN { - rpn_Number(&$$, math_Pow($3, $5)); + rpn_Number(&$$, fix_Pow($3, $5)); } | T_OP_LOG T_LPAREN const T_COMMA const T_RPAREN { - rpn_Number(&$$, math_Log($3, $5)); + rpn_Number(&$$, fix_Log($3, $5)); } | T_OP_SIN T_LPAREN const T_RPAREN { - rpn_Number(&$$, math_Sin($3)); + rpn_Number(&$$, fix_Sin($3)); } | T_OP_COS T_LPAREN const T_RPAREN { - rpn_Number(&$$, math_Cos($3)); + rpn_Number(&$$, fix_Cos($3)); } | T_OP_TAN T_LPAREN const T_RPAREN { - rpn_Number(&$$, math_Tan($3)); + rpn_Number(&$$, fix_Tan($3)); } | T_OP_ASIN T_LPAREN const T_RPAREN { - rpn_Number(&$$, math_ASin($3)); + rpn_Number(&$$, fix_ASin($3)); } | T_OP_ACOS T_LPAREN const T_RPAREN { - rpn_Number(&$$, math_ACos($3)); + rpn_Number(&$$, fix_ACos($3)); } | T_OP_ATAN T_LPAREN const T_RPAREN { - rpn_Number(&$$, math_ATan($3)); + rpn_Number(&$$, fix_ATan($3)); } | T_OP_ATAN2 T_LPAREN const T_COMMA const T_RPAREN { - rpn_Number(&$$, math_ATan2($3, $5)); + rpn_Number(&$$, fix_ATan2($3, $5)); } | T_OP_STRCMP T_LPAREN string T_COMMA string T_RPAREN { rpn_Number(&$$, strcmp($3, $5)); diff --git a/src/asm/symbol.c b/src/asm/symbol.c index 9c07a1e0..da8eae26 100644 --- a/src/asm/symbol.c +++ b/src/asm/symbol.c @@ -18,10 +18,10 @@ #include #include +#include "asm/fixpoint.h" #include "asm/fstack.h" #include "asm/macro.h" #include "asm/main.h" -#include "asm/mymath.h" #include "asm/output.h" #include "asm/section.h" #include "asm/symbol.h" @@ -775,5 +775,5 @@ void sym_Init(time_t now) _PISymbol->src = NULL; _PISymbol->fileLine = 0; _PISymbol->hasCallback = true; - _PISymbol->numCallback = math_Callback_PI; + _PISymbol->numCallback = fix_Callback_PI; }