NSMBW-Decomp
A decompilation of New Super Mario Bros. Wii
Loading...
Searching...
No Matches
arithmetic.hpp
Go to the documentation of this file.
1#pragma once
2#include <types.h>
3/// @file
4
5namespace nw4r {
6namespace math {
7
8/// @brief Computes the inverse square root of the given value.
9float FrSqrt(float x);
10
11/// @brief Computes the square root of the given value.
12inline float FSqrt(float x) {
13 return (x <= 0) ? 0.0f : x * FrSqrt(x);
14}
15
16/// @brief Computes 1 / n efficiently with fres + 1 Newton iteration
17inline float FInv(register float n) {
18 register float x0, tmp1, tmp2;
19 asm {
20 // Approximate with fres, then use Newton iteration (https://cr.yp.to/2005-590/powerpc-cwg.pdf 3.3.6.3)
21 // https://en.wikipedia.org/wiki/Division_algorithm#Newton%E2%80%93Raphson_division
22 // Want to calculate x_1 = 2 * x_0 - n * x_0 ^ 2
23
24 fres x0, n
25
26 ps_add tmp1, x0, x0 // tmp1 = 2 * x_0
27 ps_mul tmp2, x0, x0 // tmp2 = x_0 ^ 2
28 ps_nmsub x0, n, tmp2, tmp1 // x_1 = -(n * tmp2 - tmp1) = tmp1 - n * tmp2
29 }
30
31 return x0;
32}
33
34} // namespace math
35} // namespace nw4r
Math library.
Definition aabb.hpp:6
float FSqrt(float x)
Computes the square root of the given value.
float FInv(register float n)
Computes 1 / n efficiently with fres + 1 Newton iteration.
float FrSqrt(float x)
Computes the inverse square root of the given value.