sead
Loading...
Searching...
No Matches
seadMathCalcCommon.hpp
Go to the documentation of this file.
1
#
pragma
once
2
3
#
ifdef
cafe
4
#
include
<
math
/
cafe
/
seadMathCalcCommonCafe
.
h
>
5
#
endif
// cafe
6
7
#
include
<
basis
/
seadAssert
.
h
>
8
9
#
include
<
cmath
>
10
#
include
<
limits
>
11
#
include
<
bit
>
12
13
namespace
sead
{
14
15
template
<
typename
T
>
16
inline
constexpr
T
17
MathCalcCommon
<
T
>::
sqrt
(
T
t
)
18
{
19
return
std
::
sqrt
(
t
);
20
}
21
22
template
<
typename
T
>
23
inline
constexpr
T
24
MathCalcCommon
<
T
>::
rsqrt
(
T
t
)
25
{
26
return
1 /
std
::
sqrt
(
t
);
27
}
28
29
#
ifdef
cafe
30
31
template
<>
32
inline
constexpr
f32
33
MathCalcCommon
<
f32
>::
sqrt
(
f32
t
)
34
{
35
return
MathCafe
<
f32
>::
rsqrt
(
t
) *
t
;
36
}
37
38
template
<>
39
inline
constexpr
f32
40
MathCalcCommon
<
f32
>::
rsqrt
(
f32
t
)
41
{
42
return
MathCafe
<
f32
>::
rsqrt
(
t
);
43
}
44
45
#
endif
// cafe
46
47
template
<
typename
T
>
48
inline
constexpr
T
49
MathCalcCommon
<
T
>::
pow
(
T
x
,
T
y
)
50
{
51
return
std
::
pow
(
x
,
y
);
52
}
53
54
template
<
typename
T
>
55
inline
constexpr
T
56
MathCalcCommon
<
T
>::
sin
(
T
t
)
57
{
58
return
std
::
sin
(
t
);
59
}
60
61
template
<
typename
T
>
62
inline
constexpr
T
63
MathCalcCommon
<
T
>::
cos
(
T
t
)
64
{
65
return
std
::
cos
(
t
);
66
}
67
68
template
<
typename
T
>
69
inline
constexpr
T
70
MathCalcCommon
<
T
>::
tan
(
T
t
)
71
{
72
return
std
::
tan
(
t
);
73
}
74
75
template
<
typename
T
>
76
inline
constexpr
T
77
MathCalcCommon
<
T
>::
asin
(
T
s
)
78
{
79
return
std
::
asin
(
s
);
80
}
81
82
template
<
typename
T
>
83
inline
constexpr
T
84
MathCalcCommon
<
T
>::
acos
(
T
c
)
85
{
86
return
std
::
acos
(
c
);
87
}
88
89
template
<
typename
T
>
90
inline
constexpr
T
91
MathCalcCommon
<
T
>::
atan
(
T
t
)
92
{
93
return
std
::
atan
(
t
);
94
}
95
96
template
<
typename
T
>
97
inline
constexpr
T
98
MathCalcCommon
<
T
>::
atan2
(
T
y
,
T
x
)
99
{
100
return
std
::
atan2
(
y
,
x
);
101
}
102
103
template
<>
104
inline
f32
105
MathCalcCommon
<
f32
>::
sinIdx
(
u32
idx
)
106
{
107
u32
index
= (
idx
>> 24) & 0xff;
108
u32
rest
=
idx
& 0xffffff;
109
110
return
cSinCosTbl
[
index
].
sin_val
+
cSinCosTbl
[
index
].
sin_delta
* (
static_cast
<
float
>(
rest
) /
static_cast
<
float
>(0x1000000));
111
}
112
113
template
<>
114
inline
f32
115
MathCalcCommon
<
f32
>::
cosIdx
(
u32
idx
)
116
{
117
u32
index
= (
idx
>> 24) & 0xff;
118
u32
rest
=
idx
& 0xffffff;
119
120
return
cSinCosTbl
[
index
].
cos_val
+
cSinCosTbl
[
index
].
cos_delta
* (
static_cast
<
float
>(
rest
) /
static_cast
<
float
>(0x1000000));
121
}
122
123
template
<>
124
inline
f32
125
MathCalcCommon
<
f32
>::
tanIdx
(
u32
idx
)
126
{
127
u32
index
= (
idx
>> 24) & 0xff;
128
f32
rest
=
static_cast
<
f32
>(
idx
& 0xffffff) / 0x1000000;
129
const
SinCosSample
&
sample
=
cSinCosTbl
[
index
];
130
131
return
(
sample
.
sin_val
+
sample
.
sin_delta
*
rest
) / (
sample
.
cos_val
+
sample
.
cos_delta
*
rest
);
132
}
133
134
template
<>
135
inline
u32
136
MathCalcCommon
<
f32
>::
asinIdx
(
f32
s
)
137
{
138
SEAD_ASSERT_MSG
(
s
<= 1 &&
s
>= -1,
"s(%f) is not in [-1, 1]."
,
s
);
139
140
const
f32
rsqrt_2
= 0.7071067690849304f;
// rsqrt(2)
141
142
if
(
s
>= 0)
143
{
144
if
(
s
>
rsqrt_2
)
145
return
0x40000000 -
atanIdx_
(
sqrt
(1 -
s
*
s
) /
s
);
146
147
else
148
return
atanIdx_
(
s
/
sqrt
(1 -
s
*
s
));
149
}
150
else
151
{
152
if
(
s
< -
rsqrt_2
)
153
return
0xC0000000 +
atanIdx_
(-
sqrt
(1 -
s
*
s
) /
s
);
154
155
else
156
return
-
atanIdx_
(-
s
/
sqrt
(1 -
s
*
s
));
157
}
158
}
159
160
template
<>
161
inline
u32
162
MathCalcCommon
<
f32
>::
acosIdx
(
f32
c
)
163
{
164
SEAD_ASSERT_MSG
(
c
<= 1 &&
c
>= -1,
"c(%f) is not in [-1, 1]."
,
c
);
165
166
const
f32
rsqrt_2
= 0.7071067690849304f;
// rsqrt(2)
167
168
if
(
c
>= 0)
169
{
170
if
(
c
>
rsqrt_2
)
171
return
atanIdx_
(
sqrt
(1 -
c
*
c
) /
c
);
172
173
else
174
return
0x40000000 -
atanIdx_
(
c
/
sqrt
(1 -
c
*
c
));
175
}
176
else
177
{
178
if
(
c
< -
rsqrt_2
)
179
return
0x80000000 -
atanIdx_
(-
sqrt
(1 -
c
*
c
) /
c
);
180
181
else
182
return
0x40000000 +
atanIdx_
(-
c
/
sqrt
(1 -
c
*
c
));
183
}
184
}
185
186
template
<>
187
inline
u32
188
MathCalcCommon
<
f32
>::
atanIdx
(
f32
t
)
189
{
190
if
(
t
>= 0)
191
{
192
if
(
t
> 1)
193
return
0x40000000 -
atanIdx_
(1 /
t
);
194
195
else
196
return
atanIdx_
(
t
);
197
}
198
else
199
{
200
if
(
t
< -1)
201
return
0xC0000000 +
atanIdx_
(-1 /
t
);
202
203
else
204
return
-
atanIdx_
(-
t
);
205
}
206
}
207
208
template
<>
209
inline
u32
210
MathCalcCommon
<
f32
>::
atan2Idx
(
f32
y
,
f32
x
)
211
{
212
#
pragma
clang
diagnostic
push
213
#
pragma
clang
diagnostic
ignored
"-Wfloat-equal"
214
if
(
x
== 0 &&
y
== 0)
215
return
0;
216
#
pragma
clang
diagnostic
pop
217
218
if
(
x
>= 0)
219
{
220
if
(
y
>= 0)
221
{
222
if
(
x
>=
y
)
223
return
atanIdx_
(
y
/
x
);
224
225
else
226
return
0x40000000 -
atanIdx_
(
x
/
y
);
227
}
228
else
229
{
230
if
(
x
>= -
y
)
231
return
-
atanIdx_
(-
y
/
x
);
232
233
else
234
return
0xC0000000 +
atanIdx_
(
x
/ -
y
);
235
}
236
}
237
else
238
{
239
if
(
y
>= 0)
240
{
241
if
(-
x
>=
y
)
242
return
0x80000000 -
atanIdx_
(
y
/ -
x
);
243
244
else
245
return
0x40000000 +
atanIdx_
(-
x
/
y
);
246
}
247
else
248
{
249
if
(
x
<=
y
)
250
return
0x80000000 +
atanIdx_
(
y
/
x
);
251
252
else
253
return
0xC0000000 -
atanIdx_
(
x
/
y
);
254
}
255
}
256
}
257
258
template
<>
259
inline
void
260
MathCalcCommon
<
f32
>::
sinCosIdx
(
f32
*
p_sin
,
f32
*
p_cos
,
u32
idx
)
261
{
262
u32
index
= (
idx
>> 24) & 0xff;
263
f32
rest
=
static_cast
<
f32
>(
idx
& 0xffffff) / 0x1000000;
264
const
SinCosSample
&
sample
=
cSinCosTbl
[
index
];
265
266
/*if (p_sin != nullptr)*/
*
p_sin
=
sample
.
sin_val
+
sample
.
sin_delta
*
rest
;
267
/*if (p_cos != nullptr)*/
*
p_cos
=
sample
.
cos_val
+
sample
.
cos_delta
*
rest
;
268
}
269
270
template
<
typename
T
>
271
inline
constexpr
T
272
MathCalcCommon
<
T
>::
exp
(
T
t
)
273
{
274
return
std
::
exp
(
t
);
275
}
276
277
template
<
typename
T
>
278
inline
constexpr
T
279
MathCalcCommon
<
T
>::
log
(
T
t
)
280
{
281
return
std
::
log
(
t
);
282
}
283
284
template
<
typename
T
>
285
inline
constexpr
T
286
MathCalcCommon
<
T
>::
log2
(
T
t
)
287
{
288
return
std
::
log2
(
t
);
289
}
290
291
template
<
typename
T
>
292
inline
constexpr
T
293
MathCalcCommon
<
T
>::
log10
(
T
t
)
294
{
295
return
std
::
log10
(
t
);
296
}
297
298
template
<
typename
T
>
299
inline
constexpr
T
300
MathCalcCommon
<
T
>::
minNumber
()
301
{
302
return
std
::
numeric_limits
<
T
>::
min
();
303
}
304
305
template
<
typename
T
>
306
inline
constexpr
T
307
MathCalcCommon
<
T
>::
maxNumber
()
308
{
309
return
std
::
numeric_limits
<
T
>::
max
();
310
}
311
312
template
<>
313
inline
constexpr
float
314
MathCalcCommon
<
float
>::
minNumber
()
315
{
316
return
-
std
::
numeric_limits
<
float
>::
max
();
317
}
318
319
template
<>
320
inline
constexpr
float
321
MathCalcCommon
<
float
>::
maxNumber
()
322
{
323
return
std
::
numeric_limits
<
float
>::
max
();
324
}
325
326
template
<>
327
inline
constexpr
double
328
MathCalcCommon
<
double
>::
minNumber
()
329
{
330
return
-
std
::
numeric_limits
<
double
>::
max
();
331
}
332
333
template
<>
334
inline
constexpr
double
335
MathCalcCommon
<
double
>::
maxNumber
()
336
{
337
return
std
::
numeric_limits
<
double
>::
max
();
338
}
339
340
template
<>
341
inline
constexpr
long
double
342
MathCalcCommon
<
long
double
>::
minNumber
()
343
{
344
return
-
std
::
numeric_limits
<
long
double
>::
max
();
345
}
346
347
template
<>
348
inline
constexpr
long
double
349
MathCalcCommon
<
long
double
>::
maxNumber
()
350
{
351
return
std
::
numeric_limits
<
long
double
>::
max
();
352
}
353
354
template
<
typename
T
>
355
inline
constexpr
T
356
MathCalcCommon
<
T
>::
infinity
()
357
{
358
return
std
::
numeric_limits
<
T
>::
infinity
();
359
}
360
361
template
<>
362
inline
constexpr
f32
363
MathCalcCommon
<
f32
>::
nan
()
364
{
365
return
std
::
bit_cast
<
f32
>(0x7FFFFFFF);
366
}
367
368
template
<>
369
inline
constexpr
f64
370
MathCalcCommon
<
f64
>::
nan
()
371
{
372
return
std
::
bit_cast
<
f64
>(0x7FFFFFFFFFFFFFFF);
373
}
374
375
template
<
typename
T
>
376
inline
constexpr
T
377
MathCalcCommon
<
T
>::
epsilon
()
378
{
379
return
std
::
numeric_limits
<
T
>::
epsilon
();
380
}
381
382
template
<>
383
inline
constexpr
s32
384
MathCalcCommon
<
s32
>::
abs
(
s32
t
)
385
{
386
return
(
t
^
t
>> 31) - (
t
>> 31);
387
}
388
389
template
<>
390
inline
constexpr
u32
391
MathCalcCommon
<
u32
>::
abs
(
u32
t
)
392
{
393
return
t
;
394
}
395
396
#
ifdef
cafe
397
398
template
<>
399
inline
constexpr
f32
400
MathCalcCommon
<
f32
>::
abs
(
f32
t
)
401
{
402
return
std
::
fabs
(
t
);
403
}
404
405
template
<>
406
inline
constexpr
f64
407
MathCalcCommon
<
f64
>::
abs
(
f64
t
)
408
{
409
return
std
::
fabs
(
t
);
410
}
411
412
#
endif
// cafe
413
414
template
<
typename
T
>
415
inline
constexpr
s32
416
MathCalcCommon
<
T
>::
roundOff
(
T
val
)
417
{
418
return
std
::
floor
(
val
+ 0.5f);
419
}
420
421
template
<>
422
inline
constexpr
s32
423
MathCalcCommon
<
s32
>::
roundOff
(
s32
val
)
424
{
425
return
val
;
426
}
427
428
//template <>
429
//inline s32
430
//MathCalcCommon<u32>::roundOff(u32 val)
431
//{
432
// return val;
433
//}
434
435
template
<
typename
T
>
436
inline
constexpr
s32
437
MathCalcCommon
<
T
>::
floor
(
T
val
)
438
{
439
return
std
::
floor
(
val
);
440
}
441
442
template
<>
443
inline
constexpr
s32
444
MathCalcCommon
<
s32
>::
floor
(
s32
val
)
445
{
446
return
val
;
447
}
448
449
//template <>
450
//inline s32
451
//MathCalcCommon<u32>::floor(u32 val)
452
//{
453
// return val;
454
//}
455
456
template
<
typename
T
>
457
inline
constexpr
s32
458
MathCalcCommon
<
T
>::
ceil
(
T
val
)
459
{
460
return
std
::
ceil
(
val
);
461
}
462
463
template
<>
464
inline
constexpr
s32
465
MathCalcCommon
<
s32
>::
ceil
(
s32
val
)
466
{
467
return
val
;
468
}
469
470
//template <>
471
//inline s32
472
//MathCalcCommon<u32>::ceil(u32 val)
473
//{
474
// return val;
475
//}
476
477
template
<>
478
inline
constexpr
s32
479
MathCalcCommon
<
s32
>::
roundUpPow2
(
s32
val
,
s32
base
)
480
{
481
SEAD_ASSERT_MSG
(
val
>= 0 && (
base
- 1u &
base
) == 0,
"illegal param[val:%d, base:%d]"
,
val
,
base
);
482
return
static_cast
<
s32
>((
static_cast
<
u32
>(
val
) + (
static_cast
<
u32
>(
base
) - 1u)) & ~(
static_cast
<
u32
>(
base
) - 1u));
483
}
484
485
template
<>
486
inline
constexpr
u32
487
MathCalcCommon
<
u32
>::
roundUpPow2
(
u32
val
,
s32
base
)
488
{
489
SEAD_ASSERT_MSG
((
base
- 1u &
base
) == 0,
"illegal param[base:%d]"
,
base
);
490
return
val
+ (
static_cast
<
u32
>(
base
) - 1u) & ~(
static_cast
<
u32
>(
base
) - 1u);
491
}
492
493
template
<
typename
T
>
494
inline
constexpr
T
495
MathCalcCommon
<
T
>::
clampMax
(
T
val
,
T
max_
)
496
{
497
if
(
val
>
max_
)
val
=
max_
;
498
499
return
val
;
500
}
501
502
template
<
typename
T
>
503
inline
constexpr
T
504
MathCalcCommon
<
T
>::
clampMin
(
T
val
,
T
min_
)
505
{
506
if
(
val
<
min_
)
val
=
min_
;
507
508
return
val
;
509
}
510
511
template
<
typename
T
>
512
inline
constexpr
T
513
MathCalcCommon
<
T
>::
clamp2
(
T
min_
,
T
val
,
T
max_
)
514
{
515
if
(
val
<
min_
)
val
=
min_
;
516
else
if
(
val
>
max_
)
val
=
max_
;
517
518
return
val
;
519
}
520
521
}
// namespace sead
sead
Definition
seadAssert.h:44
SEAD_ASSERT_MSG
#define SEAD_ASSERT_MSG(condition, format,...)
Definition
seadAssert.h:33
engine
library
include
math
seadMathCalcCommon.hpp
Generated by
1.14.0