]> git.ipfire.org Git - thirdparty/gcc.git/blame - libgfortran/generated/maxloc2_8_s1.c
re PR fortran/36313 ([F03] {MIN,MAX}{LOC,VAL} should accept character arguments)
[thirdparty/gcc.git] / libgfortran / generated / maxloc2_8_s1.c
CommitLineData
ddc9995b
TK
1/* Implementation of the MAXLOC intrinsic
2 Copyright 2017 Free Software Foundation, Inc.
3 Contributed by Thomas Koenig
4
5This file is part of the GNU Fortran runtime library (libgfortran).
6
7Libgfortran is free software; you can redistribute it and/or
8modify it under the terms of the GNU General Public
9License as published by the Free Software Foundation; either
10version 3 of the License, or (at your option) any later version.
11
12Libgfortran is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17Under Section 7 of GPL version 3, you are granted additional
18permissions described in the GCC Runtime Library Exception, version
193.1, as published by the Free Software Foundation.
20
21You should have received a copy of the GNU General Public License and
22a copy of the GCC Runtime Library Exception along with this program;
23see 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_1) && defined (HAVE_GFC_INTEGER_8)
31
32static inline int
207180e1 33compare_fcn (const GFC_INTEGER_1 *a, const GFC_INTEGER_1 *b, gfc_charlen_type n)
ddc9995b
TK
34{
35 if (sizeof (GFC_INTEGER_1) == 1)
36 return memcmp (a, b, n);
37 else
38 return memcmp_char4 (a, b, n);
39}
40
207180e1
TK
41extern GFC_INTEGER_8 maxloc2_8_s1 (gfc_array_s1 * const restrict,
42 gfc_charlen_type);
ddc9995b
TK
43export_proto(maxloc2_8_s1);
44
45GFC_INTEGER_8
46maxloc2_8_s1 (gfc_array_s1 * const restrict array, gfc_charlen_type len)
47{
48 index_type ret;
49 index_type sstride;
50 index_type extent;
51 const GFC_INTEGER_1 *src;
52 const GFC_INTEGER_1 *maxval;
53 index_type i;
54
55 extent = GFC_DESCRIPTOR_EXTENT(array,0);
56 if (extent <= 0)
57 return 0;
58
59 sstride = GFC_DESCRIPTOR_STRIDE(array,0) * len;
60
61 ret = 1;
62 src = array->base_addr;
63 maxval = src;
64 for (i=2; i<=extent; i++)
65 {
66 src += sstride;
67 if (compare_fcn (src, maxval, len) > 0)
68 {
69 ret = i;
70 maxval = src;
71 }
72 }
73 return ret;
74}
75
76extern GFC_INTEGER_8 mmaxloc2_8_s1 (gfc_array_s1 * const restrict,
77 gfc_array_l1 *const restrict mask, gfc_charlen_type);
78export_proto(mmaxloc2_8_s1);
79
80GFC_INTEGER_8
81mmaxloc2_8_s1 (gfc_array_s1 * const restrict array,
82 gfc_array_l1 * const restrict mask,
83 gfc_charlen_type len)
84{
85 index_type ret;
86 index_type sstride;
87 index_type extent;
88 const GFC_INTEGER_1 *src;
89 const GFC_INTEGER_1 *maxval;
90 index_type i, j;
91 GFC_LOGICAL_1 *mbase;
92 int mask_kind;
93 index_type mstride;
94
95 extent = GFC_DESCRIPTOR_EXTENT(array,0);
96 if (extent <= 0)
97 return 0;
98
99 sstride = GFC_DESCRIPTOR_STRIDE(array,0) * len;
100
101 mask_kind = GFC_DESCRIPTOR_SIZE (mask);
102 mbase = mask->base_addr;
103
104 if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
105#ifdef HAVE_GFC_LOGICAL_16
106 || mask_kind == 16
107#endif
108 )
109 mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind);
110 else
111 internal_error (NULL, "Funny sized logical array");
112
113 mstride = GFC_DESCRIPTOR_STRIDE_BYTES(mask,0);
114
115 /* Search for the first occurrence of a true element in mask. */
116 for (j=0; j<extent; j++)
117 {
118 if (*mbase)
119 break;
120 mbase += mstride;
121 }
122
123 if (j == extent)
124 return 0;
125
126 ret = j + 1;
127 src = array->base_addr + j * sstride;
128 maxval = src;
129
130 for (i=j+1; i<=extent; i++)
131 {
132 if (*mbase && compare_fcn (src, maxval, len) > 0)
133 {
134 ret = i;
135 maxval = src;
136 }
137 src += sstride;
138 mbase += mstride;
139 }
140 return ret;
141}
142
143extern GFC_INTEGER_8 smaxloc2_8_s1 (gfc_array_s1 * const restrict,
144 GFC_LOGICAL_4 *mask, int);
145export_proto(smaxloc2_8_s1);
146
147GFC_INTEGER_8
148smaxloc2_8_s1 (gfc_array_s1 * const restrict array,
149 GFC_LOGICAL_4 *mask, gfc_charlen_type len)
150{
151 if (mask)
152 return maxloc2_8_s1 (array, len);
153 else
154 return 0;
155}
156
157#endif