1627f7eb2Smrg`/* Helper function for repacking arrays. 2*4c3eb207Smrg Copyright (C) 2003-2020 Free Software Foundation, Inc. 3627f7eb2Smrg Contributed by Paul Brook <paul@nowt.org> 4627f7eb2Smrg 5627f7eb2SmrgThis file is part of the GNU Fortran runtime library (libgfortran). 6627f7eb2Smrg 7627f7eb2SmrgLibgfortran is free software; you can redistribute it and/or 8627f7eb2Smrgmodify it under the terms of the GNU General Public 9627f7eb2SmrgLicense as published by the Free Software Foundation; either 10627f7eb2Smrgversion 3 of the License, or (at your option) any later version. 11627f7eb2Smrg 12627f7eb2SmrgLibgfortran is distributed in the hope that it will be useful, 13627f7eb2Smrgbut WITHOUT ANY WARRANTY; without even the implied warranty of 14627f7eb2SmrgMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15627f7eb2SmrgGNU General Public License for more details. 16627f7eb2Smrg 17627f7eb2SmrgUnder Section 7 of GPL version 3, you are granted additional 18627f7eb2Smrgpermissions described in the GCC Runtime Library Exception, version 19627f7eb2Smrg3.1, as published by the Free Software Foundation. 20627f7eb2Smrg 21627f7eb2SmrgYou should have received a copy of the GNU General Public License and 22627f7eb2Smrga copy of the GCC Runtime Library Exception along with this program; 23627f7eb2Smrgsee the files COPYING3 and COPYING.RUNTIME respectively. If not, see 24627f7eb2Smrg<http://www.gnu.org/licenses/>. */ 25627f7eb2Smrg 26627f7eb2Smrg#include "libgfortran.h"' 27627f7eb2Smrg 28627f7eb2Smrginclude(iparm.m4)dnl 29627f7eb2Smrg 30627f7eb2Smrg`#if defined (HAVE_'rtype_name`) 31627f7eb2Smrg 32627f7eb2Smrg/* Allocates a block of memory with internal_malloc if the array needs 33627f7eb2Smrg repacking. */ 34627f7eb2Smrg' 35627f7eb2Smrgdnl The kind (ie size) is used to name the function for logicals, integers 36627f7eb2Smrgdnl and reals. For complex, it's c4 or c8. 37627f7eb2Smrgrtype_name` * 38627f7eb2Smrginternal_pack_'rtype_ccode` ('rtype` * source) 39627f7eb2Smrg{ 40627f7eb2Smrg index_type count[GFC_MAX_DIMENSIONS]; 41627f7eb2Smrg index_type extent[GFC_MAX_DIMENSIONS]; 42627f7eb2Smrg index_type stride[GFC_MAX_DIMENSIONS]; 43627f7eb2Smrg index_type stride0; 44627f7eb2Smrg index_type dim; 45627f7eb2Smrg index_type ssize; 46627f7eb2Smrg const 'rtype_name` *src; 47627f7eb2Smrg 'rtype_name` * restrict dest; 48627f7eb2Smrg 'rtype_name` *destptr; 49627f7eb2Smrg int packed; 50627f7eb2Smrg 51627f7eb2Smrg /* TODO: Investigate how we can figure out if this is a temporary 52627f7eb2Smrg since the stride=0 thing has been removed from the frontend. */ 53627f7eb2Smrg 54627f7eb2Smrg dim = GFC_DESCRIPTOR_RANK (source); 55627f7eb2Smrg ssize = 1; 56627f7eb2Smrg packed = 1; 57627f7eb2Smrg for (index_type n = 0; n < dim; n++) 58627f7eb2Smrg { 59627f7eb2Smrg count[n] = 0; 60627f7eb2Smrg stride[n] = GFC_DESCRIPTOR_STRIDE(source,n); 61627f7eb2Smrg extent[n] = GFC_DESCRIPTOR_EXTENT(source,n); 62627f7eb2Smrg if (extent[n] <= 0) 63627f7eb2Smrg { 64627f7eb2Smrg /* Do nothing. */ 65627f7eb2Smrg packed = 1; 66627f7eb2Smrg break; 67627f7eb2Smrg } 68627f7eb2Smrg 69627f7eb2Smrg if (ssize != stride[n]) 70627f7eb2Smrg packed = 0; 71627f7eb2Smrg 72627f7eb2Smrg ssize *= extent[n]; 73627f7eb2Smrg } 74627f7eb2Smrg 75627f7eb2Smrg if (packed) 76627f7eb2Smrg return source->base_addr; 77627f7eb2Smrg 78627f7eb2Smrg /* Allocate storage for the destination. */ 79627f7eb2Smrg destptr = xmallocarray (ssize, sizeof ('rtype_name`)); 80627f7eb2Smrg dest = destptr; 81627f7eb2Smrg src = source->base_addr; 82627f7eb2Smrg stride0 = stride[0]; 83627f7eb2Smrg 84627f7eb2Smrg 85627f7eb2Smrg while (src) 86627f7eb2Smrg { 87627f7eb2Smrg /* Copy the data. */ 88627f7eb2Smrg *(dest++) = *src; 89627f7eb2Smrg /* Advance to the next element. */ 90627f7eb2Smrg src += stride0; 91627f7eb2Smrg count[0]++; 92627f7eb2Smrg /* Advance to the next source element. */ 93627f7eb2Smrg index_type n = 0; 94627f7eb2Smrg while (count[n] == extent[n]) 95627f7eb2Smrg { 96627f7eb2Smrg /* When we get to the end of a dimension, reset it and increment 97627f7eb2Smrg the next dimension. */ 98627f7eb2Smrg count[n] = 0; 99627f7eb2Smrg /* We could precalculate these products, but this is a less 100627f7eb2Smrg frequently used path so probably not worth it. */ 101627f7eb2Smrg src -= stride[n] * extent[n]; 102627f7eb2Smrg n++; 103627f7eb2Smrg if (n == dim) 104627f7eb2Smrg { 105627f7eb2Smrg src = NULL; 106627f7eb2Smrg break; 107627f7eb2Smrg } 108627f7eb2Smrg else 109627f7eb2Smrg { 110627f7eb2Smrg count[n]++; 111627f7eb2Smrg src += stride[n]; 112627f7eb2Smrg } 113627f7eb2Smrg } 114627f7eb2Smrg } 115627f7eb2Smrg return destptr; 116627f7eb2Smrg} 117627f7eb2Smrg 118627f7eb2Smrg#endif 119627f7eb2Smrg' 120