mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2025-05-19 01:15:08 -04:00
kern: optimize timespan -> tick codegen, improve .text layout
This commit is contained in:
parent
89926f44c6
commit
aaa3770806
5 changed files with 88 additions and 45 deletions
|
@ -255,4 +255,28 @@ namespace ams::util {
|
|||
return static_cast<T>((v + add) / d);
|
||||
}
|
||||
|
||||
template<typename T, T N, T D>
|
||||
constexpr ALWAYS_INLINE T ScaleByConstantFactor(const T V) {
|
||||
/* Multiplying and dividing by large numerator/denominator can cause error to be introduced. */
|
||||
/* This algorithm multiples/divides in stages, so as to mitigate this (particularly with large denominator). */
|
||||
|
||||
/* Justification for the algorithm. */
|
||||
/* Calculate: (V * N) / D */
|
||||
/* = (Quot_V * D + Rem_V) * (Quot_N * D + Rem_N) / D */
|
||||
/* = (D^2 * (Quot_V * Quot_N) + D * (Quot_V * Rem_N + Rem_V * Quot_N) + Rem_V * Rem_N) / D */
|
||||
/* = (D * Quot_V * Quot_N) + (Quot_V * Rem_N) + (Rem_V * Quot_N) + ((Rem_V * Rem_N) / D) */
|
||||
|
||||
/* Calculate quotients/remainders. */
|
||||
const T Quot_V = V / D;
|
||||
const T Rem_V = V % D;
|
||||
constexpr T Quot_N = N / D;
|
||||
constexpr T Rem_N = N % D;
|
||||
|
||||
/* Calculate the remainder multiplication, rounding up. */
|
||||
const T rem_mult = ((Rem_V * Rem_N) + (D - 1)) / D;
|
||||
|
||||
/* Calculate results. */
|
||||
return (D * Quot_N * Quot_V) + (Quot_V * Rem_N) + (Rem_V * Quot_N) + rem_mult;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue