aboutsummaryrefslogtreecommitdiff
path: root/ic-reals-6.3/math-lib/sqrt_R.c~
blob: b4e3b18fbef3ca957eb1a7da013eee30847becb4 (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
/*
 * 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"

Real
sqrt_R(Real x)
{
	Bool xLtEq4, xGtEqOneFifth;
	Cls *in, *gtEq3, *ltEqOneQuarter;
	Real lt0;
	void force_To_DigsX_From_Sqrt_TenXY_Entry();
	void sqrtInside();
	void sqrtGtEq3();
	void sqrtLtEqOneQuarter();
	void sqrtLtEqOneQuarterCont();
	void force_To_DigsX_From_Sqrt_TenXY_Entry();
	void force_To_DigsX_From_Sqrt_TenXY_Cont();
	void force_To_DigsX_From_Sqrt_MatX_Entry();
	void force_To_DigsX_From_Sqrt_MatX_Cont();
	void force_To_DigsX_From_Sqrt_Reduce();
	void force_To_MatX_Until_Refining();
	static int doneInit = 0;
	Real sqrt_QZ(mpz_t, mpz_t);

	if (!doneInit) {
		registerForceFunc(force_To_DigsX_From_Sqrt_TenXY_Entry,
								"force_To_DigsX_From_Sqrt_TenXY_Entry", 3);
		registerForceFunc(force_To_DigsX_From_Sqrt_TenXY_Cont,
								"force_To_DigsX_From_Sqrt_TenXY_Cont", 3);
		registerForceFunc(force_To_DigsX_From_Sqrt_MatX_Entry,
								"force_To_DigsX_From_Sqrt_MatX_Entry", 3);
		registerForceFunc(force_To_DigsX_From_Sqrt_MatX_Cont,
								"force_To_DigsX_From_Sqrt_MatX_Cont", 3);
		registerForceFunc(force_To_DigsX_From_Sqrt_Reduce,
								"force_To_DigsX_From_Sqrt_Reduce", 2);
		registerForceFunc(force_To_MatX_Until_Refining,
								"force_To_MatX_Until_Refining", 2);
		registerForceFunc(sqrtInside, "sqrtInside", 2);
		registerForceFunc(sqrtGtEq3, "sqrtGtEq3", 2);
		registerForceFunc(sqrtLtEqOneQuarter, "sqrtLtEqOneQuarter", 2);
		registerForceFunc(sqrtLtEqOneQuarterCont, "sqrtLtEqOneQuarterCont", 2);
		doneInit++;
	}

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

	xLtEq4 = ltEq_R_QInt(x, 4, 1);
	xGtEqOneFifth = gtEq_R_QInt(x, 1, 5);

	in = allocCls(sqrtInside, (void *) x);
	in->tag.isSigned = FALSE;

	gtEq3 = allocCls(sqrtGtEq3, (void *) x);
	gtEq3->tag.isSigned = FALSE;

	ltEqOneQuarter = allocCls(sqrtLtEqOneQuarter, (void *) x);
	ltEqOneQuarter->tag.isSigned = FALSE;

	if (DAVINCI) {
		beginGraphUpdate();
		newEdgeToOnlyChild(in, x);
		newEdgeToOnlyChild(gtEq3, x);
		newEdgeToOnlyChild(ltEqOneQuarter, x);
		endGraphUpdate();
	}

	lt0 = realError("(sqrt_R x) and x < 0");

	/*
	 * The order of the tests in the realAlt is not semantically
	 * significant. Note that the sqrt_R is always unsigned since
	 * each case is unsigned.
	 */
	return realIf(4,
		and_B_B(xLtEq4, xGtEqOneFifth),	(Real) in,
		gtEq_R_QInt(x, 3, 1),			(Real) gtEq3,
		and_B_B(ltEq_R_QInt(x, 1, 4), gtEq_R_0(x)),	(Real) ltEqOneQuarter,
		lt_R_0(x),						lt0);
}

