aboutsummaryrefslogtreecommitdiff
path: root/ic-reals-6.3/math-lib/exp_R.c~
blob: 9723dec0cff1c2324842b19fcd17517763082871 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * Copyright (C) 2000, Imperial College
 *
 * This file is part of the Imperial College Exact Real Arithmetic Library.
 * See the copyright notice included in the distribution for conditions
 * of use.
 */

#include <stdio.h>
#include "real.h"
#include "real-impl.h"
#include "math-lib.h"

static TenXY *nextTensor(Real, Real, int);
static void expInside();
static void expOutside();

Real
exp_R(Real x)
{
	Real u, v;
	Bool xLtEq1, xGtEqNeg1, xLtEq1_and_GtEqNeg1;
	static int doneInit = 0;

	if (!doneInit) {
		registerForceFunc(expInside, "expInside", 2);
		registerForceFunc(expOutside, "expOutside", 2);
		doneInit++;
	}

	/*
	if (x->gen.tag.type == VECTOR)
		return exp_QZ(x->vec.vec[0], x->vec.vec[1]);
	*/

	xLtEq1 = ltEq_R_QInt(x, 1, 1);
	xGtEqNeg1 = gtEq_R_QInt(x, -1, 1);
	xLtEq1_and_GtEqNeg1 = and_B_B(xLtEq1, xGtEqNeg1);
		
	u = (Real) allocCls(expInside, (void *) x);
	u->cls.tag.isSigned = TRUE;
	v = (Real) allocCls(expOutside, (void *) x);
	v->cls.tag.isSigned = TRUE;
	
	if (DAVINCI) {
		beginGraphUpdate();
		newEdgeToOnlyChild(u, x);
		newEdgeToOnlyChild(v, x);
		endGraphUpdate();
	}

	/*
	 * The order of the tests in the alt is not semantically
	 * significant. The tests are applied in order, so there is a
	 * very modest performance improvement by putting the -1,1 tests
	 * before the 999/1000,-999/1000 tests.  Also there is a very
	 * small win by doing the negative test before the positive test.
	 */
	return realIf(4,
		not_B(xLtEq1_and_GtEqNeg1),   v,
		xLtEq1_and_GtEqNeg1,			u,
		gtEq_R_QInt(x, 999, 1000),		v,
		ltEq_R_QInt(x, -999, 1000),		v);
}

static TenXY *
nextTensor(Real x, Real y, int n)
{
    return (TenXY *) tensor_Int(x, y, -1, 0, 0, 1, 2*n+1, 2*n+1, 2*n+1, 2*n+1);
}

static void
expInside()
{
	Cls *cls, *newCls;
	Real x, exp_x;
	ClsData *data;
	void stdTensorCont();

	cls = (Cls *) POP;
	x = (Real) cls->userData;
	
	if ((data = (ClsData *) malloc(sizeof(ClsData))) == NULL)
		Error(FATAL, E_INT, "exp_R", "malloc failed");

	data->n = 1;
	data->x = x;
	data->nextTensor = nextTensor;

	newCls = allocCls(stdTensorCont, (void *) data);
	newCls->tag.isSigned = FALSE;

	cls->redirect = tensor_Int(x, (Real) newCls, 0, -1, 1, 0, 1, 1, 1, 1);

	if (DAVINCI) {
		beginGraphUpdate();
		newEdgeToOnlyChild(newCls, x);
		drawEqEdge(cls, cls->redirect);
		endGraphUpdate();
	}
}

static void
expOutside()
{
	Cls *cls;
	Real u, v, w, x;
	Bool xLtEq1, xGtEqNeg1, xLtEq1_and_GtEqNeg1; 

	cls = (Cls *) POP;
	x = (Real) cls->userData;
	x = div_R_Int(x, 2);

	w = exp_R(x);

	cls->redirect = mul_R_R(w, w);

	if (DAVINCI) {
		beginGraphUpdate();
		drawEqEdge(cls, cls->redirect);
		endGraphUpdate();
	}
}