| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/Lib/fix.h $ |
| 21 | * $Revision: 4 $ |
| 22 | * $Date: 10/21/99 9:27p $ |
| 23 | * $Author: Jeff $ |
| 24 | * |
| 25 | * Header for fixed-point math functions |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/Lib/fix.h $ |
| 28 | * |
| 29 | * 4 10/21/99 9:27p Jeff |
| 30 | * B.A. Macintosh code merge |
| 31 | * |
| 32 | * 3 9/09/97 3:53p Chris |
| 33 | * Added a #define PI to fix.h |
| 34 | * |
| 35 | * 2 8/22/97 12:34p Jason |
| 36 | * added FixCeil and FixFloor |
| 37 | * |
| 38 | * 1 6/23/97 9:25p Samir |
| 39 | * added because source safe sucks |
| 40 | * |
| 41 | * 7 2/27/97 4:57p Samir |
| 42 | * include fixwin32.h from win\fixwin32.h |
| 43 | * |
| 44 | * 6 2/27/97 4:52 PM Jeremy |
| 45 | * moved inline asm fixmul/div functions into separate header files for |
| 46 | * mac and pc. |
| 47 | * |
| 48 | * 5 2/12/97 5:27p Jason |
| 49 | * implemented asin,acos,atan2 |
| 50 | * |
| 51 | * 4 2/10/97 1:57p Jason |
| 52 | * changed FloatToFix back to what it originally was |
| 53 | * |
| 54 | * 3 2/07/97 5:44p Matt |
| 55 | * Moved fixed-point math funcs from vecmat to fix |
| 56 | * Changed trig functions to use small table like D2 |
| 57 | * |
| 58 | * $NoKeywords: $ |
| 59 | */ |
| 60 | |
| 61 | #ifndef _FIX_H |
| 62 | #define _FIX_Hcxx . . $ `fix/fix.h:62:9`! |
| 63 | |
| 64 | #include <cmath> |
| 65 | #include <cstdint> |
| 66 | |
| 67 | // Angles are unsigned shorts |
| 68 | typedef uint16_tcxx . . $ uint16_t# anglecxx . . $ angle#; |
| 69 | |
| 70 | // The basic fixed-point type |
| 71 | typedef int32_t fixcxx . . $ fix#; |
| 72 | |
| 73 | #define PIcxx . . $ `fix/fix.h:73:9`! 3.141592654f |
| 74 | #define PIOVER2cxx . . $ `fix/fix.h:74:9`! 1.570796327 // DAJ |
| 75 | |
| 76 | // Constants for converted between fix and float |
| 77 | #define FLOAT_SCALERcxx . . $ `fix/fix.h:77:9`! 65536.0f |
| 78 | #define FIX_SHIFTcxx . . $ `fix/fix.h:78:9`! 16 |
| 79 | |
| 80 | // 1.0 in fixed-point |
| 81 | #define F1_0cxx . . $ `fix/fix.h:81:9`! (1 << FIX_SHIFTcxx . . $ `fix/fix.h:78:9`!) |
| 82 | |
| 83 | // Generate the data for the trig tables. Must be called before trig functions |
| 84 | void InitMathTablescxx . . $ InitMathTables(49f6e7a06ebc5aa8).(); |
| 85 | |
| 86 | // Returns the sine of the given angle. Linearly interpolates between two entries in a 256-entry table |
| 87 | float FixSincxx . . $ FixSin(9a38df040a81ca35).(anglecxx . . $ angle# a); |
| 88 | |
| 89 | // Returns the cosine of the given angle. Linearly interpolates between two entries in a 256-entry table |
| 90 | float FixCoscxx . . $ FixCos(9a38df040a81ca35).(anglecxx . . $ angle# a); |
| 91 | |
| 92 | // Returns the sine of the given angle, but does no interpolation |
| 93 | float FixSinFastcxx . . $ FixSinFast(9a38df040a81ca35).(anglecxx . . $ angle# a); |
| 94 | |
| 95 | // Returns the cosine of the given angle, but does no interpolation |
| 96 | float FixCosFastcxx . . $ FixCosFast(9a38df040a81ca35).(anglecxx . . $ angle# a); |
| 97 | |
| 98 | #define Roundcxx . . $ `fix/fix.h:98:9`!(x) ((int)(x + 0.5)) |
| 99 | |
| 100 | fixcxx . . $ fix# FloatToFixFastcxx . . $ FloatToFixFast(1bb0578f3efdc255).(float num); |
| 101 | |
| 102 | // Conversion macros |
| 103 | //??#define FloatToFix(num) Round((num) * FLOAT_SCALER) |
| 104 | #define FloatToFixcxx . . $ `fix/fix.h:104:9`!(num) ((fix)((num) * FLOAT_SCALERcxx . . $ `fix/fix.h:77:9`!)) |
| 105 | #define IntToFixcxx . . $ `fix/fix.h:105:9`!(num) ((num) << FIX_SHIFTcxx . . $ `fix/fix.h:78:9`!) |
| 106 | #define ShortToFixcxx . . $ `fix/fix.h:106:9`!(num) (((int32_t)(num)) << FIX_SHIFT) |
| 107 | #define FixToFloatcxx . . $ `fix/fix.h:107:9`!(num) (((float)(num)) / FLOAT_SCALER) |
| 108 | #define FixToIntcxx . . $ `fix/fix.h:108:9`!(num) ((num) >> FIX_SHIFTcxx . . $ `fix/fix.h:78:9`!) |
| 109 | #define FixToShortcxx . . $ `fix/fix.h:109:9`!(num) ((int16_t)((num) >> FIX_SHIFT)) |
| 110 | |
| 111 | // use this instead of: |
| 112 | // for: (int)floor(x+0.5f) use FloatRound(x) |
| 113 | // (int)ceil(x-0.5f) use FloatRound(x) |
| 114 | // (int)floor(x-0.5f) use FloatRound(x-1.0f) |
| 115 | // (int)floor(x) use FloatRound(x-0.5f) |
| 116 | // for values in the range -2048 to 2048 |
| 117 | int FloatRoundcxx . . $ FloatRound(1bb0578f3efdc255).(float x); |
| 118 | |
| 119 | anglecxx . . $ angle# FixAtan2cxx . . $ FixAtan2(e98d38e0c629e912).(float cos, float sin); |
| 120 | anglecxx . . $ angle# FixAsincxx . . $ FixAsin(a7602afe0c5a7a59).(float v); |
| 121 | anglecxx . . $ angle# FixAcoscxx . . $ FixAcos(a7602afe0c5a7a59).(float v); |
| 122 | |
| 123 | // Does a ceiling operation on a fixed number |
| 124 | fixcxx . . $ fix# FixCeilcxx . . $ FixCeil(7864480464b09eea).(fixcxx . . $ fix# num); |
| 125 | |
| 126 | // Floors a fixed number |
| 127 | fixcxx . . $ fix# FixFloorcxx . . $ FixFloor(7864480464b09eea).(fixcxx . . $ fix# num); |
| 128 | |
| 129 | #endif |