#include "Bdef.h"

void cgupk_amx(MatInf, buff)
MATINFO  *MatInf;
char  *buff;
/*
 * Unpacks the amx and distance matrices a column at a time into buff,
 * and then does a amx into the matrices pointed at by MatInf
 */
{
   int m, n, lda, ldd, i, j;
   SCOMPLEX *A, *v;
   float diff;
   unsigned short *Ad, *vd;

   m = MatInf->M;
   n = MatInf->N;
   A = (SCOMPLEX *) MatInf->A;
   lda = MatInf->lda;
   Ad = (unsigned short *) MatInf->B;
   ldd = (MatInf->ldb) * (sizeof(int) / sizeof(short));
   v = (SCOMPLEX *) buff;
   vd = (unsigned short *) &v[m];

   for(j=0; j < n; j++)
   {
      i = pvm_upkcplx((float *) v, m, 1);
      Mpvmerror(i, "pvm_upkcplx", __LINE__, __FILE__);
      i = pvm_upkshort((short *) vd, m, 1);
      Mpvmerror(i, "pvm_upkshort", __LINE__, __FILE__);
      for (i=0; i < m; i++)
      {
         diff = Cabs(A[i]) - Cabs(v[i]);
         if (diff <= 0)
         {
            if (diff != 0)
	    {
               A[i].r = v[i].r;
               A[i].i = v[i].i;
               Ad[i] = vd[i];
	    }
	    else if (vd[i] < Ad[i])
	    {
               A[i].r = v[i].r;
               A[i].i = v[i].i;
               Ad[i] = vd[i];
	    }
	 }
      }
      A += lda;
      Ad += ldd;
   }
}
