FloatingPointHelper.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // Copyright (c) 2019-2020 Ryujinx
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. //
  17. using System;
  18. using System.Runtime.CompilerServices;
  19. namespace Ryujinx.Audio.Renderer.Dsp
  20. {
  21. public static class FloatingPointHelper
  22. {
  23. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  24. public static float MultiplyRoundDown(float a, float b)
  25. {
  26. return RoundDown(a * b);
  27. }
  28. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  29. public static float RoundDown(float a)
  30. {
  31. return MathF.Round(a, 0);
  32. }
  33. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  34. public static float RoundUp(float a)
  35. {
  36. return MathF.Round(a);
  37. }
  38. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  39. public static float MultiplyRoundUp(float a, float b)
  40. {
  41. return RoundUp(a * b);
  42. }
  43. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  44. public static float Pow10(float x)
  45. {
  46. // NOTE: Nintendo implementation uses Q15 and a LUT for this, we don't.
  47. // As such, we support the same ranges as Nintendo to avoid unexpected behaviours.
  48. if (x >= 0.0f)
  49. {
  50. return 1.0f;
  51. }
  52. else if (x <= -5.3f)
  53. {
  54. return 0.0f;
  55. }
  56. return MathF.Pow(10, x);
  57. }
  58. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  59. public static float DegreesToRadians(float degrees)
  60. {
  61. return degrees * MathF.PI / 180.0f;
  62. }
  63. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  64. public static float Cos(float value)
  65. {
  66. return MathF.Cos(DegreesToRadians(value));
  67. }
  68. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  69. public static float Sin(float value)
  70. {
  71. return MathF.Sin(DegreesToRadians(value));
  72. }
  73. }
  74. }