void
sqrtInside()
{
	Cls *cls;
	TenXY *tenXY;
	DigsX *digsX;
	void force_To_TenXY_X_Until_Refining();
	void force_To_DigsX_From_Sqrt_TenXY_Entry();
	Real x;

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

	digsX = allocDigsX();
	digsX->force = force_To_DigsX_From_Sqrt_TenXY_Entry;

	tenXY = (TenXY *) tensor_Int(x, (Real) digsX, 1, 0, 2, 1, 1, 2, 0, 1);
	tenXY->tag.isSigned = FALSE;

	digsX->x = (Real) tenXY;

	if (DAVINCI) {
		beginGraphUpdate();
		newEdgeToOnlyChild(digsX, tenXY);
		endGraphUpdate();
	}
	
	/*
	 * We must still absorb the sign (if any) and enough information 
	 * to ensure the tensor is refining. Note that, if we get to
	 * this point, then we know the argument is within the interval
	 * 1/5 >= x <= 4.  I claim that the n'th tensor for n >= 2
	 * can always be made refining by absorbing
	 * information from the left.
	 */
	PUSH_2(force_To_TenXY_X_Until_Refining, tenXY);

	if (tenXY->x->gen.tag.isSigned) 
		PUSH_2(tenXY->forceX, tenXY);

	cls->redirect = (Real) digsX;
	cls->userData = NULL;

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

void
sqrtGtEq3()
{
	Cls *cls;
	Real w, x;

	cls = (Cls *) POP;
	x = (Real) cls->userData;
	
	w = div_R_Int(x, 4);
	w = sqrt_R(w);
	cls->redirect = mul_R_Int(w, 2);
	cls->userData = NULL;

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

void
sqrtLtEqOneQuarter()
{
	Cls *cls;
	Real w, x;

	cls = (Cls *) POP;
	x = (Real) cls->userData;
	
	w = mul_R_Int(x, 4);
	w = sqrt_R(w);
	cls->redirect = div_R_Int(w, 2);
	cls->userData = NULL;

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

#ifdef LATER
void
sqrtLtEqOneQuarter()
{
	Cls *cls, *newCls;
	void sqrtLtEqOneQuarterCont();
	Real x;

	cls = (Cls *) POP;
	x = (Real) cls->userData;
	
	newCls = allocCls(sqrtLtEqOneQuarterCont, (void *) x);
	newCls->tag.isSigned = FALSE;
	cls->redirect = matrix_Int((Real) newCls, 0, 1, 1, 2);
	cls->userData = NULL;

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

void
sqrtLtEqOneQuarterCont()
{
	Cls *cls;
	Real w, x;
	void force_To_MatX_Until_Refining();

	cls = (Cls *) POP;
	x = (Real) cls->userData;
	
	/* w = matrix_Int(x, 0, 1, 1, 0);	 reciprocal of x */
	w = mul_R_Int(x, 4);
	w = sqrt_R(w);
	/* cls->redirect = matrix_Int(w, 1, 0, -2, 1); */
	cls->redirect = w;
	cls->redirect->gen.tag.isSigned = FALSE;
	cls->userData = NULL;

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

	PUSH_2(force_To_MatX_Until_Refining, cls->redirect);
}
	
void
force_To_DigsX_From_Sqrt_TenXY_Entry()
{
	DigsX *digsX;
	TenXY *tenXY;
	int digitsNeeded;
	void force_To_DigsX_From_Sqrt_TenXY_Cont();
	void force_To_DigsX_From_DigsX_Entry();
	void force_To_DigsX_From_Sqrt_Reduce();

	digsX = (DigsX *) POP;
	digitsNeeded = (int) POP;
	tenXY = (TenXY *) digsX->x;

	/*
	 * The current strategy is that when forced, we create a list
	 * of DigsX structures rather than a single struct with the
	 * requested number of digits. So, after we are done emitting,
	 * we arrange to reduce the list at the end.
	 *
	 * A better strategy would be to allocate two DigsX structures at the
	 * very start. The second one would be private and only visible to the sqrt
	 * closure. Then the cycle would be:
	 *	emit from tensor into DigsX-2
	 *	absorb DigsX-2 digits into tensor
	 *	absorb DigsX-2 digits into DigsX-1
	 *	clear DigsX-2 of all digits.
	 *
	 * This would avoid allocating a chain of DigsX structures and the
	 * need for reducing at the end.
	PUSH_3(force_To_DigsX_From_DigsX_Entry, digsX, digitsNeeded);
	 */
	PUSH_2(force_To_DigsX_From_Sqrt_Reduce, digsX);
	PUSH_3(force_To_DigsX_From_Sqrt_TenXY_Cont, digsX, digitsNeeded);
}

void
force_To_DigsX_From_Sqrt_TenXY_Cont()
{
	DigsX *digsX;
	TenXY *tenXY;
	int digitsNeeded;
	int nX;
	int digitsEmitted, bitsShifted;
	int epsDelTensorX(Tensor, int);
	bool emitDigitFromTensor(Tensor, Digit *);

	digsX = (DigsX *) POP;
	digitsNeeded = (int) POP;
	tenXY = (TenXY *) digsX->x;

	digitsEmitted = emitDigits(digsX,
								(edf) emitDigitFromTensor,
								(void *) tenXY->ten,
								digitsNeeded);

	if (digitsEmitted > 0)
		bitsShifted = normalizeTensor(tenXY->ten);

	if (TRACE) {
		debugp("force_To_DigsX_From_Sqrt_TenXY_Cont",
				"%x %x emitted=%d shifted=%d\n",
				(unsigned) digsX,
				(unsigned) tenXY,
				digitsEmitted,
				bitsShifted);
	}

	digitsNeeded -= digitsEmitted;

	if (digsX->count > 0)
		newDigsX(digsX);

	if (digitsNeeded <= 0)
		return;

	/*
	 * So now we emitted what we can but still need more. First arrange
	 * to come back and try to emit again after forcing the necessary
	 * number of digits from the the argument.
	 */
	if (digsX->count > 0)
		PUSH_3(force_To_DigsX_From_Sqrt_TenXY_Cont, digsX->x, digitsNeeded);
	else
		PUSH_3(force_To_DigsX_From_Sqrt_TenXY_Cont, digsX, digitsNeeded);

	/*
	 * Now absorb everything emitted into the tensor.
	 */
	absorbDigsXIntoTenXY_Y(tenXY);

	nX = epsDelTensorX(tenXY->ten, digitsNeeded);

	if (TRACE) {
		debugp("force_To_DigsX_From_Sqrt_TenXY_Cont",
				"%x %x nX=%d\n",
				(unsigned) digsX,
				(unsigned) tenXY,
				nX);
	}

	if (nX > 0)
		PUSH_3(tenXY->forceX, tenXY, nX);
	else
		PUSH_3(tenXY->forceX, tenXY, 1);
}

void
force_To_DigsX_From_Sqrt_MatX_Entry()
{
	DigsX *digsX;
	MatX *matX;
	int digitsNeeded;
	void force_To_DigsX_From_Sqrt_MatX_Cont();
	void force_To_DigsX_From_DigsX_Entry();
	void force_To_DigsX_From_Sqrt_Reduce();

	digsX = (DigsX *) POP;
	digitsNeeded = (int) POP;
	matX = (MatX *) digsX->x;

/*
	PUSH_3(force_To_DigsX_From_DigsX_Entry, digsX, digitsNeeded);
*/
	PUSH_2(force_To_DigsX_From_Sqrt_Reduce, digsX);
	PUSH_3(force_To_DigsX_From_Sqrt_MatX_Cont, digsX, digitsNeeded);
}

/*
 * This is not used at this stage. This will be used when reduction is
 * performed after the tensor is first allocated and when the tensor
 * reduces to a matrix.
 */
void
force_To_DigsX_From_Sqrt_MatX_Cont()
{
	DigsX *digsX;
	MatX *matX;
	int digitsNeeded;
	int nX;
	int digitsEmitted, bitsShifted;
	bool emitDigitFromMatrix(Matrix, Digit *);

	digsX = (DigsX *) POP;
	digitsNeeded = (int) POP;
	matX = (MatX *) digsX->x;

	digitsEmitted = emitDigits(digsX,
								(edf) emitDigitFromMatrix,
								(void *) matX->mat,
								digitsNeeded);

	if (digitsEmitted > 0)
		bitsShifted = normalizeMatrix(matX->mat);

	if (TRACE) {
		debugp("force_To_DigsX_From_Sqrt_MatX_Cont",
				"%x %x emitted=%d shifted=%d\n",
				(unsigned) digsX,
				(unsigned) matX,
				digitsEmitted,
				bitsShifted);
	}

	digitsNeeded -= digitsEmitted;

	if (digsX->count > 0)
		newDigsX(digsX);

	if (digitsNeeded <= 0)
		return;

	/*
	 * Now absorb everything emitted into the matrix.
	 */
	absorbDigsXIntoMatX(matX);

	/*
	 * So now we emitted what we can but still need more. First arrange
	 * to come back and try to emit again after forcing the necessary
	 * number of digits from the the argument.
	 */
	if (digsX->count > 0)
		PUSH_3(force_To_DigsX_From_Sqrt_MatX_Cont, digsX->x, digitsNeeded);
	else
		PUSH_3(force_To_DigsX_From_Sqrt_MatX_Cont, digsX, digitsNeeded);
}

/*
 * In some cases when we generate a matrix which is not refining, but
 * where we no that the argument of the matrix is constrained in such a
 * way that after a finite amount of absorption, it will become refining.
 * What we do is force information from the argument until the matrix is
 * refining.
 */
void
force_To_MatX_Until_Refining()
{
	MatX *matX;
	int sgn;

	matX = (MatX *) POP;

	if (matX->tag.type != MATX)
		return;

	sgn = matrixSign(matX->mat);
	
	if (sgn > 0)		/* matrix is refining and entries positive */
		return;

	if (sgn < 0) {	  /* matrix is refining and entries negative */
		negateMatrix(matX->mat);
		return;
	}

	PUSH_2(force_To_MatX_Until_Refining, matX);
	PUSH_3(matX->force, matX, 1);
}

void
force_To_DigsX_From_Sqrt_Reduce()
{
	DigsX *digsX;

	digsX = (DigsX *) POP;
	reduceDigsXList(digsX);
}