
/*******************************************************************************
*
* File events.h
*
* Copyright (C) 2006,2007 Martin Luescher
*
* This software is distributed under the terms of the GNU General Public
* License (GPL)
*
* Full-lattice events
*
*******************************************************************************/

#define EVENTS_H

#if (defined FLAGS_C)

static void (*event_fcts[EVENTS])(void)={NULL};

static void LatNewU(void)
{
   int n;

   lat.u=next_tag();
   lat.bc=0;

   for (n=0;n<BLK_GRIDS;n++)
      gfs[n].ub=lat.u;
}

static void LatNewUd(void)
{
   int n;

   lat.ud=next_tag();
   lat.bcd=0;

   for (n=0;n<BLK_GRIDS;n++)
      gfs[n].ubd=lat.ud;
}

static void LatFreedU(void)
{
   int n;

   lat.u=0;
   lat.ubuf=0;
   lat.bc=0;
   lat.bcbuf=0;

   for (n=0;n<BLK_GRIDS;n++)
      gfs[n].ub=0;
}

static void LatFreedUd(void)
{
   int n;

   lat.ud=0;
   lat.udbuf=0;
   lat.bcd=0;
   lat.bcdbuf=0;

   for (n=0;n<BLK_GRIDS;n++)
      gfs[n].ubd=0;
}

static void LatAssignedU2ud(void)
{
   int n;

   lat.ud=next_tag();
   lat.bcd=lat.bc;

   for (n=0;n<BLK_GRIDS;n++)
      gfs[n].ubd=lat.ud;
}

static void LatAssignedUd2u(void)
{
   int n;

   lat.u=lat.ud;
   lat.bc=lat.bcd;

   for (n=0;n<BLK_GRIDS;n++)
      gfs[n].ub=gfs[n].ubd;
}

static void LatCopiedBndU(void)
{
   lat.ubuf=lat.u;
   lat.bcbuf=lat.bc;
}

static void LatCopiedBndUd(void)
{
   lat.udbuf=lat.ud;
   lat.bcdbuf=lat.bcd;
}

static void LatShiftedU(void)
{
   LatNewU();

   lat.ubuf=0;   
   lat.bcbuf=0;
}

static void LatShiftedUd(void)
{
   LatNewUd();

   lat.udbuf=0;
   lat.bcdbuf=0;
}

static void FreedBndbufsU(void)
{
   lat.ubuf=0;
   lat.bcbuf=0;
}

static void FreedBndbufsUd(void)
{
   lat.udbuf=0;
   lat.bcdbuf=0;
}

static void LatComputedSwd(void)
{
   lat.swd[0]=lat.ud;
   lat.swd[1]=0;
   lat.swd[2]=0;
}

static void LatFreedSw(void)
{
   lat.sw[0]=0;
   lat.sw[1]=0;
   lat.sw[2]=0;
}

static void LatFreedSwd(void)
{
   lat.swd[0]=0;
   lat.swd[1]=0;
   lat.swd[2]=0;
}

static void LatAssignedSwd2sw(void)
{
   lat.sw[0]=lat.swd[0];
   lat.sw[1]=lat.swd[1];
   lat.sw[2]=lat.swd[2];
}

static void LatInvertedSwdE(void)
{
   lat.swd[1]^=1;
}

static void LatInvertedSwdO(void)
{
   lat.swd[2]^=1;
}

static void LatFlippedBc(void)
{
   lat.bc^=1;
}

static void LatFlippedBcd(void)
{
   lat.bcd^=1;
}

static void LatChangedQuarkMass(void)
{
   int n,nb,nbf,l;
   blk_flags_t *bf;

   lat.swd[0]=0;
   lat.swd[1]=0;
   lat.swd[2]=0;

   lat.sw[0]=0;
   lat.sw[1]=0;
   lat.sw[2]=0;   

   for (l=0;l<ZOPS;l++)
   {
      lat.zop[l][0]=0;
      lat.zop[l][1]=0;
   }
   
   for (n=0;n<BLK_GRIDS;n++)
   {
      nbf=gfs[n].nb;
      bf=gfs[n].bf;

      for (nb=0;nb<nbf;nb++)
      {
         bf[nb].sw[0]=0;
         bf[nb].sw[1]=0;
         bf[nb].sw[2]=0;

         bf[nb].swd[0]=0;
         bf[nb].swd[1]=0;
         bf[nb].swd[2]=0;

         bf[nb].swb=0;
      }
   }
}

