NSMBW-Decomp
A decompilation of New Super Mario Bros. Wii
Loading...
Searching...
No Matches
math_arithmetic.h
1#ifndef NW4R_MATH_ARITHMETIC_H
2#define NW4R_MATH_ARITHMETIC_H
3#include <nw4r/types_nw4r.h>
4
5#include <nw4r/math/math_constant.h>
6
7#include <revolution/OS.h>
8
9#include <cmath>
10
11namespace nw4r {
12namespace math {
13
14/******************************************************************************
15 *
16 * Implementation details
17 *
18 ******************************************************************************/
19namespace detail {
20
21f32 FExp(f32 x);
22f32 FLog(f32 x);
23
24} // namespace detail
25
26/******************************************************************************
27 *
28 * Arithmetic functions
29 *
30 ******************************************************************************/
31f32 FrSqrt(f32 x);
32
33inline f32 FAbs(register f32 x) {
34 register f32 ax;
35
36 // clang-format off
37 asm {
38 fabs ax, x
39 }
40 // clang-format on
41
42 return ax;
43}
44
45inline f32 FCeil(f32 x) {
46 return std::ceilf(x);
47}
48
49inline f32 FExp(f32 x) {
50 return detail::FExp(x);
51}
52
53inline f32 FFloor(f32 x) {
54 return std::floorf(x);
55}
56
57inline f32 FInv(register f32 x) {
58 register f32 work0, work1, work2, work3;
59
60 // clang-format off
61 asm {
62 fmr work1, x // x
63 fres work0, work1 // 1/x
64
65 // Refine estimate
66 ps_add work2, work0, work0 // 2/x
67 ps_mul work3, work0, work0 // 1/x^2
68 ps_nmsub work0, work1, work3, work2 // -(x * 1/x^2 - 2/x)
69 }
70 // clang-format on
71
72 return work0;
73}
74
75inline f32 FMod(f32 x, f32 y) {
76 return std::fmodf(x, y);
77}
78
79inline f32 FModf(f32 x, f32* pY) {
80 return std::modff(x, pY);
81}
82
83inline f32 FSqrt(f32 x) {
84 return x <= 0.0f ? 0.0f : x * FrSqrt(x);
85}
86
87inline f32 FLog(f32 x) {
88 if (x > 0.0f) {
89 return detail::FLog(x);
90 }
91
92 return NW4R_MATH_QNAN;
93}
94
95inline f32 FSelect(register f32 value, register f32 ge_zero,
96 register f32 lt_zero) {
97 register f32 ret;
98
99 // clang-format off
100 asm {
101 fsel ret, value, ge_zero, lt_zero
102 }
103 // clang-format on
104
105 return ret;
106}
107
108/******************************************************************************
109 *
110 * Fastcast functions
111 *
112 ******************************************************************************/
113inline f32 U16ToF32(u16 arg) {
114 f32 ret;
115 OSu16tof32(&arg, &ret);
116 return ret;
117}
118inline u16 F32ToU16(f32 arg) {
119 u16 ret;
120 OSf32tou16(&arg, &ret);
121 return ret;
122}
123
124inline f32 S16ToF32(s16 arg) {
125 f32 ret;
126 OSs16tof32(&arg, &ret);
127 return ret;
128}
129inline s16 F32ToS16(f32 arg) {
130 s16 ret;
131 OSf32tos16(&arg, &ret);
132 return ret;
133}
134
135inline ulong F32Asulong(f32 arg) {
136 return *reinterpret_cast<ulong*>(&arg);
137}
138inline f32 ulongAsF32(ulong arg) {
139 return *reinterpret_cast<f32*>(&arg);
140}
141
142inline s32 FGetExpPart(f32 x) {
143 s32 s = F32Asulong(x);
144 return ((s >> 23) & 0xFF) - 127;
145}
146inline f32 FGetMantPart(f32 x) {
147 ulong u = F32Asulong(x);
148 return ulongAsF32((u & 0x807FFFFF) | 0x3F800000);
149}
150
151} // namespace math
152} // namespace nw4r
153
154#endif
Math library.