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

/*
 * This computes the absolute value of a real. For now the argument
 * must be either a vector or a stream.
 */
Real
abs_R(Real x)
{
	Real r;
	Cls *cls;
	SignX *signX;
	int sign_a, sign_b;
	void force_To_SignX_From_Abs_Entry();
	void force_To_SignX_From_Abs_Cont();
	void force_To_DigsX_From_Abs_Entry();
	void force_To_DigsX_From_Abs_Cont();
	void force_To_DigsX_From_Abs_Copy_Entry();
	void force_To_DigsX_From_Abs_Copy_Cont();
	void force_To_DigsX_From_Abs_Copy_and_Negate();
	void force_To_DigsX_From_Abs_Copy_and_Negate_Entry();
	void force_To_DigsX_From_Abs_Copy_and_Negate_Cont();
	static int doneInit = 0;

	if (!doneInit) {
		registerForceFunc(force_To_SignX_From_Abs_Entry,
							"force_To_SignX_From_Abs_Entry", 2);
		registerForceFunc(force_To_SignX_From_Abs_Cont,
							"force_To_SignX_From_Abs_Cont", 2);
		registerForceFunc(force_To_DigsX_From_Abs_Entry,
							"force_To_DigsX_From_Abs_Entry", 3);
		registerForceFunc(force_To_DigsX_From_Abs_Cont,
							"force_To_DigsX_From_Abs_Cont", 3);
		registerForceFunc(force_To_DigsX_From_Abs_Copy_Entry,
							"force_To_DigsX_From_Abs_Copy_Entry", 3);
		registerForceFunc(force_To_DigsX_From_Abs_Copy_Cont,
							"force_To_DigsX_From_Abs_Copy_Cont", 3);
		registerForceFunc(force_To_DigsX_From_Abs_Copy_and_Negate_Entry,
							"force_To_DigsX_From_Abs_Copy_and_Negate_Entry", 3);
		registerForceFunc(force_To_DigsX_From_Abs_Copy_and_Negate_Cont,
							"force_To_DigsX_From_Abs_Copy_and_Negate_Cont", 3);
		doneInit++;
	}

	/*
	 * A slight optimization. In the case of a vector we just inspect the 
	 * entries and then make them both the same sign (in a copy of the vector).
	 */
	if (x->gen.tag.type == VECTOR) {
		sign_a = mpz_sgn(x->vec.vec[0]);
		sign_b = mpz_sgn(x->vec.vec[1]);
		if (sign_a * sign_b < 0) {
			r = vector_Z(x->vec.vec[0], x->vec.vec[1]);
			mpz_neg(r->vec.vec[0], r->vec.vec[0]);
			return r;
		}
		else
			return x;
	}

	/*
	 * For all other argument types, we simply work with the stream
	 * representation of the argument.
	 */
	if (x->gen.tag.isSigned) {
		r = makeStream(x);	/* this will make a signed stream */

		/*
		 * All the work is done in the closure.
		 */
		cls = allocCls(force_To_SignX_From_Abs_Entry, (void *) r);
#ifdef DAVINCI
			beginGraphUpdate();
			newEdgeToOnlyChild(cls, r);
			endGraphUpdate();
#endif
		signX = allocSignX((Real) cls, SIGN_UNKN);
		signX->force = force_To_SignX_From_Abs_Entry;
	}
	else
		return x;
	return NULL;
}

/*
 * In spite of the fact that the output is positive, we don't
 * really know the sign of abs_R(x) until we get the sign of x.
 */
void
force_To_SignX_From_Abs_Entry()
{
	SignX *signX, *source;
	Cls *cls;
	void force_To_SignX_From_Abs_Cont();

	signX = (SignX *) POP;
	cls = (Cls *) signX->x;
	source = (SignX *) cls->userData;

	PUSH_2(force_To_SignX_From_Abs_Cont, signX);

	if (source->tag.value == SIGN_UNKN)
		PUSH_2(source->force, source);
}

/*
 * When this gets activated, then we know the sign of the argument
 * has been determined. The sign of the output is the same as the argument
 * unless the argument is SNEG in which case we switch to SPOS.
 */
