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