norm2_r17.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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_17) && defined (HAVE_GFC_REAL_17) && 1 /* FIXME: figure this out later. */ && 1 /* FIXME: figure this out later. */
  22. #if defined(POWER_IEEE128)
  23. #define MATHFUNC(funcname) __ ## funcname ## ieee128
  24. #else
  25. #define MATHFUNC(funcname) funcname ## q
  26. #endif
  27. extern void norm2_r17 (gfc_array_r17 * const restrict,
  28. gfc_array_r17 * const restrict, const index_type * const restrict);
  29. export_proto(norm2_r17);
  30. void
  31. norm2_r17 (gfc_array_r17 * const restrict retarray,
  32. gfc_array_r17 * const restrict array,
  33. const index_type * const restrict pdim)
  34. {
  35. index_type count[GFC_MAX_DIMENSIONS];
  36. index_type extent[GFC_MAX_DIMENSIONS];
  37. index_type sstride[GFC_MAX_DIMENSIONS];
  38. index_type dstride[GFC_MAX_DIMENSIONS];
  39. const GFC_REAL_17 * restrict base;
  40. GFC_REAL_17 * restrict dest;
  41. index_type rank;
  42. index_type n;
  43. index_type len;
  44. index_type delta;
  45. index_type dim;
  46. int continue_loop;
  47. /* Make dim zero based to avoid confusion. */
  48. rank = GFC_DESCRIPTOR_RANK (array) - 1;
  49. dim = (*pdim) - 1;
  50. if (unlikely (dim < 0 || dim > rank))
  51. {
  52. runtime_error ("Dim argument incorrect in NORM intrinsic: "
  53. "is %ld, should be between 1 and %ld",
  54. (long int) dim + 1, (long int) rank + 1);
  55. }
  56. len = GFC_DESCRIPTOR_EXTENT(array,dim);
  57. if (len < 0)
  58. len = 0;
  59. delta = GFC_DESCRIPTOR_STRIDE(array,dim);
  60. for (n = 0; n < dim; n++)
  61. {
  62. sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
  63. extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
  64. if (extent[n] < 0)
  65. extent[n] = 0;
  66. }
  67. for (n = dim; n < rank; n++)
  68. {
  69. sstride[n] = GFC_DESCRIPTOR_STRIDE(array, n + 1);
  70. extent[n] = GFC_DESCRIPTOR_EXTENT(array, n + 1);
  71. if (extent[n] < 0)
  72. extent[n] = 0;
  73. }
  74. if (retarray->base_addr == NULL)
  75. {
  76. size_t alloc_size, str;
  77. for (n = 0; n < rank; n++)
  78. {
  79. if (n == 0)
  80. str = 1;
  81. else
  82. str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
  83. GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
  84. }
  85. retarray->offset = 0;
  86. retarray->dtype.rank = rank;
  87. alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
  88. retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_REAL_17));
  89. if (alloc_size == 0)
  90. {
  91. /* Make sure we have a zero-sized array. */
  92. GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
  93. return;
  94. }
  95. }
  96. else
  97. {
  98. if (rank != GFC_DESCRIPTOR_RANK (retarray))
  99. runtime_error ("rank of return array incorrect in"
  100. " NORM intrinsic: is %ld, should be %ld",
  101. (long int) (GFC_DESCRIPTOR_RANK (retarray)),
  102. (long int) rank);
  103. if (unlikely (compile_options.bounds_check))
  104. bounds_ifunction_return ((array_t *) retarray, extent,
  105. "return value", "NORM");
  106. }
  107. for (n = 0; n < rank; n++)
  108. {
  109. count[n] = 0;
  110. dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
  111. if (extent[n] <= 0)
  112. return;
  113. }
  114. base = array->base_addr;
  115. dest = retarray->base_addr;
  116. continue_loop = 1;
  117. while (continue_loop)
  118. {
  119. const GFC_REAL_17 * restrict src;
  120. GFC_REAL_17 result;
  121. src = base;
  122. {
  123. GFC_REAL_17 scale;
  124. result = 0;
  125. scale = 1;
  126. if (len <= 0)
  127. *dest = 0;
  128. else
  129. {
  130. #if ! defined HAVE_BACK_ARG
  131. for (n = 0; n < len; n++, src += delta)
  132. {
  133. #endif
  134. if (*src != 0)
  135. {
  136. GFC_REAL_17 absX, val;
  137. absX = MATHFUNC(fabs) (*src);
  138. if (scale < absX)
  139. {
  140. val = scale / absX;
  141. result = 1 + result * val * val;
  142. scale = absX;
  143. }
  144. else
  145. {
  146. val = absX / scale;
  147. result += val * val;
  148. }
  149. }
  150. }
  151. result = scale * MATHFUNC(sqrt) (result);
  152. *dest = result;
  153. }
  154. }
  155. /* Advance to the next element. */
  156. count[0]++;
  157. base += sstride[0];
  158. dest += dstride[0];
  159. n = 0;
  160. while (count[n] == extent[n])
  161. {
  162. /* When we get to the end of a dimension, reset it and increment
  163. the next dimension. */
  164. count[n] = 0;
  165. /* We could precalculate these products, but this is a less
  166. frequently used path so probably not worth it. */
  167. base -= sstride[n] * extent[n];
  168. dest -= dstride[n] * extent[n];
  169. n++;
  170. if (n >= rank)
  171. {
  172. /* Break out of the loop. */
  173. continue_loop = 0;
  174. break;
  175. }
  176. else
  177. {
  178. count[n]++;
  179. base += sstride[n];
  180. dest += dstride[n];
  181. }
  182. }
  183. }
  184. }
  185. #endif