/lib/msun/src/s_ccoshf.c

https://bitbucket.org/freebsd/freebsd-base · C · 104 lines · 52 code · 18 blank · 34 comment · 17 complexity · 2c692763e514c76854aaffcfc7c912cb MD5 · raw file

  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
  3. *
  4. * Copyright (c) 2005 Bruce D. Evans and Steven G. Kargl
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice unmodified, this list of conditions, and the following
  12. * disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. /*
  29. * Float version of ccosh(). See s_ccosh.c for details.
  30. */
  31. #include <sys/cdefs.h>
  32. __FBSDID("$FreeBSD$");
  33. #include <complex.h>
  34. #include <math.h>
  35. #include "math_private.h"
  36. static const float huge = 0x1p127;
  37. float complex
  38. ccoshf(float complex z)
  39. {
  40. float x, y, h;
  41. int32_t hx, hy, ix, iy;
  42. x = crealf(z);
  43. y = cimagf(z);
  44. GET_FLOAT_WORD(hx, x);
  45. GET_FLOAT_WORD(hy, y);
  46. ix = 0x7fffffff & hx;
  47. iy = 0x7fffffff & hy;
  48. if (ix < 0x7f800000 && iy < 0x7f800000) {
  49. if (iy == 0)
  50. return (CMPLXF(coshf(x), x * y));
  51. if (ix < 0x41100000) /* |x| < 9: normal case */
  52. return (CMPLXF(coshf(x) * cosf(y), sinhf(x) * sinf(y)));
  53. /* |x| >= 9, so cosh(x) ~= exp(|x|) */
  54. if (ix < 0x42b17218) {
  55. /* x < 88.7: expf(|x|) won't overflow */
  56. h = expf(fabsf(x)) * 0.5F;
  57. return (CMPLXF(h * cosf(y), copysignf(h, x) * sinf(y)));
  58. } else if (ix < 0x4340b1e7) {
  59. /* x < 192.7: scale to avoid overflow */
  60. z = __ldexp_cexpf(CMPLXF(fabsf(x), y), -1);
  61. return (CMPLXF(crealf(z), cimagf(z) * copysignf(1, x)));
  62. } else {
  63. /* x >= 192.7: the result always overflows */
  64. h = huge * x;
  65. return (CMPLXF(h * h * cosf(y), h * sinf(y)));
  66. }
  67. }
  68. if (ix == 0) /* && iy >= 0x7f800000 */
  69. return (CMPLXF(y - y, x * copysignf(0, y)));
  70. if (iy == 0) /* && ix >= 0x7f800000 */
  71. return (CMPLXF(x * x, copysignf(0, x) * y));
  72. if (ix < 0x7f800000) /* && iy >= 0x7f800000 */
  73. return (CMPLXF(y - y, x * (y - y)));
  74. if (ix == 0x7f800000) {
  75. if (iy >= 0x7f800000)
  76. return (CMPLXF(INFINITY, x * (y - y)));
  77. return (CMPLXF(INFINITY * cosf(y), x * sinf(y)));
  78. }
  79. return (CMPLXF(((long double)x * x) * (y - y),
  80. ((long double)x + x) * (y - y)));
  81. }
  82. float complex
  83. ccosf(float complex z)
  84. {
  85. return (ccoshf(CMPLXF(-cimagf(z), crealf(z))));
  86. }