#include "config.h"
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "libgfortran.h"
static void
pack_internal (gfc_array_char *ret, const gfc_array_char *array,
const gfc_array_l4 *mask, const gfc_array_char *vector,
index_type size)
{
index_type rstride0;
char *rptr;
index_type sstride[GFC_MAX_DIMENSIONS];
index_type sstride0;
const char *sptr;
index_type mstride[GFC_MAX_DIMENSIONS];
index_type mstride0;
const GFC_LOGICAL_4 *mptr;
index_type count[GFC_MAX_DIMENSIONS];
index_type extent[GFC_MAX_DIMENSIONS];
int zero_sized;
index_type n;
index_type dim;
index_type nelem;
dim = GFC_DESCRIPTOR_RANK (array);
zero_sized = 0;
for (n = 0; n < dim; n++)
{
count[n] = 0;
extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
if (extent[n] <= 0)
zero_sized = 1;
sstride[n] = array->dim[n].stride * size;
mstride[n] = mask->dim[n].stride;
}
if (sstride[0] == 0)
sstride[0] = size;
if (mstride[0] == 0)
mstride[0] = 1;
sptr = array->data;
mptr = mask->data;
if (GFC_DESCRIPTOR_SIZE (mask) != 4)
{
if (GFC_DESCRIPTOR_SIZE (mask) != 8)
runtime_error ("Funny sized logical array");
for (n = 0; n < dim; n++)
mstride[n] <<= 1;
mptr = GFOR_POINTER_L8_TO_L4 (mptr);
}
if (ret->data == NULL)
{
int total;
if (vector != NULL)
{
total = vector->dim[0].ubound + 1 - vector->dim[0].lbound;
}
else
{
const GFC_LOGICAL_4 *m = mptr;
total = 0;
if (zero_sized)
m = NULL;
while (m)
{
if (*m)
total++;
m += mstride[0];
count[0]++;
n = 0;
while (count[n] == extent[n])
{
count[n] = 0;
m -= mstride[n] * extent[n];
n++;
if (n >= dim)
{
m = NULL;
break;
}
else
{
count[n]++;
m += mstride[n];
}
}
}
}
ret->dim[0].lbound = 0;
ret->dim[0].ubound = total - 1;
ret->dim[0].stride = 1;
ret->offset = 0;
if (total == 0)
{
ret->data = internal_malloc_size (1);
return;
}
else
ret->data = internal_malloc_size (size * total);
}
rstride0 = ret->dim[0].stride * size;
if (rstride0 == 0)
rstride0 = size;
sstride0 = sstride[0];
mstride0 = mstride[0];
rptr = ret->data;
while (sptr && mptr)
{
if (*mptr)
{
memcpy (rptr, sptr, size);
rptr += rstride0;
}
sptr += sstride0;
mptr += mstride0;
count[0]++;
n = 0;
while (count[n] == extent[n])
{
count[n] = 0;
sptr -= sstride[n] * extent[n];
mptr -= mstride[n] * extent[n];
n++;
if (n >= dim)
{
sptr = NULL;
break;
}
else
{
count[n]++;
sptr += sstride[n];
mptr += mstride[n];
}
}
}
if (vector)
{
n = vector->dim[0].ubound + 1 - vector->dim[0].lbound;
nelem = ((rptr - ret->data) / rstride0);
if (n > nelem)
{
sstride0 = vector->dim[0].stride * size;
if (sstride0 == 0)
sstride0 = size;
sptr = vector->data + sstride0 * nelem;
n -= nelem;
while (n--)
{
memcpy (rptr, sptr, size);
rptr += rstride0;
sptr += sstride0;
}
}
}
}
extern void pack (gfc_array_char *, const gfc_array_char *,
const gfc_array_l4 *, const gfc_array_char *);
export_proto(pack);
void
pack (gfc_array_char *ret, const gfc_array_char *array,
const gfc_array_l4 *mask, const gfc_array_char *vector)
{
pack_internal (ret, array, mask, vector, GFC_DESCRIPTOR_SIZE (array));
}
extern void pack_char (gfc_array_char *, GFC_INTEGER_4, const gfc_array_char *,
const gfc_array_l4 *, const gfc_array_char *,
GFC_INTEGER_4, GFC_INTEGER_4);
export_proto(pack_char);
void
pack_char (gfc_array_char *ret,
GFC_INTEGER_4 ret_length __attribute__((unused)),
const gfc_array_char *array, const gfc_array_l4 *mask,
const gfc_array_char *vector, GFC_INTEGER_4 array_length,
GFC_INTEGER_4 vector_length __attribute__((unused)))
{
pack_internal (ret, array, mask, vector, array_length);
}
static void
pack_s_internal (gfc_array_char *ret, const gfc_array_char *array,
const GFC_LOGICAL_4 *mask, const gfc_array_char *vector,
index_type size)
{
index_type rstride0;
char *rptr;
index_type sstride[GFC_MAX_DIMENSIONS];
index_type sstride0;
const char *sptr;
index_type count[GFC_MAX_DIMENSIONS];
index_type extent[GFC_MAX_DIMENSIONS];
index_type n;
index_type dim;
index_type ssize;
index_type nelem;
dim = GFC_DESCRIPTOR_RANK (array);
ssize = 1;
for (n = 0; n < dim; n++)
{
count[n] = 0;
extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
sstride[n] = array->dim[n].stride * size;
ssize *= extent[n];
}
if (sstride[0] == 0)
sstride[0] = size;
sstride0 = sstride[0];
sptr = array->data;
if (ret->data == NULL)
{
int total;
if (vector != NULL)
{
total = vector->dim[0].ubound + 1 - vector->dim[0].lbound;
}
else
{
if (*mask)
{
total = extent[0];
for (n = 1; n < dim; n++)
total *= extent[n];
}
else
total = 0;
}
ret->dim[0].lbound = 0;
ret->dim[0].ubound = total - 1;
ret->dim[0].stride = 1;
ret->offset = 0;
if (total == 0)
{
ret->data = internal_malloc_size (1);
return;
}
else
ret->data = internal_malloc_size (size * total);
}
rstride0 = ret->dim[0].stride * size;
if (rstride0 == 0)
rstride0 = size;
rptr = ret->data;
if (*mask && ssize != 0)
{
while (sptr)
{
memcpy (rptr, sptr, size);
rptr += rstride0;
sptr += sstride0;
count[0]++;
n = 0;
while (count[n] == extent[n])
{
count[n] = 0;
sptr -= sstride[n] * extent[n];
n++;
if (n >= dim)
{
sptr = NULL;
break;
}
else
{
count[n]++;
sptr += sstride[n];
}
}
}
}
if (vector)
{
n = vector->dim[0].ubound + 1 - vector->dim[0].lbound;
nelem = ((rptr - ret->data) / rstride0);
if (n > nelem)
{
sstride0 = vector->dim[0].stride * size;
if (sstride0 == 0)
sstride0 = size;
sptr = vector->data + sstride0 * nelem;
n -= nelem;
while (n--)
{
memcpy (rptr, sptr, size);
rptr += rstride0;
sptr += sstride0;
}
}
}
}
extern void pack_s (gfc_array_char *ret, const gfc_array_char *array,
const GFC_LOGICAL_4 *, const gfc_array_char *);
export_proto(pack_s);
void
pack_s (gfc_array_char *ret, const gfc_array_char *array,
const GFC_LOGICAL_4 *mask, const gfc_array_char *vector)
{
pack_s_internal (ret, array, mask, vector, GFC_DESCRIPTOR_SIZE (array));
}
extern void pack_s_char (gfc_array_char *ret, GFC_INTEGER_4,
const gfc_array_char *array, const GFC_LOGICAL_4 *,
const gfc_array_char *, GFC_INTEGER_4,
GFC_INTEGER_4);
export_proto(pack_s_char);
void
pack_s_char (gfc_array_char *ret,
GFC_INTEGER_4 ret_length __attribute__((unused)),
const gfc_array_char *array, const GFC_LOGICAL_4 *mask,
const gfc_array_char *vector, GFC_INTEGER_4 array_length,
GFC_INTEGER_4 vector_length __attribute__((unused)))
{
pack_s_internal (ret, array, mask, vector, array_length);
}