1 /* Implementation of the BESSEL_JN and BESSEL_YN transformational
2 function using a recurrence algorithm.
3 Copyright (C) 2010-2017 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"
31 #define MATHFUNC(funcname) funcname ## f
33 #if defined (HAVE_GFC_REAL_4)
37 #if defined (HAVE_JNF)
38 extern void bessel_jn_r4 (gfc_array_r4
* const restrict ret
, int n1
,
39 int n2
, GFC_REAL_4 x
);
40 export_proto(bessel_jn_r4
);
43 bessel_jn_r4 (gfc_array_r4
* const restrict ret
, int n1
, int n2
, GFC_REAL_4 x
)
48 GFC_REAL_4 last1
, last2
, x2rev
;
50 stride
= GFC_DESCRIPTOR_STRIDE(ret
,0);
52 if (ret
->base_addr
== NULL
)
54 size_t size
= n2
< n1
? 0 : n2
-n1
+1;
55 GFC_DIMENSION_SET(ret
->dim
[0], 0, size
-1, 1);
56 ret
->base_addr
= xmallocarray (size
, sizeof (GFC_REAL_4
));
60 if (unlikely (n2
< n1
))
63 if (unlikely (compile_options
.bounds_check
)
64 && GFC_DESCRIPTOR_EXTENT(ret
,0) != (n2
-n1
+1))
65 runtime_error("Incorrect extent in return value of BESSEL_JN "
66 "(%ld vs. %ld)", (long int) n2
-n1
,
67 (long int) GFC_DESCRIPTOR_EXTENT(ret
,0));
69 stride
= GFC_DESCRIPTOR_STRIDE(ret
,0);
71 if (unlikely (x
== 0))
73 ret
->base_addr
[0] = 1;
74 for (i
= 1; i
<= n2
-n1
; i
++)
75 ret
->base_addr
[i
*stride
] = 0;
79 last1
= MATHFUNC(jn
) (n2
, x
);
80 ret
->base_addr
[(n2
-n1
)*stride
] = last1
;
85 last2
= MATHFUNC(jn
) (n2
- 1, x
);
86 ret
->base_addr
[(n2
-n1
-1)*stride
] = last2
;
91 x2rev
= GFC_REAL_4_LITERAL(2.)/x
;
93 for (i
= n2
-n1
-2; i
>= 0; i
--)
95 ret
->base_addr
[i
*stride
] = x2rev
* (i
+1+n1
) * last2
- last1
;
97 last2
= ret
->base_addr
[i
*stride
];
103 #if defined (HAVE_YNF)
104 extern void bessel_yn_r4 (gfc_array_r4
* const restrict ret
,
105 int n1
, int n2
, GFC_REAL_4 x
);
106 export_proto(bessel_yn_r4
);
109 bessel_yn_r4 (gfc_array_r4
* const restrict ret
, int n1
, int n2
,
115 GFC_REAL_4 last1
, last2
, x2rev
;
117 stride
= GFC_DESCRIPTOR_STRIDE(ret
,0);
119 if (ret
->base_addr
== NULL
)
121 size_t size
= n2
< n1
? 0 : n2
-n1
+1;
122 GFC_DIMENSION_SET(ret
->dim
[0], 0, size
-1, 1);
123 ret
->base_addr
= xmallocarray (size
, sizeof (GFC_REAL_4
));
127 if (unlikely (n2
< n1
))
130 if (unlikely (compile_options
.bounds_check
)
131 && GFC_DESCRIPTOR_EXTENT(ret
,0) != (n2
-n1
+1))
132 runtime_error("Incorrect extent in return value of BESSEL_JN "
133 "(%ld vs. %ld)", (long int) n2
-n1
,
134 (long int) GFC_DESCRIPTOR_EXTENT(ret
,0));
136 stride
= GFC_DESCRIPTOR_STRIDE(ret
,0);
138 if (unlikely (x
== 0))
140 for (i
= 0; i
<= n2
-n1
; i
++)
141 #if defined(GFC_REAL_4_INFINITY)
142 ret
->base_addr
[i
*stride
] = -GFC_REAL_4_INFINITY
;
144 ret
->base_addr
[i
*stride
] = -GFC_REAL_4_HUGE
;
149 last1
= MATHFUNC(yn
) (n1
, x
);
150 ret
->base_addr
[0] = last1
;
155 last2
= MATHFUNC(yn
) (n1
+ 1, x
);
156 ret
->base_addr
[1*stride
] = last2
;
161 x2rev
= GFC_REAL_4_LITERAL(2.)/x
;
163 for (i
= 2; i
<= n2
- n1
; i
++)
165 #if defined(GFC_REAL_4_INFINITY)
166 if (unlikely (last2
== -GFC_REAL_4_INFINITY
))
168 ret
->base_addr
[i
*stride
] = -GFC_REAL_4_INFINITY
;
173 ret
->base_addr
[i
*stride
] = x2rev
* (i
-1+n1
) * last2
- last1
;
175 last2
= ret
->base_addr
[i
*stride
];