math.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. */
  9. #include <math.h>
  10. /*
  11. * COPYRIGHT: See COPYING in the top level directory
  12. * PROJECT: ReactOS CRT
  13. * FILE: lib/crt/math/cos.c
  14. * PURPOSE: Generic C Implementation of cos
  15. * PROGRAMMER: Timo Kreuzer (timo.kreuzer@reactos.org)
  16. */
  17. #define PRECISION 9
  18. static double cos_off_tbl[] = {0.0, -M_PI/2., 0, -M_PI/2.};
  19. static double cos_sign_tbl[] = {1,-1,-1,1};
  20. static double sin_off_tbl[] = {0.0, -M_PI/2., 0, -M_PI/2.};
  21. static double sin_sign_tbl[] = {1,-1,-1,1};
  22. double sin(double x)
  23. {
  24. int quadrant;
  25. double x2, result;
  26. /* Calculate the quadrant */
  27. quadrant = x * (2./M_PI);
  28. /* Get offset inside quadrant */
  29. x = x - quadrant * (M_PI/2.);
  30. /* Normalize quadrant to [0..3] */
  31. quadrant = (quadrant - 1) & 0x3;
  32. /* Fixup value for the generic function */
  33. x += sin_off_tbl[quadrant];
  34. /* Calculate the negative of the square of x */
  35. x2 = - (x * x);
  36. /* This is an unrolled taylor series using <PRECISION> iterations
  37. * Example with 4 iterations:
  38. * result = 1 - x^2/2! + x^4/4! - x^6/6! + x^8/8!
  39. * To save multiplications and to keep the precision high, it's performed
  40. * like this:
  41. * result = 1 - x^2 * (1/2! - x^2 * (1/4! - x^2 * (1/6! - x^2 * (1/8!))))
  42. */
  43. /* Start with 0, compiler will optimize this away */
  44. result = 0;
  45. #if (PRECISION >= 10)
  46. result += 1./(1.*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18*19*20);
  47. result *= x2;
  48. #endif
  49. #if (PRECISION >= 9)
  50. result += 1./(1.*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18);
  51. result *= x2;
  52. #endif
  53. #if (PRECISION >= 8)
  54. result += 1./(1.*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16);
  55. result *= x2;
  56. #endif
  57. #if (PRECISION >= 7)
  58. result += 1./(1.*2*3*4*5*6*7*8*9*10*11*12*13*14);
  59. result *= x2;
  60. #endif
  61. #if (PRECISION >= 6)
  62. result += 1./(1.*2*3*4*5*6*7*8*9*10*11*12);
  63. result *= x2;
  64. #endif
  65. #if (PRECISION >= 5)
  66. result += 1./(1.*2*3*4*5*6*7*8*9*10);
  67. result *= x2;
  68. #endif
  69. result += 1./(1.*2*3*4*5*6*7*8);
  70. result *= x2;
  71. result += 1./(1.*2*3*4*5*6);
  72. result *= x2;
  73. result += 1./(1.*2*3*4);
  74. result *= x2;
  75. result += 1./(1.*2);
  76. result *= x2;
  77. result += 1;
  78. /* Apply correct sign */
  79. result *= sin_sign_tbl[quadrant];
  80. return result;
  81. }
  82. double cos(double x)
  83. {
  84. int quadrant;
  85. double x2, result;
  86. /* Calculate the quadrant */
  87. quadrant = x * (2./M_PI);
  88. /* Get offset inside quadrant */
  89. x = x - quadrant * (M_PI/2.);
  90. /* Normalize quadrant to [0..3] */
  91. quadrant = quadrant & 0x3;
  92. /* Fixup value for the generic function */
  93. x += cos_off_tbl[quadrant];
  94. /* Calculate the negative of the square of x */
  95. x2 = - (x * x);
  96. /* This is an unrolled taylor series using <PRECISION> iterations
  97. * Example with 4 iterations:
  98. * result = 1 - x^2/2! + x^4/4! - x^6/6! + x^8/8!
  99. * To save multiplications and to keep the precision high, it's performed
  100. * like this:
  101. * result = 1 - x^2 * (1/2! - x^2 * (1/4! - x^2 * (1/6! - x^2 * (1/8!))))
  102. */
  103. /* Start with 0, compiler will optimize this away */
  104. result = 0;
  105. #if (PRECISION >= 10)
  106. result += 1./(1.*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18*19*20);
  107. result *= x2;
  108. #endif
  109. #if (PRECISION >= 9)
  110. result += 1./(1.*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18);
  111. result *= x2;
  112. #endif
  113. #if (PRECISION >= 8)
  114. result += 1./(1.*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16);
  115. result *= x2;
  116. #endif
  117. #if (PRECISION >= 7)
  118. result += 1./(1.*2*3*4*5*6*7*8*9*10*11*12*13*14);
  119. result *= x2;
  120. #endif
  121. #if (PRECISION >= 6)
  122. result += 1./(1.*2*3*4*5*6*7*8*9*10*11*12);
  123. result *= x2;
  124. #endif
  125. #if (PRECISION >= 5)
  126. result += 1./(1.*2*3*4*5*6*7*8*9*10);
  127. result *= x2;
  128. #endif
  129. result += 1./(1.*2*3*4*5*6*7*8);
  130. result *= x2;
  131. result += 1./(1.*2*3*4*5*6);
  132. result *= x2;
  133. result += 1./(1.*2*3*4);
  134. result *= x2;
  135. result += 1./(1.*2);
  136. result *= x2;
  137. result += 1;
  138. /* Apply correct sign */
  139. result *= cos_sign_tbl[quadrant];
  140. return result;
  141. }