1 1.1 mrg `/* Implementation of the MAXLOC intrinsic 2 1.1.1.3 mrg Copyright (C) 2002-2022 Free Software Foundation, Inc. 3 1.1 mrg Contributed by Paul Brook <paul (a] nowt.org> 4 1.1 mrg 5 1.1 mrg This file is part of the GNU Fortran runtime library (libgfortran). 6 1.1 mrg 7 1.1 mrg Libgfortran is free software; you can redistribute it and/or 8 1.1 mrg modify it under the terms of the GNU General Public 9 1.1 mrg License as published by the Free Software Foundation; either 10 1.1 mrg version 3 of the License, or (at your option) any later version. 11 1.1 mrg 12 1.1 mrg Libgfortran is distributed in the hope that it will be useful, 13 1.1 mrg but WITHOUT ANY WARRANTY; without even the implied warranty of 14 1.1 mrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 1.1 mrg GNU General Public License for more details. 16 1.1 mrg 17 1.1 mrg Under Section 7 of GPL version 3, you are granted additional 18 1.1 mrg permissions described in the GCC Runtime Library Exception, version 19 1.1 mrg 3.1, as published by the Free Software Foundation. 20 1.1 mrg 21 1.1 mrg You should have received a copy of the GNU General Public License and 22 1.1 mrg a copy of the GCC Runtime Library Exception along with this program; 23 1.1 mrg see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 24 1.1 mrg <http://www.gnu.org/licenses/>. */ 25 1.1 mrg 26 1.1 mrg #include "libgfortran.h" 27 1.1 mrg #include <assert.h>' 28 1.1 mrg 29 1.1 mrg include(iparm.m4)dnl 30 1.1 mrg include(ifunction.m4)dnl 31 1.1 mrg 32 1.1 mrg `#if defined (HAVE_'atype_name`) && defined (HAVE_'rtype_name`)' 33 1.1 mrg 34 1.1 mrg #define HAVE_BACK_ARG 1 35 1.1 mrg 36 1.1 mrg ARRAY_FUNCTION(0, 37 1.1 mrg ` atype_name maxval; 38 1.1 mrg #if defined ('atype_inf`) 39 1.1 mrg maxval = -atype_inf; 40 1.1 mrg #else 41 1.1 mrg maxval = atype_min; 42 1.1 mrg #endif 43 1.1 mrg result = 1;', 44 1.1 mrg `#if defined ('atype_nan`) 45 1.1 mrg for (n = 0; n < len; n++, src += delta) 46 1.1 mrg { 47 1.1 mrg if (*src >= maxval) 48 1.1 mrg { 49 1.1 mrg maxval = *src; 50 1.1 mrg result = (rtype_name)n + 1; 51 1.1 mrg break; 52 1.1 mrg } 53 1.1 mrg } 54 1.1 mrg #else 55 1.1 mrg n = 0; 56 1.1 mrg #endif 57 1.1 mrg for (; n < len; n++, src += delta) 58 1.1 mrg { 59 1.1 mrg if (back ? *src >= maxval : *src > maxval) 60 1.1 mrg { 61 1.1 mrg maxval = *src; 62 1.1 mrg result = (rtype_name)n + 1; 63 1.1 mrg }', `') 64 1.1 mrg 65 1.1 mrg MASKED_ARRAY_FUNCTION(0, 66 1.1 mrg ` atype_name maxval; 67 1.1 mrg #if defined ('atype_inf`) 68 1.1 mrg maxval = -atype_inf; 69 1.1 mrg #else 70 1.1 mrg maxval = atype_min; 71 1.1 mrg #endif 72 1.1 mrg #if defined ('atype_nan`) 73 1.1 mrg rtype_name result2 = 0; 74 1.1 mrg #endif 75 1.1 mrg result = 0;', 76 1.1 mrg ` if (*msrc) 77 1.1 mrg { 78 1.1 mrg #if defined ('atype_nan`) 79 1.1 mrg if (!result2) 80 1.1 mrg result2 = (rtype_name)n + 1; 81 1.1 mrg if (*src >= maxval) 82 1.1 mrg #endif 83 1.1 mrg { 84 1.1 mrg maxval = *src; 85 1.1 mrg result = (rtype_name)n + 1; 86 1.1 mrg break; 87 1.1 mrg } 88 1.1 mrg } 89 1.1 mrg } 90 1.1 mrg #if defined ('atype_nan`) 91 1.1 mrg if (unlikely (n >= len)) 92 1.1 mrg result = result2; 93 1.1 mrg else 94 1.1 mrg #endif 95 1.1 mrg if (back) 96 1.1 mrg for (; n < len; n++, src += delta, msrc += mdelta) 97 1.1 mrg { 98 1.1 mrg if (*msrc && unlikely (*src >= maxval)) 99 1.1 mrg { 100 1.1 mrg maxval = *src; 101 1.1 mrg result = (rtype_name)n + 1; 102 1.1 mrg } 103 1.1 mrg } 104 1.1 mrg else 105 1.1 mrg for (; n < len; n++, src += delta, msrc += mdelta) 106 1.1 mrg { 107 1.1 mrg if (*msrc && unlikely (*src > maxval)) 108 1.1 mrg { 109 1.1 mrg maxval = *src; 110 1.1 mrg result = (rtype_name)n + 1; 111 1.1 mrg }') 112 1.1 mrg 113 1.1 mrg SCALAR_ARRAY_FUNCTION(0) 114 1.1 mrg 115 1.1 mrg #endif 116