s_cosf.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* s_cosf.c -- float version of s_cos.c.
  2. * Conversion to float by Ian Lance Taylor, Cygnus Support, [email protected].
  3. * Optimized by Bruce D. Evans.
  4. */
  5. /*
  6. * ====================================================
  7. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  8. *
  9. * Developed at SunPro, a Sun Microsystems, Inc. business.
  10. * Permission to use, copy, modify, and distribute this
  11. * software is freely granted, provided that this notice
  12. * is preserved.
  13. * ====================================================
  14. */
  15. #include "cdefs-compat.h"
  16. //__FBSDID("$FreeBSD: src/lib/msun/src/s_cosf.c,v 1.18 2008/02/25 22:19:17 bde Exp $");
  17. #include <float.h>
  18. #include "openlibm.h"
  19. //#define INLINE_KERNEL_COSDF
  20. //#define INLINE_KERNEL_SINDF
  21. //#define INLINE_REM_PIO2F
  22. #include "math_private.h"
  23. //#include "e_rem_pio2f.c"
  24. //#include "k_cosf.c"
  25. //#include "k_sinf.c"
  26. /* Small multiples of pi/2 rounded to double precision. */
  27. static const double
  28. c1pio2 = 1*M_PI_2, /* 0x3FF921FB, 0x54442D18 */
  29. c2pio2 = 2*M_PI_2, /* 0x400921FB, 0x54442D18 */
  30. c3pio2 = 3*M_PI_2, /* 0x4012D97C, 0x7F3321D2 */
  31. c4pio2 = 4*M_PI_2; /* 0x401921FB, 0x54442D18 */
  32. float
  33. cosf(float x)
  34. {
  35. double y;
  36. int32_t n, hx, ix;
  37. GET_FLOAT_WORD(hx,x);
  38. ix = hx & 0x7fffffff;
  39. if(ix <= 0x3f490fda) { /* |x| ~<= pi/4 */
  40. if(ix<0x39800000) /* |x| < 2**-12 */
  41. if(((int)x)==0) return 1.0; /* 1 with inexact if x != 0 */
  42. return __kernel_cosdf(x);
  43. }
  44. if(ix<=0x407b53d1) { /* |x| ~<= 5*pi/4 */
  45. if(ix>0x4016cbe3) /* |x| ~> 3*pi/4 */
  46. return -__kernel_cosdf(x + (hx > 0 ? -c2pio2 : c2pio2));
  47. else {
  48. if(hx>0)
  49. return __kernel_sindf(c1pio2 - x);
  50. else
  51. return __kernel_sindf(x + c1pio2);
  52. }
  53. }
  54. if(ix<=0x40e231d5) { /* |x| ~<= 9*pi/4 */
  55. if(ix>0x40afeddf) /* |x| ~> 7*pi/4 */
  56. return __kernel_cosdf(x + (hx > 0 ? -c4pio2 : c4pio2));
  57. else {
  58. if(hx>0)
  59. return __kernel_sindf(x - c3pio2);
  60. else
  61. return __kernel_sindf(-c3pio2 - x);
  62. }
  63. }
  64. /* cos(Inf or NaN) is NaN */
  65. else if (ix>=0x7f800000) return x-x;
  66. /* general argument reduction needed */
  67. else {
  68. n = __ieee754_rem_pio2f(x,&y);
  69. switch(n&3) {
  70. case 0: return __kernel_cosdf(y);
  71. case 1: return __kernel_sindf(-y);
  72. case 2: return -__kernel_cosdf(y);
  73. default:
  74. return __kernel_sindf(y);
  75. }
  76. }
  77. }