__cosl.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* origin: FreeBSD /usr/src/lib/msun/ld80/k_cosl.c */
  2. /* origin: FreeBSD /usr/src/lib/msun/ld128/k_cosl.c */
  3. /*
  4. * ====================================================
  5. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  6. * Copyright (c) 2008 Steven G. Kargl, David Schultz, Bruce D. Evans.
  7. *
  8. * Developed at SunSoft, a Sun Microsystems, Inc. business.
  9. * Permission to use, copy, modify, and distribute this
  10. * software is freely granted, provided that this notice
  11. * is preserved.
  12. * ====================================================
  13. */
  14. #include "libm.h"
  15. #if (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
  16. #if LDBL_MANT_DIG == 64
  17. /*
  18. * ld80 version of __cos.c. See __cos.c for most comments.
  19. */
  20. /*
  21. * Domain [-0.7854, 0.7854], range ~[-2.43e-23, 2.425e-23]:
  22. * |cos(x) - c(x)| < 2**-75.1
  23. *
  24. * The coefficients of c(x) were generated by a pari-gp script using
  25. * a Remez algorithm that searches for the best higher coefficients
  26. * after rounding leading coefficients to a specified precision.
  27. *
  28. * Simpler methods like Chebyshev or basic Remez barely suffice for
  29. * cos() in 64-bit precision, because we want the coefficient of x^2
  30. * to be precisely -0.5 so that multiplying by it is exact, and plain
  31. * rounding of the coefficients of a good polynomial approximation only
  32. * gives this up to about 64-bit precision. Plain rounding also gives
  33. * a mediocre approximation for the coefficient of x^4, but a rounding
  34. * error of 0.5 ulps for this coefficient would only contribute ~0.01
  35. * ulps to the final error, so this is unimportant. Rounding errors in
  36. * higher coefficients are even less important.
  37. *
  38. * In fact, coefficients above the x^4 one only need to have 53-bit
  39. * precision, and this is more efficient. We get this optimization
  40. * almost for free from the complications needed to search for the best
  41. * higher coefficients.
  42. */
  43. static const long double
  44. C1 = 0.0416666666666666666136L; /* 0xaaaaaaaaaaaaaa9b.0p-68 */
  45. static const double
  46. C2 = -0.0013888888888888874, /* -0x16c16c16c16c10.0p-62 */
  47. C3 = 0.000024801587301571716, /* 0x1a01a01a018e22.0p-68 */
  48. C4 = -0.00000027557319215507120, /* -0x127e4fb7602f22.0p-74 */
  49. C5 = 0.0000000020876754400407278, /* 0x11eed8caaeccf1.0p-81 */
  50. C6 = -1.1470297442401303e-11, /* -0x19393412bd1529.0p-89 */
  51. C7 = 4.7383039476436467e-14; /* 0x1aac9d9af5c43e.0p-97 */
  52. #define POLY(z) (z*(C1+z*(C2+z*(C3+z*(C4+z*(C5+z*(C6+z*C7)))))))
  53. #elif LDBL_MANT_DIG == 113
  54. /*
  55. * ld128 version of __cos.c. See __cos.c for most comments.
  56. */
  57. /*
  58. * Domain [-0.7854, 0.7854], range ~[-1.80e-37, 1.79e-37]:
  59. * |cos(x) - c(x))| < 2**-122.0
  60. *
  61. * 113-bit precision requires more care than 64-bit precision, since
  62. * simple methods give a minimax polynomial with coefficient for x^2
  63. * that is 1 ulp below 0.5, but we want it to be precisely 0.5. See
  64. * above for more details.
  65. */
  66. static const long double
  67. C1 = 0.04166666666666666666666666666666658424671L,
  68. C2 = -0.001388888888888888888888888888863490893732L,
  69. C3 = 0.00002480158730158730158730158600795304914210L,
  70. C4 = -0.2755731922398589065255474947078934284324e-6L,
  71. C5 = 0.2087675698786809897659225313136400793948e-8L,
  72. C6 = -0.1147074559772972315817149986812031204775e-10L,
  73. C7 = 0.4779477332386808976875457937252120293400e-13L;
  74. static const double
  75. C8 = -0.1561920696721507929516718307820958119868e-15,
  76. C9 = 0.4110317413744594971475941557607804508039e-18,
  77. C10 = -0.8896592467191938803288521958313920156409e-21,
  78. C11 = 0.1601061435794535138244346256065192782581e-23;
  79. #define POLY(z) (z*(C1+z*(C2+z*(C3+z*(C4+z*(C5+z*(C6+z*(C7+ \
  80. z*(C8+z*(C9+z*(C10+z*C11)))))))))))
  81. #endif
  82. long double __cosl(long double x, long double y)
  83. {
  84. long double hz,z,r,w;
  85. z = x*x;
  86. r = POLY(z);
  87. hz = 0.5*z;
  88. w = 1.0-hz;
  89. return w + (((1.0-w)-hz) + (z*r-x*y));
  90. }
  91. #endif