maxloc2_8_s4.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* Implementation of the MAXLOC intrinsic
  2. Copyright (C) 2017-2022 Free Software Foundation, Inc.
  3. Contributed by Thomas Koenig
  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 <string.h>
  23. #include <assert.h>
  24. #if defined (HAVE_GFC_UINTEGER_4) && defined (HAVE_GFC_INTEGER_8)
  25. static inline int
  26. compare_fcn (const GFC_UINTEGER_4 *a, const GFC_UINTEGER_4 *b, gfc_charlen_type n)
  27. {
  28. if (sizeof (GFC_UINTEGER_4) == 1)
  29. return memcmp (a, b, n);
  30. else
  31. return memcmp_char4 (a, b, n);
  32. }
  33. extern GFC_INTEGER_8 maxloc2_8_s4 (gfc_array_s4 * const restrict, GFC_LOGICAL_4 back,
  34. gfc_charlen_type);
  35. export_proto(maxloc2_8_s4);
  36. GFC_INTEGER_8
  37. maxloc2_8_s4 (gfc_array_s4 * const restrict array, GFC_LOGICAL_4 back, gfc_charlen_type len)
  38. {
  39. index_type ret;
  40. index_type sstride;
  41. index_type extent;
  42. const GFC_UINTEGER_4 *src;
  43. const GFC_UINTEGER_4 *maxval;
  44. index_type i;
  45. extent = GFC_DESCRIPTOR_EXTENT(array,0);
  46. if (extent <= 0)
  47. return 0;
  48. sstride = GFC_DESCRIPTOR_STRIDE(array,0) * len;
  49. ret = 1;
  50. src = array->base_addr;
  51. maxval = NULL;
  52. for (i=1; i<=extent; i++)
  53. {
  54. if (maxval == NULL || (back ? compare_fcn (src, maxval, len) >= 0 :
  55. compare_fcn (src, maxval, len) > 0))
  56. {
  57. ret = i;
  58. maxval = src;
  59. }
  60. src += sstride;
  61. }
  62. return ret;
  63. }
  64. extern GFC_INTEGER_8 mmaxloc2_8_s4 (gfc_array_s4 * const restrict,
  65. gfc_array_l1 *const restrict mask, GFC_LOGICAL_4 back,
  66. gfc_charlen_type);
  67. export_proto(mmaxloc2_8_s4);
  68. GFC_INTEGER_8
  69. mmaxloc2_8_s4 (gfc_array_s4 * const restrict array,
  70. gfc_array_l1 * const restrict mask, GFC_LOGICAL_4 back,
  71. gfc_charlen_type len)
  72. {
  73. index_type ret;
  74. index_type sstride;
  75. index_type extent;
  76. const GFC_UINTEGER_4 *src;
  77. const GFC_UINTEGER_4 *maxval;
  78. index_type i, j;
  79. GFC_LOGICAL_1 *mbase;
  80. int mask_kind;
  81. index_type mstride;
  82. extent = GFC_DESCRIPTOR_EXTENT(array,0);
  83. if (extent <= 0)
  84. return 0;
  85. sstride = GFC_DESCRIPTOR_STRIDE(array,0) * len;
  86. mask_kind = GFC_DESCRIPTOR_SIZE (mask);
  87. mbase = mask->base_addr;
  88. if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
  89. #ifdef HAVE_GFC_LOGICAL_16
  90. || mask_kind == 16
  91. #endif
  92. )
  93. mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind);
  94. else
  95. internal_error (NULL, "Funny sized logical array");
  96. mstride = GFC_DESCRIPTOR_STRIDE_BYTES(mask,0);
  97. /* Search for the first occurrence of a true element in mask. */
  98. for (j=0; j<extent; j++)
  99. {
  100. if (*mbase)
  101. break;
  102. mbase += mstride;
  103. }
  104. if (j == extent)
  105. return 0;
  106. ret = j + 1;
  107. src = array->base_addr + j * sstride;
  108. maxval = src;
  109. for (i=j+1; i<=extent; i++)
  110. {
  111. if (*mbase && (back ? compare_fcn (src, maxval, len) >= 0 :
  112. compare_fcn (src, maxval, len) > 0))
  113. {
  114. ret = i;
  115. maxval = src;
  116. }
  117. src += sstride;
  118. mbase += mstride;
  119. }
  120. return ret;
  121. }
  122. extern GFC_INTEGER_8 smaxloc2_8_s4 (gfc_array_s4 * const restrict,
  123. GFC_LOGICAL_4 *mask, GFC_LOGICAL_4 back, gfc_charlen_type);
  124. export_proto(smaxloc2_8_s4);
  125. GFC_INTEGER_8
  126. smaxloc2_8_s4 (gfc_array_s4 * const restrict array,
  127. GFC_LOGICAL_4 *mask, GFC_LOGICAL_4 back, gfc_charlen_type len)
  128. {
  129. if (mask)
  130. return maxloc2_8_s4 (array, len, back);
  131. else
  132. return 0;
  133. }
  134. #endif