-Warray-bounds: Fix false positive in some "switch" stmts (PR tree-optimization/83510)
[official-gcc.git] / libgfortran / generated / minloc2_16_s4.c
blob256784fd8cef68aa7d009de257db55e754aeabe5
1 /* Implementation of the MINLOC 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_4) && defined (HAVE_GFC_INTEGER_16)
33 static inline int
34 compare_fcn (const GFC_INTEGER_4 *a, const GFC_INTEGER_4 *b, gfc_charlen_type n)
36 if (sizeof (GFC_INTEGER_4) == 1)
37 return memcmp (a, b, n);
38 else
39 return memcmp_char4 (a, b, n);
42 extern GFC_INTEGER_16 minloc2_16_s4 (gfc_array_s4 * const restrict, GFC_LOGICAL_4 back,
43 gfc_charlen_type);
44 export_proto(minloc2_16_s4);
46 GFC_INTEGER_16
47 minloc2_16_s4 (gfc_array_s4 * const restrict array, GFC_LOGICAL_4 back,
48 gfc_charlen_type len)
50 index_type ret;
51 index_type sstride;
52 index_type extent;
53 const GFC_INTEGER_4 *src;
54 const GFC_INTEGER_4 *maxval;
55 index_type i;
57 assert(back == 0);
58 extent = GFC_DESCRIPTOR_EXTENT(array,0);
59 if (extent <= 0)
60 return 0;
62 sstride = GFC_DESCRIPTOR_STRIDE(array,0) * len;
64 ret = 1;
65 src = array->base_addr;
66 maxval = src;
67 for (i=2; i<=extent; i++)
69 src += sstride;
70 if (compare_fcn (src, maxval, len) < 0)
72 ret = i;
73 maxval = src;
76 return ret;
79 extern GFC_INTEGER_16 mminloc2_16_s4 (gfc_array_s4 * const restrict,
80 gfc_array_l1 *const restrict mask, GFC_LOGICAL_4 back,
81 gfc_charlen_type);
82 export_proto(mminloc2_16_s4);
84 GFC_INTEGER_16
85 mminloc2_16_s4 (gfc_array_s4 * const restrict array,
86 gfc_array_l1 * const restrict mask, GFC_LOGICAL_4 back,
87 gfc_charlen_type len)
89 index_type ret;
90 index_type sstride;
91 index_type extent;
92 const GFC_INTEGER_4 *src;
93 const GFC_INTEGER_4 *maxval;
94 index_type i, j;
95 GFC_LOGICAL_1 *mbase;
96 int mask_kind;
97 index_type mstride;
99 assert (back == 0);
100 extent = GFC_DESCRIPTOR_EXTENT(array,0);
101 if (extent <= 0)
102 return 0;
104 sstride = GFC_DESCRIPTOR_STRIDE(array,0) * len;
106 mask_kind = GFC_DESCRIPTOR_SIZE (mask);
107 mbase = mask->base_addr;
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
114 mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind);
115 else
116 internal_error (NULL, "Funny sized logical array");
118 mstride = GFC_DESCRIPTOR_STRIDE_BYTES(mask,0);
120 /* Search for the first occurrence of a true element in mask. */
121 for (j=0; j<extent; j++)
123 if (*mbase)
124 break;
125 mbase += mstride;
128 if (j == extent)
129 return 0;
131 ret = j + 1;
132 src = array->base_addr + j * sstride;
133 maxval = src;
135 for (i=j+1; i<=extent; i++)
137 if (*mbase && compare_fcn (src, maxval, len) < 0)
139 ret = i;
140 maxval = src;
142 src += sstride;
143 mbase += mstride;
145 return ret;
148 extern GFC_INTEGER_16 sminloc2_16_s4 (gfc_array_s4 * const restrict,
149 GFC_LOGICAL_4 *mask, GFC_LOGICAL_4 back, gfc_charlen_type);
150 export_proto(sminloc2_16_s4);
152 GFC_INTEGER_16
153 sminloc2_16_s4 (gfc_array_s4 * const restrict array,
154 GFC_LOGICAL_4 *mask, GFC_LOGICAL_4 back, gfc_charlen_type len)
156 if (mask)
157 return minloc2_16_s4 (array, len, back);
158 else
159 return 0;
162 #endif