norm2_r10.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* Implementation of the NORM2 intrinsic
  2. Copyright (C) 2010-2022 Free Software Foundation, Inc.
  3. Contributed by Tobias Burnus <burnus@net-b.de>
  4. This file is part of the GNU Fortran runtime library (libgfortran).
  5. Libgfortran is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public
  7. License as published by the Free Software Foundation; either
  8. version 3 of the License, or (at your option) any later version.
  9. Libgfortran is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #include "libgfortran.h"
  21. #if defined (HAVE_GFC_REAL_10) && defined (HAVE_GFC_REAL_10) && defined (HAVE_SQRTL) && defined (HAVE_FABSL)
  22. #define MATHFUNC(funcname) funcname ## l
  23. extern void norm2_r10 (gfc_array_r10 * const restrict,
  24. gfc_array_r10 * const restrict, const index_type * const restrict);
  25. export_proto(norm2_r10);
  26. void
  27. norm2_r10 (gfc_array_r10 * const restrict retarray,
  28. gfc_array_r10 * const restrict array,
  29. const index_type * const restrict pdim)
  30. {
  31. index_type count[GFC_MAX_DIMENSIONS];
  32. index_type extent[GFC_MAX_DIMENSIONS];
  33. index_type sstride[GFC_MAX_DIMENSIONS];
  34. index_type dstride[GFC_MAX_DIMENSIONS];
  35. const GFC_REAL_10 * restrict base;
  36. GFC_REAL_10 * restrict dest;
  37. index_type rank;
  38. index_type n;
  39. index_type len;
  40. index_type delta;
  41. index_type dim;
  42. int continue_loop;
  43. /* Make dim zero based to avoid confusion. */
  44. rank = GFC_DESCRIPTOR_RANK (array) - 1;
  45. dim = (*pdim) - 1;
  46. if (unlikely (dim < 0 || dim > rank))
  47. {
  48. runtime_error ("Dim argument incorrect in NORM intrinsic: "
  49. "is %ld, should be between 1 and %ld",
  50. (long int) dim + 1, (long int) rank + 1);
  51. }
  52. len = GFC_DESCRIPTOR_EXTENT(array,dim);
  53. if (len < 0)
  54. len = 0;
  55. delta = GFC_DESCRIPTOR_STRIDE(array,dim);
  56. for (n = 0; n < dim; n++)
  57. {
  58. sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
  59. extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
  60. if (extent[n] < 0)
  61. extent[n] = 0;
  62. }
  63. for (n = dim; n < rank; n++)
  64. {
  65. sstride[n] = GFC_DESCRIPTOR_STRIDE(array, n + 1);
  66. extent[n] = GFC_DESCRIPTOR_EXTENT(array, n + 1);
  67. if (extent[n] < 0)
  68. extent[n] = 0;
  69. }
  70. if (retarray->base_addr == NULL)
  71. {
  72. size_t alloc_size, str;
  73. for (n = 0; n < rank; n++)
  74. {
  75. if (n == 0)
  76. str = 1;
  77. else
  78. str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
  79. GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
  80. }
  81. retarray->offset = 0;
  82. retarray->dtype.rank = rank;
  83. alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
  84. retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_REAL_10));
  85. if (alloc_size == 0)
  86. {
  87. /* Make sure we have a zero-sized array. */
  88. GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
  89. return;
  90. }
  91. }
  92. else
  93. {
  94. if (rank != GFC_DESCRIPTOR_RANK (retarray))
  95. runtime_error ("rank of return array incorrect in"
  96. " NORM intrinsic: is %ld, should be %ld",
  97. (long int) (GFC_DESCRIPTOR_RANK (retarray)),
  98. (long int) rank);
  99. if (unlikely (compile_options.bounds_check))
  100. bounds_ifunction_return ((array_t *) retarray, extent,
  101. "return value", "NORM");
  102. }
  103. for (n = 0; n < rank; n++)
  104. {
  105. count[n] = 0;
  106. dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
  107. if (extent[n] <= 0)
  108. return;
  109. }
  110. base = array->base_addr;
  111. dest = retarray->base_addr;
  112. continue_loop = 1;
  113. while (continue_loop)
  114. {
  115. const GFC_REAL_10 * restrict src;
  116. GFC_REAL_10 result;
  117. src = base;
  118. {
  119. GFC_REAL_10 scale;
  120. result = 0;
  121. scale = 1;
  122. if (len <= 0)
  123. *dest = 0;
  124. else
  125. {
  126. #if ! defined HAVE_BACK_ARG
  127. for (n = 0; n < len; n++, src += delta)
  128. {
  129. #endif
  130. if (*src != 0)
  131. {
  132. GFC_REAL_10 absX, val;
  133. absX = MATHFUNC(fabs) (*src);
  134. if (scale < absX)
  135. {
  136. val = scale / absX;
  137. result = 1 + result * val * val;
  138. scale = absX;
  139. }
  140. else
  141. {
  142. val = absX / scale;
  143. result += val * val;
  144. }
  145. }
  146. }
  147. result = scale * MATHFUNC(sqrt) (result);
  148. *dest = result;
  149. }
  150. }
  151. /* Advance to the next element. */
  152. count[0]++;
  153. base += sstride[0];
  154. dest += dstride[0];
  155. n = 0;
  156. while (count[n] == extent[n])
  157. {
  158. /* When we get to the end of a dimension, reset it and increment
  159. the next dimension. */
  160. count[n] = 0;
  161. /* We could precalculate these products, but this is a less
  162. frequently used path so probably not worth it. */
  163. base -= sstride[n] * extent[n];
  164. dest -= dstride[n] * extent[n];
  165. n++;
  166. if (n >= rank)
  167. {
  168. /* Break out of the loop. */
  169. continue_loop = 0;
  170. break;
  171. }
  172. else
  173. {
  174. count[n]++;
  175. base += sstride[n];
  176. dest += dstride[n];
  177. }
  178. }
  179. }
  180. }
  181. #endif