]>
Commit | Line | Data |
---|---|---|
ddc9995b TK |
1 | /* Implementation of the MAXLOC intrinsic |
2 | Copyright 2017 Free Software Foundation, Inc. | |
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 | ||
30 | #if defined (HAVE_GFC_INTEGER_4) && defined (HAVE_GFC_INTEGER_8) | |
31 | ||
32 | static inline int | |
33 | compare_fcn (const GFC_INTEGER_4 *a, const GFC_INTEGER_4 *b, int n) | |
34 | { | |
35 | if (sizeof (GFC_INTEGER_4) == 1) | |
36 | return memcmp (a, b, n); | |
37 | else | |
38 | return memcmp_char4 (a, b, n); | |
39 | } | |
40 | ||
41 | extern GFC_INTEGER_8 maxloc2_8_s4 (gfc_array_s4 * const restrict, int); | |
42 | export_proto(maxloc2_8_s4); | |
43 | ||
44 | GFC_INTEGER_8 | |
45 | maxloc2_8_s4 (gfc_array_s4 * const restrict array, gfc_charlen_type len) | |
46 | { | |
47 | index_type ret; | |
48 | index_type sstride; | |
49 | index_type extent; | |
50 | const GFC_INTEGER_4 *src; | |
51 | const GFC_INTEGER_4 *maxval; | |
52 | index_type i; | |
53 | ||
54 | extent = GFC_DESCRIPTOR_EXTENT(array,0); | |
55 | if (extent <= 0) | |
56 | return 0; | |
57 | ||
58 | sstride = GFC_DESCRIPTOR_STRIDE(array,0) * len; | |
59 | ||
60 | ret = 1; | |
61 | src = array->base_addr; | |
62 | maxval = src; | |
63 | for (i=2; i<=extent; i++) | |
64 | { | |
65 | src += sstride; | |
66 | if (compare_fcn (src, maxval, len) > 0) | |
67 | { | |
68 | ret = i; | |
69 | maxval = src; | |
70 | } | |
71 | } | |
72 | return ret; | |
73 | } | |
74 | ||
75 | extern GFC_INTEGER_8 mmaxloc2_8_s4 (gfc_array_s4 * const restrict, | |
76 | gfc_array_l1 *const restrict mask, gfc_charlen_type); | |
77 | export_proto(mmaxloc2_8_s4); | |
78 | ||
79 | GFC_INTEGER_8 | |
80 | mmaxloc2_8_s4 (gfc_array_s4 * const restrict array, | |
81 | gfc_array_l1 * const restrict mask, | |
82 | gfc_charlen_type len) | |
83 | { | |
84 | index_type ret; | |
85 | index_type sstride; | |
86 | index_type extent; | |
87 | const GFC_INTEGER_4 *src; | |
88 | const GFC_INTEGER_4 *maxval; | |
89 | index_type i, j; | |
90 | GFC_LOGICAL_1 *mbase; | |
91 | int mask_kind; | |
92 | index_type mstride; | |
93 | ||
94 | extent = GFC_DESCRIPTOR_EXTENT(array,0); | |
95 | if (extent <= 0) | |
96 | return 0; | |
97 | ||
98 | sstride = GFC_DESCRIPTOR_STRIDE(array,0) * len; | |
99 | ||
100 | mask_kind = GFC_DESCRIPTOR_SIZE (mask); | |
101 | mbase = mask->base_addr; | |
102 | ||
103 | if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8 | |
104 | #ifdef HAVE_GFC_LOGICAL_16 | |
105 | || mask_kind == 16 | |
106 | #endif | |
107 | ) | |
108 | mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind); | |
109 | else | |
110 | internal_error (NULL, "Funny sized logical array"); | |
111 | ||
112 | mstride = GFC_DESCRIPTOR_STRIDE_BYTES(mask,0); | |
113 | ||
114 | /* Search for the first occurrence of a true element in mask. */ | |
115 | for (j=0; j<extent; j++) | |
116 | { | |
117 | if (*mbase) | |
118 | break; | |
119 | mbase += mstride; | |
120 | } | |
121 | ||
122 | if (j == extent) | |
123 | return 0; | |
124 | ||
125 | ret = j + 1; | |
126 | src = array->base_addr + j * sstride; | |
127 | maxval = src; | |
128 | ||
129 | for (i=j+1; i<=extent; i++) | |
130 | { | |
131 | if (*mbase && compare_fcn (src, maxval, len) > 0) | |
132 | { | |
133 | ret = i; | |
134 | maxval = src; | |
135 | } | |
136 | src += sstride; | |
137 | mbase += mstride; | |
138 | } | |
139 | return ret; | |
140 | } | |
141 | ||
142 | extern GFC_INTEGER_8 smaxloc2_8_s4 (gfc_array_s4 * const restrict, | |
143 | GFC_LOGICAL_4 *mask, int); | |
144 | export_proto(smaxloc2_8_s4); | |
145 | ||
146 | GFC_INTEGER_8 | |
147 | smaxloc2_8_s4 (gfc_array_s4 * const restrict array, | |
148 | GFC_LOGICAL_4 *mask, gfc_charlen_type len) | |
149 | { | |
150 | if (mask) | |
151 | return maxloc2_8_s4 (array, len); | |
152 | else | |
153 | return 0; | |
154 | } | |
155 | ||
156 | #endif |