1
0

sincosf.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* origin: FreeBSD /usr/src/lib/msun/src/s_sinf.c */
  2. /*
  3. * Conversion to float by Ian Lance Taylor, Cygnus Support, [email protected].
  4. * Optimized by Bruce D. Evans.
  5. */
  6. /*
  7. * ====================================================
  8. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  9. *
  10. * Developed at SunPro, a Sun Microsystems, Inc. business.
  11. * Permission to use, copy, modify, and distribute this
  12. * software is freely granted, provided that this notice
  13. * is preserved.
  14. * ====================================================
  15. */
  16. #include "libm.h"
  17. /* Small multiples of pi/2 rounded to double precision. */
  18. static const double
  19. s1pio2 = 1*M_PI_2, /* 0x3FF921FB, 0x54442D18 */
  20. s2pio2 = 2*M_PI_2, /* 0x400921FB, 0x54442D18 */
  21. s3pio2 = 3*M_PI_2, /* 0x4012D97C, 0x7F3321D2 */
  22. s4pio2 = 4*M_PI_2; /* 0x401921FB, 0x54442D18 */
  23. void sincosf(float x, float *sin, float *cos)
  24. {
  25. double y, s, c;
  26. uint32_t n, hx, ix;
  27. GET_FLOAT_WORD(hx, x);
  28. ix = hx & 0x7fffffff;
  29. /* |x| ~<= pi/4 */
  30. if (ix <= 0x3f490fda) {
  31. /* |x| < 2**-12 */
  32. if (ix < 0x39800000) {
  33. /* raise inexact if x != 0 */
  34. if((int)x == 0) {
  35. *sin = x;
  36. *cos = 1.0f;
  37. }
  38. return;
  39. }
  40. *sin = __sindf(x);
  41. *cos = __cosdf(x);
  42. return;
  43. }
  44. /* |x| ~<= 5*pi/4 */
  45. if (ix <= 0x407b53d1) {
  46. if (ix <= 0x4016cbe3) { /* |x| ~<= 3pi/4 */
  47. if (hx < 0x80000000) {
  48. *sin = __cosdf(x - s1pio2);
  49. *cos = __sindf(s1pio2 - x);
  50. } else {
  51. *sin = -__cosdf(x + s1pio2);
  52. *cos = __sindf(x + s1pio2);
  53. }
  54. return;
  55. }
  56. *sin = __sindf(hx < 0x80000000 ? s2pio2 - x : -s2pio2 - x);
  57. *cos = -__cosdf(hx < 0x80000000 ? x - s2pio2 : x + s2pio2);
  58. return;
  59. }
  60. /* |x| ~<= 9*pi/4 */
  61. if (ix <= 0x40e231d5) {
  62. if (ix <= 0x40afeddf) { /* |x| ~<= 7*pi/4 */
  63. if (hx < 0x80000000) {
  64. *sin = -__cosdf(x - s3pio2);
  65. *cos = __sindf(x - s3pio2);
  66. } else {
  67. *sin = __cosdf(x + s3pio2);
  68. *cos = __sindf(-s3pio2 - x);
  69. }
  70. return;
  71. }
  72. *sin = __sindf(hx < 0x80000000 ? x - s4pio2 : x + s4pio2);
  73. *cos = __cosdf(hx < 0x80000000 ? x - s4pio2 : x + s4pio2);
  74. return;
  75. }
  76. /* sin(Inf or NaN) is NaN */
  77. if (ix >= 0x7f800000) {
  78. *sin = *cos = x - x;
  79. return;
  80. }
  81. /* general argument reduction needed */
  82. n = __rem_pio2f(x, &y);
  83. s = __sindf(y);
  84. c = __cosdf(y);
  85. switch (n&3) {
  86. case 0:
  87. *sin = s;
  88. *cos = c;
  89. break;
  90. case 1:
  91. *sin = c;
  92. *cos = -s;
  93. break;
  94. case 2:
  95. *sin = -s;
  96. *cos = -c;
  97. break;
  98. case 3:
  99. default:
  100. *sin = -c;
  101. *cos = s;
  102. break;
  103. }
  104. }