/************************************************************************
 *                                                                    	*
 *                       Copyright (c) 1989, 1990                       *
 *                     MasPar Computer Corporation                      *
 *                         All Rights Reserved                     	*
 *                                                                	*
 * This program contains proprietary and trade secret information of	*
 * MasPar Computer Corporation and has been licensed subject to 	*
 * restrictions on reproduction, use and disclosure.  The copyright 	*
 * notice does not imply that the program or the related ideas or 	*
 * algorithms are in the public domain.                                 *
 ************************************************************************/
#include <mpl.h>

#define nowrapXN(d,val)	((iyproc < d) ? 0 : xnetN[d].val)
#define nowrapXS(d,val)	((iyproc >= nyproc-d) ? 0 : xnetS[d].val)
#define nowrapXW(d,val)	((ixproc < d) ? 0 : xnetW[d].val)
#define nowrapXE(d,val)	((ixproc >= nxproc-d) ? 0 : xnetE[d].val)




/* avg() averages each pixel with the values of it's 8 neighbors
 *   (N, NE, E, SE, S, SW, W, NW), using toroidal wraparound for
 *   processors that are on an edge of the PE array (i.e., proc[0][0]
 *   has as its 8 neighbors proc[nyproc-1][0], proc[nyproc-1][1],
 *   proc[0][1], proc[1][1], proc[1][0], proc[1][nxproc-1], proc[0][nxproc-1],
 *   and proc[nyproc-1][nxproc-1]).
 * It turns out that only 4 xnet's are necessary since the first line
 *   accumulates the NW and NE values into the N neighbor and the
 *   SW and SE values into the S neighbor.
 */
plural int
avg(src)
register plural int src;
{
    src += xnetW[1].src + xnetE[1].src;
    src += xnetN[1].src + xnetS[1].src;

    return(src/9);
}




/* nowrapAvg() averages the each pixel with the values of it's 8 neighbors
 *   (N, NE, E, SE, S, SW, W, NW), but doesn't wrap around the PE Array
 *   to access it's neighbors values (i.e., proc[0] only has as its
 *   neighbors proc[1][0], proc[1][1], and proc[0][1]).
 * The same algorithm works here as for avg().
 */
plural int
nowrapAvg(src)
register plural int src;
{
    src += nowrapXW(1,src) + nowrapXE(1,src);
    src += nowrapXN(1,src) + nowrapXS(1,src);

    return(src/9);
}
