| 1 |
/* |
| 2 |
* This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>. |
| 3 |
* It is copyright by its individual contributors, as recorded in the |
| 4 |
* project's Git history. See COPYING.txt at the top level for license |
| 5 |
* terms and a link to the Git history. |
| 6 |
*/ |
| 7 |
/* Maths.h library header file */ |
| 8 |
|
| 9 |
#pragma once |
| 10 |
|
| 11 |
#include <cstdint> |
| 12 |
#include <optional> |
| 13 |
#include <stdlib.h> |
| 14 |
#include "pstypes.h" |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#define D_RAND_MAX 32767 |
| 19 |
|
| 20 |
#include <cstddef> |
| 21 |
#include "dsx-ns.h" |
| 22 |
#include <array> |
| 23 |
|
| 24 |
namespace dcx { |
| 25 |
|
| 26 |
void d_srand (unsigned int seed); |
| 27 |
|
| 28 |
[[nodiscard]] |
| 29 |
int d_rand (); // Random number function which returns in the range 0-0x7FFF |
| 30 |
|
| 31 |
|
| 32 |
//=============================== FIXED POINT =============================== |
| 33 |
|
| 34 |
typedef int64_t fix64; //64 bits int, for timers |
| 35 |
typedef int32_t fix; //16 bits int, 16 bits frac |
| 36 |
typedef int16_t fixang; //angles |
| 37 |
|
| 38 |
enum class quadint : int64_t // integer 64 bit, previously called "quad" |
| 39 |
{ |
| 40 |
}; |
| 41 |
|
| 42 |
//Convert an int to a fix/fix64 and back |
| 43 |
static constexpr fix i2f(const int &i) |
| 44 |
{ |
| 45 |
return i << 16; |
| 46 |
} |
| 47 |
|
| 48 |
static constexpr int f2i(const fix &f) |
| 49 |
{ |
| 50 |
return f >> 16; |
| 51 |
} |
| 52 |
|
| 53 |
//Convert fix to float and float to fix |
| 54 |
static constexpr float f2fl(const fix &f) |
| 55 |
{ |
| 56 |
return static_cast<float>(f) / 65536.0f; |
| 57 |
} |
| 58 |
|
| 59 |
static constexpr double f2db(const fix &f) |
| 60 |
{ |
| 61 |
return static_cast<double>(f) / 65536.0; |
| 62 |
} |
| 63 |
|
| 64 |
static constexpr fix fl2f(const float &f) |
| 65 |
{ |
| 66 |
return static_cast<fix>(f * 65536); |
| 67 |
} |
| 68 |
|
| 69 |
//Some handy constants |
| 70 |
#define f0_0 0 |
| 71 |
#define f1_0 0x10000 |
| 72 |
#define f2_0 0x20000 |
| 73 |
#define f3_0 0x30000 |
| 74 |
#define f10_0 0xa0000 |
| 75 |
|
| 76 |
#define f0_5 0x8000 |
| 77 |
#define f0_1 0x199a |
| 78 |
|
| 79 |
//Get the int part of a fix, with rounding |
| 80 |
static constexpr int f2ir(const fix &f) |
| 81 |
{ |
| 82 |
return (f + f0_5) >> 16; |
| 83 |
} |
| 84 |
|
| 85 |
#define F0_0 f0_0 |
| 86 |
#define F1_0 f1_0 |
| 87 |
#define F2_0 f2_0 |
| 88 |
#define F3_0 f3_0 |
| 89 |
#define F10_0 f10_0 |
| 90 |
|
| 91 |
#define F0_5 f0_5 |
| 92 |
#define F0_1 f0_1 |
| 93 |
|
| 94 |
//multiply two fixes, return a fix(64) |
| 95 |
[[nodiscard]] |
| 96 |
fix64 fixmul64 (fix a, fix b); |
| 97 |
|
| 98 |
/* On x86/amd64 for Windows/Linux, truncating fix64->fix is free. */ |
| 99 |
[[nodiscard]] |
| 100 |
static inline fix fixmul(fix a, fix b) |
| 101 |
{ |
| 102 |
return static_cast<fix>(fixmul64(a, b)); |
| 103 |
} |
| 104 |
|
| 105 |
//divide two fixes, return a fix |
| 106 |
[[nodiscard]] |
| 107 |
fix fixdiv (fix a, fix b); |
| 108 |
|
| 109 |
//multiply two fixes, then divide by a third, return a fix |
| 110 |
[[nodiscard]] |
| 111 |
fix fixmuldiv (fix a, fix b, fix c); |
| 112 |
|
| 113 |
//multiply two fixes, and add 64-bit product to a quadint |
| 114 |
[[nodiscard]] |
| 115 |
static inline quadint fixmulaccum(const quadint q, const fix &a, const fix &b) |
| 116 |
{ |
| 117 |
return quadint{static_cast<int64_t>(q) + (int64_t{a} * int64_t{b})}; |
| 118 |
} |
| 119 |
|
| 120 |
//extract a fix from a quadint product |
| 121 |
[[nodiscard]] |
| 122 |
static inline fix fixquadadjust(const quadint q) |
| 123 |
{ |
| 124 |
return static_cast<fix>(static_cast<int64_t>(q) >> 16); |
| 125 |
} |
| 126 |
|
| 127 |
//computes the square root of a long, returning a short |
| 128 |
[[nodiscard]] |
| 129 |
ushort long_sqrt (int32_t a); |
| 130 |
|
| 131 |
//computes the square root of a quadint, returning a long |
| 132 |
[[nodiscard]] |
| 133 |
uint32_t quad_sqrt (quadint); |
| 134 |
|
| 135 |
//computes the square root of a fix, returning a fix |
| 136 |
[[nodiscard]] |
| 137 |
fix fix_sqrt (fix a); |
| 138 |
|
| 139 |
struct fix_sincos_result |
| 140 |
{ |
| 141 |
fix sin, cos; |
| 142 |
}; |
| 143 |
|
| 144 |
[[nodiscard]] |
| 145 |
fix_sincos_result fix_sincos(fixang); |
| 146 |
|
| 147 |
//compute sine and cosine of an angle, filling in the variables |
| 148 |
//either of the pointers can be NULL |
| 149 |
|
| 150 |
[[nodiscard]] |
| 151 |
fix fix_sin(fixang a); |
| 152 |
|
| 153 |
[[nodiscard]] |
| 154 |
fix fix_cos(fixang a); |
| 155 |
|
| 156 |
[[nodiscard]] |
| 157 |
fix fix_fastsin(fixang a); //no interpolation |
| 158 |
|
| 159 |
//compute inverse sine & cosine |
| 160 |
[[nodiscard]] |
| 161 |
fixang fix_asin (fix v); |
| 162 |
|
| 163 |
[[nodiscard]] |
| 164 |
fixang fix_acos (fix v); |
| 165 |
|
| 166 |
//given cos & sin of an angle, return that angle. |
| 167 |
//parms need not be normalized, that is, the ratio of the parms cos/sin must |
| 168 |
//equal the ratio of the actual cos & sin for the result angle, but the parms |
| 169 |
//need not be the actual cos & sin. |
| 170 |
//NOTE: this is different from the standard C atan2, since it is left-handed. |
| 171 |
[[nodiscard]] |
| 172 |
fixang fix_atan2 (fix cos, fix sin); |
| 173 |
|
| 174 |
[[nodiscard]] |
| 175 |
std::optional<int32_t> checkmuldiv(fix a, fix b, fix divisor); |
| 176 |
|
| 177 |
extern const std::array<uint8_t, 256> guess_table; |
| 178 |
extern const std::array<int16_t, 256> sincos_table; |
| 179 |
extern const std::array<ushort, 258> asin_table; |
| 180 |
extern const std::array<ushort, 258> acos_table; |
| 181 |
|
| 182 |
static inline void clamp_fix_lh(fix& f, const fix& low, const fix& high) |
| 183 |
{ |
| 184 |
if (f < low) |
| 185 |
f = low; |
| 186 |
else if (high < f) |
| 187 |
f = high; |
| 188 |
} |
| 189 |
|
| 190 |
static inline void clamp_fix_symmetric(fix& f, const fix& bound) |
| 191 |
{ |
| 192 |
clamp_fix_lh(f, -bound, bound); |
| 193 |
} |
| 194 |
|
| 195 |
} |