void
force_To_SignX_From_Abs_Cont()
{
	SignX *signX;
	Cls *cls;
	SignX *source;
	void force_To_DigsX_From_Abs_Entry();

	signX = (SignX *) POP;
	cls = (Cls *) signX->x;
	source = (SignX *) cls->userData;

	/*
	 * This allocates an empty DigsX structure between the sign and the
	 * closure. That's where we will copy digits to eventually.
	 */
	introDigsX(signX);
	signX->x->digsX.force = force_To_DigsX_From_Abs_Entry;

	switch (source->tag.value) {
	case SPOS :
	case SZERO :
	case SINF :
		signX->tag.value = source->tag.value;
		break;
	case SNEG :
		signX->tag.value = SPOS; /* the only case where sign is changed */
		break;
	case SIGN_UNKN :
		Error(FATAL, E_INT, "force_To_SignX_From_Abs_Cont", "sign not set");
		return;
	default :
		Error(FATAL, E_INT, "force_To_SignX_From_Abs_Cont", "bad sign");
		return;
	}

	/*
	 * Now we skip over the sign of the argument. It has been ``absorbed''.
	 * But we keep a copy of the value for future use.
	 */
	cls->tag.value = source->tag.value;
	cls->userData = (void *) source->x;

#ifdef DAVINCI
		beginGraphUpdate();
		deleteOnlyEdge(cls, source);
		newEdgeToOnlyChild(cls, source->x);
		endGraphUpdate();
#endif
}

void
force_To_DigsX_From_Abs_Entry()
{
	DigsX *target, *source;
	Cls *cls;
	int digitsNeeded;
	void force_To_DigsX_From_Abs_Cont();

	target = (DigsX *) POP;
	digitsNeeded = (int) POP;
	cls = (Cls *) target->x;
	source = (DigsX *) cls->userData;

	PUSH_3(force_To_DigsX_From_Abs_Cont, target, digitsNeeded);

	/*
	 * Now see if the source has the number of digits we need. If not,
	 * then force the remaining.
	 */
	if (source->count < (unsigned int)digitsNeeded)
		PUSH_3(source->force, source, digitsNeeded - source->count);
}

/*
 * The is the function which translates one stream of digits into 
 * another. The rewrite rules are as follows depending on the sign of
 * the argument x.
 *   SZERO : if the word in the characteristic pair is negative, then
 *         the argument is negative and we must negate the word and every
 *         word in subsequent characteristic pairs.
 *   SINF : if the word in the characteristic pair is positive, then
 *         the argument is negative and we must negate the word and every
 *         word in subsequent characteristic pairs.
 *   SNEG : the number is negative, so we must negate the word of every
 *         characteristic pair.
 *   SPOS : just copy characteristic pairs from the source to the target.
 *         There *might* be an opportunity to optimize things by just
 *         short circuiting around the closure. Needs thought.
 * The code is more or less copied from the force methods for DigsX structures
 * with the negation stuff thrown in along the way.
 */
void
force_To_DigsX_From_Abs_Cont()
{
	DigsX *target, *source;
	Cls *cls;
	int digitsNeeded;
	void force_To_DigsX_From_Abs_Copy_Entry();
	void force_To_DigsX_From_Abs_Copy_and_Negate_Entry();

	target = (DigsX *) POP;
	digitsNeeded = (int) POP;
	cls = (Cls *) target->x;
	source = (DigsX *) cls->userData;

	if (source->count > 0) {
		switch (cls->tag.value) {
		case SZERO :
#ifdef OPTIM
			if (source->count <= DIGITS_PER_WORD) {
				if (source->word.small < 0)
					cls->force = force_To_DigsX_From_Abs_Copy_and_Negate_Entry;
				else
					cls->force = force_To_DigsX_From_Abs_Copy_Entry;
			}
			else {
#endif
				if (mpz_sgn(source->word.big) < 0)
					cls->force = force_To_DigsX_From_Abs_Copy_and_Negate_Entry;
				else
					cls->force = force_To_DigsX_From_Abs_Copy_Entry;
#ifdef OPTIM
			}
#endif
			break;

		case SINF:
#ifdef OPTIM
			if (source->count <= DIGITS_PER_WORD) {
				if (source->word.small > 0)
					cls->force = force_To_DigsX_From_Abs_Copy_and_Negate_Entry;
				else
					cls->force = force_To_DigsX_From_Abs_Copy_Entry;
			}
			else {
#endif
				if (mpz_sgn(source->word.big) > 0)
					cls->force = force_To_DigsX_From_Abs_Copy_and_Negate_Entry;
				else
					cls->force = force_To_DigsX_From_Abs_Copy_Entry;
#ifdef OPTIM
			}
#endif
			break;

		case SNEG:
			cls->force = force_To_DigsX_From_Abs_Copy_and_Negate_Entry;
			break;

		case SPOS:
			cls->force = force_To_DigsX_From_Abs_Copy_Entry;
			break;

		default :
			Error(FATAL, E_INT, "force_To_DigsX_From_Abs_Cont",
					"bad sign");
		}
		target->force = cls->force;
		PUSH_3(target->force, target, digitsNeeded);
	}
}

void
force_To_DigsX_From_Abs_Copy_and_Negate_Entry()
{
	DigsX *target, *source;
	Cls *cls;
	int digitsNeeded;
	void force_To_DigsX_From_Abs_Copy_and_Negate_Cont();

	target = (DigsX *) POP;
	digitsNeeded = (int) POP;
	cls = (Cls *) target->x;
	source = (DigsX *) cls->userData;

	PUSH_3(force_To_DigsX_From_Abs_Copy_and_Negate_Cont, target, digitsNeeded);

	/*
	 * See if the source has the number of digits we need. If not,
	 * then force the remaining.
	 */
	if (source->count < (unsigned int)digitsNeeded)
		PUSH_3(source->force, source, digitsNeeded - source->count);
}

