/*
 * Copyright (1987) Jeff Elman.  University of California, San Diego
 * This software may be redistributed without charge; this notice
 * should be preserved.
 */

/*
 * exp.c
 *
 * Do the exp table.  Instead of using the exponent call
 * which is slow, we use a huge table.
 *
 */

#include <stdio.h>
#include <math.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "defs.h"

float	*exp_array;
float	exp_mult;
long	exp_add;

double
exp_table(f)
	double	f;
{
	return exp_array[((long) (f * exp_mult)) + exp_add];
}

exp_init()
{
	extern	char *malloc();
	extern	char exp_path[];
	struct	stat statb;
	int	fd;

	fd = open(exp_path, O_RDONLY, 0);
	if (fd < 0) {
		perror("exp_table");
		exit(1);
	}
	fstat(fd, &statb);
	exp_add = (statb.st_size / sizeof(float)) / 2L;
	exp_mult = (float) (exp_add / 16.0);
	exp_array = (float *)malloc((u_int)statb.st_size);
	if (read(fd, exp_array, statb.st_size) != statb.st_size) {
		perror("read exp array");
		exit(1);
	}
	if (exp_array[0] < 0 || exp_array[0] > 0.01) {
		fprintf(stderr, "exp_init: warning; exp table is bad\n");
		exit(0);
	}
}
