      subroutine sxcbld(ilast,n,nmaxpoints,nxcfunctions,npoints,iwkvec,
     &                  dlarge,cutoff,saux,extent,g,gw,weight,wkvec)

c  this subroutine written by alain st-amant of the department of
c  pharmaceutical chemistry, university of california, san francisco.
c  all rights reserved.  this is part of the DeFT project.

      implicit real*8(a-h,o-z)

      dimension iwkvec(*)

      dimension saux(n,*),extent(*),g(nmaxpoints,*),gw(*),weight(*),
     &          wkvec(*)

      ihits=0

      do 1001 i=1,nxcfunctions
      if(extent(i).gt.cutoff) then

                   ihits=ihits+1
                   iwkvec(ihits)=i
                   factor=extent(i)*dlarge

                   do 1002 k=1,npoints
 1002              gw(k)=g(k,i)*weight(k)

                   do 1003 j=1,ihits
                   jj=iwkvec(j)
                   if(factor*extent(jj).gt.cutoff) then

                                    do 1004 k=1,npoints
 1004                               saux(jj,i)=saux(jj,i)+g(k,jj)*gw(k)

                                                   endif
 1003              continue

                              endif
 1001 continue

      if(ilast.eq.1) then
                           nloop=0

                           do 1005 i=1,nxcfunctions
                           do 1005 j=1,i
                           nloop=nloop+1
 1005                      wkvec(nloop)=saux(j,i)
                     endif
      
      return
      end
