You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.9KB

  1. // Copyright 2014 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. // See http://creativecommons.org/licenses/MIT/ for more information.
  24. //
  25. // -----------------------------------------------------------------------------
  26. //
  27. // Fast arc-tangent routines.
  28. #ifndef STMLIB_DSP_ATAN_H_
  29. #define STMLIB_DSP_ATAN_H_
  30. #include "stmlib/stmlib.h"
  31. #include "stmlib/dsp/rsqrt.h"
  32. #include <cmath>
  33. namespace stmlib {
  34. static inline uint16_t fast_atan2(float y, float x) {
  35. static const uint32_t sign_mask = 0x80000000;
  36. static const float b = 0.596227f;
  37. uint32_t ux_s = sign_mask & unsafe_bit_cast<uint32_t, float>(x);
  38. uint32_t uy_s = sign_mask & unsafe_bit_cast<uint32_t, float>(y);
  39. uint32_t offset = ((~ux_s & uy_s) >> 29 | ux_s >> 30) << 14;
  40. float bxy_a = fabs(b * x * y);
  41. float num = bxy_a + y * y;
  42. float atan_1q = num / (x * x + bxy_a + num);
  43. uint32_t uatan_2q = (ux_s ^ uy_s) | unsafe_bit_cast<uint32_t, float>(atan_1q);
  44. return unsafe_bit_cast<float, uint32_t>(uatan_2q) * 16384 + offset;
  45. }
  46. extern const uint16_t atan_lut[513];
  47. static inline uint16_t fast_atan2r(float y, float x, float* r) {
  48. float squared_magnitude = x * x + y * y;
  49. if (squared_magnitude == 0.0f) {
  50. *r = 0.0f;
  51. return 0.0f;
  52. }
  53. float rinv = fast_rsqrt_carmack(squared_magnitude);
  54. *r = rinv * squared_magnitude;
  55. static const uint32_t sign_mask = 0x80000000;
  56. uint32_t ux_s = sign_mask & unsafe_bit_cast<uint32_t, float>(x);
  57. uint32_t uy_s = sign_mask & unsafe_bit_cast<uint32_t, float>(y);
  58. uint32_t quadrant = ((~ux_s & uy_s) >> 29 | ux_s >> 30);
  59. uint16_t angle = 0;
  60. x = fabs(x);
  61. y = fabs(y);
  62. if (y > x) {
  63. angle = 16384 - atan_lut[static_cast<uint32_t>(x * rinv * 512.0f + 0.5f)];
  64. } else {
  65. angle = atan_lut[static_cast<uint32_t>(y * rinv * 512.0f + 0.5f)];
  66. }
  67. if (ux_s ^ uy_s) {
  68. angle = -angle;
  69. }
  70. return angle + (quadrant << 14);
  71. }
  72. } // namespace stmlib
  73. #endif // STMLIB_DSP_ATAN_H_