/lib/libm/complex/cephes_subrf.c

https://bitbucket.org/cooljeanius/dragonflybsd · C · 123 lines · 73 code · 17 blank · 33 comment · 4 complexity · a7f34aa484f066efa6b591c64b46257b MD5 · raw file

  1. /* $NetBSD: cephes_subrf.c,v 1.1 2007/08/20 16:01:34 drochner Exp $ */
  2. /*-
  3. * Copyright (c) 2007 The NetBSD Foundation, Inc.
  4. * All rights reserved.
  5. *
  6. * This code is derived from software written by Stephen L. Moshier.
  7. * It is redistributed by the NetBSD Foundation by permission of the author.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  19. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  20. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  21. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  22. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include <complex.h>
  31. #include <math.h>
  32. #include "cephes_subrf.h"
  33. /* calculate cosh and sinh */
  34. void
  35. _cchshf(float x, float *c, float *s)
  36. {
  37. float e, ei;
  38. if (fabsf(x) <= 0.5f) {
  39. *c = coshf(x);
  40. *s = sinhf(x);
  41. } else {
  42. e = expf(x);
  43. ei = 0.5f / e;
  44. e = 0.5f * e;
  45. *s = e - ei;
  46. *c = e + ei;
  47. }
  48. }
  49. /* Program to subtract nearest integer multiple of PI */
  50. /* extended precision value of PI: */
  51. static const double DP1 = 3.140625;
  52. static const double DP2 = 9.67502593994140625E-4;
  53. static const double DP3 = 1.509957990978376432E-7;
  54. #define MACHEPF 3.0e-8
  55. float
  56. _redupif(float x)
  57. {
  58. float t;
  59. long i;
  60. t = x / (float)M_PI;
  61. if (t >= 0.0f)
  62. t += 0.5f;
  63. else
  64. t -= 0.5f;
  65. i = t; /* the multiple */
  66. t = i;
  67. t = ((x - t * DP1) - t * DP2) - t * DP3;
  68. return t;
  69. }
  70. /* Taylor series expansion for cosh(2y) - cos(2x) */
  71. float
  72. _ctansf(float complex z)
  73. {
  74. float f, x, x2, y, y2, rn, t, d;
  75. x = fabsf(2.0f * crealf(z));
  76. y = fabsf(2.0f * cimagf(z));
  77. x = _redupif(x);
  78. x = x * x;
  79. y = y * y;
  80. x2 = 1.0f;
  81. y2 = 1.0f;
  82. f = 1.0f;
  83. rn = 0.0f;
  84. d = 0.0f;
  85. do {
  86. rn += 1.0f;
  87. f *= rn;
  88. rn += 1.0f;
  89. f *= rn;
  90. x2 *= x;
  91. y2 *= y;
  92. t = y2 + x2;
  93. t /= f;
  94. d += t;
  95. rn += 1.0f;
  96. f *= rn;
  97. rn += 1.0f;
  98. f *= rn;
  99. x2 *= x;
  100. y2 *= y;
  101. t = y2 - x2;
  102. t /= f;
  103. d += t;
  104. } while (fabsf(t/d) > MACHEPF);
  105. return d;
  106. }