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