coshq.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * ====================================================
  3. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  4. *
  5. * Developed at SunPro, a Sun Microsystems, Inc. business.
  6. * Permission to use, copy, modify, and distribute this
  7. * software is freely granted, provided that this notice
  8. * is preserved.
  9. * ====================================================
  10. */
  11. /* Changes for 128-bit __float128 are
  12. Copyright (C) 2001 Stephen L. Moshier <moshier@na-net.ornl.gov>
  13. and are incorporated herein by permission of the author. The author
  14. reserves the right to distribute this material elsewhere under different
  15. copying permissions. These modifications are distributed here under
  16. the following terms:
  17. This library is free software; you can redistribute it and/or
  18. modify it under the terms of the GNU Lesser General Public
  19. License as published by the Free Software Foundation; either
  20. version 2.1 of the License, or (at your option) any later version.
  21. This library is distributed in the hope that it will be useful,
  22. but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  24. Lesser General Public License for more details.
  25. You should have received a copy of the GNU Lesser General Public
  26. License along with this library; if not, write to the Free Software
  27. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  28. /* coshq(x)
  29. * Method :
  30. * mathematically coshq(x) if defined to be (exp(x)+exp(-x))/2
  31. * 1. Replace x by |x| (coshq(x) = coshq(-x)).
  32. * 2.
  33. * [ exp(x) - 1 ]^2
  34. * 0 <= x <= ln2/2 : coshq(x) := 1 + -------------------
  35. * 2*exp(x)
  36. *
  37. * exp(x) + 1/exp(x)
  38. * ln2/2 <= x <= 22 : coshq(x) := -------------------
  39. * 2
  40. * 22 <= x <= lnovft : coshq(x) := expq(x)/2
  41. * lnovft <= x <= ln2ovft: coshq(x) := expq(x/2)/2 * expq(x/2)
  42. * ln2ovft < x : coshq(x) := huge*huge (overflow)
  43. *
  44. * Special cases:
  45. * coshq(x) is |x| if x is +INF, -INF, or NaN.
  46. * only coshq(0)=1 is exact for finite x.
  47. */
  48. #include "quadmath-imp.h"
  49. static const __float128 one = 1.0Q, half = 0.5Q, huge = 1.0e4900Q,
  50. ovf_thresh = 1.1357216553474703894801348310092223067821E4Q;
  51. __float128
  52. coshq (__float128 x)
  53. {
  54. __float128 t, w;
  55. int32_t ex;
  56. ieee854_float128 u;
  57. u.value = x;
  58. ex = u.words32.w0 & 0x7fffffff;
  59. /* Absolute value of x. */
  60. u.words32.w0 = ex;
  61. /* x is INF or NaN */
  62. if (ex >= 0x7fff0000)
  63. return x * x;
  64. /* |x| in [0,0.5*ln2], return 1+expm1l(|x|)^2/(2*expq(|x|)) */
  65. if (ex < 0x3ffd62e4) /* 0.3465728759765625 */
  66. {
  67. t = expm1q (u.value);
  68. w = one + t;
  69. if (ex < 0x3fb80000) /* |x| < 2^-116 */
  70. return w; /* cosh(tiny) = 1 */
  71. return one + (t * t) / (w + w);
  72. }
  73. /* |x| in [0.5*ln2,40], return (exp(|x|)+1/exp(|x|)/2; */
  74. if (ex < 0x40044000)
  75. {
  76. t = expq (u.value);
  77. return half * t + half / t;
  78. }
  79. /* |x| in [22, ln(maxdouble)] return half*exp(|x|) */
  80. if (ex <= 0x400c62e3) /* 11356.375 */
  81. return half * expq (u.value);
  82. /* |x| in [log(maxdouble), overflowthresold] */
  83. if (u.value <= ovf_thresh)
  84. {
  85. w = expq (half * u.value);
  86. t = half * w;
  87. return t * w;
  88. }
  89. /* |x| > overflowthresold, cosh(x) overflow */
  90. return huge * huge;
  91. }