131
|
1 `/* Implementation of the MINLOC intrinsic
|
145
|
2 Copyright (C) 2017-2020 Free Software Foundation, Inc.
|
131
|
3 Contributed by Thomas Koenig
|
|
4
|
|
5 This file is part of the GNU Fortran runtime library (libgfortran).
|
|
6
|
|
7 Libgfortran is free software; you can redistribute it and/or
|
|
8 modify it under the terms of the GNU General Public
|
|
9 License as published by the Free Software Foundation; either
|
|
10 version 3 of the License, or (at your option) any later version.
|
|
11
|
|
12 Libgfortran is distributed in the hope that it will be useful,
|
|
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15 GNU General Public License for more details.
|
|
16
|
|
17 Under Section 7 of GPL version 3, you are granted additional
|
|
18 permissions described in the GCC Runtime Library Exception, version
|
|
19 3.1, as published by the Free Software Foundation.
|
|
20
|
|
21 You should have received a copy of the GNU General Public License and
|
|
22 a copy of the GCC Runtime Library Exception along with this program;
|
|
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
|
|
24 <http://www.gnu.org/licenses/>. */
|
|
25
|
|
26 #include "libgfortran.h"
|
|
27 #include <stdlib.h>
|
|
28 #include <string.h>
|
|
29 #include <assert.h>'
|
|
30 include(iparm.m4)dnl
|
|
31
|
|
32 `#if defined (HAVE_'atype_name`) && defined (HAVE_'rtype_name`)
|
|
33
|
|
34 static inline int
|
|
35 compare_fcn (const 'atype_name` *a, const 'atype_name` *b, gfc_charlen_type n)
|
|
36 {
|
|
37 if (sizeof ('atype_name`) == 1)
|
|
38 return memcmp (a, b, n);
|
|
39 else
|
|
40 return memcmp_char4 (a, b, n);
|
|
41 }
|
|
42
|
|
43 extern 'rtype_name` 'name`'rtype_qual`_'atype_code` ('atype` * const restrict'back_arg`,
|
|
44 gfc_charlen_type);
|
|
45 export_proto('name`'rtype_qual`_'atype_code`);
|
|
46
|
|
47 'rtype_name`
|
|
48 'name`'rtype_qual`_'atype_code` ('atype` * const restrict array'back_arg`,
|
|
49 gfc_charlen_type len)
|
|
50 {
|
|
51 index_type ret;
|
|
52 index_type sstride;
|
|
53 index_type extent;
|
|
54 const 'atype_name` *src;
|
|
55 const 'atype_name` *minval;
|
|
56 index_type i;
|
|
57
|
|
58 extent = GFC_DESCRIPTOR_EXTENT(array,0);
|
|
59 if (extent <= 0)
|
|
60 return 0;
|
|
61
|
|
62 sstride = GFC_DESCRIPTOR_STRIDE(array,0) * len;
|
|
63
|
|
64 ret = 1;
|
|
65 src = array->base_addr;
|
|
66 minval = NULL;
|
|
67 for (i=1; i<=extent; i++)
|
|
68 {
|
|
69 if (minval == NULL || (back ? compare_fcn (src, minval, len) <= 0 :
|
|
70 compare_fcn (src, minval, len) < 0))
|
|
71 {
|
|
72 ret = i;
|
|
73 minval = src;
|
|
74 }
|
|
75 src += sstride;
|
|
76 }
|
|
77 return ret;
|
|
78 }
|
|
79
|
|
80 extern 'rtype_name` m'name`'rtype_qual`_'atype_code` ('atype` * const restrict,
|
|
81 gfc_array_l1 *const restrict mask'back_arg`,
|
|
82 gfc_charlen_type);
|
|
83 export_proto(m'name`'rtype_qual`_'atype_code`);
|
|
84
|
|
85 'rtype_name`
|
|
86 m'name`'rtype_qual`_'atype_code` ('atype` * const restrict array,
|
|
87 gfc_array_l1 * const restrict mask'back_arg`,
|
|
88 gfc_charlen_type len)
|
|
89 {
|
|
90 index_type ret;
|
|
91 index_type sstride;
|
|
92 index_type extent;
|
|
93 const 'atype_name` *src;
|
|
94 const 'atype_name` *maxval;
|
|
95 index_type i, j;
|
|
96 GFC_LOGICAL_1 *mbase;
|
|
97 int mask_kind;
|
|
98 index_type mstride;
|
|
99
|
|
100 extent = GFC_DESCRIPTOR_EXTENT(array,0);
|
|
101 if (extent <= 0)
|
|
102 return 0;
|
|
103
|
|
104 sstride = GFC_DESCRIPTOR_STRIDE(array,0) * len;
|
|
105
|
|
106 mask_kind = GFC_DESCRIPTOR_SIZE (mask);
|
|
107 mbase = mask->base_addr;
|
|
108
|
|
109 if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
|
|
110 #ifdef HAVE_GFC_LOGICAL_16
|
|
111 || mask_kind == 16
|
|
112 #endif
|
|
113 )
|
|
114 mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind);
|
|
115 else
|
|
116 internal_error (NULL, "Funny sized logical array");
|
|
117
|
|
118 mstride = GFC_DESCRIPTOR_STRIDE_BYTES(mask,0);
|
|
119
|
|
120 /* Search for the first occurrence of a true element in mask. */
|
|
121 for (j=0; j<extent; j++)
|
|
122 {
|
|
123 if (*mbase)
|
|
124 break;
|
|
125 mbase += mstride;
|
|
126 }
|
|
127
|
|
128 if (j == extent)
|
|
129 return 0;
|
|
130
|
|
131 ret = j + 1;
|
|
132 src = array->base_addr + j * sstride;
|
|
133 maxval = src;
|
|
134
|
|
135 for (i=j+1; i<=extent; i++)
|
|
136 {
|
|
137
|
|
138 if (*mbase && (back ? compare_fcn (src, maxval, len) <= 0 :
|
|
139 compare_fcn (src, maxval, len) < 0))
|
|
140 {
|
|
141 ret = i;
|
|
142 maxval = src;
|
|
143 }
|
|
144 src += sstride;
|
|
145 mbase += mstride;
|
|
146 }
|
|
147 return ret;
|
|
148 }
|
|
149
|
|
150 extern 'rtype_name` s'name`'rtype_qual`_'atype_code` ('atype` * const restrict,
|
|
151 GFC_LOGICAL_4 *mask'back_arg`, gfc_charlen_type);
|
|
152 export_proto(s'name`'rtype_qual`_'atype_code`);
|
|
153
|
|
154 'rtype_name`
|
|
155 s'name`'rtype_qual`_'atype_code` ('atype` * const restrict array,
|
|
156 GFC_LOGICAL_4 *mask'back_arg`, gfc_charlen_type len)
|
|
157 {
|
|
158 if (mask)
|
|
159 return 'name`'rtype_qual`_'atype_code` (array, len, back);
|
|
160 else
|
|
161 return 0;
|
|
162 }
|
|
163
|
|
164 #endif'
|