]> git.ipfire.org Git - thirdparty/gcc.git/blame - libgfortran/generated/in_pack_r4.c
abort.c: Remove unused headers.
[thirdparty/gcc.git] / libgfortran / generated / in_pack_r4.c
CommitLineData
8e1d7686 1/* Helper function for repacking arrays.
818ab71a 2 Copyright (C) 2003-2016 Free Software Foundation, Inc.
8e1d7686
TK
3 Contributed by Paul Brook <paul@nowt.org>
4
21d1335b 5This file is part of the GNU Fortran runtime library (libgfortran).
8e1d7686
TK
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
748086b7 10version 3 of the License, or (at your option) any later version.
8e1d7686
TK
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
748086b7
JJ
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/>. */
8e1d7686
TK
25
26#include "libgfortran.h"
27#include <stdlib.h>
8e1d7686
TK
28
29
30#if defined (HAVE_GFC_REAL_4)
31
32/* Allocates a block of memory with internal_malloc if the array needs
33 repacking. */
34
35GFC_REAL_4 *
36internal_pack_r4 (gfc_array_r4 * source)
37{
38 index_type count[GFC_MAX_DIMENSIONS];
39 index_type extent[GFC_MAX_DIMENSIONS];
40 index_type stride[GFC_MAX_DIMENSIONS];
41 index_type stride0;
42 index_type dim;
43 index_type ssize;
44 const GFC_REAL_4 *src;
5863aacf 45 GFC_REAL_4 * restrict dest;
8e1d7686
TK
46 GFC_REAL_4 *destptr;
47 int n;
48 int packed;
49
50 /* TODO: Investigate how we can figure out if this is a temporary
51 since the stride=0 thing has been removed from the frontend. */
52
53 dim = GFC_DESCRIPTOR_RANK (source);
54 ssize = 1;
55 packed = 1;
56 for (n = 0; n < dim; n++)
57 {
58 count[n] = 0;
dfb55fdc
TK
59 stride[n] = GFC_DESCRIPTOR_STRIDE(source,n);
60 extent[n] = GFC_DESCRIPTOR_EXTENT(source,n);
8e1d7686
TK
61 if (extent[n] <= 0)
62 {
63 /* Do nothing. */
64 packed = 1;
65 break;
66 }
67
68 if (ssize != stride[n])
69 packed = 0;
70
71 ssize *= extent[n];
72 }
73
74 if (packed)
21d1335b 75 return source->base_addr;
8e1d7686
TK
76
77 /* Allocate storage for the destination. */
92e6f3a4 78 destptr = xmallocarray (ssize, sizeof (GFC_REAL_4));
8e1d7686 79 dest = destptr;
21d1335b 80 src = source->base_addr;
8e1d7686
TK
81 stride0 = stride[0];
82
83
84 while (src)
85 {
86 /* Copy the data. */
87 *(dest++) = *src;
88 /* Advance to the next element. */
89 src += stride0;
90 count[0]++;
91 /* Advance to the next source element. */
92 n = 0;
93 while (count[n] == extent[n])
94 {
95 /* When we get to the end of a dimension, reset it and increment
96 the next dimension. */
97 count[n] = 0;
98 /* We could precalculate these products, but this is a less
99 frequently used path so probably not worth it. */
100 src -= stride[n] * extent[n];
101 n++;
102 if (n == dim)
103 {
104 src = NULL;
105 break;
106 }
107 else
108 {
109 count[n]++;
110 src += stride[n];
111 }
112 }
113 }
114 return destptr;
115}
116
117#endif
118