]>
Commit | Line | Data |
---|---|---|
6de9cd9a | 1 | `/* Helper function for repacking arrays. |
85ec4feb | 2 | Copyright (C) 2003-2018 Free Software Foundation, Inc. |
6de9cd9a DN |
3 | Contributed by Paul Brook <paul@nowt.org> |
4 | ||
21d1335b | 5 | This file is part of the GNU Fortran runtime library (libgfortran). |
6de9cd9a | 6 | |
57dea9f6 TM |
7 | Libgfortran is free software; you can redistribute it and/or |
8 | modify it under the terms of the GNU General Public | |
6de9cd9a | 9 | License as published by the Free Software Foundation; either |
748086b7 | 10 | version 3 of the License, or (at your option) any later version. |
57dea9f6 TM |
11 | |
12 | Libgfortran is distributed in the hope that it will be useful, | |
6de9cd9a DN |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
57dea9f6 | 15 | GNU General Public License for more details. |
6de9cd9a | 16 | |
748086b7 JJ |
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. | |
20 | ||
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/>. */ | |
6de9cd9a | 25 | |
887d9b8b | 26 | #include "libgfortran.h"' |
36ae8a61 | 27 | |
c9e66eda | 28 | include(iparm.m4)dnl |
6de9cd9a | 29 | |
adea5e16 | 30 | `#if defined (HAVE_'rtype_name`) |
644cb69f | 31 | |
6de9cd9a DN |
32 | /* Allocates a block of memory with internal_malloc if the array needs |
33 | repacking. */ | |
adea5e16 | 34 | ' |
39328081 TK |
35 | dnl The kind (ie size) is used to name the function for logicals, integers |
36 | dnl and reals. For complex, it's c4 or c8. | |
adea5e16 TK |
37 | rtype_name` * |
38 | internal_pack_'rtype_ccode` ('rtype` * source) | |
6de9cd9a | 39 | { |
e33e218b TK |
40 | index_type count[GFC_MAX_DIMENSIONS]; |
41 | index_type extent[GFC_MAX_DIMENSIONS]; | |
42 | index_type stride[GFC_MAX_DIMENSIONS]; | |
6de9cd9a DN |
43 | index_type stride0; |
44 | index_type dim; | |
45 | index_type ssize; | |
adea5e16 | 46 | const 'rtype_name` *src; |
5863aacf | 47 | 'rtype_name` * restrict dest; |
adea5e16 | 48 | 'rtype_name` *destptr; |
6de9cd9a DN |
49 | int n; |
50 | int packed; | |
51 | ||
6ff24d45 JB |
52 | /* TODO: Investigate how we can figure out if this is a temporary |
53 | since the stride=0 thing has been removed from the frontend. */ | |
6de9cd9a DN |
54 | |
55 | dim = GFC_DESCRIPTOR_RANK (source); | |
56 | ssize = 1; | |
57 | packed = 1; | |
58 | for (n = 0; n < dim; n++) | |
59 | { | |
60 | count[n] = 0; | |
dfb55fdc TK |
61 | stride[n] = GFC_DESCRIPTOR_STRIDE(source,n); |
62 | extent[n] = GFC_DESCRIPTOR_EXTENT(source,n); | |
6de9cd9a DN |
63 | if (extent[n] <= 0) |
64 | { | |
65 | /* Do nothing. */ | |
66 | packed = 1; | |
67 | break; | |
68 | } | |
69 | ||
70 | if (ssize != stride[n]) | |
71 | packed = 0; | |
72 | ||
73 | ssize *= extent[n]; | |
74 | } | |
75 | ||
76 | if (packed) | |
21d1335b | 77 | return source->base_addr; |
6de9cd9a DN |
78 | |
79 | /* Allocate storage for the destination. */ | |
92e6f3a4 | 80 | destptr = xmallocarray (ssize, sizeof ('rtype_name`)); |
6de9cd9a | 81 | dest = destptr; |
21d1335b | 82 | src = source->base_addr; |
6de9cd9a DN |
83 | stride0 = stride[0]; |
84 | ||
85 | ||
86 | while (src) | |
87 | { | |
88 | /* Copy the data. */ | |
89 | *(dest++) = *src; | |
90 | /* Advance to the next element. */ | |
91 | src += stride0; | |
92 | count[0]++; | |
93 | /* Advance to the next source element. */ | |
94 | n = 0; | |
95 | while (count[n] == extent[n]) | |
96 | { | |
97 | /* When we get to the end of a dimension, reset it and increment | |
98 | the next dimension. */ | |
99 | count[n] = 0; | |
100 | /* We could precalculate these products, but this is a less | |
8b6dba81 | 101 | frequently used path so probably not worth it. */ |
6de9cd9a DN |
102 | src -= stride[n] * extent[n]; |
103 | n++; | |
104 | if (n == dim) | |
105 | { | |
106 | src = NULL; | |
107 | break; | |
108 | } | |
109 | else | |
110 | { | |
111 | count[n]++; | |
112 | src += stride[n]; | |
113 | } | |
114 | } | |
115 | } | |
116 | return destptr; | |
117 | } | |
118 | ||
644cb69f | 119 | #endif |
36ae8a61 | 120 | ' |