in_unpack_generic.c [plain text]
#include "config.h"
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "libgfortran.h"
void
internal_unpack (gfc_array_char * d, const void * s)
{
index_type count[GFC_MAX_DIMENSIONS - 1];
index_type extent[GFC_MAX_DIMENSIONS - 1];
index_type stride[GFC_MAX_DIMENSIONS - 1];
index_type stride0;
index_type dim;
index_type dsize;
char *dest;
const char *src;
int n;
int size;
dest = d->data;
if (s == dest || !s)
return;
size = GFC_DESCRIPTOR_SIZE (d);
switch (size)
{
case 4:
internal_unpack_4 ((gfc_array_i4 *)d, (const GFC_INTEGER_4 *)s);
return;
case 8:
internal_unpack_8 ((gfc_array_i8 *)d, (const GFC_INTEGER_8 *)s);
return;
}
if (d->dim[0].stride == 0)
d->dim[0].stride = 1;
dim = GFC_DESCRIPTOR_RANK (d);
dsize = 1;
for (n = 0; n < dim; n++)
{
count[n] = 0;
stride[n] = d->dim[n].stride;
extent[n] = d->dim[n].ubound + 1 - d->dim[n].lbound;
if (extent[n] <= 0)
abort ();
if (dsize == stride[n])
dsize *= extent[n];
else
dsize = 0;
}
src = s;
if (dsize != 0)
{
memcpy (dest, src, dsize * size);
return;
}
stride0 = stride[0] * size;
while (dest)
{
memcpy (dest, src, size);
src += size;
dest += stride0;
count[0]++;
n = 0;
while (count[n] == extent[n])
{
count[n] = 0;
dest -= stride[n] * extent[n] * size;
n++;
if (n == dim)
{
dest = NULL;
break;
}
else
{
count[n]++;
dest += stride[n] * size;
}
}
}
}