sinf.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. float sinf(float x)
  24. {
  25. double y;
  26. uint32_t ix;
  27. int n, sign;
  28. GET_FLOAT_WORD(ix, x);
  29. sign = ix >> 31;
  30. ix &= 0x7fffffff;
  31. if (ix <= 0x3f490fda) { /* |x| ~<= pi/4 */
  32. if (ix < 0x39800000) { /* |x| < 2**-12 */
  33. /* raise inexact if x!=0 and underflow if subnormal */
  34. FORCE_EVAL(ix < 0x00800000 ? x/0x1p120f : x+0x1p120f);
  35. return x;
  36. }
  37. return __sindf(x);
  38. }
  39. if (ix <= 0x407b53d1) { /* |x| ~<= 5*pi/4 */
  40. if (ix <= 0x4016cbe3) { /* |x| ~<= 3pi/4 */
  41. if (sign)
  42. return -__cosdf(x + s1pio2);
  43. else
  44. return __cosdf(x - s1pio2);
  45. }
  46. return __sindf(sign ? -(x + s2pio2) : -(x - s2pio2));
  47. }
  48. if (ix <= 0x40e231d5) { /* |x| ~<= 9*pi/4 */
  49. if (ix <= 0x40afeddf) { /* |x| ~<= 7*pi/4 */
  50. if (sign)
  51. return __cosdf(x + s3pio2);
  52. else
  53. return -__cosdf(x - s3pio2);
  54. }
  55. return __sindf(sign ? x + s4pio2 : x - s4pio2);
  56. }
  57. /* sin(Inf or NaN) is NaN */
  58. if (ix >= 0x7f800000)
  59. return x - x;
  60. /* general argument reduction needed */
  61. n = __rem_pio2f(x, &y);
  62. switch (n&3) {
  63. case 0: return __sindf(y);
  64. case 1: return __cosdf(y);
  65. case 2: return __sindf(-y);
  66. default:
  67. return -__cosdf(y);
  68. }
  69. }