- CMath: sqrt_, log2_, exp2_ (+ tables), solvequad_, normalizeangle0to2pi_, normalizeAngleNegPiToPi_; inline fint_ / ftol_round_0_255_ - CImVector::operator= rounds via fint_ (was truncating); Set via ftol_round_0_255_ - CRandom::uint32_ (guessed name) + table; CRndSeed moved to CRandom.h - NTempest: ray/box, ray/plane, ray/tri (u16/u32), point-in-polygon, 2d box/sphere, segment and polygon distances - CFrustum (0xE4) with culls, transforms, planes from corners; CWFrustum derives from it - C3Vector::EAxis; Major/MinorAxis return it Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
176 lines
7.2 KiB
C++
176 lines
7.2 KiB
C++
#include <cmath>
|
|
#include <cstdint>
|
|
#include "Engine/Source/Tempest/CMath.h"
|
|
|
|
// STATIC: 0x009F1970
|
|
static const double s_exp2Table[64] = {
|
|
1.0, 1.0108892860517005, 1.0218971486541166, 1.0330248790212284, 1.0442737824274138, 1.0556451783605572,
|
|
1.0671404006768237, 1.0787607977571199, 1.0905077326652577, 1.102382583307841, 1.1143867425958924, 1.1265216186082418,
|
|
1.1387886347566916, 1.1511892299529827, 1.1637248587775775, 1.1763969916502812, 1.189207115002721, 1.202156731452703,
|
|
1.215247359980469, 1.22848053610687, 1.241857812073484, 1.255380757024691, 1.2690509571917332, 1.2828700160787783,
|
|
1.2968395546510096, 1.3109612115247644, 1.3252366431597413, 1.339667524053303, 1.3542555469368927, 1.3690024229745905,
|
|
1.383909881963832, 1.3989796725383112, 1.4142135623730951, 1.42961333839197, 1.4451808069770467, 1.460917794180647,
|
|
1.4768261459394993, 1.4929077282912648, 1.5091644275934228, 1.5255981507445384, 1.5422108254079407, 1.559004400237837,
|
|
1.5759808451078865, 1.593142151342267, 1.6104903319492543, 1.6280274218573478, 1.645755478153965, 1.6636765803267364,
|
|
1.681792830507429, 1.7001063537185235, 1.718619298122478, 1.7373338352737062, 1.7562521603732995, 1.7753764925265212,
|
|
1.7947090750031072, 1.8142521755003989, 1.8340080864093424, 1.8539791250833855, 1.8741676341103, 1.8945759815869656,
|
|
1.9152065613971474, 1.9360617934922943, 1.9571441241754002, 1.978456026387951
|
|
};
|
|
|
|
// STATIC: 0x009F1B70
|
|
static const double s_log2MulTable[64] = {
|
|
1.0, 0.9846153846153847, 0.9696969696969697, 0.9552238805970149, 0.9411764705882353, 0.927536231884058,
|
|
0.9142857142857143, 0.9014084507042254, 0.8888888888888888, 0.8767123287671232, 0.8648648648648649, 0.8533333333333334,
|
|
0.8421052631578947, 0.8311688311688312, 0.8205128205128205, 0.810126582278481, 0.8, 0.7901234567901234,
|
|
0.7804878048780488, 0.7710843373493976, 0.7619047619047619, 0.7529411764705882, 0.7441860465116279, 0.735632183908046,
|
|
0.7272727272727273, 0.7191011235955056, 0.7111111111111111, 0.7032967032967034, 0.6956521739130435, 0.6881720430107527,
|
|
0.6808510638297872, 0.6736842105263158, 0.6666666666666666, 0.6597938144329897, 0.6530612244897959, 0.6464646464646465,
|
|
0.64, 0.6336633663366337, 0.6274509803921569, 0.6213592233009708, 0.6153846153846154, 0.6095238095238096,
|
|
0.6037735849056604, 0.5981308411214953, 0.5925925925925926, 0.5871559633027523, 0.5818181818181818, 0.5765765765765766,
|
|
0.5714285714285714, 0.5663716814159292, 0.5614035087719298, 0.5565217391304348, 0.5517241379310345, 0.5470085470085471,
|
|
0.5423728813559322, 0.5378151260504201, 0.5333333333333333, 0.5289256198347108, 0.5245901639344263, 0.5203252032520326,
|
|
0.5161290322580645, 0.512, 0.5079365079365079, 0.5039370078740157
|
|
};
|
|
|
|
// STATIC: 0x009F1D70
|
|
static const double s_log2AddTable[64] = {
|
|
0.0, 0.02236781302845451, 0.044394119358453436, 0.06608919045777244, 0.0874628412503394, 0.10852445677816905,
|
|
0.12928301694496647, 0.14974711950468206, 0.16992500144231237, 0.18982455888001723, 0.20945336562894978,
|
|
0.22881869049588088, 0.2479275134435855, 0.2667865406949014, 0.28540221886224837, 0.30378074817710293,
|
|
0.32192809488736235, 0.33985000288462475, 0.3575520046180837, 0.37503943134692475, 0.3923174227787603,
|
|
0.4093909361377018, 0.42626475470209796, 0.4429434958487283, 0.45943161863729726, 0.47573343096639775,
|
|
0.4918530963296747, 0.5077946401986962, 0.5235619560570128, 0.5391588111080314, 0.5545888516776374,
|
|
0.5698556083309478, 0.5849625007211562, 0.5999128421871277, 0.6147098441152082, 0.6293566200796096,
|
|
0.6438561897747247, 0.6582114827517948, 0.6724253419714956, 0.6865005271832184, 0.7004397181410922,
|
|
0.7142455176661227, 0.7279204545631992, 0.7414669864011469, 0.7548875021634686, 0.7681843247769263,
|
|
0.7813597135246596, 0.794415866350106, 0.8073549220576041, 0.8201789624151877, 0.8328900141647416,
|
|
0.8454900509443752, 0.8579809951275721, 0.8703647195834046, 0.8826430493618412, 0.8948177633079435,
|
|
0.9068905956085185, 0.9188632372745945, 0.9307373375628862, 0.9425145053392399, 0.9541963103868752,
|
|
0.965784284662087, 0.9772799234999164, 0.9886846867721658
|
|
};
|
|
|
|
// VERIFY: b2ec8d01 auto
|
|
// OFFSET: 0x004C4E10
|
|
uint32_t CMath::sqrt_(uint32_t n) {
|
|
uint32_t x;
|
|
|
|
if (n <= 0xFF) {
|
|
x = n / 12 + 1;
|
|
} else if (n <= 0xFFFF) {
|
|
x = n / 200 + 21;
|
|
} else {
|
|
x = n / 26743 + 444;
|
|
}
|
|
|
|
int32_t delta;
|
|
|
|
do {
|
|
uint32_t q = n / x;
|
|
delta = static_cast<int32_t>(x - q) / 2;
|
|
x = static_cast<int32_t>(q + x) / 2;
|
|
} while (delta);
|
|
|
|
return x;
|
|
}
|
|
|
|
// VERIFY: de17052a manual
|
|
// OFFSET: 0x004C4E90
|
|
double CMath::log2_(double x) {
|
|
uint32_t hi = reinterpret_cast<uint32_t*>(&x)[1];
|
|
uint32_t index = (hi >> 14) & 0x3F;
|
|
|
|
double mantissa = x;
|
|
reinterpret_cast<uint32_t*>(&mantissa)[1] = (hi & 0xFFFFF) | 0x3FF00000;
|
|
|
|
double t = s_log2MulTable[index] * mantissa - 1.0;
|
|
double t2 = t * t;
|
|
|
|
if (!(x > 1e-307)) {
|
|
return -HUGE_VAL;
|
|
}
|
|
|
|
int32_t exponent = static_cast<int32_t>((hi >> 20) & 0x7FF) - 1023;
|
|
return t * (t2 * ((0.2883070248990067 - 0.22949900023246153 * t) * t2 + (0.48089833404997356 - 0.36067132973951144 * t)) + (1.442695040888937 - 0.7213475204127876 * t)) + (exponent + s_log2AddTable[index]);
|
|
}
|
|
|
|
// VERIFY: 8b38845f manual
|
|
// OFFSET: 0x004C4F40
|
|
double CMath::exp2_(double x) {
|
|
double biased = x + 1023.0;
|
|
uint32_t exponent = static_cast<uint32_t>(biased) - 1;
|
|
|
|
double fraction = biased - exponent;
|
|
uint32_t index = reinterpret_cast<uint32_t*>(&fraction)[1] & 0xFC000;
|
|
reinterpret_cast<uint32_t*>(&fraction)[1] ^= index;
|
|
|
|
double scale;
|
|
reinterpret_cast<uint32_t*>(&scale)[0] = 0;
|
|
reinterpret_cast<uint32_t*>(&scale)[1] = exponent << 20;
|
|
|
|
double f2 = fraction * fraction;
|
|
double t = s_exp2Table[index >> 14] * scale;
|
|
|
|
if (exponent > 0x7FF) {
|
|
return static_cast<int32_t>(exponent) > 0 ? HUGE_VAL : 0.0;
|
|
}
|
|
|
|
return t * (f2 * ((fraction * 0.0026811946517564956 + 0.005830032491308937) * f2 + (fraction * 0.06087614283854009 + 0.23603244393034895)) + (fraction * 0.6948749415195616 + 0.9997052445684839));
|
|
}
|
|
|
|
// VERIFY: 0ee3a17b auto
|
|
|
|
// VERIFY: c2ddb11f manual
|
|
// OFFSET: 0x004C5000
|
|
bool CMath::solvequad_(float a, float b, float c, float& r0, float& r1) {
|
|
float fourAC = a * c * 4.0f;
|
|
float bb = b * b;
|
|
|
|
if (bb <= fourAC) {
|
|
return false;
|
|
}
|
|
|
|
float root = sqrt(bb - fourAC);
|
|
float q = (b > 0.0f ? b + root : b - root) * -0.5f;
|
|
float inv = 1.0f / (q * a);
|
|
float x0 = q * (inv * q);
|
|
float x1 = c * (a * inv);
|
|
|
|
if (x1 > x0) {
|
|
r0 = x0;
|
|
r1 = x1;
|
|
} else {
|
|
r0 = x1;
|
|
r1 = x0;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
// VERIFY: f11908d7 auto
|
|
// OFFSET: 0x004C5090
|
|
float CMath::normalizeangle0to2pi_(float angle) {
|
|
double result = fmod(angle, 6.2831854820251465);
|
|
|
|
if (result < 0.0) {
|
|
result = result + 6.2831855f;
|
|
}
|
|
|
|
return static_cast<float>(result);
|
|
}
|
|
|
|
// VERIFY: fc69d5aa auto
|
|
// OFFSET: 0x004C50C0
|
|
float CMath::normalizeAngleNegPiToPi_(float angle) {
|
|
double result = fmod(angle, 6.2831854820251465);
|
|
|
|
if (result < -3.1415927f) {
|
|
return static_cast<float>(result + 6.2831855f);
|
|
}
|
|
|
|
if (result > 3.1415927f) {
|
|
result = result - 6.2831855f;
|
|
}
|
|
|
|
return static_cast<float>(result);
|
|
}
|