bessel_r16.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* Implementation of the BESSEL_JN and BESSEL_YN transformational
  2. function using a recurrence algorithm.
  3. Copyright (C) 2010-2022 Free Software Foundation, Inc.
  4. Contributed by Tobias Burnus <burnus@net-b.de>
  5. This file is part of the GNU Fortran runtime library (libgfortran).
  6. Libgfortran is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public
  8. License as published by the Free Software Foundation; either
  9. version 3 of the License, or (at your option) any later version.
  10. Libgfortran is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. Under Section 7 of GPL version 3, you are granted additional
  15. permissions described in the GCC Runtime Library Exception, version
  16. 3.1, as published by the Free Software Foundation.
  17. You should have received a copy of the GNU General Public License and
  18. a copy of the GCC Runtime Library Exception along with this program;
  19. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  20. <http://www.gnu.org/licenses/>. */
  21. #include "libgfortran.h"
  22. #if defined(GFC_REAL_16_IS_FLOAT128)
  23. #define MATHFUNC(funcname) funcname ## q
  24. #else
  25. #define MATHFUNC(funcname) funcname ## l
  26. #endif
  27. #if defined (HAVE_GFC_REAL_16)
  28. #if (defined(GFC_REAL_16_IS_FLOAT128) || defined(HAVE_JNL))
  29. extern void bessel_jn_r16 (gfc_array_r16 * const restrict ret, int n1,
  30. int n2, GFC_REAL_16 x);
  31. export_proto(bessel_jn_r16);
  32. void
  33. bessel_jn_r16 (gfc_array_r16 * const restrict ret, int n1, int n2, GFC_REAL_16 x)
  34. {
  35. int i;
  36. index_type stride;
  37. GFC_REAL_16 last1, last2, x2rev;
  38. stride = GFC_DESCRIPTOR_STRIDE(ret,0);
  39. if (ret->base_addr == NULL)
  40. {
  41. size_t size = n2 < n1 ? 0 : n2-n1+1;
  42. GFC_DIMENSION_SET(ret->dim[0], 0, size-1, 1);
  43. ret->base_addr = xmallocarray (size, sizeof (GFC_REAL_16));
  44. ret->offset = 0;
  45. }
  46. if (unlikely (n2 < n1))
  47. return;
  48. if (unlikely (compile_options.bounds_check)
  49. && GFC_DESCRIPTOR_EXTENT(ret,0) != (n2-n1+1))
  50. runtime_error("Incorrect extent in return value of BESSEL_JN "
  51. "(%ld vs. %ld)", (long int) n2-n1,
  52. (long int) GFC_DESCRIPTOR_EXTENT(ret,0));
  53. stride = GFC_DESCRIPTOR_STRIDE(ret,0);
  54. if (unlikely (x == 0))
  55. {
  56. ret->base_addr[0] = 1;
  57. for (i = 1; i <= n2-n1; i++)
  58. ret->base_addr[i*stride] = 0;
  59. return;
  60. }
  61. last1 = MATHFUNC(jn) (n2, x);
  62. ret->base_addr[(n2-n1)*stride] = last1;
  63. if (n1 == n2)
  64. return;
  65. last2 = MATHFUNC(jn) (n2 - 1, x);
  66. ret->base_addr[(n2-n1-1)*stride] = last2;
  67. if (n1 + 1 == n2)
  68. return;
  69. x2rev = GFC_REAL_16_LITERAL(2.)/x;
  70. for (i = n2-n1-2; i >= 0; i--)
  71. {
  72. ret->base_addr[i*stride] = x2rev * (i+1+n1) * last2 - last1;
  73. last1 = last2;
  74. last2 = ret->base_addr[i*stride];
  75. }
  76. }
  77. #endif
  78. #if (defined(GFC_REAL_16_IS_FLOAT128) || defined(HAVE_YNL))
  79. extern void bessel_yn_r16 (gfc_array_r16 * const restrict ret,
  80. int n1, int n2, GFC_REAL_16 x);
  81. export_proto(bessel_yn_r16);
  82. void
  83. bessel_yn_r16 (gfc_array_r16 * const restrict ret, int n1, int n2,
  84. GFC_REAL_16 x)
  85. {
  86. int i;
  87. index_type stride;
  88. GFC_REAL_16 last1, last2, x2rev;
  89. stride = GFC_DESCRIPTOR_STRIDE(ret,0);
  90. if (ret->base_addr == NULL)
  91. {
  92. size_t size = n2 < n1 ? 0 : n2-n1+1;
  93. GFC_DIMENSION_SET(ret->dim[0], 0, size-1, 1);
  94. ret->base_addr = xmallocarray (size, sizeof (GFC_REAL_16));
  95. ret->offset = 0;
  96. }
  97. if (unlikely (n2 < n1))
  98. return;
  99. if (unlikely (compile_options.bounds_check)
  100. && GFC_DESCRIPTOR_EXTENT(ret,0) != (n2-n1+1))
  101. runtime_error("Incorrect extent in return value of BESSEL_JN "
  102. "(%ld vs. %ld)", (long int) n2-n1,
  103. (long int) GFC_DESCRIPTOR_EXTENT(ret,0));
  104. stride = GFC_DESCRIPTOR_STRIDE(ret,0);
  105. if (unlikely (x == 0))
  106. {
  107. for (i = 0; i <= n2-n1; i++)
  108. #if defined(GFC_REAL_16_INFINITY)
  109. ret->base_addr[i*stride] = -GFC_REAL_16_INFINITY;
  110. #else
  111. ret->base_addr[i*stride] = -GFC_REAL_16_HUGE;
  112. #endif
  113. return;
  114. }
  115. last1 = MATHFUNC(yn) (n1, x);
  116. ret->base_addr[0] = last1;
  117. if (n1 == n2)
  118. return;
  119. last2 = MATHFUNC(yn) (n1 + 1, x);
  120. ret->base_addr[1*stride] = last2;
  121. if (n1 + 1 == n2)
  122. return;
  123. x2rev = GFC_REAL_16_LITERAL(2.)/x;
  124. for (i = 2; i <= n2 - n1; i++)
  125. {
  126. #if defined(GFC_REAL_16_INFINITY)
  127. if (unlikely (last2 == -GFC_REAL_16_INFINITY))
  128. {
  129. ret->base_addr[i*stride] = -GFC_REAL_16_INFINITY;
  130. }
  131. else
  132. #endif
  133. {
  134. ret->base_addr[i*stride] = x2rev * (i-1+n1) * last2 - last1;
  135. last1 = last2;
  136. last2 = ret->base_addr[i*stride];
  137. }
  138. }
  139. }
  140. #endif
  141. #endif