ref: 0774f5eb9d07a83600b10282a02a07e24bb9eb01
parent: 76d88629009d98292890804161ecb7e455f0751f
author: Rangi <remy.oukaour+rangi42@gmail.com>
date: Sun Feb 28 11:30:48 EST 2021
Rename math.c/mymath.h to fixpoint.c/.h This also changes the functions' prefix from "math_" to "fix_".
--- a/Makefile
+++ b/Makefile
@@ -54,12 +54,12 @@
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 \
--- /dev/null
+++ b/include/asm/fixpoint.h
@@ -1,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 <stdint.h>
+
+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 */
--- a/include/asm/mymath.h
+++ /dev/null
@@ -1,31 +1,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 <stdint.h>
-
-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 */
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -44,12 +44,12 @@
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"
--- /dev/null
+++ b/src/asm/fixpoint.c
@@ -1,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 <inttypes.h>
+#include <math.h>
+#include <stdint.h>
+#include <stdio.h>
+
+#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)));
+}
--- a/src/asm/math.c
+++ /dev/null
@@ -1,170 +1,0 @@
-/*
- * This file is part of RGBDS.
- *
- * Copyright (c) 1997-2018, Carsten Sorensen and RGBDS contributors.
- *
- * SPDX-License-Identifier: MIT
- */
-
-/*
- * Fixedpoint math routines
- */
-
-#include <inttypes.h>
-#include <math.h>
-#include <stdint.h>
-#include <stdio.h>
-
-#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)));
-}
--- a/src/asm/parser.y
+++ b/src/asm/parser.y
@@ -17,12 +17,12 @@
#include <string.h>
#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 @@
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 @@
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));
--- a/src/asm/symbol.c
+++ b/src/asm/symbol.c
@@ -18,10 +18,10 @@
#include <string.h>
#include <time.h>
+#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 @@
_PISymbol->src = NULL;
_PISymbol->fileLine = 0;
_PISymbol->hasCallback = true;
- _PISymbol->numCallback = math_Callback_PI;
+ _PISymbol->numCallback = fix_Callback_PI;
}