aboutsummaryrefslogtreecommitdiff
path: root/ic-reals-6.3/base/realLib.c
blob: 15da22a2e925815067d88bc5da74a0066974549b (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*
 * 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 <stdlib.h>
#include <string.h>
#include "real.h"
#include "real-impl.h"

void initEmitDigit(void);
void initEmitSign(void);
void initEpsDel();
void initTmp();
void initStack();
void initForceMethodLookupTable();
void initStrategy();

int defaultForceCount = DEFAULT_FORCE_COUNT;
int forceDecUpperBound = FORCE_DEC_UPPER_BOUND;
int stackSize = STACK_SIZE * 1024;

/*
 * Initialization of the real library
 */
void
initRealBase()
{
	char *p;

	if ((p = getenv("ICR_DEFAULT_FORCE_COUNT")) != NULL)
		defaultForceCount = atoi(p);

	if ((p = getenv("ICR_FORCE_DEC_UPPER_BOUND")) != NULL)
		forceDecUpperBound = atoi(p);

	if ((p = getenv("ICR_STACK_SIZE")) != NULL)
		stackSize = atoi(p) * 1024;

#ifdef DAVINCI
	initDaVinci();
#endif

	initStack();
	initForceMethodLookupTable();
	initEmitDigit();
	initEmitSign();
	initTmp();
	initEpsDel();
	initStrategy();
}

/*
 * For a real x, this returns a new real which denotes the same value as
 * x, but where the new real is a stream (ie prefixed by a SignX or DigsX).
 * The only interesting cases are when the expression rooted at x is an
 * lft. In this case we create a new real object but also record in x
 * a link to the new real to avoid doing it again.
 */
Real
makeStream(Real x)
{
	Real r;
	DigsX *digsX;
	void force_To_DigsX_From_Vec();
	void force_To_DigsX_From_MatX_Entry();
	void force_To_DigsX_From_TenXY_Entry();
	void force_To_DigsX_From_Cls_Entry();
	void force_To_DigsX_From_Alt_Entry();

	/*
	 * When we want to make a stream from a vector or matrix, we
	 * must make a copy of the lft first since the LFT might be shared 
	 * and emitting a sign or digit changes the lft. For tensors,
	 * this is unnecessary as, while the tensor might be shared, 
	 * any consumer of the tensor can consume only the stream
	 * and changes to the original tensor are acceptable.
	 */
	switch (x->gen.tag.type) {
	case VECTOR :
		/*
		 * Check that we haven't already made a stream for this lft
		 */
		if (x->vec.strm == NULL) {
			
			/* create a copy of the lft */
			r = vector_Z(x->vec.vec[0], x->vec.vec[1]);

			/* to make a signed lft into a stream, we prefix it by a SignX */
			if (r->gen.tag.isSigned)
				x->vec.strm = (Real) allocSignX(r, SIGN_UNKN);

			/* for an unsigned lft, we prefix it by a DigsX */
			else {
				digsX = allocDigsX();
				digsX->force = force_To_DigsX_From_Vec;
				digsX->x = r;
#ifdef DAVINCI
				beginGraphUpdate();
				newEdgeToOnlyChild(digsX, r);
				endGraphUpdate();
#endif
				x->vec.strm = (Real) digsX;
			}
		}

		/*
		 * If daVinci is running then we draw a double line from the original
		 * real and its stream version to indicate that there are two
		 * expressions in the heap which point denote the same real value.
		 */
#ifdef DAVINCI
		beginGraphUpdate();
		drawEqEdge(x, x->vec.strm);
		endGraphUpdate();
#endif
		return x->vec.strm;
		break;

	/* this case is exactly the same as the above */
	case MATX :
		if (x->matX.strm == NULL) {
			r = matrix_Z(x->matX.x, x->matX.mat[0][0], x->matX.mat[0][1],
									x->matX.mat[1][0], x->matX.mat[1][1]);
			if (r->gen.tag.isSigned)
				x->matX.strm = (Real) allocSignX(r, SIGN_UNKN);
			else {
				digsX = allocDigsX();
				digsX->force = force_To_DigsX_From_MatX_Entry;
				digsX->x = r;
#ifdef DAVINCI
				beginGraphUpdate();
				newEdgeToOnlyChild(digsX, r);
				endGraphUpdate();
#endif
				x->matX.strm = (Real) digsX;
			}
		}
#ifdef DAVINCI
		beginGraphUpdate();
		drawEqEdge(x, x->matX.strm);
		endGraphUpdate();
#endif
		return x->matX.strm;
		break;

	/* same as for vectors */
	case TENXY :
		if (x->tenXY.strm == NULL) {
			r = tensor_Z(x->tenXY.x, x->tenXY.y,
					x->tenXY.ten[0][0], x->tenXY.ten[0][1],
					x->tenXY.ten[1][0], x->tenXY.ten[1][1],
					x->tenXY.ten[2][0], x->tenXY.ten[2][1],
					x->tenXY.ten[3][0], x->tenXY.ten[3][1]);
			if (r->gen.tag.isSigned)
				x->tenXY.strm = (Real) allocSignX(r, SIGN_UNKN);
			else {
				digsX = allocDigsX();
				digsX->force = force_To_DigsX_From_TenXY_Entry;
				digsX->x = r;
#ifdef DAVINCI
				beginGraphUpdate();
				newEdgeToOnlyChild(digsX, r);
				endGraphUpdate();
#endif
				x->tenXY.strm = (Real) digsX;
			}
#ifdef DAVINCI
			beginGraphUpdate();
			drawEqEdge(x, x->tenXY.strm);
			endGraphUpdate();
#endif
		}
		return x->tenXY.strm;
		break;

	case ALT :
		if (x->alt.tag.isSigned)
			r = (Real) allocSignX(x, SIGN_UNKN);
		else {
			r = (Real) allocDigsX();
			r->digsX.force = force_To_DigsX_From_Alt_Entry;
			r->digsX.x = x;
#ifdef DAVINCI
			beginGraphUpdate();
			newEdgeToOnlyChild(r, x);
			endGraphUpdate();
#endif
		}
		return r;
		break;

	case CLOSURE :
		if (x->cls.tag.isSigned)
			r = (Real) allocSignX(x, SIGN_UNKN);
		else {
			r = (Real) allocDigsX();
			r->digsX.force = force_To_DigsX_From_Cls_Entry;
			r->digsX.x = x;
#ifdef DAVINCI
			beginGraphUpdate();
			newEdgeToOnlyChild(r, x);
			endGraphUpdate();
#endif
		}
		return r;
		break;

	case DIGSX :
	case SIGNX :
		return x;

	default :
		Error(FATAL, E_INT, "makeStream",
					"trying to make a stream from a non-real");
		return NULL;
	}
}

/*
 * This creates a closure (typically a function which unfolds a tensor
 * chain).
 */
Cls *
allocCls(void (*force)(), void *userData)
{
	Cls *cls;

	if ((cls = (Cls *) malloc (sizeof(Cls))) == NULL) 
		Error(FATAL, E_INT, "allocCls", "malloc failed");

#ifdef DAVINCI
	newNodeId(cls);
#else
#ifdef TRACE
	newNodeId(cls);
#endif
#endif

	cls->tag.type = CLOSURE;
	cls->tag.dumped = FALSE;
	cls->tag.isSigned = FALSE;
	cls->force = force;
	cls->userData = userData;
	cls->redirect = NULL;

#ifdef DAVINCI
	beginGraphUpdate();
	newNode(cls, CLOSURE);
	endGraphUpdate();
#endif

	return cls;
}

/*
 * The next family of functions are simple arithmetic operations.
 */
Real
add_R_R(Real x, Real y)
{
	return tensor_Int(x, y, 0, 0, 1, 0, 1, 0, 0, 1);
}

Real
add_R_Int(Real x, int y)
{
	return matrix_Int(x, 1, 0, y, 1);
}

Real
add_R_QInt(Real x, int a, int b)
{
	return matrix_Int(x, b, 0, a, b);
}

Real
sub_R_R(Real x, Real y)
{
	return tensor_Int(x, y, 0, 0, 1, 0, -1, 0, 0, 1);
}

Real
sub_R_Int(Real x, int y)
{
	return matrix_Int(x, 1, 0, -y, 1);
}

Real
sub_R_QInt(Real x, int a, int b)
{
	return matrix_Int(x, b, 0, -a, b);
}

Real
sub_Int_R(int x, Real y)
{
	return matrix_Int(y, -1, 0, x, 1);
}

Real
sub_QInt_R(int a, int b, Real y)
{
	return matrix_Int(y, -b, 0, a, b);
}

Real
mul_R_R(Real x, Real y)
{
	return tensor_Int(x, y, 1, 0, 0, 0, 0, 0, 0, 1);
}

Real
mul_R_Int(Real x, int y)
{
	return matrix_Int(x, y, 0, 0, 1);
}

Real
mul_R_QInt(Real x, int a, int b)
{
	return matrix_Int(x, a, 0, 0, b);
}

Real
div_R_R(Real x, Real y)
{
	return tensor_Int(x, y, 0, 0, 1, 0, 0, 1, 0, 0);
}

Real
div_R_Int(Real x, int y)
{
	return matrix_Int(x, 1, 0, 0, y);
}

Real
div_Int_R(int x, Real y)
{
	return matrix_Int(y, 0, 1, x, 0);
}

Real
div_R_QInt(Real x, int a, int b)
{
	return matrix_Int(x, b, 0, 0, a);
}

Real
div_QInt_R(int a, int b, Real x)
{
	return matrix_Int(x, 0, b, a, 0);
}


/*
 * This creates a vector (a,b) and then prepends it by the characteristic
 * pair (c,n) and finally sets the sign to sign. This is used only for
 * debugging .. to set up a real argument for a function.
 */
Real
makeRealSignCNQInt(Sign sign, char *c, int n, int a, int b)
{
	DigsX *digsX;
	Real y;
	void force_To_DigsX_From_Vec();
	void force_To_DigsX_From_DigsX_Entry();

	y = (Real) vector_Int(a, b);

	if (n > 0) {
		digsX = allocDigsX();
		digsX->x = y;
		digsX->force = force_To_DigsX_From_Vec;

#ifdef DAVINCI
		beginGraphUpdate();
		newEdgeToOnlyChild(digsX, y);
		endGraphUpdate();
#endif

		y = (Real) digsX;

		digsX = allocDigsX();
		digsX->x = y;
		digsX->force = force_To_DigsX_From_DigsX_Entry;

#ifdef DAVINCI
		beginGraphUpdate();
		newEdgeToOnlyChild(digsX, y);
		endGraphUpdate();
#endif

		digsX->count = n;
#ifdef PACK_DIGITS
		if (n <= DIGITS_PER_WORD)
			digsX->word.small = atoi(c);
		else
			mpz_init_set_str(digsX->word.big, c, 10);
#else
		mpz_set_str(digsX->word.big, c, 10);
#endif
		return (Real) allocSignX((Real) digsX, sign);
	}
	else {
		switch (sign) {
		case SZERO :
		case SPOS :
		case SNEG :
		case SINF :
			digsX = allocDigsX();
			digsX->x = y;
			digsX->force = force_To_DigsX_From_Vec;

#ifdef DAVINCI
			beginGraphUpdate();
			newEdgeToOnlyChild(digsX, y);
			endGraphUpdate();
#endif

			digsX->count = 0;
			return (Real) allocSignX((Real) digsX, sign);

		case SIGN_UNKN :
			return (Real) allocSignX(y, sign);

		default :
			Error(FATAL, E_INT, "makeRealSignCNQInt", "invalid sign");
			return NULL;
		}
	}
}

/*
 * These are the new preferred names for the basic lft functions.
 * The other names will disappear in a future release.
 */
Real
real_QInt(int a, int b)
{
	return vector_Int(a, b);
}

Real
real_QZ(mpz_t a, mpz_t b)
{
	return vector_Z(a, b);
}

/*
 * These are the same as matrix_ functions but take their arguments
 * in a different order (row order rather than column order).
 */
Real
lft_R_Int(Real x, int a, int b, int c, int d)
{
	return matrix_Int(x, a, c, b, d);
}

Real
lft_R_Z(Real x, mpz_t a, mpz_t b, mpz_t c, mpz_t d)
{
	return matrix_Z(x, a, c, b, d);
}

/*
 * These are the same as tensor_ functions but take their arguments
 * in a different order (row order rather than column order).
 */
Real
lft_R_R_Int(Real x, Real y, int a, int b, int c, int d,
                            int e, int f, int g, int h)
{
	return tensor_Int(x, y, a, e, b, f, c, g, d, h);
}

Real
lft_R_R_Z(Real x, Real y, mpz_t a, mpz_t b, mpz_t c, mpz_t d,
                          mpz_t e, mpz_t f, mpz_t g, mpz_t h)
{
	return tensor_Z(x, y, a, e, b, f, c, g, d, h);
}