sincos.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* origin: FreeBSD /usr/src/lib/msun/src/s_sin.c */
  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. #include "libm.h"
  13. void sincos(double x, double *sin, double *cos)
  14. {
  15. double y[2], s, c;
  16. uint32_t ix;
  17. unsigned n;
  18. GET_HIGH_WORD(ix, x);
  19. ix &= 0x7fffffff;
  20. /* |x| ~< pi/4 */
  21. if (ix <= 0x3fe921fb) {
  22. /* if |x| < 2**-27 * sqrt(2) */
  23. if (ix < 0x3e46a09e) {
  24. /* raise inexact if x!=0 and underflow if subnormal */
  25. FORCE_EVAL(ix < 0x00100000 ? x/0x1p120f : x+0x1p120f);
  26. *sin = x;
  27. *cos = 1.0;
  28. return;
  29. }
  30. *sin = __sin(x, 0.0, 0);
  31. *cos = __cos(x, 0.0);
  32. return;
  33. }
  34. /* sincos(Inf or NaN) is NaN */
  35. if (ix >= 0x7ff00000) {
  36. *sin = *cos = x - x;
  37. return;
  38. }
  39. /* argument reduction needed */
  40. n = __rem_pio2(x, y);
  41. s = __sin(y[0], y[1], 1);
  42. c = __cos(y[0], y[1]);
  43. switch (n&3) {
  44. case 0:
  45. *sin = s;
  46. *cos = c;
  47. break;
  48. case 1:
  49. *sin = c;
  50. *cos = -s;
  51. break;
  52. case 2:
  53. *sin = -s;
  54. *cos = -c;
  55. break;
  56. case 3:
  57. default:
  58. *sin = -c;
  59. *cos = s;
  60. break;
  61. }
  62. }