2015-01-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / libgfortran / generated / bessel_r8.c
blob52e21dda93158c3b23ffb63caec8e5c056125037
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>
6 This file is part of the GNU Fortran runtime library (libgfortran).
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.
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.
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.
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/>. */
27 #include "libgfortran.h"
28 #include <stdlib.h>
29 #include <assert.h>
33 #define MATHFUNC(funcname) funcname
35 #if defined (HAVE_GFC_REAL_8)
39 #if defined (HAVE_JN)
40 extern void bessel_jn_r8 (gfc_array_r8 * const restrict ret, int n1,
41 int n2, GFC_REAL_8 x);
42 export_proto(bessel_jn_r8);
44 void
45 bessel_jn_r8 (gfc_array_r8 * const restrict ret, int n1, int n2, GFC_REAL_8 x)
47 int i;
48 index_type stride;
50 GFC_REAL_8 last1, last2, x2rev;
52 stride = GFC_DESCRIPTOR_STRIDE(ret,0);
54 if (ret->base_addr == NULL)
56 size_t size = n2 < n1 ? 0 : n2-n1+1;
57 GFC_DIMENSION_SET(ret->dim[0], 0, size-1, 1);
58 ret->base_addr = xmallocarray (size, sizeof (GFC_REAL_8));
59 ret->offset = 0;
62 if (unlikely (n2 < n1))
63 return;
65 if (unlikely (compile_options.bounds_check)
66 && GFC_DESCRIPTOR_EXTENT(ret,0) != (n2-n1+1))
67 runtime_error("Incorrect extent in return value of BESSEL_JN "
68 "(%ld vs. %ld)", (long int) n2-n1,
69 (long int) GFC_DESCRIPTOR_EXTENT(ret,0));
71 stride = GFC_DESCRIPTOR_STRIDE(ret,0);
73 if (unlikely (x == 0))
75 ret->base_addr[0] = 1;
76 for (i = 1; i <= n2-n1; i++)
77 ret->base_addr[i*stride] = 0;
78 return;
81 last1 = MATHFUNC(jn) (n2, x);
82 ret->base_addr[(n2-n1)*stride] = last1;
84 if (n1 == n2)
85 return;
87 last2 = MATHFUNC(jn) (n2 - 1, x);
88 ret->base_addr[(n2-n1-1)*stride] = last2;
90 if (n1 + 1 == n2)
91 return;
93 x2rev = GFC_REAL_8_LITERAL(2.)/x;
95 for (i = n2-n1-2; i >= 0; i--)
97 ret->base_addr[i*stride] = x2rev * (i+1+n1) * last2 - last1;
98 last1 = last2;
99 last2 = ret->base_addr[i*stride];
103 #endif
105 #if defined (HAVE_YN)
106 extern void bessel_yn_r8 (gfc_array_r8 * const restrict ret,
107 int n1, int n2, GFC_REAL_8 x);
108 export_proto(bessel_yn_r8);
110 void
111 bessel_yn_r8 (gfc_array_r8 * const restrict ret, int n1, int n2,
112 GFC_REAL_8 x)
114 int i;
115 index_type stride;
117 GFC_REAL_8 last1, last2, x2rev;
119 stride = GFC_DESCRIPTOR_STRIDE(ret,0);
121 if (ret->base_addr == NULL)
123 size_t size = n2 < n1 ? 0 : n2-n1+1;
124 GFC_DIMENSION_SET(ret->dim[0], 0, size-1, 1);
125 ret->base_addr = xmallocarray (size, sizeof (GFC_REAL_8));
126 ret->offset = 0;
129 if (unlikely (n2 < n1))
130 return;
132 if (unlikely (compile_options.bounds_check)
133 && GFC_DESCRIPTOR_EXTENT(ret,0) != (n2-n1+1))
134 runtime_error("Incorrect extent in return value of BESSEL_JN "
135 "(%ld vs. %ld)", (long int) n2-n1,
136 (long int) GFC_DESCRIPTOR_EXTENT(ret,0));
138 stride = GFC_DESCRIPTOR_STRIDE(ret,0);
140 if (unlikely (x == 0))
142 for (i = 0; i <= n2-n1; i++)
143 #if defined(GFC_REAL_8_INFINITY)
144 ret->base_addr[i*stride] = -GFC_REAL_8_INFINITY;
145 #else
146 ret->base_addr[i*stride] = -GFC_REAL_8_HUGE;
147 #endif
148 return;
151 last1 = MATHFUNC(yn) (n1, x);
152 ret->base_addr[0] = last1;
154 if (n1 == n2)
155 return;
157 last2 = MATHFUNC(yn) (n1 + 1, x);
158 ret->base_addr[1*stride] = last2;
160 if (n1 + 1 == n2)
161 return;
163 x2rev = GFC_REAL_8_LITERAL(2.)/x;
165 for (i = 2; i <= n2 - n1; i++)
167 #if defined(GFC_REAL_8_INFINITY)
168 if (unlikely (last2 == -GFC_REAL_8_INFINITY))
170 ret->base_addr[i*stride] = -GFC_REAL_8_INFINITY;
172 else
173 #endif
175 ret->base_addr[i*stride] = x2rev * (i-1+n1) * last2 - last1;
176 last1 = last2;
177 last2 = ret->base_addr[i*stride];
181 #endif
183 #endif