aboutsummaryrefslogtreecommitdiff
path: root/ic-reals-6.3/base/MatX.c
blob: 87cdcd6c9a9924a5c04296b7ef33d96774a23708 (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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
/*
 * 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"

/*
 * Functions for allocating and manipulating matrix LFTs in the heap.
 */

void setMatXMethodSigned(MatX *);
void setMatXMethodUnsigned(MatX *);
void absorbDigsXIntoMatX(MatX *);

MatX *
allocMatX()
{
	MatX *matX;
 
	if ((matX = (MatX *) malloc (sizeof(MatX))) == NULL) 
		Error(FATAL, E_INT, "allocMatX", "malloc failed");
 
#ifdef DAVINCI
	newNodeId(matX);
#else
#ifdef TRACE
	newNodeId(matX);
#endif
#endif

	matX->tag.type = MATX;
	matX->tag.dumped = FALSE;
	matX->strm = (Real) NULL;

#ifdef DAVINCI
    	beginGraphUpdate();
    	newNode(matX, MATX);
    	endGraphUpdate();
#endif


	return matX;
}

/*
 * Allocates and fills a matrix object in the heap. Included below
 * is code for eagerly reducing a matrix against its argument. Eager
 * reduction reduces the amount of garbage in the heap. At this point,
 * eager reduction is disabled as some functions which call this expect
 * to get back a MatX object. With reduction they might get either
 * a MatX or a Vec.
 */
Real
matrix_Int(Real x, int a, int b, int c, int d)
{
	MatX *matX;

#ifdef LATER
	/*
	 * If the argument is a vector, then we eagerly reduce our given
	 * matrix to a vector. We do this in tmp storage to avoid creating
	 * garbage in the heap.
	 */
	if (x->gen.tag.type == VECTOR) {
		mpz_set_si(bigTmpMat[0][0], a);
		mpz_set_si(bigTmpMat[0][1], b);
		mpz_set_si(bigTmpMat[1][0], c);
		mpz_set_si(bigTmpMat[1][1], d);
		multVectorPairTimesVector(bigTmpMat[0], bigTmpMat[1], x->vec.vec);
		return vector_Z(bigTmpMat[0][0], bigTmpMat[0][1]);
	}
#endif

	/*
	 * So now we know we will end up with matrix, so we allocate one.
	 */
	matX = allocMatX();

	mpz_init_set_si(matX->mat[0][0], a);
	mpz_init_set_si(matX->mat[0][1], b);
	mpz_init_set_si(matX->mat[1][0], c);
	mpz_init_set_si(matX->mat[1][1], d);

	/*
	 * ### should perhaps check that there are no zero columns
	 */

	/* remove powers of 2 from the matrix */
	normalizeMatrix(matX->mat);

	/* make the matrix positive if it is negative (ie no entries > 0) */
	if (matrixSign(matX->mat) < 0)
		negateMatrix(matX->mat);

	matX->x = (Real) x;

#ifdef DAVINCI
    	beginGraphUpdate();
    	newEdgeToOnlyChild(matX, x);
    	endGraphUpdate();
#endif

#ifdef LATER
	/*
	 * Now we eagerly consume any matrix which follows. This may be unwise.
	 */
	if (x->gen.tag.type == MATX) {
		multVectorPairTimesMatrix(matX->mat[0], matX->mat[1], x->matX.mat);
#ifdef DAVINCI
		beginGraphUpdate();
		deleteOnlyEdge(matX, matX->x);
		newEdgeToOnlyChild(matX, x->matX.x);
		endGraphUpdate();
#endif
		matX->x = x->matX.x;
	}
#endif

	/*
	 * A MatX is signed if the there are entries in the matrix of different
	 * signs or if the argument to the MatX is signed.
	 */
	if (matX->x->gen.tag.isSigned || matrixSign(matX->mat) == 0)
		matX->tag.isSigned = TRUE;

	if (matX->x->gen.tag.isSigned)
		setMatXMethodSigned(matX);
	else
		setMatXMethodUnsigned(matX);
	return (Real) matX;
}

/*
 * This is exactly as above but in this case the matrix is filled with
 * large (GMP) integers rather than machine integers.
 */
Real
matrix_Z(Real x, mpz_t a, mpz_t b, mpz_t c, mpz_t d)
{
	MatX *matX;

#ifdef LATER
	/*
	 * If the argument is a vector, then we eagerly reduce our given
	 * matrix to a vector. We do this in tmp storage to avoid creating
	 * garbage in the heap.
	 */
	if (x->gen.tag.type == VECTOR) {
		mpz_set(bigTmpMat[0][0], a);
		mpz_set(bigTmpMat[0][1], b);
		mpz_set(bigTmpMat[1][0], c);
		mpz_set(bigTmpMat[1][1], d);
		multVectorPairTimesVector(bigTmpMat[0], bigTmpMat[1], x->vec.vec);
		return vector_Z(bigTmpMat[0][0], bigTmpMat[0][1]);
	}
#endif

	/*
	 * So now we know we will end up with a matrix, so we allocate one.
	 */
	matX = allocMatX();

	mpz_init_set(matX->mat[0][0], a);
	mpz_init_set(matX->mat[0][1], b);
	mpz_init_set(matX->mat[1][0], c);
	mpz_init_set(matX->mat[1][1], d);

	/*
	 * ### should perhaps check that there are no zero columns
	 */

	/* remove powers of 2 from the matrix */
	normalizeMatrix(matX->mat);

	/* make the matrix positive if it is negative (ie no entries > 0) */
	if (matrixSign(matX->mat) < 0)
		negateMatrix(matX->mat);

	matX->x = (Real) x;

#ifdef DAVINCI
    	beginGraphUpdate();
    	newEdgeToOnlyChild(matX, x);
    	endGraphUpdate();
#endif

#ifdef LATER
	/*
	 * Now we eagerly consume any matrix which follows. This may be unwise.
	 */
	if (x->gen.tag.type == MATX) {
		multVectorPairTimesMatrix(matX->mat[0], matX->mat[1], x->matX.mat);
#ifdef DAVINCI
		beginGraphUpdate();
		deleteOnlyEdge(matX, matX->x);
		newEdgeToOnlyChild(matX, x->matX.x);
		endGraphUpdate();
#endif
		matX->x = x->matX.x;
	}
#endif

	/*
	 * A MatX is signed if the there are entries in the matrix of different
	 * signs or if the argument to the MatX is signed.
	 */
	if (matX->x->gen.tag.isSigned || matrixSign(matX->mat) == 0)
		matX->tag.isSigned = TRUE;

	if (matX->x->gen.tag.isSigned)
		setMatXMethodSigned(matX);
	else
		setMatXMethodUnsigned(matX);
	return (Real) matX;
}

void
force_To_MatX_From_DigsX_Entry()
{
	MatX *matX;
	DigsX *digsX;
	int digitsNeeded;
	void force_To_MatX_From_DigsX_Cont();

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

	PUSH_3(force_To_MatX_From_DigsX_Cont, matX, digitsNeeded);

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

void
force_To_MatX_From_DigsX_Cont()
{
	MatX *matX;
	int digitsNeeded;

	matX = (MatX *) POP;
	digitsNeeded = (int) POP;

	absorbDigsXIntoMatX(matX);
}

/*
 * It can happen that, for example, an Alt is deemed signed, and yet
 * the value it ultimately yields is unsigned. In this case we need to
 * emit a sign from a DigsZ. This is little more than a no-op.
 */
void
force_To_MatX_From_DigsX_Signed()
{
	MatX *matX;
	void force_To_MatX_From_DigsX_Entry();

	matX = (MatX *) POP;
	matX->force = force_To_MatX_From_DigsX_Entry;
}

/*
 * When a matrix is applied to an vector, the matrix reduces to a vector.
 * The reduction happens in place. That is, we overwrite the matrix
 * with a vector. That way any other consumers which share the matrix
 * (now vector), end up pointing to the vector.
 */
void
force_To_MatX_From_Vec()
{
	MatX *matX;
	Vec *vec;
	mpz_t a, b;		/* temporary storage while we clobber the MatX */
	Real strm;		/* temporary storage while we clobber the MatX */
	int digitsNeeded;

	matX = (MatX *) POP;
	digitsNeeded = (int) POP;

	multVectorPairTimesVector(matX->mat[0], matX->mat[1], matX->x->vec.vec);

	a[0] = matX->mat[0][0][0];
	b[0] = matX->mat[0][1][0];
	strm = matX->strm;

	mpz_clear(matX->mat[1][0]);
	mpz_clear(matX->mat[1][1]);

#ifdef DAVINCI
	beginGraphUpdate();
	deleteOnlyEdge(matX, matX->x);
	endGraphUpdate();
#endif

	vec = (Vec *) matX;

	vec->tag.type = VECTOR;
	vec->vec[0][0] = a[0];
	vec->vec[1][0] = b[0];
	normalizeVector(vec->vec);
	vec->strm = strm;
}

/*
 * Same as the above, except for the signed case. The stack frame
 * is different.
 */
void
force_To_MatX_From_Vec_Signed()
{
	MatX *matX;
	Vec *vec;
	mpz_t a, b;		/* temporary storage while we clobber the MatX */
	Real strm;		/* temporary storage while we clobber the MatX */

	matX = (MatX *) POP;

	multVectorPairTimesVector(matX->mat[0], matX->mat[1], matX->x->vec.vec);

	a[0] = matX->mat[0][0][0];
	b[0] = matX->mat[0][1][0];
	strm = matX->strm;

	mpz_clear(matX->mat[1][0]);
	mpz_clear(matX->mat[1][1]);

#ifdef DAVINCI
	beginGraphUpdate();
	deleteOnlyEdge(matX, matX->x);
	endGraphUpdate();
#endif

	vec = (Vec *) matX;

	vec->tag.type = VECTOR;
	vec->vec[0][0] = a[0];
	vec->vec[1][0] = b[0];
	normalizeVector(vec->vec);
	vec->tag.isSigned = TRUE;
	vec->strm = strm;
}

void
force_To_MatX_From_MatX()
{
	MatX *matX;
	int digitsNeeded;
	void force_To_MatX_From_Vec();

	matX = (MatX *) POP;
	digitsNeeded = (int) POP;

	if (matX->x->gen.tag.type == VECTOR) {
		PUSH_3(force_To_MatX_From_Vec, matX, digitsNeeded);
		return;
	}

	multVectorPairTimesMatrix(matX->mat[0], matX->mat[1], matX->x->matX.mat);
	normalizeMatrix(matX->mat);

#ifdef DAVINCI
	beginGraphUpdate();
	deleteOnlyEdge(matX, matX->x);
	newEdgeToOnlyChild(matX, matX->x->matX.x);
	endGraphUpdate();
#endif
	matX->x = matX->x->matX.x;
	setMatXMethodUnsigned(matX);
}

/*
 * This handles the case when the arg of a signed MatX is another MatX.
 * This version is strict. It inspects its argument first. If it too is
 * signed, then it forces it before reducing the two matrices to one.
 */
void
force_To_MatX_From_MatX_Signed_Entry()
{
	MatX *matX, *arg;
	void force_To_MatX_From_MatX_Signed_Cont();
	void force_To_MatX_From_Vec_Signed();

	matX = (MatX *) POP;

	if (matX->x->gen.tag.type == VECTOR) {
		PUSH_2(force_To_MatX_From_Vec_Signed, matX);
		return;
	}

	arg = (MatX *) matX->x;

	PUSH_2(force_To_MatX_From_MatX_Signed_Cont, matX);
	if (arg->x->gen.tag.isSigned)
		PUSH_2(arg->force, arg);
}

/*
 * The following code is exactly the same as the unsigned case except there
 * are fewer things on the stack. The two can probably be reconciled
 * as the number of digits is irrelevant when reducing matrices. We leave
 * them separate in case one or other can be improved at a later time.
 */
void
force_To_MatX_From_MatX_Signed_Cont()
{
	MatX *matX;
	void force_To_MatX_From_Vec_Signed();

	matX = (MatX *) POP;

	if (matX->x->gen.tag.type == VECTOR) {
		PUSH_2(force_To_MatX_From_Vec_Signed, matX);
		return;
	}

	multVectorPairTimesMatrix(matX->mat[0], matX->mat[1], matX->x->matX.mat);
	normalizeMatrix(matX->mat);

#ifdef DAVINCI
	beginGraphUpdate();
	deleteOnlyEdge(matX, matX->x);
	newEdgeToOnlyChild(matX, matX->x->matX.x);
	endGraphUpdate();
#endif

	matX->x = matX->x->matX.x;
	setMatXMethodUnsigned(matX);
}

void
force_To_MatX_From_TenXY()
{
	MatX *matX;
	int digitsNeeded;
	void force_To_MatX_From_MatX();
	void force_To_MatX_From_DigsX_Entry();

	matX = (MatX *) POP;
	digitsNeeded = (int) POP;

	if (matX->x->gen.tag.type != TENXY) {
		PUSH_3(force_To_MatX_From_MatX, matX, digitsNeeded);
		return;
	}

	createUnsignedStreamForTenXY(&matX->x->tenXY);

#ifdef DAVINCI
	beginGraphUpdate();
	deleteOnlyEdge(matX, matX->x);
	newEdgeToOnlyChild(matX, matX->x->tenXY.strm);
	endGraphUpdate();
#endif

	matX->x = matX->x->tenXY.strm;
	matX->force = force_To_MatX_From_DigsX_Entry;
	PUSH_3(matX->force, matX, digitsNeeded);
}

void
force_To_MatX_From_TenXY_Signed_Entry()
{
	MatX *matX;
	TenXY *tenXY;
	void force_To_MatX_From_MatX_Signed_Entry();
	void force_To_MatX_From_TenXY_Signed_Cont();
	void force_To_MatX_From_TenXY_Signed_Cont_X();

	matX = (MatX *) POP;
	tenXY = (TenXY *) matX->x;

	if (matX->x->gen.tag.type != TENXY) {
		PUSH_2(force_To_MatX_From_MatX_Signed_Entry, matX);
		return;
	}

	if (tenXY->x->gen.tag.isSigned) {
		if (tenXY->y->gen.tag.isSigned) {
			PUSH_2(force_To_MatX_From_TenXY_Signed_Cont_X, matX);
			PUSH_2(tenXY->forceY, tenXY);
		}
		else {
			PUSH_2(force_To_MatX_From_TenXY_Signed_Cont, matX);
			PUSH_2(tenXY->forceX, tenXY);
		}
	}
	else {
		if (tenXY->y->gen.tag.isSigned) {
			PUSH_2(force_To_MatX_From_TenXY_Signed_Cont, matX);
			PUSH_2(tenXY->forceY, tenXY);
		}
		else
			PUSH_2(force_To_MatX_From_TenXY_Signed_Cont, matX);
	}
}

/*
 * Here we have already forced the y argument, and now we force the sign
 * from the x side of the tensor.
 */
void
force_To_MatX_From_TenXY_Signed_Cont_X()
{
	MatX *matX;
	TenXY *tenXY;
	void force_To_MatX_From_MatX_Signed_Entry();
	void force_To_MatX_From_TenXY_Signed_Cont();

	matX = (MatX *) POP;
	tenXY = (TenXY *) matX->x;

	if (matX->x->gen.tag.type != TENXY) {
		PUSH_2(force_To_MatX_From_MatX_Signed_Entry, matX);
		return;
	}

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

void
force_To_MatX_From_TenXY_Signed_Cont()
{
	MatX *matX;
	void force_To_MatX_From_MatX_Signed_Cont();
	void force_To_MatX_From_SignX_Entry();

	matX = (MatX *) POP;

	if (matX->x->gen.tag.type != TENXY) {
		PUSH_2(force_To_MatX_From_MatX_Signed_Cont, matX);
		return;
	}

	createSignedStreamForTenXY(&matX->x->tenXY);

#ifdef DAVINCI
	beginGraphUpdate();
	deleteOnlyEdge(matX, matX->x);
	newEdgeToOnlyChild(matX, matX->x->tenXY.strm);
	endGraphUpdate();
#endif

	matX->x = matX->x->tenXY.strm;
	matX->force = force_To_MatX_From_SignX_Entry;
	PUSH_2(matX->force, matX);
}

void
force_To_MatX_From_Alt_Entry()
{
	MatX *matX;
	Alt *alt;
	void force_To_Alt_Entry();
	void force_To_MatX_From_Alt_Cont();
	int digitsNeeded;

	matX = (MatX *) POP;
	digitsNeeded = (int) POP;
	alt = (Alt *) matX->x;
	
	PUSH_3(force_To_MatX_From_Alt_Cont, matX, digitsNeeded);

	/*
	 * If alt->redirect is not valid (equals NULL) then the value of
	 * the conditional has not been determined so we need to force it.
	 */
	if (alt->redirect == NULL)
		PUSH_2(force_To_Alt_Entry, alt);
}

void
force_To_MatX_From_Alt_Cont()
{
	MatX *matX;
	Alt *alt;
	int digitsNeeded;

	matX = (MatX *) POP;
	digitsNeeded = (int) POP;
	alt = (Alt *) matX->x;

#ifdef DAVINCI
	beginGraphUpdate();
	deleteOnlyEdge(matX, alt);
	newEdgeToOnlyChild(matX, alt->redirect);
	endGraphUpdate();
#endif
	matX->x = alt->redirect;
	setMatXMethodUnsigned(matX);

	PUSH_3(matX->force, matX, digitsNeeded);
}

void
force_To_MatX_From_Cls_Entry()
{
	MatX *matX;
	Cls *cls;
	void force_To_MatX_From_Cls_Cont();
	int digitsNeeded;

	matX = (MatX *) POP;
	digitsNeeded = (int) POP;
	cls = (Cls *) matX->x;
	
	PUSH_3(force_To_MatX_From_Cls_Cont, matX, digitsNeeded);

	/*
	 * If cls->redirect is not valid (equals NULL) then the value of
	 * the closure has not been determined so we need to force it.
	 */
	if (cls->redirect == NULL)
		PUSH_2(cls->force, cls);
}

void
force_To_MatX_From_Cls_Cont()
{
	MatX *matX;
	Cls *cls;
	int digitsNeeded;

	matX = (MatX *) POP;
	digitsNeeded = (int) POP;
	cls = (Cls *) matX->x;

#ifdef DAVINCI
	beginGraphUpdate();
	deleteOnlyEdge(matX, cls);
	newEdgeToOnlyChild(matX, cls->redirect);
	endGraphUpdate();
#endif
	matX->x = cls->redirect;
	setMatXMethodUnsigned(matX);

	PUSH_3(matX->force, matX, digitsNeeded);
}

void
force_To_MatX_From_Alt_Signed_Entry()
{
	MatX *matX;
	Alt *alt;
	void force_To_Alt_Entry();
	void force_To_MatX_From_Alt_Signed_Cont();

	matX = (MatX *) POP;
	alt = (Alt *) matX->x;
	
	PUSH_2(force_To_MatX_From_Alt_Signed_Cont, matX);

	/*
	 * If alt->redirect is not valid (equals NULL) then the value of
	 * the conditional has not been determined so we need to force it.
	 */
	if (alt->redirect == NULL)
		PUSH_2(force_To_Alt_Entry, alt);
}

void
force_To_MatX_From_Alt_Signed_Cont()
{
	MatX *matX;
	Alt *alt;

	matX = (MatX *) POP;
	alt = (Alt *) matX->x;

#ifdef DAVINCI
	beginGraphUpdate();
	deleteOnlyEdge(matX, alt);
	newEdgeToOnlyChild(matX, alt->redirect);
	endGraphUpdate();
#endif

	matX->x = alt->redirect;
	setMatXMethodSigned(matX);

	PUSH_2(matX->force, matX);
}

void
force_To_MatX_From_Cls_Signed_Entry()
{
	MatX *matX;
	Cls *cls;
	void force_To_MatX_From_Cls_Signed_Cont();

	matX = (MatX *) POP;
	cls = (Cls *) matX->x;
	
	PUSH_2(force_To_MatX_From_Cls_Signed_Cont, matX);

	/*
	 * If cls->redirect is not valid (equals NULL) then the value of
	 * the closure has not been determined so we need to force it.
	 */
	if (cls->redirect == NULL)
		PUSH_2(cls->force, cls);
}

void
force_To_MatX_From_Cls_Signed_Cont()
{
	MatX *matX;
	Cls *cls;

	matX = (MatX *) POP;
	cls = (Cls *) matX->x;

#ifdef DAVINCI
	beginGraphUpdate();
	deleteOnlyEdge(matX, cls);
	newEdgeToOnlyChild(matX, cls->redirect);
	endGraphUpdate();
#endif

	matX->x = cls->redirect;
	setMatXMethodSigned(matX);

	PUSH_2(matX->force, matX);
}

void
force_To_MatX_From_SignX_Entry()
{
	MatX *matX;
	SignX *signX;
	void force_To_MatX_From_SignX_Cont();

	matX = (MatX *) POP;
	signX = (SignX *) matX->x;

	PUSH_2(force_To_MatX_From_SignX_Cont, matX);
	if (signX->tag.value == SIGN_UNKN)
		PUSH_2(signX->force, signX);
}

void
force_To_MatX_From_SignX_Cont()
{
	MatX *matX;
	void absorbSignIntoMatX(MatX *);

	matX = (MatX *) POP;
	absorbSignIntoMatX(matX);
}

void
absorbSignIntoMatX(MatX *matX)
{
	absorbSignIntoVectorPair(matX->mat[0], matX->mat[1],
					matX->x->signX.tag.value);
#ifdef DAVINCI
	beginGraphUpdate();
	deleteOnlyEdge(matX, matX->x);
	newEdgeToOnlyChild(matX, matX->x->signX.x);
	endGraphUpdate();
#endif
	matX->x = matX->x->signX.x;
	setMatXMethodUnsigned(matX);
}

void
setMatXMethodSigned(MatX *matX)
{
	void force_To_MatX_From_SignX_Entry();
	void force_To_MatX_From_DigsX_Signed();
	void force_To_MatX_From_Vec_Signed();
	void force_To_MatX_From_MatX_Signed_Entry();
	void force_To_MatX_From_TenXY_Signed_Entry();
	void force_To_MatX_From_Alt_Signed_Entry();
	void force_To_MatX_From_Cls_Signed_Entry();

	switch (matX->x->gen.tag.type) {
	case SIGNX :
		matX->force = force_To_MatX_From_SignX_Entry;
		break;
	case DIGSX :
		matX->force = force_To_MatX_From_DigsX_Signed;
		break;
	case ALT :
		matX->force = force_To_MatX_From_Alt_Signed_Entry;
		break;
	case VECTOR :
		matX->force = force_To_MatX_From_Vec_Signed;
		break;
	case MATX :
		matX->force = force_To_MatX_From_MatX_Signed_Entry;
		break;
	case TENXY :
		matX->force = force_To_MatX_From_TenXY_Signed_Entry;
		break;
	case CLOSURE :
		matX->force = force_To_MatX_From_Cls_Signed_Entry;
		break;
	default :
		Error(FATAL, E_INT, "setMatXMethodSigned", "something wrong with x");
		break;
	}
}

void
setMatXMethodUnsigned(MatX *matX)
{
	void force_To_MatX_From_SignX_Entry();
	void force_To_MatX_From_DigsX_Entry();
	void force_To_MatX_From_Vec();
	void force_To_MatX_From_MatX();
	void force_To_MatX_From_TenXY();
	void force_To_MatX_From_Alt_Entry();
	void force_To_MatX_From_Cls_Entry();

	switch (matX->x->gen.tag.type) {
	case SIGNX :
		Error(FATAL, E_INT, "setMatXMethodUnsigned", "x is signed");
		break;
	case DIGSX :
		matX->force = force_To_MatX_From_DigsX_Entry;
		break;
	case ALT :
		matX->force = force_To_MatX_From_Alt_Entry;
		break;
	case VECTOR :
		matX->force = force_To_MatX_From_Vec;
		break;
	case MATX :
		matX->force = force_To_MatX_From_MatX;
		break;
	case TENXY :
		matX->force = force_To_MatX_From_TenXY;
		break;
	case CLOSURE :
		matX->force = force_To_MatX_From_Cls_Entry;
		break;
	default :
		Error(FATAL, E_INT, "setMatXMethodUnsigned", "something wrong with x");
		break;
	}
}

void
absorbDigsXIntoMatX(MatX *matX)
{
	DigsX *digsX;
	SmallMatrix smallAccumMat;

	digsX = (DigsX *) matX->x;

	/*
	 * Now accumulate the digits into a matrix (large or small integers)
	 * and augment the matrix (matX) with the information.
	 */
	if (digsX->count > 0) {
#ifdef PACK_DIGITS
		if (digsX->count <= DIGITS_PER_WORD) {
			makeSmallMatrixFromDigits(smallAccumMat, digsX);
			multVectorPairTimesSmallMatrix(matX->mat[0], matX->mat[1],
											smallAccumMat);
		}
		else {
#endif
			makeMatrixFromDigits(bigTmpMat, digsX);
			multVectorPairTimesMatrix(matX->mat[0], matX->mat[1], bigTmpMat);
#ifdef PACK_DIGITS
		}
#endif
		matX->x = digsX->x;
#ifdef DAVINCI
		beginGraphUpdate();
		deleteOnlyEdge(matX, digsX);
		newEdgeToOnlyChild(matX, digsX->x);
		endGraphUpdate();
#endif

		/*
		 * Now try to remove powers of 2 from the residual matrix.
		 */
		normalizeMatrix(matX->mat);

	}

#ifdef TRACE
	debugp("force_To_MatX_From_DigsX",
	       "%x %x absorbed=%d\n",
	       (unsigned) matX,
	       (unsigned) digsX,
	       digsX->count);
#endif
}