fixp-arith.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. #ifndef _FIXP_ARITH_H
  3. #define _FIXP_ARITH_H
  4. #include <linux/math64.h>
  5. /*
  6. * Simplistic fixed-point arithmetics.
  7. * Hmm, I'm probably duplicating some code :(
  8. *
  9. * Copyright (c) 2002 Johann Deneux
  10. */
  11. /*
  12. *
  13. * Should you need to contact me, the author, you can do so by
  14. * e-mail - mail your message to <johann.deneux@gmail.com>
  15. */
  16. #include <linux/types.h>
  17. static const s32 sin_table[] = {
  18. 0x00000000, 0x023be165, 0x04779632, 0x06b2f1d2, 0x08edc7b6, 0x0b27eb5c,
  19. 0x0d61304d, 0x0f996a26, 0x11d06c96, 0x14060b67, 0x163a1a7d, 0x186c6ddd,
  20. 0x1a9cd9ac, 0x1ccb3236, 0x1ef74bf2, 0x2120fb82, 0x234815ba, 0x256c6f9e,
  21. 0x278dde6e, 0x29ac379f, 0x2bc750e8, 0x2ddf003f, 0x2ff31bdd, 0x32037a44,
  22. 0x340ff241, 0x36185aee, 0x381c8bb5, 0x3a1c5c56, 0x3c17a4e7, 0x3e0e3ddb,
  23. 0x3fffffff, 0x41ecc483, 0x43d464fa, 0x45b6bb5d, 0x4793a20f, 0x496af3e1,
  24. 0x4b3c8c11, 0x4d084650, 0x4ecdfec6, 0x508d9210, 0x5246dd48, 0x53f9be04,
  25. 0x55a6125a, 0x574bb8e5, 0x58ea90c2, 0x5a827999, 0x5c135399, 0x5d9cff82,
  26. 0x5f1f5ea0, 0x609a52d1, 0x620dbe8a, 0x637984d3, 0x64dd894f, 0x6639b039,
  27. 0x678dde6d, 0x68d9f963, 0x6a1de735, 0x6b598ea1, 0x6c8cd70a, 0x6db7a879,
  28. 0x6ed9eba0, 0x6ff389de, 0x71046d3c, 0x720c8074, 0x730baeec, 0x7401e4bf,
  29. 0x74ef0ebb, 0x75d31a5f, 0x76adf5e5, 0x777f903b, 0x7847d908, 0x7906c0af,
  30. 0x79bc384c, 0x7a6831b8, 0x7b0a9f8c, 0x7ba3751c, 0x7c32a67c, 0x7cb82884,
  31. 0x7d33f0c8, 0x7da5f5a3, 0x7e0e2e31, 0x7e6c924f, 0x7ec11aa3, 0x7f0bc095,
  32. 0x7f4c7e52, 0x7f834ecf, 0x7fb02dc4, 0x7fd317b3, 0x7fec09e1, 0x7ffb025e,
  33. 0x7fffffff
  34. };
  35. /**
  36. * __fixp_sin32() returns the sin of an angle in degrees
  37. *
  38. * @degrees: angle, in degrees, from 0 to 360.
  39. *
  40. * The returned value ranges from -0x7fffffff to +0x7fffffff.
  41. */
  42. static inline s32 __fixp_sin32(int degrees)
  43. {
  44. s32 ret;
  45. bool negative = false;
  46. if (degrees > 180) {
  47. negative = true;
  48. degrees -= 180;
  49. }
  50. if (degrees > 90)
  51. degrees = 180 - degrees;
  52. ret = sin_table[degrees];
  53. return negative ? -ret : ret;
  54. }
  55. /**
  56. * fixp_sin32() returns the sin of an angle in degrees
  57. *
  58. * @degrees: angle, in degrees. The angle can be positive or negative
  59. *
  60. * The returned value ranges from -0x7fffffff to +0x7fffffff.
  61. */
  62. static inline s32 fixp_sin32(int degrees)
  63. {
  64. degrees = (degrees % 360 + 360) % 360;
  65. return __fixp_sin32(degrees);
  66. }
  67. /* cos(x) = sin(x + 90 degrees) */
  68. #define fixp_cos32(v) fixp_sin32((v) + 90)
  69. /*
  70. * 16 bits variants
  71. *
  72. * The returned value ranges from -0x7fff to 0x7fff
  73. */
  74. #define fixp_sin16(v) (fixp_sin32(v) >> 16)
  75. #define fixp_cos16(v) (fixp_cos32(v) >> 16)
  76. /**
  77. * fixp_sin32_rad() - calculates the sin of an angle in radians
  78. *
  79. * @radians: angle, in radians
  80. * @twopi: value to be used for 2*pi
  81. *
  82. * Provides a variant for the cases where just 360
  83. * values is not enough. This function uses linear
  84. * interpolation to a wider range of values given by
  85. * twopi var.
  86. *
  87. * Experimental tests gave a maximum difference of
  88. * 0.000038 between the value calculated by sin() and
  89. * the one produced by this function, when twopi is
  90. * equal to 360000. That seems to be enough precision
  91. * for practical purposes.
  92. *
  93. * Please notice that two high numbers for twopi could cause
  94. * overflows, so the routine will not allow values of twopi
  95. * bigger than 1^18.
  96. */
  97. static inline s32 fixp_sin32_rad(u32 radians, u32 twopi)
  98. {
  99. int degrees;
  100. s32 v1, v2, dx, dy;
  101. s64 tmp;
  102. /*
  103. * Avoid too large values for twopi, as we don't want overflows.
  104. */
  105. BUG_ON(twopi > 1 << 18);
  106. degrees = (radians * 360) / twopi;
  107. tmp = radians - (degrees * twopi) / 360;
  108. degrees = (degrees % 360 + 360) % 360;
  109. v1 = __fixp_sin32(degrees);
  110. v2 = fixp_sin32(degrees + 1);
  111. dx = twopi / 360;
  112. dy = v2 - v1;
  113. tmp *= dy;
  114. return v1 + div_s64(tmp, dx);
  115. }
  116. /* cos(x) = sin(x + pi/2 radians) */
  117. #define fixp_cos32_rad(rad, twopi) \
  118. fixp_sin32_rad(rad + twopi / 4, twopi)
  119. #endif