1  #include <stdlib.h>
       2  #include <stdio.h>
       3  
       4  #include <ISO_Fortran_binding.h>
       5  #include "dump-descriptors.h"
       6  
       7  struct m {
       8    int i, j, k, l;
       9  };
      10  
      11  extern void ctest (void);
      12  
      13  #define IMAX 6
      14  #define JMAX 8
      15  #define KMAX 10
      16  #define LMAX 12
      17  
      18  static struct m buffer[LMAX][KMAX][JMAX][IMAX];
      19  
      20  static void
      21  check_element (struct m *mp, int i, int j, int k, int l)
      22  {
      23  #if 0
      24    fprintf (stderr, "expected (%d, %d, %d, %d), got (%d, %d, %d, %d)\n",
      25  	   i, j, k, l, mp->i, mp->j, mp->k, mp->l);
      26  #endif  
      27    if (mp->i != i || mp->j != j || mp->k != k || mp->l != l)
      28      abort ();
      29  }
      30  
      31  void
      32  ctest (void)
      33  {
      34    CFI_CDESC_T(4) sdesc;
      35    CFI_cdesc_t *source = (CFI_cdesc_t *) &sdesc;
      36    CFI_CDESC_T(4) rdesc;
      37    CFI_cdesc_t *result = (CFI_cdesc_t *) &rdesc;
      38    CFI_index_t extents[4] = { IMAX, JMAX, KMAX, LMAX };
      39    CFI_index_t lb[4], ub[4], s[4];
      40    int i, j, k, l;
      41    int ii, jj, kk, ll;
      42  
      43    /* Initialize the buffer to uniquely label each element.  */
      44    for (i = 0; i < IMAX; i++)
      45      for (j = 0; j < JMAX; j++)
      46        for (k = 0; k < KMAX; k++)
      47  	for (l = 0; l < LMAX; l++)
      48  	  {
      49  	    buffer[l][k][j][i].i = i;
      50  	    buffer[l][k][j][i].j = j;
      51  	    buffer[l][k][j][i].k = k;
      52  	    buffer[l][k][j][i].l = l;
      53  	  }
      54  
      55    /* Establish the source array.  */
      56    check_CFI_status ("CFI_establish",
      57  		    CFI_establish (source, (void *)buffer,
      58  				   CFI_attribute_pointer, CFI_type_struct,
      59  				   sizeof (struct m), 4, extents));
      60  
      61    /* Try taking a degenerate section (single element).  */
      62    check_CFI_status ("CFI_establish",
      63  		    CFI_establish (result, NULL,
      64  				   CFI_attribute_pointer, CFI_type_struct,
      65  				   sizeof (struct m), 0, NULL));
      66    lb[0] = 3; lb[1] = 4; lb[2] = 5; lb[3] = 6;
      67    ub[0] = 3; ub[1] = 4; ub[2] = 5; ub[3] = 6;
      68    s[0] = 0; s[1] = 0; s[2] = 0; s[3] = 0;
      69    check_CFI_status ("CFI_section",
      70  		    CFI_section (result, source, lb, ub, s));
      71    dump_CFI_cdesc_t (result);
      72    check_element ((struct m *)result->base_addr, 3, 4, 5, 6);
      73  
      74    /* Try taking a 2d chunk out of the 4d array.  */
      75    check_CFI_status ("CFI_establish",
      76  		    CFI_establish (result, NULL,
      77  				   CFI_attribute_pointer, CFI_type_struct,
      78  				   sizeof (struct m), 2, NULL));
      79    lb[0] = 1; lb[1] = 2; lb[2] = 3; lb[3] = 4;
      80    ub[0] = 1; ub[1] = JMAX - 2; ub[2] = 3; ub[3] = LMAX - 2;
      81    s[0] = 0; s[1] = 2; s[2] = 0; s[3] = 3;
      82    check_CFI_status ("CFI_section",
      83  		    CFI_section (result, source, lb, ub, s));
      84    dump_CFI_cdesc_t (result);
      85  
      86    i = lb[0];
      87    k = lb[2];
      88    for (j = lb[1], jj = result->dim[0].lower_bound;
      89         j <= ub[1];
      90         j += s[1], jj++)
      91      for (l = lb[3], ll = result->dim[1].lower_bound;
      92  	 l <= ub[3];
      93  	 l += s[3], ll++)
      94        {
      95  	CFI_index_t subscripts[2];
      96  	subscripts[0] = jj;
      97  	subscripts[1] = ll;
      98  	check_element ((struct m *) CFI_address (result, subscripts),
      99  		       i, j, k, l);
     100        }
     101  }