static void LatChangedCsw(void)
{
   LatChangedQuarkMass();

   lat.dfl[0]=0;
   lat.dfl[1]=0;
}

static void LatAllocatedMsapBufs(void)
{
   gfs[GCR_BLOCKS].nsb=1;
}

static void LatFreedMsapBufs(void)
{
   gfs[GCR_BLOCKS].nsb=0;
}

static void LatNewDflmodes(void)
{
   int l;

   lat.dfl[0]=lat.ud;
   lat.dfl[1]=lat.bcd;
   
   for (l=0;l<ZOPS;l++)
   {
      lat.zop[l][0]=0;
      lat.zop[l][1]=0;
   }
}

static void LatComputedZopnohatdble(void)
{
   lat.zop[ZOPNOHAT_DBLE][0]=lat.ud;
   lat.zop[ZOPNOHAT_DBLE][1]=lat.bcd;
}

static void LatComputedZophatdble(void)
{
   lat.zop[ZOPHAT_DBLE][0]=lat.zop[ZOPNOHAT_DBLE][0];
   lat.zop[ZOPHAT_DBLE][1]=lat.zop[ZOPNOHAT_DBLE][1];
}

static void LatComputedZopnohat(void)
{
   lat.zop[ZOPNOHAT][0]=lat.zop[ZOPNOHAT_DBLE][0];
   lat.zop[ZOPNOHAT][1]=lat.zop[ZOPNOHAT_DBLE][1];
}

static void LatComputedZophat(void)
{
   lat.zop[ZOPHAT][0]=lat.zop[ZOPHAT_DBLE][0];
   lat.zop[ZOPHAT][1]=lat.zop[ZOPHAT_DBLE][1];
}

static void LatFreedZops(void)
{
   int l;

   for (l=0;l<ZOPS;l++)
   {
      lat.zop[l][0]=0;
      lat.zop[l][1]=0;
   }
}

static void set_events(void)
{
   event_fcts[NEW_U]=LatNewU;
   event_fcts[NEW_UD]=LatNewUd;
   event_fcts[FREED_U]=LatFreedU;
   event_fcts[FREED_UD]=LatFreedUd;
   event_fcts[ASSIGNED_U2UD]=LatAssignedU2ud;
   event_fcts[ASSIGNED_UD2U]=LatAssignedUd2u;
   event_fcts[COPIED_BND_U]=LatCopiedBndU;
   event_fcts[COPIED_BND_UD]=LatCopiedBndUd;
   event_fcts[SHIFTED_U]=LatShiftedU;
   event_fcts[SHIFTED_UD]=LatShiftedUd;
   event_fcts[FREED_BNDBUFS_U]=FreedBndbufsU;
   event_fcts[FREED_BNDBUFS_UD]=FreedBndbufsUd;
   event_fcts[COMPUTED_SWD]=LatComputedSwd;
   event_fcts[FREED_SW]=LatFreedSw;
   event_fcts[FREED_SWD]=LatFreedSwd;
   event_fcts[ASSIGNED_SWD2SW]=LatAssignedSwd2sw;
   event_fcts[INVERTED_SWD_E]=LatInvertedSwdE;
   event_fcts[INVERTED_SWD_O]=LatInvertedSwdO;
   event_fcts[FLIPPED_BC]=LatFlippedBc;
   event_fcts[FLIPPED_BCD]=LatFlippedBcd;
   event_fcts[CHANGED_QUARK_MASS]=LatChangedQuarkMass;
   event_fcts[CHANGED_CSW]=LatChangedCsw;
   event_fcts[ALLOCATED_MSAP_BUFS]=LatAllocatedMsapBufs;
   event_fcts[FREED_MSAP_BUFS]=LatFreedMsapBufs;
   event_fcts[NEW_DFLMODES]=LatNewDflmodes;      
   event_fcts[COMPUTED_ZOPNOHATDBLE]=LatComputedZopnohatdble;   
   event_fcts[COMPUTED_ZOPHATDBLE]=LatComputedZophatdble;
   event_fcts[COMPUTED_ZOPNOHAT]=LatComputedZopnohat;      
   event_fcts[COMPUTED_ZOPHAT]=LatComputedZophat;      
   event_fcts[FREED_ZOPS]=LatFreedZops;      
}

#endif
