1
0

sincosf.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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;
  26. float_t s, c;
  27. uint32_t ix;
  28. unsigned n, sign;
  29. GET_FLOAT_WORD(ix, x);
  30. sign = ix >> 31;
  31. ix &= 0x7fffffff;
  32. /* |x| ~<= pi/4 */
  33. if (ix <= 0x3f490fda) {
  34. /* |x| < 2**-12 */
  35. if (ix < 0x39800000) {
  36. /* raise inexact if x!=0 and underflow if subnormal */
  37. FORCE_EVAL(ix < 0x00100000 ? x/0x1p120f : x+0x1p120f);
  38. *sin = x;
  39. *cos = 1.0f;
  40. return;
  41. }
  42. *sin = __sindf(x);
  43. *cos = __cosdf(x);
  44. return;
  45. }
  46. /* |x| ~<= 5*pi/4 */
  47. if (ix <= 0x407b53d1) {
  48. if (ix <= 0x4016cbe3) { /* |x| ~<= 3pi/4 */
  49. if (sign) {
  50. *sin = -__cosdf(x + s1pio2);
  51. *cos = __sindf(x + s1pio2);
  52. } else {
  53. *sin = __cosdf(s1pio2 - x);
  54. *cos = __sindf(s1pio2 - x);
  55. }
  56. return;
  57. }
  58. /* -sin(x+c) is not correct if x+c could be 0: -0 vs +0 */
  59. *sin = -__sindf(sign ? x + s2pio2 : x - s2pio2);
  60. *cos = -__cosdf(sign ? x + s2pio2 : x - s2pio2);
  61. return;
  62. }
  63. /* |x| ~<= 9*pi/4 */
  64. if (ix <= 0x40e231d5) {
  65. if (ix <= 0x40afeddf) { /* |x| ~<= 7*pi/4 */
  66. if (sign) {
  67. *sin = __cosdf(x + s3pio2);
  68. *cos = -__sindf(x + s3pio2);
  69. } else {
  70. *sin = -__cosdf(x - s3pio2);
  71. *cos = __sindf(x - s3pio2);
  72. }
  73. return;
  74. }
  75. *sin = __sindf(sign ? x + s4pio2 : x - s4pio2);
  76. *cos = __cosdf(sign ? x + s4pio2 : x - s4pio2);
  77. return;
  78. }
  79. /* sin(Inf or NaN) is NaN */
  80. if (ix >= 0x7f800000) {
  81. *sin = *cos = x - x;
  82. return;
  83. }
  84. /* general argument reduction needed */
  85. n = __rem_pio2f(x, &y);
  86. s = __sindf(y);
  87. c = __cosdf(y);
  88. switch (n&3) {
  89. case 0:
  90. *sin = s;
  91. *cos = c;
  92. break;
  93. case 1:
  94. *sin = c;
  95. *cos = -s;
  96. break;
  97. case 2:
  98. *sin = -s;
  99. *cos = -c;
  100. break;
  101. case 3:
  102. default:
  103. *sin = -c;
  104. *cos = s;
  105. break;
  106. }
  107. }