bessel_r16.c 4.61 KB
Newer Older
1 2
/* Implementation of the BESSEL_JN and BESSEL_YN transformational
   function using a recurrence algorithm.
3
   Copyright (C) 2010-2017 Free Software Foundation, Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
   Contributed by Tobias Burnus <burnus@net-b.de>

This file is part of the GNU Fortran runtime library (libgfortran).

Libgfortran is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.

Libgfortran is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.

You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
<http://www.gnu.org/licenses/>.  */

#include "libgfortran.h"


30 31 32 33 34 35 36

#if defined(GFC_REAL_16_IS_FLOAT128)
#define MATHFUNC(funcname) funcname ## q
#else
#define MATHFUNC(funcname) funcname ## l
#endif

37 38 39 40
#if defined (HAVE_GFC_REAL_16)



41
#if (defined(GFC_REAL_16_IS_FLOAT128) || defined(HAVE_JNL))
42 43 44 45 46 47 48 49 50 51 52 53 54 55
extern void bessel_jn_r16 (gfc_array_r16 * const restrict ret, int n1,
				     int n2, GFC_REAL_16 x);
export_proto(bessel_jn_r16);

void
bessel_jn_r16 (gfc_array_r16 * const restrict ret, int n1, int n2, GFC_REAL_16 x)
{
  int i;
  index_type stride;

  GFC_REAL_16 last1, last2, x2rev;

  stride = GFC_DESCRIPTOR_STRIDE(ret,0);

56
  if (ret->base_addr == NULL)
57 58 59
    {
      size_t size = n2 < n1 ? 0 : n2-n1+1; 
      GFC_DIMENSION_SET(ret->dim[0], 0, size-1, 1);
60
      ret->base_addr = xmallocarray (size, sizeof (GFC_REAL_16));
61 62 63 64 65 66 67 68 69 70
      ret->offset = 0;
    }

  if (unlikely (n2 < n1))
    return;

  if (unlikely (compile_options.bounds_check)
      && GFC_DESCRIPTOR_EXTENT(ret,0) != (n2-n1+1))
    runtime_error("Incorrect extent in return value of BESSEL_JN "
		  "(%ld vs. %ld)", (long int) n2-n1,
71
		  (long int) GFC_DESCRIPTOR_EXTENT(ret,0));
72 73 74

  stride = GFC_DESCRIPTOR_STRIDE(ret,0);

75
  if (unlikely (x == 0))
76
    {
77
      ret->base_addr[0] = 1;
78
      for (i = 1; i <= n2-n1; i++)
79
        ret->base_addr[i*stride] = 0;
80 81 82
      return;
    }

83
  last1 = MATHFUNC(jn) (n2, x);
84
  ret->base_addr[(n2-n1)*stride] = last1;
85 86 87 88

  if (n1 == n2)
    return;

89
  last2 = MATHFUNC(jn) (n2 - 1, x);
90
  ret->base_addr[(n2-n1-1)*stride] = last2;
91 92 93 94

  if (n1 + 1 == n2)
    return;

95
  x2rev = GFC_REAL_16_LITERAL(2.)/x;
96 97 98

  for (i = n2-n1-2; i >= 0; i--)
    {
99
      ret->base_addr[i*stride] = x2rev * (i+1+n1) * last2 - last1;
100
      last1 = last2;
101
      last2 = ret->base_addr[i*stride];
102 103 104 105 106
    }
}

#endif

107
#if (defined(GFC_REAL_16_IS_FLOAT128) || defined(HAVE_YNL))
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
extern void bessel_yn_r16 (gfc_array_r16 * const restrict ret,
				     int n1, int n2, GFC_REAL_16 x);
export_proto(bessel_yn_r16);

void
bessel_yn_r16 (gfc_array_r16 * const restrict ret, int n1, int n2,
			 GFC_REAL_16 x)
{
  int i;
  index_type stride;

  GFC_REAL_16 last1, last2, x2rev;

  stride = GFC_DESCRIPTOR_STRIDE(ret,0);

123
  if (ret->base_addr == NULL)
124 125 126
    {
      size_t size = n2 < n1 ? 0 : n2-n1+1; 
      GFC_DIMENSION_SET(ret->dim[0], 0, size-1, 1);
127
      ret->base_addr = xmallocarray (size, sizeof (GFC_REAL_16));
128 129 130 131 132 133 134 135 136 137
      ret->offset = 0;
    }

  if (unlikely (n2 < n1))
    return;

  if (unlikely (compile_options.bounds_check)
      && GFC_DESCRIPTOR_EXTENT(ret,0) != (n2-n1+1))
    runtime_error("Incorrect extent in return value of BESSEL_JN "
		  "(%ld vs. %ld)", (long int) n2-n1,
138
		  (long int) GFC_DESCRIPTOR_EXTENT(ret,0));
139 140 141

  stride = GFC_DESCRIPTOR_STRIDE(ret,0);

142
  if (unlikely (x == 0))
143 144 145
    {
      for (i = 0; i <= n2-n1; i++)
#if defined(GFC_REAL_16_INFINITY)
146
        ret->base_addr[i*stride] = -GFC_REAL_16_INFINITY;
147
#else
148
        ret->base_addr[i*stride] = -GFC_REAL_16_HUGE;
149 150 151 152
#endif
      return;
    }

153
  last1 = MATHFUNC(yn) (n1, x);
154
  ret->base_addr[0] = last1;
155 156 157 158

  if (n1 == n2)
    return;

159
  last2 = MATHFUNC(yn) (n1 + 1, x);
160
  ret->base_addr[1*stride] = last2;
161 162 163 164

  if (n1 + 1 == n2)
    return;

165
  x2rev = GFC_REAL_16_LITERAL(2.)/x;
166

167
  for (i = 2; i <= n2 - n1; i++)
168 169 170 171
    {
#if defined(GFC_REAL_16_INFINITY)
      if (unlikely (last2 == -GFC_REAL_16_INFINITY))
	{
172
	  ret->base_addr[i*stride] = -GFC_REAL_16_INFINITY;
173 174 175 176
	}
      else
#endif
	{
177
	  ret->base_addr[i*stride] = x2rev * (i-1+n1) * last2 - last1;
178
	  last1 = last2;
179
	  last2 = ret->base_addr[i*stride];
180 181 182 183 184 185 186
	}
    }
}
#endif

#endif