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