2011-08-19 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / libgfortran / generated / bessel_r16.c
blob7097f6b04609cea665ce891e5133310d81769a31
1 /* Implementation of the BESSEL_JN and BESSEL_YN transformational
2 function using a recurrence algorithm.
3 Copyright 2010 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 #if defined(GFC_REAL_16_IS_FLOAT128)
34 #define MATHFUNC(funcname) funcname ## q
35 #else
36 #define MATHFUNC(funcname) funcname ## l
37 #endif
38 #if defined(GFC_REAL_16_IS_FLOAT128)
39 #define BUILTINMATHFUNC(funcname) funcname ## q
40 #else
41 #define BUILTINMATHFUNC(funcname) funcname ## l
42 #endif
44 #if defined (HAVE_GFC_REAL_16)
48 #if (defined(GFC_REAL_16_IS_FLOAT128) || defined(HAVE_JNL))
49 extern void bessel_jn_r16 (gfc_array_r16 * const restrict ret, int n1,
50 int n2, GFC_REAL_16 x);
51 export_proto(bessel_jn_r16);
53 void
54 bessel_jn_r16 (gfc_array_r16 * const restrict ret, int n1, int n2, GFC_REAL_16 x)
56 int i;
57 index_type stride;
59 GFC_REAL_16 last1, last2, x2rev;
61 stride = GFC_DESCRIPTOR_STRIDE(ret,0);
63 if (ret->data == NULL)
65 size_t size = n2 < n1 ? 0 : n2-n1+1;
66 GFC_DIMENSION_SET(ret->dim[0], 0, size-1, 1);
67 ret->data = internal_malloc_size (sizeof (GFC_REAL_16) * size);
68 ret->offset = 0;
71 if (unlikely (n2 < n1))
72 return;
74 if (unlikely (compile_options.bounds_check)
75 && GFC_DESCRIPTOR_EXTENT(ret,0) != (n2-n1+1))
76 runtime_error("Incorrect extent in return value of BESSEL_JN "
77 "(%ld vs. %ld)", (long int) n2-n1,
78 (long int) GFC_DESCRIPTOR_EXTENT(ret,0));
80 stride = GFC_DESCRIPTOR_STRIDE(ret,0);
82 if (unlikely (x == 0))
84 ret->data[0] = 1;
85 for (i = 1; i <= n2-n1; i++)
86 ret->data[i*stride] = 0;
87 return;
90 ret->data = ret->data;
91 last1 = MATHFUNC(jn) (n2, x);
92 ret->data[(n2-n1)*stride] = last1;
94 if (n1 == n2)
95 return;
97 last2 = MATHFUNC(jn) (n2 - 1, x);
98 ret->data[(n2-n1-1)*stride] = last2;
100 if (n1 + 1 == n2)
101 return;
103 x2rev = GFC_REAL_16_LITERAL(2.)/x;
105 for (i = n2-n1-2; i >= 0; i--)
107 ret->data[i*stride] = x2rev * (i+1+n1) * last2 - last1;
108 last1 = last2;
109 last2 = ret->data[i*stride];
113 #endif
115 #if (defined(GFC_REAL_16_IS_FLOAT128) || defined(HAVE_YNL))
116 extern void bessel_yn_r16 (gfc_array_r16 * const restrict ret,
117 int n1, int n2, GFC_REAL_16 x);
118 export_proto(bessel_yn_r16);
120 void
121 bessel_yn_r16 (gfc_array_r16 * const restrict ret, int n1, int n2,
122 GFC_REAL_16 x)
124 int i;
125 index_type stride;
127 GFC_REAL_16 last1, last2, x2rev;
129 stride = GFC_DESCRIPTOR_STRIDE(ret,0);
131 if (ret->data == NULL)
133 size_t size = n2 < n1 ? 0 : n2-n1+1;
134 GFC_DIMENSION_SET(ret->dim[0], 0, size-1, 1);
135 ret->data = internal_malloc_size (sizeof (GFC_REAL_16) * size);
136 ret->offset = 0;
139 if (unlikely (n2 < n1))
140 return;
142 if (unlikely (compile_options.bounds_check)
143 && GFC_DESCRIPTOR_EXTENT(ret,0) != (n2-n1+1))
144 runtime_error("Incorrect extent in return value of BESSEL_JN "
145 "(%ld vs. %ld)", (long int) n2-n1,
146 (long int) GFC_DESCRIPTOR_EXTENT(ret,0));
148 stride = GFC_DESCRIPTOR_STRIDE(ret,0);
150 if (unlikely (x == 0))
152 for (i = 0; i <= n2-n1; i++)
153 #if defined(GFC_REAL_16_INFINITY)
154 ret->data[i*stride] = -GFC_REAL_16_INFINITY;
155 #else
156 ret->data[i*stride] = -GFC_REAL_16_HUGE;
157 #endif
158 return;
161 ret->data = ret->data;
162 last1 = MATHFUNC(yn) (n1, x);
163 ret->data[0] = last1;
165 if (n1 == n2)
166 return;
168 last2 = MATHFUNC(yn) (n1 + 1, x);
169 ret->data[1*stride] = last2;
171 if (n1 + 1 == n2)
172 return;
174 x2rev = GFC_REAL_16_LITERAL(2.)/x;
176 for (i = 2; i <= n1+n2; i++)
178 #if defined(GFC_REAL_16_INFINITY)
179 if (unlikely (last2 == -GFC_REAL_16_INFINITY))
181 ret->data[i*stride] = -GFC_REAL_16_INFINITY;
183 else
184 #endif
186 ret->data[i*stride] = x2rev * (i-1+n1) * last2 - last1;
187 last1 = last2;
188 last2 = ret->data[i*stride];
192 #endif
194 #endif