* gcc-interface/decl.c (gnat_to_gnu_field): Do not set the alignment
[official-gcc.git] / libgfortran / m4 / maxloc2s.m4
blobf2a061c8c0343534def494f060899055c1810350
1 `/* Implementation of the MAXLOC intrinsic
2    Copyright 2017 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(iparm.m4)dnl
31 `#if defined (HAVE_'atype_name`) && defined (HAVE_'rtype_name`)
33 static inline int
34 compare_fcn (const 'atype_name` *a, const 'atype_name` *b, gfc_charlen_type n)
36   if (sizeof ('atype_name`) == 1)
37     return memcmp (a, b, n);
38   else
39     return memcmp_char4 (a, b, n);
42 extern 'rtype_name` 'name`'rtype_qual`_'atype_code` ('atype` * const restrict,
43        gfc_charlen_type);
44 export_proto('name`'rtype_qual`_'atype_code`);
46 'rtype_name`
47 'name`'rtype_qual`_'atype_code` ('atype` * const restrict array, gfc_charlen_type len)
49   index_type ret;
50   index_type sstride;
51   index_type extent;
52   const 'atype_name` *src;
53   const 'atype_name` *maxval;
54   index_type i;
56   extent = GFC_DESCRIPTOR_EXTENT(array,0);
57   if (extent <= 0)
58     return 0;
60   sstride = GFC_DESCRIPTOR_STRIDE(array,0) * len;
62   ret = 1;
63   src = array->base_addr;
64   maxval = src;
65   for (i=2; i<=extent; i++)
66     {
67       src += sstride;
68       if (compare_fcn (src, maxval, len) > 0)
69       {
70          ret = i;
71          maxval = src;
72       }
73     }
74   return ret;
77 extern 'rtype_name` m'name`'rtype_qual`_'atype_code` ('atype` * const restrict,
78                         gfc_array_l1 *const restrict mask, gfc_charlen_type);
79 export_proto(m'name`'rtype_qual`_'atype_code`);
81 'rtype_name`
82 m'name`'rtype_qual`_'atype_code` ('atype` * const restrict array,
83                                  gfc_array_l1 * const restrict mask,
84                                  gfc_charlen_type len)
86   index_type ret;
87   index_type sstride;
88   index_type extent;
89   const 'atype_name` *src;
90   const 'atype_name` *maxval;
91   index_type i, j;
92   GFC_LOGICAL_1 *mbase;
93   int mask_kind;
94   index_type mstride;
96   extent = GFC_DESCRIPTOR_EXTENT(array,0);
97   if (extent <= 0)
98     return 0;
100   sstride = GFC_DESCRIPTOR_STRIDE(array,0) * len;
102   mask_kind = GFC_DESCRIPTOR_SIZE (mask);
103   mbase = mask->base_addr;
105   if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
106 #ifdef HAVE_GFC_LOGICAL_16
107       || mask_kind == 16
108 #endif
109       )
110     mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind);
111   else
112     internal_error (NULL, "Funny sized logical array");
114   mstride = GFC_DESCRIPTOR_STRIDE_BYTES(mask,0);
116   /* Search for the first occurrence of a true element in mask. */
117   for (j=0; j<extent; j++)
118     {
119       if (*mbase)
120         break;
121       mbase += mstride;
122     }
124   if (j == extent)
125     return 0;
127   ret = j + 1;
128   src = array->base_addr + j * sstride;
129   maxval = src;
131   for (i=j+1; i<=extent; i++)
132     {
133       if (*mbase && compare_fcn (src, maxval, len) > 0)
134       {
135          ret = i;
136          maxval = src;
137       }
138       src += sstride;
139       mbase += mstride;
140     }
141   return ret;
144 extern 'rtype_name` s'name`'rtype_qual`_'atype_code` ('atype` * const restrict,
145                                GFC_LOGICAL_4 *mask, gfc_charlen_type);
146 export_proto(s'name`'rtype_qual`_'atype_code`);
148 'rtype_name`
149 s'name`'rtype_qual`_'atype_code` ('atype` * const restrict array,
150                                  GFC_LOGICAL_4 *mask, gfc_charlen_type len)
152   if (mask)
153     return 'name`'rtype_qual`_'atype_code` (array, len);
154   else
155     return 0;
158 #endif'