void
force_To_DigsX_From_Abs_Copy_and_Negate_Cont()
{
	DigsX *target, *source;
	Cls *cls;
	int digitsNeeded;

	target = (DigsX *) POP;
	digitsNeeded = (int) POP;
	cls = (Cls *) target->x;
	source = (DigsX *) cls->userData;

#ifdef OPTIM
	if (target->count + source->count <= DIGITS_PER_WORD)
			target->word.small = (target->word.small << source->count)
										- source->word.small;
	else {
		if (target->count <= DIGITS_PER_WORD)
			mpz_init_set_si(target->word.big, target->word.small);
#endif
		mpz_mul_2exp(target->word.big, target->word.big, source->count);
#ifdef OPTIM
		if (source->count <= DIGITS_PER_WORD) {
			if (source->word.small >= 0)
				mpz_sub_ui(target->word.big, target->word.big,
												source->word.small);
			else
				mpz_add_ui(target->word.big, target->word.big,
												-(source->word.small));
		}
		else
#endif
			mpz_sub(target->word.big, target->word.big, source->word.big);
#ifdef OPTIM
	}
#endif

	target->count += source->count;

#ifdef TRACE
	if (TRACE) {
		debugp("force_To_DigsX_From_Abs_Copy_and_Negate",
				"%x %x emitted=%d\n",
				(unsigned) target,
				(unsigned) source,
				source->count);
	}
#endif
	/*
	 * We've consumed the source so advance to the next possible source
	 * of information
	 */
	cls->userData = (void *) source->x;

#ifdef DAVINCI
		beginGraphUpdate();
		deleteOnlyEdge(cls, source);
		newEdgeToOnlyChild(cls, source->x);
		endGraphUpdate();
#endif
	newDigsX(target);
}

void
force_To_DigsX_From_Abs_Copy_Entry()
{
	DigsX *target, *source;
	Cls *cls;
	int digitsNeeded;
	void force_To_DigsX_From_Abs_Copy_Cont();

	target = (DigsX *) POP;
	digitsNeeded = (int) POP;
	cls = (Cls *) target->x;
	source = (DigsX *) cls->userData;

	PUSH_3(force_To_DigsX_From_Abs_Copy_Cont, target, digitsNeeded);

	/*
	 * See if the source has the number of digits we need. If not,
	 * then force the remaining.
	 */
	if (source->count < (unsigned int)digitsNeeded)
		PUSH_3(source->force, source, digitsNeeded - source->count);
}

void
force_To_DigsX_From_Abs_Copy_Cont()
{
	DigsX *target, *source;
	Cls *cls;
	int digitsNeeded;

	target = (DigsX *) POP;
	digitsNeeded = (int) POP;
	cls = (Cls *) target->x;
	source = (DigsX *) cls->userData;

#ifdef OPTIM
	if (target->count + source->count <= DIGITS_PER_WORD)
		target->word.small = (target->word.small << source->count)
										+ source->word.small;
	else {
		if (target->count <= DIGITS_PER_WORD)
			mpz_init_set_si(target->word.big, target->word.small);
#endif
		mpz_mul_2exp(target->word.big, target->word.big, source->count);
#ifdef OPTIM
		if (source->count <= DIGITS_PER_WORD) {
			if (source->word.small >= 0) {
				mpz_add_ui(target->word.big, target->word.big,
												source->word.small);
			}
			else {
				mpz_sub_ui(target->word.big, target->word.big,
												-(source->word.small));
			}
		}
		else
#endif
			mpz_add(target->word.big, target->word.big, source->word.big);
#ifdef OPTIM
	}
#endif

	target->count += source->count;

#ifdef TRACE
	if (TRACE) {
		debugp("force_To_DigsX_From_Abs_Copy",
				"%x %x emitted=%d\n",
				(unsigned) target,
				(unsigned) source,
				source->count);
	}
#endif
	/*
	 * We've consumed the source so advance to the next possible source
	 * of information
	 */
	cls->userData = (void *) source->x;

#ifdef DAVINCI
		beginGraphUpdate();
		deleteOnlyEdge(cls, source);
		newEdgeToOnlyChild(cls, source->x);
		endGraphUpdate();
#endif
	newDigsX(target);
}

#ifdef JUNK
In some cases it would be better to use the matices to
negate things. But to do this for absolute value
we must know the sign of the argument.

As a rule we want to use a matrix for reciprocal and negation
except at the top level when we might prefer to use algorithms
in Peters thesis. This is also the case when we real is
the argument to a predicate.
#endif