function  [info,icscmx]=optstp(n,xp,f,g,x,itncnt,icscmx,gradtl,steptl,sx, ..
                               fscale,maxits,iretcd,mxtake)
info=0;
if iretcd==1;    % last global step failed to locate a point lower than x
  info=3;   
  return
end;

%
% FIND DIRECTION IN WHICH RELATIVE GRADIENT MAXIMUM.
% CHECK WHETHER WITHIN TOLERANCE
%
d=max(abs(f),fscale);
relgrad=abs(g).*max(abs(x),ones(n,1)./sx)/d;
rgx=max(0,relgrad);
if rgx<=gradtl; 
  info=1;
  return; 
end;
if itncnt==0;
  info=0;
  return;
end;
%
% FIND DIRECTION IN WHICH RELATIVE STEPSIZE MAXIMUM
% CHECK WHETHER WITHIN TOLERANCE.
%
relstp=abs(xp-x)./max(abs(xp),ones(n,1)./sx);
rsx=max(0,relstp);
if rsx<=steptl;
  info=2;
  return;
end;
%
% CHECK ITERATION LIMIT
%
if itncnt>=maxits;
  info=4;
  return;
end;

%
% CHECK NUMBER OF CONSECUTIVE STEPS \ STEPMX
%
if ~mxtake;
  icscmx=0;
  return;
else;
  icscmx=icscmx+1;
  if icscmx<5;
    return;
  end;
  info=5;
end;
