s_sincos.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* @(#)s_sincos.c 5.1 13/07/15 */
  2. /*
  3. * ====================================================
  4. * Copyright (C) 2013 Elliot Saba. All rights reserved.
  5. *
  6. * Developed at the University of Washington.
  7. * Permission to use, copy, modify, and distribute this
  8. * software is freely granted, provided that this notice
  9. * is preserved.
  10. * ====================================================
  11. */
  12. #include "cdefs-compat.h"
  13. /* sincos(x, s, c)
  14. * Several applications need sine and cosine of the same
  15. * angle x. This function computes both at the same time,
  16. * and stores the results in *sin and *cos.
  17. *
  18. * kernel function:
  19. * __kernel_sin ... sine function on [-pi/4,pi/4]
  20. * __kernel_cos ... cose function on [-pi/4,pi/4]
  21. * __ieee754_rem_pio2 ... argument reduction routine
  22. *
  23. * Method.
  24. * Borrow liberally from s_sin.c and s_cos.c, merging
  25. * efforts where applicable and returning their values in
  26. * appropriate variables, thereby slightly reducing the
  27. * amount of work relative to just calling sin/cos(x)
  28. * separately
  29. *
  30. * Special cases:
  31. * Let trig be any of sin, cos, or tan.
  32. * sincos(+-INF, s, c) is NaN, with signals;
  33. * sincos(NaN, s, c) is that NaN;
  34. */
  35. #include <float.h>
  36. #include <openlibm_math.h>
  37. //#define INLINE_REM_PIO2
  38. #include "math_private.h"
  39. //#include "e_rem_pio2.c"
  40. /* Constants used in polynomial approximation of sin/cos */
  41. static const double
  42. one = 1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */
  43. half = 5.00000000000000000000e-01, /* 0x3FE00000, 0x00000000 */
  44. S1 = -1.66666666666666324348e-01, /* 0xBFC55555, 0x55555549 */
  45. S2 = 8.33333333332248946124e-03, /* 0x3F811111, 0x1110F8A6 */
  46. S3 = -1.98412698298579493134e-04, /* 0xBF2A01A0, 0x19C161D5 */
  47. S4 = 2.75573137070700676789e-06, /* 0x3EC71DE3, 0x57B1FE7D */
  48. S5 = -2.50507602534068634195e-08, /* 0xBE5AE5E6, 0x8A2B9CEB */
  49. S6 = 1.58969099521155010221e-10, /* 0x3DE5D93A, 0x5ACFD57C */
  50. C1 = 4.16666666666666019037e-02, /* 0x3FA55555, 0x5555554C */
  51. C2 = -1.38888888888741095749e-03, /* 0xBF56C16C, 0x16C15177 */
  52. C3 = 2.48015872894767294178e-05, /* 0x3EFA01A0, 0x19CB1590 */
  53. C4 = -2.75573143513906633035e-07, /* 0xBE927E4F, 0x809C52AD */
  54. C5 = 2.08757232129817482790e-09, /* 0x3E21EE9E, 0xBDB4B1C4 */
  55. C6 = -1.13596475577881948265e-11; /* 0xBDA8FAE9, 0xBE8838D4 */
  56. static void
  57. __kernel_sincos( double x, double y, int iy, double * k_s, double * k_c )
  58. {
  59. /* Inline calculation of sin/cos, as we can save
  60. some work, and we will always need to calculate
  61. both values, no matter the result of switch */
  62. double z, w, r, v, hz;
  63. z = x*x;
  64. w = z*z;
  65. /* cos-specific computation; equivalent to calling
  66. __kernel_cos(x,y) and storing in k_c*/
  67. r = z*(C1+z*(C2+z*C3)) + w*w*(C4+z*(C5+z*C6));
  68. hz = 0.5*z;
  69. v = one-hz;
  70. *k_c = v + (((one-v)-hz) + (z*r-x*y));
  71. /* sin-specific computation; equivalent to calling
  72. __kernel_sin(x,y,1) and storing in k_s*/
  73. r = S2+z*(S3+z*S4) + z*w*(S5+z*S6);
  74. v = z*x;
  75. if(iy == 0)
  76. *k_s = x+v*(S1+z*r);
  77. else
  78. *k_s = x-((z*(half*y-v*r)-y)-v*S1);
  79. }
  80. DLLEXPORT void
  81. sincos(double x, double * s, double * c)
  82. {
  83. double y[2];
  84. int32_t ix;
  85. /* Store high word of x in ix */
  86. GET_HIGH_WORD(ix,x);
  87. /* |x| ~< pi/4 */
  88. ix &= 0x7fffffff;
  89. if(ix <= 0x3fe921fb) {
  90. /* Check for small x for sin and cos */
  91. if(ix<0x3e46a09e) {
  92. /* Check for exact zero */
  93. if( (int)x==0 ) {
  94. *s = x;
  95. *c = 1.0;
  96. return;
  97. }
  98. }
  99. /* Call kernel function with 0 extra */
  100. __kernel_sincos(x,0.0,0, s, c);
  101. } else if( ix >= 0x7ff00000 ) {
  102. /* sincos(Inf or NaN) is NaN */
  103. *s = x-x;
  104. *c = x-x;
  105. }
  106. /*argument reduction needed*/
  107. else {
  108. double k_c, k_s;
  109. /* Calculate remainer, then sub out to kernel */
  110. int32_t n = __ieee754_rem_pio2(x,y);
  111. __kernel_sincos( y[0], y[1], 1, &k_s, &k_c );
  112. /* Figure out permutation of sin/cos outputs to true outputs */
  113. switch(n&3) {
  114. case 0:
  115. *c = k_c;
  116. *s = k_s;
  117. break;
  118. case 1:
  119. *c = -k_s;
  120. *s = k_c;
  121. break;
  122. case 2:
  123. *c = -k_c;
  124. *s = -k_s;
  125. break;
  126. default:
  127. *c = k_s;
  128. *s = -k_c;
  129. break;
  130. }
  131. }
  132. }
  133. #if (LDBL_MANT_DIG == 53)
  134. __weak_reference(sincos, sincosl);
  135. #endif