/* fetlim.f -- translated by f2c (version of 3 February 1990  3:36:42).
   You must link the resulting object file with the libraries:
	-lF77 -lI77 -lm -lc   (in that order)
*/

#include "f2c.h"

/*<       subroutine fetlim(vnew,vold,vto) >*/
/* Subroutine */ int fetlim_(vnew, vold, vto)
doublereal *vnew, *vold, *vto;
{
    /* System generated locals */
    doublereal d_1, d_2;

    /* Local variables */
    static doublereal delv, vtox, vtemp, vtsthi, vtstlo;

/*<       implicit double precision (a-h,o-z) >*/

/*     this routine limits the per-iteration change of fet voltages. */


/*      three regions of operation are identified: */

/*                  v < vto        definitely off */
/*            vto <= v <= vto+3.5d0    off or on depending on vbs */
/*        vto+3.5d0 < v              definitely on */

/*<       vtsthi=dabs(2.0d0*(vold-vto))+2.0d0 >*/
    vtsthi = (d_1 = (*vold - *vto) * 2., abs(d_1)) + 2.;
/*<       vtstlo=vtsthi/2.0d0+2.0d0 >*/
    vtstlo = vtsthi / 2. + 2.;
/*<       vtox=vto+3.5d0 >*/
    vtox = *vto + 3.5;
/*<       delv=vnew-vold >*/
    delv = *vnew - *vold;

/*<       if (vold.lt.vto) go to 300 >*/
    if (*vold < *vto) {
	goto L300;
    }
/*<       if (vold.lt.vtox) go to 200 >*/
    if (*vold < vtox) {
	goto L200;
    }

/*  on ... */

/*<       if (delv.gt.0.0d0) go to 120 >*/
    if (delv > 0.) {
	goto L120;
    }
/* ...  going off */
/*<       if (vnew.lt.vtox) go to 110 >*/
    if (*vnew < vtox) {
	goto L110;
    }
/*<       if (-delv.le.vtstlo) go to 500 >*/
    if (-delv <= vtstlo) {
	goto L500;
    }
/*<       vnew=vold-vtstlo >*/
    *vnew = *vold - vtstlo;
/*<       go to 500 >*/
    goto L500;
/*<   110 vnew=dmax1(vnew,vto+2.0d0) >*/
L110:
/* Computing MAX */
    d_1 = *vnew, d_2 = *vto + 2.;
    *vnew = max(d_2,d_1);
/*<       go to 500 >*/
    goto L500;
/* ...  staying on */
/*<   120 if (delv.lt.vtsthi) go to 500 >*/
L120:
    if (delv < vtsthi) {
	goto L500;
    }
/*<       vnew=vold+vtsthi >*/
    *vnew = *vold + vtsthi;
/*<       go to 500 >*/
    goto L500;

/*  middle region ... */

/*<   200 if (delv.gt.0.0d0) go to 210 >*/
L200:
    if (delv > 0.) {
	goto L210;
    }
/* ...  decreasing */
/*<       vnew=dmax1(vnew,vto-0.5d0) >*/
/* Computing MAX */
    d_1 = *vnew, d_2 = *vto - .5;
    *vnew = max(d_2,d_1);
/*<       go to 500 >*/
    goto L500;
/* ...  increasing */
/*<   210 vnew=dmin1(vnew,vto+4.0d0) >*/
L210:
/* Computing MAX */
    d_1 = *vnew, d_2 = *vto + 4.;
    *vnew = min(d_2,d_1);
/*<       go to 500 >*/
    goto L500;

/*  off ... */

/*<   300 if (delv.gt.0.0d0) go to 310 >*/
L300:
    if (delv > 0.) {
	goto L310;
    }
/*<       if (-delv.le.vtsthi) go to 500 >*/
    if (-delv <= vtsthi) {
	goto L500;
    }
/*<       vnew=vold-vtsthi >*/
    *vnew = *vold - vtsthi;
/*<       go to 500 >*/
    goto L500;
/*<   310 vtemp=vto+0.5d0 >*/
L310:
    vtemp = *vto + .5;
/*<       if (vnew.gt.vtemp) go to 320 >*/
    if (*vnew > vtemp) {
	goto L320;
    }
/*<       if (delv.le.vtstlo) go to 500 >*/
    if (delv <= vtstlo) {
	goto L500;
    }
/*<       vnew=vold+vtstlo >*/
    *vnew = *vold + vtstlo;
/*<       go to 500 >*/
    goto L500;
/*<   320 vnew=vtemp >*/
L320:
    *vnew = vtemp;

/*  finished */

/*<   500 return >*/
L500:
    return 0;
/*<       end >*/
} /* fetlim_ */

