cordic.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) 2011 Broadcom Corporation
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  11. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  13. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/cordic.h>
  18. static const s32 arctan_table[] = {
  19. 2949120,
  20. 1740967,
  21. 919879,
  22. 466945,
  23. 234379,
  24. 117304,
  25. 58666,
  26. 29335,
  27. 14668,
  28. 7334,
  29. 3667,
  30. 1833,
  31. 917,
  32. 458,
  33. 229,
  34. 115,
  35. 57,
  36. 29
  37. };
  38. /*
  39. * cordic_calc_iq() - calculates the i/q coordinate for given angle
  40. *
  41. * theta: angle in degrees for which i/q coordinate is to be calculated
  42. * coord: function output parameter holding the i/q coordinate
  43. */
  44. struct cordic_iq cordic_calc_iq(s32 theta)
  45. {
  46. struct cordic_iq coord;
  47. s32 angle, valtmp;
  48. unsigned iter;
  49. int signx = 1;
  50. int signtheta;
  51. coord.i = CORDIC_ANGLE_GEN;
  52. coord.q = 0;
  53. angle = 0;
  54. theta = CORDIC_FIXED(theta);
  55. signtheta = (theta < 0) ? -1 : 1;
  56. theta = ((theta + CORDIC_FIXED(180) * signtheta) % CORDIC_FIXED(360)) -
  57. CORDIC_FIXED(180) * signtheta;
  58. if (CORDIC_FLOAT(theta) > 90) {
  59. theta -= CORDIC_FIXED(180);
  60. signx = -1;
  61. } else if (CORDIC_FLOAT(theta) < -90) {
  62. theta += CORDIC_FIXED(180);
  63. signx = -1;
  64. }
  65. for (iter = 0; iter < CORDIC_NUM_ITER; iter++) {
  66. if (theta > angle) {
  67. valtmp = coord.i - (coord.q >> iter);
  68. coord.q += (coord.i >> iter);
  69. angle += arctan_table[iter];
  70. } else {
  71. valtmp = coord.i + (coord.q >> iter);
  72. coord.q -= (coord.i >> iter);
  73. angle -= arctan_table[iter];
  74. }
  75. coord.i = valtmp;
  76. }
  77. coord.i *= signx;
  78. coord.q *= signx;
  79. return coord;
  80. }
  81. EXPORT_SYMBOL(cordic_calc_iq);
  82. MODULE_DESCRIPTION("CORDIC algorithm");
  83. MODULE_AUTHOR("Broadcom Corporation");
  84. MODULE_LICENSE("Dual BSD/GPL");