gcc/
[official-gcc.git] / libgfortran / m4 / bessel.m4
blob2f01e4e48bd8a9c49f43e5853a65b16a316a89a7
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>'
31 include(iparm.m4)dnl
32 include(`mtype.m4')dnl
34 mathfunc_macro
36 `#if defined (HAVE_'rtype_name`)
40 #if 'hasmathfunc(jn)`
41 extern void bessel_jn_r'rtype_kind` ('rtype` * const restrict ret, int n1,
42                                      int n2, 'rtype_name` x);
43 export_proto(bessel_jn_r'rtype_kind`);
45 void
46 bessel_jn_r'rtype_kind` ('rtype` * const restrict ret, int n1, int n2, 'rtype_name` x)
48   int i;
49   index_type stride;
51   'rtype_name` last1, last2, x2rev;
53   stride = GFC_DESCRIPTOR_STRIDE(ret,0);
55   if (ret->base_addr == NULL)
56     {
57       size_t size = n2 < n1 ? 0 : n2-n1+1; 
58       GFC_DIMENSION_SET(ret->dim[0], 0, size-1, 1);
59       ret->base_addr = xmallocarray (size, sizeof ('rtype_name`));
60       ret->offset = 0;
61     }
63   if (unlikely (n2 < n1))
64     return;
66   if (unlikely (compile_options.bounds_check)
67       && GFC_DESCRIPTOR_EXTENT(ret,0) != (n2-n1+1))
68     runtime_error("Incorrect extent in return value of BESSEL_JN "
69                   "(%ld vs. %ld)", (long int) n2-n1,
70                   (long int) GFC_DESCRIPTOR_EXTENT(ret,0));
72   stride = GFC_DESCRIPTOR_STRIDE(ret,0);
74   if (unlikely (x == 0))
75     {
76       ret->base_addr[0] = 1;
77       for (i = 1; i <= n2-n1; i++)
78         ret->base_addr[i*stride] = 0;
79       return;
80     }
82   last1 = MATHFUNC(jn) (n2, x);
83   ret->base_addr[(n2-n1)*stride] = last1;
85   if (n1 == n2)
86     return;
88   last2 = MATHFUNC(jn) (n2 - 1, x);
89   ret->base_addr[(n2-n1-1)*stride] = last2;
91   if (n1 + 1 == n2)
92     return;
94   x2rev = GFC_REAL_'rtype_kind`_LITERAL(2.)/x;
96   for (i = n2-n1-2; i >= 0; i--)
97     {
98       ret->base_addr[i*stride] = x2rev * (i+1+n1) * last2 - last1;
99       last1 = last2;
100       last2 = ret->base_addr[i*stride];
101     }
104 #endif
106 #if 'hasmathfunc(yn)`
107 extern void bessel_yn_r'rtype_kind` ('rtype` * const restrict ret,
108                                      int n1, int n2, 'rtype_name` x);
109 export_proto(bessel_yn_r'rtype_kind`);
111 void
112 bessel_yn_r'rtype_kind` ('rtype` * const restrict ret, int n1, int n2,
113                          'rtype_name` x)
115   int i;
116   index_type stride;
118   'rtype_name` last1, last2, x2rev;
120   stride = GFC_DESCRIPTOR_STRIDE(ret,0);
122   if (ret->base_addr == NULL)
123     {
124       size_t size = n2 < n1 ? 0 : n2-n1+1; 
125       GFC_DIMENSION_SET(ret->dim[0], 0, size-1, 1);
126       ret->base_addr = xmallocarray (size, sizeof ('rtype_name`));
127       ret->offset = 0;
128     }
130   if (unlikely (n2 < n1))
131     return;
133   if (unlikely (compile_options.bounds_check)
134       && GFC_DESCRIPTOR_EXTENT(ret,0) != (n2-n1+1))
135     runtime_error("Incorrect extent in return value of BESSEL_JN "
136                   "(%ld vs. %ld)", (long int) n2-n1,
137                   (long int) GFC_DESCRIPTOR_EXTENT(ret,0));
139   stride = GFC_DESCRIPTOR_STRIDE(ret,0);
141   if (unlikely (x == 0))
142     {
143       for (i = 0; i <= n2-n1; i++)
144 #if defined('rtype_name`_INFINITY)
145         ret->base_addr[i*stride] = -'rtype_name`_INFINITY;
146 #else
147         ret->base_addr[i*stride] = -'rtype_name`_HUGE;
148 #endif
149       return;
150     }
152   last1 = MATHFUNC(yn) (n1, x);
153   ret->base_addr[0] = last1;
155   if (n1 == n2)
156     return;
158   last2 = MATHFUNC(yn) (n1 + 1, x);
159   ret->base_addr[1*stride] = last2;
161   if (n1 + 1 == n2)
162     return;
164   x2rev = GFC_REAL_'rtype_kind`_LITERAL(2.)/x;
166   for (i = 2; i <= n2 - n1; i++)
167     {
168 #if defined('rtype_name`_INFINITY)
169       if (unlikely (last2 == -'rtype_name`_INFINITY))
170         {
171           ret->base_addr[i*stride] = -'rtype_name`_INFINITY;
172         }
173       else
174 #endif
175         {
176           ret->base_addr[i*stride] = x2rev * (i-1+n1) * last2 - last1;
177           last1 = last2;
178           last2 = ret->base_addr[i*stride];
179         }
180     }
182 #endif
184 #endif'