norm2_r8.c 5.1 KB

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