FloatingPointHelper.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. namespace Ryujinx.Audio.Renderer.Dsp
  4. {
  5. public static class FloatingPointHelper
  6. {
  7. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  8. public static float MultiplyRoundDown(float a, float b)
  9. {
  10. return RoundDown(a * b);
  11. }
  12. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  13. public static float RoundDown(float a)
  14. {
  15. return MathF.Round(a, 0);
  16. }
  17. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  18. public static float RoundUp(float a)
  19. {
  20. return MathF.Round(a);
  21. }
  22. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  23. public static float MultiplyRoundUp(float a, float b)
  24. {
  25. return RoundUp(a * b);
  26. }
  27. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  28. public static float Pow10(float x)
  29. {
  30. // NOTE: Nintendo implementation uses Q15 and a LUT for this, we don't.
  31. // As such, we support the same ranges as Nintendo to avoid unexpected behaviours.
  32. if (x >= 0.0f)
  33. {
  34. return 1.0f;
  35. }
  36. else if (x <= -5.3f)
  37. {
  38. return 0.0f;
  39. }
  40. return MathF.Pow(10, x);
  41. }
  42. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  43. public static float DegreesToRadians(float degrees)
  44. {
  45. return degrees * MathF.PI / 180.0f;
  46. }
  47. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  48. public static float Cos(float value)
  49. {
  50. return MathF.Cos(DegreesToRadians(value));
  51. }
  52. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  53. public static float Sin(float value)
  54. {
  55. return MathF.Sin(DegreesToRadians(value));
  56. }
  57. }
  58. }