kern: optimize timespan -> tick codegen, improve .text layout

This commit is contained in:
Michael Scire 2021-10-24 01:04:19 -07:00
parent 89926f44c6
commit aaa3770806
5 changed files with 88 additions and 45 deletions

View file

@ -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;
}
}