bessel_r16.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* Implementation of the BESSEL_JN and BESSEL_YN transformational
  2. function using a recurrence algorithm.
  3. Copyright (C) 2010-2015 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. #include <stdlib.h>
  23. #include <assert.h>
  24. #if defined(GFC_REAL_16_IS_FLOAT128)
  25. #define MATHFUNC(funcname) funcname ## q
  26. #else
  27. #define MATHFUNC(funcname) funcname ## l
  28. #endif
  29. #if defined (HAVE_GFC_REAL_16)
  30. #if (defined(GFC_REAL_16_IS_FLOAT128) || defined(HAVE_JNL))
  31. extern void bessel_jn_r16 (gfc_array_r16 * const restrict ret, int n1,
  32. int n2, GFC_REAL_16 x);
  33. export_proto(bessel_jn_r16);
  34. void
  35. bessel_jn_r16 (gfc_array_r16 * const restrict ret, int n1, int n2, GFC_REAL_16 x)
  36. {
  37. int i;
  38. index_type stride;
  39. GFC_REAL_16 last1, last2, x2rev;
  40. stride = GFC_DESCRIPTOR_STRIDE(ret,0);
  41. if (ret->base_addr == NULL)
  42. {
  43. size_t size = n2 < n1 ? 0 : n2-n1+1;
  44. GFC_DIMENSION_SET(ret->dim[0], 0, size-1, 1);
  45. ret->base_addr = xmallocarray (size, sizeof (GFC_REAL_16));
  46. ret->offset = 0;
  47. }
  48. if (unlikely (n2 < n1))
  49. return;
  50. if (unlikely (compile_options.bounds_check)
  51. && GFC_DESCRIPTOR_EXTENT(ret,0) != (n2-n1+1))
  52. runtime_error("Incorrect extent in return value of BESSEL_JN "
  53. "(%ld vs. %ld)", (long int) n2-n1,
  54. (long int) GFC_DESCRIPTOR_EXTENT(ret,0));
  55. stride = GFC_DESCRIPTOR_STRIDE(ret,0);
  56. if (unlikely (x == 0))
  57. {
  58. ret->base_addr[0] = 1;
  59. for (i = 1; i <= n2-n1; i++)
  60. ret->base_addr[i*stride] = 0;
  61. return;
  62. }
  63. last1 = MATHFUNC(jn) (n2, x);
  64. ret->base_addr[(n2-n1)*stride] = last1;
  65. if (n1 == n2)
  66. return;
  67. last2 = MATHFUNC(jn) (n2 - 1, x);
  68. ret->base_addr[(n2-n1-1)*stride] = last2;
  69. if (n1 + 1 == n2)
  70. return;
  71. x2rev = GFC_REAL_16_LITERAL(2.)/x;
  72. for (i = n2-n1-2; i >= 0; i--)
  73. {
  74. ret->base_addr[i*stride] = x2rev * (i+1+n1) * last2 - last1;
  75. last1 = last2;
  76. last2 = ret->base_addr[i*stride];
  77. }
  78. }
  79. #endif
  80. #if (defined(GFC_REAL_16_IS_FLOAT128) || defined(HAVE_YNL))
  81. extern void bessel_yn_r16 (gfc_array_r16 * const restrict ret,
  82. int n1, int n2, GFC_REAL_16 x);
  83. export_proto(bessel_yn_r16);
  84. void
  85. bessel_yn_r16 (gfc_array_r16 * const restrict ret, int n1, int n2,
  86. GFC_REAL_16 x)
  87. {
  88. int i;
  89. index_type stride;
  90. GFC_REAL_16 last1, last2, x2rev;
  91. stride = GFC_DESCRIPTOR_STRIDE(ret,0);
  92. if (ret->base_addr == NULL)
  93. {
  94. size_t size = n2 < n1 ? 0 : n2-n1+1;
  95. GFC_DIMENSION_SET(ret->dim[0], 0, size-1, 1);
  96. ret->base_addr = xmallocarray (size, sizeof (GFC_REAL_16));
  97. ret->offset = 0;
  98. }
  99. if (unlikely (n2 < n1))
  100. return;
  101. if (unlikely (compile_options.bounds_check)
  102. && GFC_DESCRIPTOR_EXTENT(ret,0) != (n2-n1+1))
  103. runtime_error("Incorrect extent in return value of BESSEL_JN "
  104. "(%ld vs. %ld)", (long int) n2-n1,
  105. (long int) GFC_DESCRIPTOR_EXTENT(ret,0));
  106. stride = GFC_DESCRIPTOR_STRIDE(ret,0);
  107. if (unlikely (x == 0))
  108. {
  109. for (i = 0; i <= n2-n1; i++)
  110. #if defined(GFC_REAL_16_INFINITY)
  111. ret->base_addr[i*stride] = -GFC_REAL_16_INFINITY;
  112. #else
  113. ret->base_addr[i*stride] = -GFC_REAL_16_HUGE;
  114. #endif
  115. return;
  116. }
  117. last1 = MATHFUNC(yn) (n1, x);
  118. ret->base_addr[0] = last1;
  119. if (n1 == n2)
  120. return;
  121. last2 = MATHFUNC(yn) (n1 + 1, x);
  122. ret->base_addr[1*stride] = last2;
  123. if (n1 + 1 == n2)
  124. return;
  125. x2rev = GFC_REAL_16_LITERAL(2.)/x;
  126. for (i = 2; i <= n2 - n1; i++)
  127. {
  128. #if defined(GFC_REAL_16_INFINITY)
  129. if (unlikely (last2 == -GFC_REAL_16_INFINITY))
  130. {
  131. ret->base_addr[i*stride] = -GFC_REAL_16_INFINITY;
  132. }
  133. else
  134. #endif
  135. {
  136. ret->base_addr[i*stride] = x2rev * (i-1+n1) * last2 - last1;
  137. last1 = last2;
  138. last2 = ret->base_addr[i*stride];
  139. }
  140. }
  141. }
  142. #endif
  143. #endif