s_sin.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* @(#)s_sin.c 5.1 93/09/24 */
  2. /*
  3. * ====================================================
  4. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  5. *
  6. * Developed at SunPro, a Sun Microsystems, Inc. business.
  7. * Permission to use, copy, modify, and distribute this
  8. * software is freely granted, provided that this notice
  9. * is preserved.
  10. * ====================================================
  11. */
  12. #if defined(LIBM_SCCS) && !defined(lint)
  13. static const char rcsid[] =
  14. "$NetBSD: s_sin.c,v 1.7 1995/05/10 20:48:15 jtc Exp $";
  15. #endif
  16. /* sin(x)
  17. * Return sine function of x.
  18. *
  19. * kernel function:
  20. * __kernel_sin ... sine function on [-pi/4,pi/4]
  21. * __kernel_cos ... cose function on [-pi/4,pi/4]
  22. * __ieee754_rem_pio2 ... argument reduction routine
  23. *
  24. * Method.
  25. * Let S,C and T denote the sin, cos and tan respectively on
  26. * [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2
  27. * in [-pi/4 , +pi/4], and let n = k mod 4.
  28. * We have
  29. *
  30. * n sin(x) cos(x) tan(x)
  31. * ----------------------------------------------------------
  32. * 0 S C T
  33. * 1 C -S -1/T
  34. * 2 -S -C T
  35. * 3 -C S -1/T
  36. * ----------------------------------------------------------
  37. *
  38. * Special cases:
  39. * Let trig be any of sin, cos, or tan.
  40. * trig(+-INF) is NaN, with signals;
  41. * trig(NaN) is that NaN;
  42. *
  43. * Accuracy:
  44. * TRIG(x) returns trig(x) nearly rounded
  45. */
  46. #include "math_libm.h"
  47. #include "math_private.h"
  48. libm_hidden_proto(sin)
  49. #ifdef __STDC__
  50. double sin(double x)
  51. #else
  52. double sin(x)
  53. double x;
  54. #endif
  55. {
  56. double y[2], z = 0.0;
  57. int32_t n, ix;
  58. /* High word of x. */
  59. GET_HIGH_WORD(ix, x);
  60. /* |x| ~< pi/4 */
  61. ix &= 0x7fffffff;
  62. if (ix <= 0x3fe921fb)
  63. return __kernel_sin(x, z, 0);
  64. /* sin(Inf or NaN) is NaN */
  65. else if (ix >= 0x7ff00000)
  66. return x - x;
  67. /* argument reduction needed */
  68. else {
  69. n = __ieee754_rem_pio2(x, y);
  70. switch (n & 3) {
  71. case 0:
  72. return __kernel_sin(y[0], y[1], 1);
  73. case 1:
  74. return __kernel_cos(y[0], y[1]);
  75. case 2:
  76. return -__kernel_sin(y[0], y[1], 1);
  77. default:
  78. return -__kernel_cos(y[0], y[1]);
  79. }
  80. }
  81. }
  82. libm_hidden_def(sin)