From eeaee57f3bebfd1866baceb651b3545126d611dd Mon Sep 17 00:00:00 2001 From: "Azamat H. Hackimov" Date: Sat, 27 Apr 2024 00:01:23 +0300 Subject: [PATCH 1/5] Reorganizing fix library --- CMakeLists.txt | 1 + fix/CMakeLists.txt | 5 +++++ {lib => fix}/fix.h | 4 ++-- {lib/win => fix}/fixwin32.h | 0 vecmat/CMakeLists.txt | 1 + 5 files changed, 9 insertions(+), 2 deletions(-) rename {lib => fix}/fix.h (98%) rename {lib/win => fix}/fixwin32.h (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index e2ad20428..a8e562be4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -144,6 +144,7 @@ endif() include_directories( "cfile" # TODO: Remove after untying all modules "ddebug" # -*- + "fix" # -*- "lib" # TODO: Remove after untying all modules "Descent3" ${PLATFORM_INCLUDES} diff --git a/fix/CMakeLists.txt b/fix/CMakeLists.txt index 8fd1cba21..765148ded 100644 --- a/fix/CMakeLists.txt +++ b/fix/CMakeLists.txt @@ -3,3 +3,8 @@ set(CPPS fix.cpp) add_library(fix STATIC ${HEADERS} ${CPPS}) +target_include_directories(fix PUBLIC + $ +) diff --git a/lib/fix.h b/fix/fix.h similarity index 98% rename from lib/fix.h rename to fix/fix.h index ad52b2bf8..15a8bb856 100644 --- a/lib/fix.h +++ b/fix/fix.h @@ -61,7 +61,7 @@ #ifndef _FIX_H #define _FIX_H -#include "math.h" +#include // Disable the "possible loss of data" warning #pragma warning(disable : 4244) @@ -112,7 +112,7 @@ fix FloatToFixFast(float num); // Fixed-point math functions in inline ASM form #if defined(WIN32) -#include "win\fixwin32.h" +#include "fixwin32.h" #endif // use this instead of: diff --git a/lib/win/fixwin32.h b/fix/fixwin32.h similarity index 100% rename from lib/win/fixwin32.h rename to fix/fixwin32.h diff --git a/vecmat/CMakeLists.txt b/vecmat/CMakeLists.txt index dd8543b31..6a5269ce6 100644 --- a/vecmat/CMakeLists.txt +++ b/vecmat/CMakeLists.txt @@ -3,3 +3,4 @@ set(CPPS vector.cpp) add_library(vecmat STATIC ${HEADERS} ${CPPS}) +target_link_libraries(vecmat fix) From 2e1ab8fba847983759e91fdc4bfe3cbcdf26bbff Mon Sep 17 00:00:00 2001 From: "Azamat H. Hackimov" Date: Sat, 27 Apr 2024 01:25:45 +0300 Subject: [PATCH 2/5] Remove unused fix asm implementations --- fix/fix.h | 5 --- fix/fixwin32.h | 85 -------------------------------------------------- 2 files changed, 90 deletions(-) delete mode 100644 fix/fixwin32.h diff --git a/fix/fix.h b/fix/fix.h index 15a8bb856..c86708359 100644 --- a/fix/fix.h +++ b/fix/fix.h @@ -110,11 +110,6 @@ fix FloatToFixFast(float num); #define FixToInt(num) ((num) >> FIX_SHIFT) #define FixToShort(num) ((short)((num) >> FIX_SHIFT)) -// Fixed-point math functions in inline ASM form -#if defined(WIN32) -#include "fixwin32.h" -#endif - // use this instead of: // for: (int)floor(x+0.5f) use FloatRound(x) // (int)ceil(x-0.5f) use FloatRound(x) diff --git a/fix/fixwin32.h b/fix/fixwin32.h deleted file mode 100644 index e02170735..000000000 --- a/fix/fixwin32.h +++ /dev/null @@ -1,85 +0,0 @@ -/* -* Descent 3 -* Copyright (C) 2024 Parallax Software -* -* This program is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program. If not, see . -*/ - -/* - * $Logfile: /descent3/main/lib/win/fixwin32.h $ - * $Revision: 3 $ - * $Date: 2/27/97 4:57p $ - * $Author: Samir $ - * - * $Log: /descent3/main/lib/win/fixwin32.h $ - * - * 3 2/27/97 4:57p Samir - * disabled annoying no return value message for this header. - * - * 2 2/27/97 4:49 PM Jeremy - * fixed a typo - * - * 1 2/27/97 4:45 PM Jeremy - * inline assembly language functions for Intel processor for - * fixmul/fixdiv/fixmuldiv - * - * $NoKeywords: $ - */ - -#ifndef _FIXWIN32_H -#define _FIXWIN32_H - -// what does this do? Why didn't Jason put a comment here? -// Jason replies: This pragma disables the "no return value" warning that -// is generated when converting doubles to floats -// A thousand pardons for the confusion - -#pragma warning(disable : 4035) - -inline fix FixDiv(fix a, fix b) { - __asm - { - mov eax,a - mov ebx,b - cdq - shld edx, eax, 16 - sal eax,16 - idiv ebx - } -} - -inline fix FixMul(fix a, fix b) { - __asm - { - mov eax,a - mov ebx,b - imul ebx - shrd eax,edx,16 - } -} - -inline fix FixMulDiv(fix a, fix b, fix c) { - __asm - { - mov eax, a - mov edx, b - mov ebx, c - imul edx - idiv ebx - } -} - -#pragma warning(default : 4035) - -#endif From 1cda68a553f30a863c13180ebd5af84784b76373 Mon Sep 17 00:00:00 2001 From: "Azamat H. Hackimov" Date: Sat, 27 Apr 2024 02:26:20 +0300 Subject: [PATCH 3/5] Fix warnings in fix library Fix most of "narrowing conversion" warnings. --- fix/fix.cpp | 25 ++++++++++++------------- fix/fix.h | 16 +++++++--------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/fix/fix.cpp b/fix/fix.cpp index 693b7f482..d77dea4ef 100644 --- a/fix/fix.cpp +++ b/fix/fix.cpp @@ -64,18 +64,17 @@ * $NoKeywords: $ */ -#include "fix.h" +#include +#include -#include -#include +#include "fix.h" #include "psrand.h" + // Tables for trig functions float sincos_table[321]; // 256 entries + 64 sin-only + 1 for interpolation angle asin_table[257]; // 1 quadrants worth, +1 for interpolation angle acos_table[257]; -#define PI 3.141592654 - // Generate the data for the trig tables void InitMathTables() { int i; @@ -83,13 +82,13 @@ void InitMathTables() { for (i = 0; i < 321; i++) { rad = (float)((double)i / 256.0 * 2 * PI); - sincos_table[i] = (float)sin(rad); + sincos_table[i] = std::sin(rad); } for (i = 0; i < 256; i++) { - s = asin((float)i / 256.0); - c = acos((float)i / 256.0); + s = std::asin((float)i / 256.0f); + c = std::acos((float)i / 256.0f); s = (s / (PI * 2)); c = (c / (PI * 2)); @@ -102,7 +101,7 @@ void InitMathTables() { acos_table[256] = acos_table[255]; // Initialize a random seed. - ps_srand(time(NULL)); + ps_srand(time(nullptr)); } // Returns the sine of the given angle. Linearly interpolates between two entries in a 256-entry table @@ -179,7 +178,7 @@ angle FixAsin(float v) { fix vv; int i, f, aa; - vv = FloatToFix(fabs(v)); + vv = FloatToFix(std::fabs(v)); if (vv >= F1_0) // check for out of range return 0x4000; @@ -201,7 +200,7 @@ angle FixAcos(float v) { fix vv; int i, f, aa; - vv = FloatToFix(fabs(v)); + vv = FloatToFix(std::fabs(v)); if (vv >= F1_0) // check for out of range return 0; @@ -231,12 +230,12 @@ angle FixAtan2(float cos, float sin) { q = (sin * sin) + (cos * cos); - m = sqrt(q); + m = std::sqrt(q); if (m == 0) return 0; - if (fabs(sin) < fabs(cos)) { + if (std::fabs(sin) < std::fabs(cos)) { // sin is smaller, use arcsin t = FixAsin(sin / m); if (cos < 0) diff --git a/fix/fix.h b/fix/fix.h index c86708359..dca5dffab 100644 --- a/fix/fix.h +++ b/fix/fix.h @@ -62,21 +62,19 @@ #define _FIX_H #include - -// Disable the "possible loss of data" warning -#pragma warning(disable : 4244) +#include // Angles are unsigned shorts -typedef unsigned short angle; +typedef uint16_t angle; // The basic fixed-point type -typedef long fix; +typedef int32_t fix; -#define PI 3.141592654 +#define PI 3.141592654f #define PIOVER2 1.570796327 // DAJ // Constants for converted between fix and float -#define FLOAT_SCALER 65536.0 +#define FLOAT_SCALER 65536.0f #define FIX_SHIFT 16 // 1.0 in fixed-point @@ -105,10 +103,10 @@ fix FloatToFixFast(float num); //??#define FloatToFix(num) Round((num) * FLOAT_SCALER) #define FloatToFix(num) ((fix)((num) * FLOAT_SCALER)) #define IntToFix(num) ((num) << FIX_SHIFT) -#define ShortToFix(num) (((long)(num)) << FIX_SHIFT) +#define ShortToFix(num) (((int32_t)(num)) << FIX_SHIFT) #define FixToFloat(num) (((float)(num)) / FLOAT_SCALER) #define FixToInt(num) ((num) >> FIX_SHIFT) -#define FixToShort(num) ((short)((num) >> FIX_SHIFT)) +#define FixToShort(num) ((int16_t)((num) >> FIX_SHIFT)) // use this instead of: // for: (int)floor(x+0.5f) use FloatRound(x) From 020d6539b068571057851c62dac25d753a2a45a6 Mon Sep 17 00:00:00 2001 From: "Azamat H. Hackimov" Date: Sat, 27 Apr 2024 02:50:10 +0300 Subject: [PATCH 4/5] Unbundle fix API from osiris_vector.h Seem this was done to ease linking mission dlls. Now these missions are reusing fix as static library. --- scripts/CMakeLists.txt | 1 + scripts/DallasFuncs.cpp | 4 - scripts/osiris_vector.h | 271 +--------------------------------------- 3 files changed, 5 insertions(+), 271 deletions(-) diff --git a/scripts/CMakeLists.txt b/scripts/CMakeLists.txt index c2e285e9a..341416f79 100644 --- a/scripts/CMakeLists.txt +++ b/scripts/CMakeLists.txt @@ -75,6 +75,7 @@ add_custom_target(HogLinuxFull-copy foreach(SCRIPT ${SCRIPTS}) add_library(${SCRIPT} SHARED ${CPPS} "${SCRIPT}.cpp") + target_link_libraries(${SCRIPT} fix) add_dependencies(${SCRIPT} HogLinuxFull-copy) # HogLinuxDemo-copy set_target_properties(${SCRIPT} PROPERTIES PREFIX "") if (UNIX) diff --git a/scripts/DallasFuncs.cpp b/scripts/DallasFuncs.cpp index 46aef4c75..db97ad418 100644 --- a/scripts/DallasFuncs.cpp +++ b/scripts/DallasFuncs.cpp @@ -5335,8 +5335,6 @@ Can object see the target object $$END */ bool qObjCanSeeObj(int handletarget, int cone, int handlesrc) { -#define PI 3.141592654 - vector vsource, vtarget; msafe_struct mstruct; @@ -5383,8 +5381,6 @@ Can object see the target object $$END */ bool qObjCanSeeObjAdvanced(int handletarget, int cone, int handlesrc, int fvi_flags) { -#define PI 3.141592654 - vector vsource, vtarget; int sourceroom; diff --git a/scripts/osiris_vector.h b/scripts/osiris_vector.h index 4a12e0b59..818067394 100644 --- a/scripts/osiris_vector.h +++ b/scripts/osiris_vector.h @@ -19,9 +19,11 @@ #ifndef OSIRIS_VECTOR_H #define OSIRIS_VECTOR_H -#include -#include +#include +#include +#include +#include "fix.h" #include "vecmat_external.h" const vector Zero_vector = {0.0f, 0.0f, 0.0f}; @@ -29,271 +31,6 @@ const vector Zero_vector = {0.0f, 0.0f, 0.0f}; // Disable the "possible loss of data" warning #pragma warning(disable : 4244) -// Angles are unsigned shorts -typedef unsigned short angle; - -// The basic fixed-point type -typedef long fix; - -#define PI 3.141592654 - -// Constants for converted between fix and float -#define FLOAT_SCALER 65536.0 -#define FIX_SHIFT 16 - -// 1.0 in fixed-point -#define F1_0 (1 << FIX_SHIFT) - -// Generate the data for the trig tables. Must be called before trig functions -void InitMathTables(); - -// Returns the sine of the given angle. Linearly interpolates between two entries in a 256-entry table -float FixSin(angle a); - -// Returns the cosine of the given angle. Linearly interpolates between two entries in a 256-entry table -float FixCos(angle a); - -// Returns the sine of the given angle, but does no interpolation -float FixSinFast(angle a); - -// Returns the cosine of the given angle, but does no interpolation -float FixCosFast(angle a); - -#define Round(x) ((int)(x + 0.5)) - -fix FloatToFixFast(float num); - -// Conversion macros -//??#define FloatToFix(num) Round((num) * FLOAT_SCALER) -#define FloatToFix(num) ((fix)((num) * FLOAT_SCALER)) -#define IntToFix(num) ((num) << FIX_SHIFT) -#define ShortToFix(num) (((long)(num)) << FIX_SHIFT) -#define FixToFloat(num) (((float)(num)) / FLOAT_SCALER) -#define FixToInt(num) ((num) >> FIX_SHIFT) -#define FixToShort(num) ((short)((num) >> FIX_SHIFT)) - -// Tables for trig functions -float sincos_table[321]; // 256 entries + 64 sin-only + 1 for interpolation -angle asin_table[257]; // 1 quadrants worth, +1 for interpolation -angle acos_table[257]; - -// Generate the data for the trig tables -void InitMathTables() { - int i; - float rad, s, c; - - for (i = 0; i < 321; i++) { - rad = (float)((double)i / 256.0 * 2 * PI); - sincos_table[i] = (float)sin(rad); - } - - for (i = 0; i < 256; i++) { - - s = asin((float)i / 256.0); - c = acos((float)i / 256.0); - - s = (s / (PI * 2)); - c = (c / (PI * 2)); - - asin_table[i] = FloatToFix(s); - acos_table[i] = FloatToFix(c); - } - - asin_table[256] = asin_table[255]; - acos_table[256] = acos_table[255]; - - // Initialize a random seed. - srand(time(NULL)); -} - -// Returns the sine of the given angle. Linearly interpolates between two entries in a 256-entry table -float FixSin(angle a) { - int i, f; - float s0, s1; - - i = (a >> 8) & 0xff; - f = a & 0xff; - - s0 = sincos_table[i]; - s1 = sincos_table[i + 1]; - return (float)(s0 + ((s1 - s0) * (double)f / 256.0)); -} - -// Returns the cosine of the given angle. Linearly interpolates between two entries in a 256-entry table -float FixCos(angle a) { - int i, f; - float c0, c1; - - i = (a >> 8) & 0xff; - f = a & 0xff; - - c0 = sincos_table[i + 64]; - c1 = sincos_table[i + 64 + 1]; - return (float)(c0 + ((c1 - c0) * (double)f / 256.0)); -} - -// Returns the sine of the given angle, but does no interpolation -float FixSinFast(angle a) { - int i; - - i = ((a + 0x80) >> 8) & 0xff; - - return sincos_table[i]; -} - -// Returns the cosine of the given angle, but does no interpolation -float FixCosFast(angle a) { - int i; - - i = ((a + 0x80) >> 8) & 0xff; - - return sincos_table[i + 64]; -} - -// use this instead of: -// for: (int)floor(x+0.5f) use FloatRound(x) -// (int)ceil(x-0.5f) use FloatRound(x) -// (int)floor(x-0.5f) use FloatRound(x-1.0f) -// (int)floor(x) use FloatRound(x-0.5f) -// for values in the range -2048 to 2048 - -// Set a vector to {0,0,0} -int FloatRound(float x) { - float nf; - nf = x + 8390656.0f; - return ((*((int *)&nf)) & 0x7FFFFF) - 2048; -} - -// A fast way to convert floats to fix -fix FloatToFixFast(float x) { - - float nf; - nf = x * 65536.0f + 8390656.0f; - return ((*((int *)&nf)) & 0x7FFFFF) - 2048; -} - -// Get rid of the "no return value" warnings in the next three functions -#pragma warning(disable : 4035) - -// compute inverse sine -angle FixAsin(float v) { - fix vv; - int i, f, aa; - - vv = FloatToFix(fabs(v)); - - if (vv >= F1_0) // check for out of range - return 0x4000; - - i = (vv >> 8) & 0xff; - f = vv & 0xff; - - aa = asin_table[i]; - aa = aa + (((asin_table[i + 1] - aa) * f) >> 8); - - if (v < 0) - aa = F1_0 - aa; - - return aa; -} - -// compute inverse cosine -angle FixAcos(float v) { - fix vv; - int i, f, aa; - - vv = FloatToFix(fabs(v)); - - if (vv >= F1_0) // check for out of range - return 0; - - i = (vv >> 8) & 0xff; - f = vv & 0xff; - - aa = acos_table[i]; - aa = aa + (((acos_table[i + 1] - aa) * f) >> 8); - - if (v < 0) - aa = 0x8000 - aa; - - return aa; -} - -// given cos & sin of an angle, return that angle. -// parms need not be normalized, that is, the ratio of the parms cos/sin must -// equal the ratio of the actual cos & sin for the result angle, but the parms -// need not be the actual cos & sin. -// NOTE: this is different from the standard C atan2, since it is left-handed. -angle FixAtan2(float cos, float sin) { - float q, m; - angle t; - - // find smaller of two - - q = (sin * sin) + (cos * cos); - - m = sqrt(q); - - if (m == 0) - return 0; - - if (fabs(sin) < fabs(cos)) { - // sin is smaller, use arcsin - t = FixAsin(sin / m); - if (cos < 0) - t = 0x8000 - t; - - return t; - } else { - t = FixAcos(cos / m); - if (sin < 0) - t = F1_0 - t; - - return t; - } -} - -// Does a ceiling operation on a fixed number -fix FixCeil(fix num) { - int int_num; - fix new_num; - - int_num = FixToInt(num); - - if (num & 0xFFFF) { - new_num = IntToFix(int_num + 1); - return new_num; - } - - new_num = IntToFix(int_num); - return (new_num); -} - -// Floors a fixed number -fix FixFloor(fix num) { - int int_num = FixToInt(num); - - return (IntToFix(int_num)); -} - -// use this instead of: -// for: (int)floor(x+0.5f) use FloatRound(x) -// (int)ceil(x-0.5f) use FloatRound(x) -// (int)floor(x-0.5f) use FloatRound(x-1.0f) -// (int)floor(x) use FloatRound(x-0.5f) -// for values in the range -2048 to 2048 -int FloatRound(float x); - -angle FixAtan2(float cos, float sin); -angle FixAsin(float v); -angle FixAcos(float v); - -// Does a ceiling operation on a fixed number -fix FixCeil(fix num); - -// Floors a fixed number -fix FixFloor(fix num); - // Used for debugging. It is used in printf's so we do not have to write out the structure 3 times // to print all the coordinates. #define XYZ(v) (v)->x, (v)->y, (v)->z From 624394ef2af34a561458dd09fdbf5bf08ec502e6 Mon Sep 17 00:00:00 2001 From: "Azamat H. Hackimov" Date: Sat, 27 Apr 2024 03:05:20 +0300 Subject: [PATCH 5/5] Exclude ps_srand() call from InitMathTables() Seems this is totally unrelated to fix library API. Moved ps_srand() to appropriate places right after InitMathTables(); --- Descent3/init.cpp | 5 +++++ fix/fix.cpp | 5 ----- scripts/AIGame3.cpp | 1 + 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Descent3/init.cpp b/Descent3/init.cpp index 03e51ace8..398a533aa 100644 --- a/Descent3/init.cpp +++ b/Descent3/init.cpp @@ -967,6 +967,7 @@ #include "stringtable.h" #include "hlsoundlib.h" #include "player.h" +#include "psrand.h" #include "ambient.h" #include "matcen.h" #include "dedicated_server.h" @@ -2114,6 +2115,10 @@ void InitD3Systems1(bool editor) { // This function needs be called before ANY 3d stuff can get done. I mean it. InitMathTables(); + // Initialize a random seed. + ps_srand(time(nullptr)); + + // This function has to be done before any sound stuff is called InitSounds(); diff --git a/fix/fix.cpp b/fix/fix.cpp index d77dea4ef..08e8b69e1 100644 --- a/fix/fix.cpp +++ b/fix/fix.cpp @@ -65,10 +65,8 @@ */ #include -#include #include "fix.h" -#include "psrand.h" // Tables for trig functions float sincos_table[321]; // 256 entries + 64 sin-only + 1 for interpolation @@ -99,9 +97,6 @@ void InitMathTables() { asin_table[256] = asin_table[255]; acos_table[256] = acos_table[255]; - - // Initialize a random seed. - ps_srand(time(nullptr)); } // Returns the sine of the given angle. Linearly interpolates between two entries in a 256-entry table diff --git a/scripts/AIGame3.cpp b/scripts/AIGame3.cpp index e8adbe478..3fdfd64c5 100644 --- a/scripts/AIGame3.cpp +++ b/scripts/AIGame3.cpp @@ -29,6 +29,7 @@ #include "AIGame3_External.h" #include "module.h" +#include "psrand.h" #ifdef __cplusplus extern "C" {