2018-03-08 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libgfortran / generated / maxloc2_16_s1.c
blobbfba7074bccb08d2c5851c4e0d6d746187d258c0
1 /* Implementation of the MAXLOC intrinsic
2 Copyright (C) 2017-2018 Free Software Foundation, Inc.
3 Contributed by Thomas Koenig
5 This file is part of the GNU Fortran runtime library (libgfortran).
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.
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.
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.
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/>. */
26 #include "libgfortran.h"
27 #include <stdlib.h>
28 #include <string.h>
29 #include <assert.h>
31 #if defined (HAVE_GFC_INTEGER_1) && defined (HAVE_GFC_INTEGER_16)
33 static inline int
34 compare_fcn (const GFC_INTEGER_1 *a, const GFC_INTEGER_1 *b, gfc_charlen_type n)
36 if (sizeof (GFC_INTEGER_1) == 1)
37 return memcmp (a, b, n);
38 else
39 return memcmp_char4 (a, b, n);
42 extern GFC_INTEGER_16 maxloc2_16_s1 (gfc_array_s1 * const restrict, GFC_LOGICAL_4 back,
43 gfc_charlen_type);
44 export_proto(maxloc2_16_s1);
46 GFC_INTEGER_16
47 maxloc2_16_s1 (gfc_array_s1 * const restrict array, GFC_LOGICAL_4 back, gfc_charlen_type len)
49 index_type ret;
50 index_type sstride;
51 index_type extent;
52 const GFC_INTEGER_1 *src;
53 const GFC_INTEGER_1 *maxval;
54 index_type i;
56 assert(back == 0);
57 extent = GFC_DESCRIPTOR_EXTENT(array,0);
58 if (extent <= 0)
59 return 0;
61 sstride = GFC_DESCRIPTOR_STRIDE(array,0) * len;
63 ret = 1;
64 src = array->base_addr;
65 maxval = src;
66 for (i=2; i<=extent; i++)
68 src += sstride;
69 if (compare_fcn (src, maxval, len) > 0)
71 ret = i;
72 maxval = src;
75 return ret;
78 extern GFC_INTEGER_16 mmaxloc2_16_s1 (gfc_array_s1 * const restrict,
79 gfc_array_l1 *const restrict mask, GFC_LOGICAL_4 back,
80 gfc_charlen_type);
81 export_proto(mmaxloc2_16_s1);
83 GFC_INTEGER_16
84 mmaxloc2_16_s1 (gfc_array_s1 * const restrict array,
85 gfc_array_l1 * const restrict mask, GFC_LOGICAL_4 back,
86 gfc_charlen_type len)
88 index_type ret;
89 index_type sstride;
90 index_type extent;
91 const GFC_INTEGER_1 *src;
92 const GFC_INTEGER_1 *maxval;
93 index_type i, j;
94 GFC_LOGICAL_1 *mbase;
95 int mask_kind;
96 index_type mstride;
98 assert(back == 0);
99 extent = GFC_DESCRIPTOR_EXTENT(array,0);
100 if (extent <= 0)
101 return 0;
103 sstride = GFC_DESCRIPTOR_STRIDE(array,0) * len;
105 mask_kind = GFC_DESCRIPTOR_SIZE (mask);
106 mbase = mask->base_addr;
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
113 mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind);
114 else
115 internal_error (NULL, "Funny sized logical array");
117 mstride = GFC_DESCRIPTOR_STRIDE_BYTES(mask,0);
119 /* Search for the first occurrence of a true element in mask. */
120 for (j=0; j<extent; j++)
122 if (*mbase)
123 break;
124 mbase += mstride;
127 if (j == extent)
128 return 0;
130 ret = j + 1;
131 src = array->base_addr + j * sstride;
132 maxval = src;
134 for (i=j+1; i<=extent; i++)
136 if (*mbase && compare_fcn (src, maxval, len) > 0)
138 ret = i;
139 maxval = src;
141 src += sstride;
142 mbase += mstride;
144 return ret;
147 extern GFC_INTEGER_16 smaxloc2_16_s1 (gfc_array_s1 * const restrict,
148 GFC_LOGICAL_4 *mask, GFC_LOGICAL_4 back, gfc_charlen_type);
149 export_proto(smaxloc2_16_s1);
151 GFC_INTEGER_16
152 smaxloc2_16_s1 (gfc_array_s1 * const restrict array,
153 GFC_LOGICAL_4 *mask, GFC_LOGICAL_4 back, gfc_charlen_type len)
155 if (mask)
156 return maxloc2_16_s1 (array, len, back);
157 else
158 return 0;
161 #endif