| 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 |
|
| 8 |
/* |
| 9 |
* |
| 10 |
* C version of fixed point library |
| 11 |
* |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <cstdint> |
| 15 |
#include <stdlib.h> |
| 16 |
#include <math.h> |
| 17 |
#include <type_traits> |
| 18 |
|
| 19 |
#include "maths.h" |
| 20 |
|
| 21 |
namespace dcx { |
| 22 |
|
| 23 |
namespace { |
| 24 |
|
| 25 |
class fix_sincos_input |
| 26 |
{ |
| 27 |
public: |
| 28 |
const uint8_t m_idx; |
| 29 |
const signed m_mul; |
| 30 |
fix_sincos_input(const fixang a) : |
| 31 |
m_idx{static_cast<uint8_t>(a >> 8)}, |
| 32 |
m_mul{static_cast<uint8_t>(a)} |
| 33 |
{ |
| 34 |
} |
| 35 |
}; |
| 36 |
|
| 37 |
} |
| 38 |
|
| 39 |
fix64 fixmul64(fix a, fix b) |
| 40 |
{ |
| 41 |
return (fix64{a} * fix64{b}) / 65536; |
| 42 |
} |
| 43 |
|
| 44 |
fix fixdiv(fix a, fix b) |
| 45 |
{ |
| 46 |
if (!b) |
| 47 |
{ |
| 48 |
[[unlikely]]; |
| 49 |
return 1; |
| 50 |
} |
| 51 |
return static_cast<fix>((fix64{a} * 65536) / b); |
| 52 |
} |
| 53 |
|
| 54 |
fix fixmuldiv(fix a, fix b, fix c) |
| 55 |
{ |
| 56 |
if (!c) |
| 57 |
{ |
| 58 |
[[unlikely]]; |
| 59 |
return 1; |
| 60 |
} |
| 61 |
return static_cast<fix>((fix64{a} * b) / c); |
| 62 |
} |
| 63 |
|
| 64 |
//given cos & sin of an angle, return that angle. |
| 65 |
//parms need not be normalized, that is, the ratio of the parms cos/sin must |
| 66 |
//equal the ratio of the actual cos & sin for the result angle, but the parms |
| 67 |
//need not be the actual cos & sin. |
| 68 |
//NOTE: this is different from the standard C atan2, since it is left-handed. |
| 69 |
|
| 70 |
fixang fix_atan2(fix cos,fix sin) |
| 71 |
{ |
| 72 |
//find smaller of two |
| 73 |
|
| 74 |
const auto dsin{static_cast<double>(sin)}; |
| 75 |
const auto dcos{static_cast<double>(cos)}; |
| 76 |
const double d{sqrt((dsin * dsin) + (dcos * dcos))}; |
| 77 |
|
| 78 |
if (d==0.0) |
| 79 |
return 0; |
| 80 |
|
| 81 |
if (labs(sin) < labs(cos)) { //sin is smaller, use arcsin |
| 82 |
const auto t{fix_asin(static_cast<fix>((dsin / d) * 65536.0))}; |
| 83 |
return cos < 0 ? static_cast<fixang>(0x8000 - t) : t; |
| 84 |
} |
| 85 |
else { |
| 86 |
const auto t{fix_acos(static_cast<fix>((dcos / d) * 65536.0))}; |
| 87 |
return sin < 0 ? static_cast<fixang>(-t) : t; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
[[nodiscard]] |
| 92 |
static unsigned fixdivquadlongu(quadint n, uint64_t d) |
| 93 |
{ |
| 94 |
return static_cast<std::underlying_type_t<quadint>>(n) / d; |
| 95 |
} |
| 96 |
|
| 97 |
uint32_t quad_sqrt(const quadint iq) |
| 98 |
{ |
| 99 |
const uint32_t low{static_cast<uint32_t>(iq)}; |
| 100 |
const int32_t high{static_cast<int32_t>(static_cast<std::underlying_type_t<quadint>>(iq) >> 32)}; |
| 101 |
int i, cnt; |
| 102 |
uint32_t r,old_r,t; |
| 103 |
|
| 104 |
if (high<0) |
| 105 |
return 0; |
| 106 |
|
| 107 |
if (high==0 && static_cast<int32_t>(low)>=0) |
| 108 |
return long_sqrt(static_cast<int32_t>(low)); |
| 109 |
|
| 110 |
if ((i = high >> 24)) { |
| 111 |
cnt=12+16; |
| 112 |
} |
| 113 |
else if ((i = high >> 16)) |
| 114 |
{ |
| 115 |
cnt=8+16; |
| 116 |
} |
| 117 |
else if ((i = high >> 8)) |
| 118 |
{ |
| 119 |
cnt=4+16; |
| 120 |
} else { |
| 121 |
cnt=0+16; i = high; |
| 122 |
} |
| 123 |
|
| 124 |
r = guess_table[i]<<cnt; |
| 125 |
|
| 126 |
//quad loop usually executed 4 times |
| 127 |
|
| 128 |
r = fixdivquadlongu(iq,r)/2 + r/2; |
| 129 |
r = fixdivquadlongu(iq,r)/2 + r/2; |
| 130 |
r = fixdivquadlongu(iq,r)/2 + r/2; |
| 131 |
|
| 132 |
do { |
| 133 |
|
| 134 |
old_r = r; |
| 135 |
t = fixdivquadlongu(iq,r); |
| 136 |
|
| 137 |
if (t==r) //got it! |
| 138 |
return r; |
| 139 |
|
| 140 |
r = t/2 + r/2; |
| 141 |
|
| 142 |
} while (!(r==t || r==old_r)); |
| 143 |
|
| 144 |
t = fixdivquadlongu(iq,r); |
| 145 |
const auto tq{fixmulaccum({}, r, t)}; |
| 146 |
if (tq != iq) |
| 147 |
r++; |
| 148 |
|
| 149 |
return r; |
| 150 |
} |
| 151 |
|
| 152 |
//computes the square root of a long, returning a short |
| 153 |
ushort long_sqrt(int32_t a) |
| 154 |
{ |
| 155 |
int cnt,r,old_r,t; |
| 156 |
|
| 157 |
if (a<=0) |
| 158 |
return 0; |
| 159 |
|
| 160 |
if (a & 0xff000000) |
| 161 |
cnt=12; |
| 162 |
else if (a & 0xff0000) |
| 163 |
cnt=8; |
| 164 |
else if (a & 0xff00) |
| 165 |
cnt=4; |
| 166 |
else |
| 167 |
cnt=0; |
| 168 |
|
| 169 |
r = guess_table[(a>>cnt)&0xff]<<cnt; |
| 170 |
|
| 171 |
//the loop nearly always executes 3 times, so we'll unroll it 2 times and |
| 172 |
//not do any checking until after the third time. By my calcutations, the |
| 173 |
//loop is executed 2 times in 99.97% of cases, 3 times in 93.65% of cases, |
| 174 |
//four times in 16.18% of cases, and five times in 0.44% of cases. It never |
| 175 |
//executes more than five times. By timing, I determined that is is faster |
| 176 |
//to always execute three times and not check for termination the first two |
| 177 |
//times through. This means that in 93.65% of cases, we save 6 cmp/jcc pairs, |
| 178 |
//and in 6.35% of cases we do an extra divide. In real life, these numbers |
| 179 |
//might not be the same. |
| 180 |
|
| 181 |
r = ((a/r)+r)/2; |
| 182 |
r = ((a/r)+r)/2; |
| 183 |
|
| 184 |
do { |
| 185 |
|
| 186 |
old_r = r; |
| 187 |
t = a/r; |
| 188 |
|
| 189 |
if (t==r) //got it! |
| 190 |
return r; |
| 191 |
|
| 192 |
r = (t+r)/2; |
| 193 |
|
| 194 |
} while (!(r==t || r==old_r)); |
| 195 |
|
| 196 |
if (a % r) |
| 197 |
r++; |
| 198 |
|
| 199 |
return r; |
| 200 |
} |
| 201 |
|
| 202 |
//computes the square root of a fix, returning a fix |
| 203 |
fix fix_sqrt(fix a) |
| 204 |
{ |
| 205 |
return static_cast<fix>(long_sqrt(a)) << 8; |
| 206 |
} |
| 207 |
|
| 208 |
[[nodiscard]] |
| 209 |
static fix fix_sincos(const uint8_t idx0, const signed mul) |
| 210 |
{ |
| 211 |
const fix t0{sincos_table[idx0]}; |
| 212 |
/* `idx1` is `uint8_t` to truncate the value, since sincos_table is |
| 213 |
* only 256 elements long. |
| 214 |
*/ |
| 215 |
const uint8_t idx1 = idx0 + 1; |
| 216 |
const fix t1{sincos_table[idx1]}; |
| 217 |
return (t0 + (((t1 - t0) * mul) >> 8)) << 2; |
| 218 |
} |
| 219 |
|
| 220 |
[[nodiscard]] |
| 221 |
static fix fix_sin(const fix_sincos_input sci) |
| 222 |
{ |
| 223 |
return fix_sincos(sci.m_idx, sci.m_mul); |
| 224 |
} |
| 225 |
|
| 226 |
[[nodiscard]] |
| 227 |
static fix fix_cos(const fix_sincos_input sci) |
| 228 |
{ |
| 229 |
return fix_sincos(static_cast<uint8_t>(sci.m_idx + 64), sci.m_mul); |
| 230 |
} |
| 231 |
|
| 232 |
//compute sine and cosine of an angle, filling in the variables |
| 233 |
//either of the pointers can be NULL |
| 234 |
//with interpolation |
| 235 |
fix_sincos_result fix_sincos(const fixang a) |
| 236 |
{ |
| 237 |
const fix_sincos_input i{a}; |
| 238 |
return {fix_sin(i), fix_cos(i)}; |
| 239 |
} |
| 240 |
|
| 241 |
fix fix_sin(const fixang a) |
| 242 |
{ |
| 243 |
return {fix_sin(fix_sincos_input{a})}; |
| 244 |
} |
| 245 |
|
| 246 |
fix fix_cos(const fixang a) |
| 247 |
{ |
| 248 |
return {fix_cos(fix_sincos_input{a})}; |
| 249 |
} |
| 250 |
|
| 251 |
//compute sine and cosine of an angle, filling in the variables |
| 252 |
//no interpolation |
| 253 |
fix fix_fastsin(const fixang a) |
| 254 |
{ |
| 255 |
const uint8_t i = static_cast<uint8_t>(a >> 8); |
| 256 |
return sincos_table[i] << 2; |
| 257 |
} |
| 258 |
|
| 259 |
//compute inverse sine |
| 260 |
fixang fix_asin(fix v) |
| 261 |
{ |
| 262 |
fix vv; |
| 263 |
int i,f,aa; |
| 264 |
|
| 265 |
vv = labs(v); |
| 266 |
|
| 267 |
if (vv >= f1_0) //check for out of range |
| 268 |
return 0x4000; |
| 269 |
|
| 270 |
i = (vv>>8)&0xff; |
| 271 |
f = vv&0xff; |
| 272 |
|
| 273 |
aa = asin_table[i]; |
| 274 |
aa = aa + (((asin_table[i+1] - aa) * f)>>8); |
| 275 |
|
| 276 |
if (v < 0) |
| 277 |
aa = -aa; |
| 278 |
|
| 279 |
return aa; |
| 280 |
} |
| 281 |
|
| 282 |
//compute inverse cosine |
| 283 |
fixang fix_acos(fix v) |
| 284 |
{ |
| 285 |
fix vv; |
| 286 |
int i,f,aa; |
| 287 |
|
| 288 |
vv = labs(v); |
| 289 |
|
| 290 |
if (vv >= f1_0) //check for out of range |
| 291 |
return 0; |
| 292 |
|
| 293 |
i = (vv>>8)&0xff; |
| 294 |
f = vv&0xff; |
| 295 |
|
| 296 |
aa = acos_table[i]; |
| 297 |
aa = aa + (((acos_table[i+1] - aa) * f)>>8); |
| 298 |
|
| 299 |
if (v < 0) |
| 300 |
aa = 0x8000 - aa; |
| 301 |
|
| 302 |
return aa; |
| 303 |
} |
| 304 |
|
| 305 |
} |