findloc2_s4.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* Implementation of the FINDLOC intrinsic
  2. Copyright (C) 2018-2022 Free Software Foundation, Inc.
  3. Contributed by Thomas König <tk@tkoenig.net>
  4. This file is part of the GNU Fortran 95 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. #ifdef HAVE_GFC_UINTEGER_4
  22. index_type findloc2_s4 (gfc_array_s4 * const restrict array,
  23. const GFC_UINTEGER_4 * restrict value, GFC_LOGICAL_4 back,
  24. gfc_charlen_type len_array, gfc_charlen_type len_value);
  25. export_proto(findloc2_s4);
  26. index_type
  27. findloc2_s4 (gfc_array_s4 * const restrict array, const GFC_UINTEGER_4 * restrict value,
  28. GFC_LOGICAL_4 back,
  29. gfc_charlen_type len_array, gfc_charlen_type len_value)
  30. {
  31. index_type i;
  32. index_type sstride;
  33. index_type extent;
  34. const GFC_UINTEGER_4 * restrict src;
  35. extent = GFC_DESCRIPTOR_EXTENT(array,0);
  36. if (extent <= 0)
  37. return 0;
  38. sstride = GFC_DESCRIPTOR_STRIDE(array,0) * len_array;
  39. if (back)
  40. {
  41. src = array->base_addr + (extent - 1) * sstride;
  42. for (i = extent; i >= 0; i--)
  43. {
  44. if (compare_string_char4 (len_array, src, len_value, value) == 0)
  45. return i;
  46. src -= sstride;
  47. }
  48. }
  49. else
  50. {
  51. src = array->base_addr;
  52. for (i = 1; i <= extent; i++)
  53. {
  54. if (compare_string_char4 (len_array, src, len_value, value) == 0)
  55. return i;
  56. src += sstride;
  57. }
  58. }
  59. return 0;
  60. }
  61. index_type mfindloc2_s4 (gfc_array_s4 * const restrict array,
  62. const GFC_UINTEGER_4 * restrict value,
  63. gfc_array_l1 *const restrict mask, GFC_LOGICAL_4 back,
  64. gfc_charlen_type len_array, gfc_charlen_type len_value);
  65. export_proto(mfindloc2_s4);
  66. index_type
  67. mfindloc2_s4 (gfc_array_s4 * const restrict array,
  68. const GFC_UINTEGER_4 * restrict value, gfc_array_l1 *const restrict mask,
  69. GFC_LOGICAL_4 back, gfc_charlen_type len_array,
  70. gfc_charlen_type len_value)
  71. {
  72. index_type i;
  73. index_type sstride;
  74. index_type extent;
  75. const GFC_UINTEGER_4 * restrict src;
  76. const GFC_LOGICAL_1 * restrict mbase;
  77. int mask_kind;
  78. index_type mstride;
  79. extent = GFC_DESCRIPTOR_EXTENT(array,0);
  80. if (extent <= 0)
  81. return 0;
  82. mask_kind = GFC_DESCRIPTOR_SIZE (mask);
  83. mbase = mask->base_addr;
  84. if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
  85. #ifdef HAVE_GFC_LOGICAL_16
  86. || mask_kind == 16
  87. #endif
  88. )
  89. mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind);
  90. else
  91. internal_error (NULL, "Funny sized logical array");
  92. sstride = GFC_DESCRIPTOR_STRIDE(array,0) * len_array;
  93. mstride = GFC_DESCRIPTOR_STRIDE_BYTES(mask,0);
  94. if (back)
  95. {
  96. src = array->base_addr + (extent - 1) * sstride;
  97. mbase += (extent - 1) * mstride;
  98. for (i = extent; i >= 0; i--)
  99. {
  100. if (*mbase && (compare_string_char4 (len_array, src, len_value, value) == 0))
  101. return i;
  102. src -= sstride;
  103. mbase -= mstride;
  104. }
  105. }
  106. else
  107. {
  108. src = array->base_addr;
  109. for (i = 1; i <= extent; i++)
  110. {
  111. if (*mbase && (compare_string_char4 (len_array, src, len_value, value) == 0))
  112. return i;
  113. src += sstride;
  114. mbase += mstride;
  115. }
  116. }
  117. return 0;
  118. }
  119. index_type sfindloc2_s4 (gfc_array_s4 * const restrict array,
  120. const GFC_UINTEGER_4 * restrict value,
  121. GFC_LOGICAL_4 *const restrict mask, GFC_LOGICAL_4 back,
  122. gfc_charlen_type len_array, gfc_charlen_type len_value);
  123. export_proto(sfindloc2_s4);
  124. index_type
  125. sfindloc2_s4 (gfc_array_s4 * const restrict array,
  126. const GFC_UINTEGER_4 * restrict value, GFC_LOGICAL_4 *const restrict mask,
  127. GFC_LOGICAL_4 back, gfc_charlen_type len_array,
  128. gfc_charlen_type len_value)
  129. {
  130. if (mask == NULL || *mask)
  131. {
  132. return findloc2_s4 (array, value, back, len_array, len_value);
  133. }
  134. return 0;
  135. }
  136. #endif