#include "Bdef.h"



int Shyp_bs(ctxt, scope)
BLACSCONTEXT  *ctxt;
char  scope;
{
   void Ssend2d00();

   int msgid;
   int bit, rdest, cdest, Np;

   scope = Mlowcase(scope);
   switch(scope)
   {
   case 'r':
      Np = ctxt->npcol;
      if (Np < 2) return(NORV);
      for (bit=2; bit < Np; bit <<= 1);
      if (bit^Np) return(NPOW2);  /* not a power of 2 */
      msgid = Mrid(ctxt);

      for(bit=1; (bit^Np); bit <<= 1)
         Ssend2d00(ctxt, ctxt->myrow, ctxt->mycol^bit, msgid);
      break;
   case 'c':
      Np = ctxt->nprow;
      if (Np < 2) return(NORV);
      for (bit=2; (bit < Np); bit <<= 1);
      if (bit^Np) return(NPOW2);  /* not a power of 2 */
      msgid = Mcid(ctxt);

      for(bit=1; (bit^Np); bit <<= 1)
         Ssend2d00(ctxt, ctxt->myrow^bit, ctxt->mycol, msgid);
      break;
   case 'a':
      Np = ctxt->Ng;
      if (Np < 2) return(NORV);
      for (bit=2; bit < Np; bit <<= 1);
      if (bit^Np) return(NPOW2);  /* not a power of 2 */
      msgid = Maid(ctxt);

      for(bit=1; (bit^Np); bit <<= 1)
      {
         Mvpcoord(ctxt, ctxt->vIam ^ bit, rdest, cdest);
         Ssend2d00(ctxt, rdest, cdest, msgid);
      }
      break;
   default:
      return(BADSCP);
   }

   return(0); 	/* no errors */
}
