-
Notifications
You must be signed in to change notification settings - Fork 1
/
eval.c
1646 lines (1535 loc) · 49.1 KB
/
eval.c
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
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* This file is part of the MAYLIB libray.
Copyright 2007-2018 Patrick Pelissier
This Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at your
option) any later version.
This Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with th Library; see the file COPYING.LESSER.txt.
If not, write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston,
MA 02110-1301, USA. */
#include "may-impl.h"
static MAY_REGPARM may_t may_eval_sum (may_t x);
static MAY_REGPARM may_t may_eval_product (may_t x);
int
may_eval_p (may_t x)
{
return MAY_EVAL_P(x);
}
may_t
may_reeval (may_t x)
{
may_mark();
return may_keep (may_eval (may_copy_c (x, 0)));
}
static may_t
evalf (may_t x)
{
mpfr_t f;
may_t y;
switch (MAY_TYPE(x)) {
case MAY_INT_T:
mpfr_init_set_z (f, MAY_INT (x), MAY_RND);
return_mpfr_no_copy:
return MAY_MPFR_NOCOPY_C (f);
case MAY_RAT_T:
mpfr_init_set_q (f, MAY_RAT(x), MAY_RND);
goto return_mpfr_no_copy;
case MAY_FLOAT_T:
return x;
case MAY_COMPLEX_T:
return MAY_COMPLEX_C (evalf (MAY_RE (x)),
evalf (MAY_IM (x)));
case MAY_STRING_T:
/* Check if PI string */
if (MAY_PI_P (x)) {
mpfr_init (f);
mpfr_const_pi (f, MAY_RND);
goto return_mpfr_no_copy;
}
/* Check if 10 base float string */
else if (MAY_NAME (x)[0] == '#') {
mpfr_init_set_str (f, &MAY_NAME (x)[1], 0, MAY_RND);
goto return_mpfr_no_copy;
} else
return x;
case MAY_POW_T:
y = MAY_NODE_C (MAY_POW_T, 2);
MAY_SET_AT (y, 0, evalf(MAY_AT(x,0)));
MAY_SET_AT (y, 1, MAY_AT(x, 1));
return y;
default:
if (MAY_UNLIKELY (MAY_ATOMIC_P (x)))
return x;
may_size_t i, n = MAY_NODE_SIZE(x);
y = MAY_NODE_C (MAY_TYPE (x), n);
for (i = 0; i < n; i++)
MAY_SET_AT (y, i, evalf(MAY_AT(x,i)));
return y;
}
}
may_t
may_evalf (may_t x)
{
may_mark();
return may_keep (may_eval (evalf(x)));
}
/* Eval 'x' in Z not inside Z/pZ (if it was set)
It is useful for evaluating power */
MAY_INLINE may_t
eval_outside_intmod (may_t x)
{
may_t saved_intmod = may_g.frame.intmod;
may_g.frame.intmod = NULL;
x = may_eval (x);
may_g.frame.intmod = saved_intmod;
return x;
}
/* Return TRUE if x is "positive" for extract_constant.
It is used for even odd functions to choose a represent between -f(x) and f(-x) */
MAY_INLINE int
pos_p (may_t x)
{
/* Get numerical coeeficient if any */
if (MAY_TYPE (x) == MAY_FACTOR_T)
x = MAY_AT (x, 0);
/* Special treatement for complex numbers */
if (MAY_UNLIKELY (MAY_TYPE (x) == MAY_COMPLEX_T))
return !may_num_negzero_p (MAY_RE (x)) || !may_num_neg_p (MAY_IM (x));
else if (MAY_LIKELY (MAY_PURENUM_P (x)))
return !may_num_neg_p (x);
return 1;
}
/* From a sum x+y, extract the overall coefficient (the integer content):
For 2*x+4*y, extract 2 as a overall coeff, and x+2*y.
It also extracts the sign, so that we can have (a-b)/(b-a)
evals to -1 */
static MAY_REGPARM may_t
extract_constant_coefficient_from_sum (may_t x)
{
may_t z, retone;
may_size_t i,n;
mpz_t gcd;
MAY_ASSERT (MAY_TYPE (x) == MAY_SUM_T);
n = MAY_NODE_SIZE(x);
MAY_ASSERT (n >= 2);
retone = pos_p (MAY_AT (x, 0)) ? MAY_ONE : MAY_N_ONE;
/* First: a fast pass to detect if we can do something */
/* Deal with 1st term which may be a numerical */
for (i = MAY_TYPE (MAY_AT (x, 0)) == MAY_INT_T; i < n; i++) {
z = MAY_AT (x, i);
if (MAY_TYPE (z) != MAY_FACTOR_T
|| MAY_TYPE (MAY_AT (z, 0)) != MAY_INT_T)
return retone;
}
/* Compute the GCD of all integer coefficients of the sum */
mpz_init (gcd);
z = MAY_AT (x, 0);
mpz_set (gcd, MAY_INT (MAY_TYPE (z) == MAY_INT_T ? z : MAY_AT (z, 0)));
for (i = 1; i < n; i++) {
z = MAY_AT (x, i);
MAY_ASSERT (MAY_TYPE (z) == MAY_FACTOR_T);
mpz_gcd (gcd, gcd, MAY_INT (MAY_AT (z, 0)));
if (mpz_cmp_ui (gcd, 1) == 0)
return retone;
}
/* Get the sign of the leading term */
if (retone == MAY_N_ONE)
mpz_neg (gcd, gcd);
return may_mpz_simplify (MAY_MPZ_NOCOPY_C (gcd));
}
static MAY_REGPARM may_t
fix_sum_after_extracting_coefficient (may_t sum, may_t gcd)
{
may_t z;
may_size_t i,n;
MAY_ASSERT (MAY_TYPE (sum) == MAY_SUM_T);
MAY_ASSERT (MAY_TYPE (gcd) == MAY_INT_T);
MAY_ASSERT (mpz_cmp_ui (MAY_INT (gcd), 1) != 0);
n = MAY_NODE_SIZE(sum);
z = MAY_NODE_C (MAY_SUM_T, n);
MAY_ASSERT (n >= 2);
/* Special case for -1 */
if (MAY_UNLIKELY (gcd == MAY_N_ONE)) {
if (MAY_PURENUM_P (MAY_AT (sum, 0))) {
i = 1;
MAY_SET_AT (z, 0, may_num_neg (MAY_DUMMY, MAY_AT (sum, 0)));
} else
i = 0;
for ( /* void */; i < n; i++) {
may_t old = MAY_AT (sum, i);
may_t new;
if (MAY_LIKELY (MAY_TYPE (old) == MAY_FACTOR_T)) {
new = may_num_neg (MAY_DUMMY, MAY_AT (old, 0));
old = MAY_AT (old, 1);
} else
new = MAY_N_ONE;
new = may_mul_c (new, old);
MAY_SET_AT (z, i, new);
}
z = may_eval (z);
return z;
}
/* Deal with 1st term which may be a numerical */
if (MAY_LIKELY (MAY_TYPE (MAY_AT (sum, 0)) == MAY_INT_T)) {
i = 1;
MAY_SET_AT (z, 0, may_div_c (MAY_AT (sum, 0), gcd));
} else
i = 0;
for ( /*void*/; i < n; i++) {
may_t old = MAY_AT (sum, i);
may_t num;
may_t term;
MAY_ASSERT (MAY_TYPE (old) == MAY_FACTOR_T);
num = may_div_c (MAY_AT (old, 0), gcd);
term = may_mul_c (num, MAY_AT (old, 1));
MAY_SET_AT (z, i, term);
}
z = may_eval (z);
return z;
}
/* Inline since it is called only once, and it is quite often used,
we want to inline this function.
Eval the power of x which must be of type MAY_POW_T. */
MAY_INLINE may_t
may_eval_pow (may_t x)
{
may_t y;
MAY_ASSERT (MAY_TYPE (x) == MAY_POW_T);
MAY_ASSERT (MAY_NODE_SIZE(x) == 2);
/* 1. Eval both sub-args. They are likely already evaluated */
may_t base = MAY_AT (x, 0);
if (MAY_UNLIKELY (!MAY_EVAL_P (base)))
base = may_eval (base);
/* Compute exponent outside the intmod */
may_t expo = MAY_AT (x, 1);
if (MAY_UNLIKELY (!MAY_EVAL_P (expo)))
expo = eval_outside_intmod (expo);
MAY_ASSERT (MAY_EVAL_P (base) && MAY_EVAL_P (expo));
may_t gcd, num, new;
may_size_t n, i;
/* Commun case is the case where expo is an integer.
Optimize it. */
if (MAY_LIKELY (MAY_TYPE (expo) == MAY_INT_T)) {
/* Special case: expo is an integer */
mpz_srcptr zexpo = MAY_INT (expo);
/* 2. If expo == 1 or 0 */
if (MAY_UNLIKELY (expo == MAY_ZERO))
y = MAY_ONE;
else if (MAY_UNLIKELY (expo == MAY_ONE))
y = base;
else
switch (MAY_TYPE (base)) {
/* 3. If both are pure numerical types, eval them. */
case MAY_INT_T:
case MAY_FLOAT_T:
case MAY_RAT_T:
case MAY_COMPLEX_T:
y = may_num_pow (base, expo);
MAY_ASSERT (MAY_EVAL_P (y));
break;
/* 4. If base is Product, and expo is int, returns product of pow.*/
case MAY_FACTOR_T:
/* (A.X) ^ INT --> A^INT.X^INT WARNING: X^INT may be num or a factor */
num = may_num_pow (MAY_AT (base, 0), expo);
new = may_eval (may_pow_c (MAY_AT (base, 1), expo) );
MAY_ASSERT (MAY_EVAL_P (num) && MAY_EVAL_P (new));
if (MAY_UNLIKELY (!MAY_PURENUM_P (num))) {
/* Case if A^INT refuses to be computed because it is too big */
y = may_mul(num, new);
} else if (MAY_UNLIKELY (MAY_ONE_P (num)))
y = new; /* (-x)^2 -->x^2 */
else if (MAY_UNLIKELY (MAY_PURENUM_P (new)))
y = may_num_simplify (may_num_mul (MAY_DUMMY, num, new));
else {
if (MAY_UNLIKELY (MAY_TYPE (new) == MAY_FACTOR_T)) {
num = may_num_simplify (may_num_mul (MAY_DUMMY, num,
MAY_AT (new, 0)));
new = MAY_AT (new, 1);
}
y = x;
MAY_OPEN_C (y, MAY_FACTOR_T);
MAY_NODE_SIZE(y) = 2;
MAY_ASSERT(MAY_PURENUM_P(num));
MAY_SET_AT (y, 0, num);
MAY_SET_AT (y, 1, new);
MAY_CLOSE_C (y, MAY_FLAGS (new), MAY_NEW_HASH2 (num, new));
}
break;
case MAY_PRODUCT_T:
n = MAY_NODE_SIZE(base);
MAY_ASSERT (n >= 2);
if (n > 2)
y = MAY_NODE_C (MAY_PRODUCT_T, n);
else {
MAY_ASSERT (n == 2);
y = x;
MAY_OPEN_C (y, MAY_PRODUCT_T);
MAY_NODE_SIZE(y) = 2;
}
for (i = 0; i < n; i++)
MAY_SET_AT (y, i, may_pow_c (MAY_AT (base, i), expo) );
y = may_eval (y);
break;
/* 4'. Extract the common GCD for all terms of a sum
(a*g+b*g+c*g)^^N --> g^N*(a+b+c)^^N */
case MAY_SUM_T:
gcd = extract_constant_coefficient_from_sum (base);
if (gcd != MAY_ONE) {
y = fix_sum_after_extracting_coefficient (base, gcd);
y = may_mul_c (may_pow_c (gcd, expo), may_pow_c (y, expo));
y = may_eval (y);
} else
goto rebuild_pow;
break;
/* 5. If Leader is Pow, combine the exponents */
case MAY_POW_T:
if (MAY_UNLIKELY (MAY_PURENUM_P (MAY_AT (base, 1)))) {
may_t newexpo = may_mul_c (expo, MAY_AT (base, 1));
y = x;
MAY_SET_AT (y, 0, MAY_AT (base, 0));
MAY_SET_AT (y, 1, newexpo);
y = may_eval (y);
} else
goto rebuild_pow;
break;
/* 7. If base=abs and expo even integer and base real, remove abs */
case MAY_ABS_T:
if (mpz_even_p (zexpo) && may_real_p (MAY_AT (base, 0))) {
y = x;
MAY_SET_AT (y, 0, MAY_AT (base, 0));
MAY_SET_AT (y, 1, expo);
y = may_eval (y);
} else
goto rebuild_pow;
break;
/* 8. Else generic */
case MAY_STRING_T:
case MAY_FUNC_T:
case MAY_EXP_T:
case MAY_LOG_T:
case MAY_SIGN_T: /* FIXME: sign(x)^2 might be simplified */
case MAY_FLOOR_T:
case MAY_SIN_T:
case MAY_COS_T:
case MAY_TAN_T:
case MAY_ASIN_T:
case MAY_ACOS_T:
case MAY_ATAN_T:
case MAY_SINH_T:
case MAY_COSH_T:
case MAY_TANH_T:
case MAY_ASINH_T:
case MAY_ACOSH_T:
case MAY_ATANH_T:
case MAY_CONJ_T:
case MAY_REAL_T:
case MAY_IMAG_T:
case MAY_ARGUMENT_T:
case MAY_GAMMA_T:
case MAY_DIFF_T:
case MAY_MOD_T:
case MAY_GCD_T:
rebuild_pow:
y = x;
MAY_SET_AT (y, 0, base);
MAY_SET_AT (y, 1, expo);
MAY_CLOSE_C (y, MAY_FLAGS(base),
MAY_NEW_HASH2 (base, expo) );
break;
default:
if (MAY_UNLIKELY (MAY_EXT_P (base))) {
y = may_eval_extension_pow (base, expo);
} else
goto rebuild_pow;
break;
} /* end switch on the type of base */
} else {
/* General case (EXPO is not an integer) */
/* 2. If base==1, or exp == 1.0 or 0.0 */
if (MAY_ONE_P (base) || MAY_ZERO_P (expo))
y = MAY_ONE;
else if (MAY_ONE_P (expo))
y = base;
else if (MAY_NAN_P (expo) || MAY_NAN_P (base))
y = MAY_NAN;
/* 3. If both are num types, eval them. */
else if (MAY_PURENUM_P (base) && MAY_PURENUM_P (expo)) {
y = may_num_pow (base, expo);
MAY_ASSERT (MAY_EVAL_P (y));
}
/* 5. If Leader is Pow, combine the exponents if:
* both powers are RAT and if the first expo looks like 1/N
* base is pos and both power are real
* base is real and both pow are real and second one looks like
1/N, with N odd
* base is real and both pow are real and second one looks like
1/N, with N even => abs (x) ^(mul(expo1,expo2)) (TODO)
*/
else if (MAY_TYPE (base) == MAY_POW_T
&& MAY_PURENUM_P (MAY_AT (base, 1))
&& ((MAY_TYPE (expo) == MAY_RAT_T
&& MAY_TYPE (MAY_AT (base, 1)) == MAY_RAT_T
&& mpz_cmp_ui (mpq_numref(MAY_RAT(MAY_AT(base,1))), 1) == 0)
|| (may_nonneg_p (MAY_AT (base, 0))
&& may_real_p (expo)
&& may_real_p (MAY_AT (base, 1)))
|| (may_real_p (MAY_AT (base, 0))
&& may_real_p (MAY_AT (base, 1))
&& MAY_TYPE (expo) == MAY_RAT_T
&& mpz_cmp_ui (mpq_numref (MAY_RAT (expo)), 1) == 0
&& mpz_odd_p (mpq_denref (MAY_RAT (expo))))
)) {
may_t newexpo = may_mul_c (expo, MAY_AT (base, 1));
y = x;
MAY_SET_AT (y, 0, MAY_AT (base, 0));
MAY_SET_AT (y, 1, newexpo);
y = may_eval (y);
}
/* 6. Check if expo = "X^(f.x) --> (X^x)^f" */
else if (MAY_TYPE (expo) == MAY_FACTOR_T
&& MAY_TYPE (MAY_AT (expo, 0)) == MAY_INT_T) {
may_t newbase = may_pow_c (base, MAY_AT (expo, 1));
y = x;
MAY_SET_AT (y, 0, newbase);
MAY_SET_AT (y, 1, MAY_AT (expo, 0) );
y = may_eval (y);
MAY_ASSERT (MAY_TYPE (MAY_AT (y, 1)) != MAY_FACTOR_T);
}
/* 7. If base=abs and expo even integer and base real, remove abs */
else if (MAY_TYPE (base) == MAY_ABS_T
&& may_even_p (expo) && may_real_p (MAY_AT (base, 0))) {
y = x;
MAY_SET_AT (y, 0, MAY_AT (base, 0));
MAY_SET_AT (y, 1, expo);
y = may_eval (y);
}
/* 8. Else generic */
else {
if (MAY_UNLIKELY (MAY_EXT_P (base) || MAY_EXT_P (expo))) {
y = may_eval_extension_pow (base, expo);
} else {
y = x;
MAY_SET_AT (y, 0, base);
MAY_SET_AT (y, 1, expo);
MAY_CLOSE_C (y, MAY_FLAGS(base) & MAY_FLAGS(expo),
MAY_NEW_HASH2 (base, expo) );
}
}
}
return y;
}
MAY_REGPARM may_t
may_eval (may_t x)
{
may_t y;
MAY_ASSERT (x != NULL);
MAY_ASSERT (MAY_TYPE (x) != MAY_INDIRECT_T || !MAY_EVAL_P (x));
MAY_ASSERT (MAY_TYPE (x) <= MAY_END_LIMIT+may_c.extension_size);
/* If x is already evaluated */
if (MAY_LIKELY (MAY_EVAL_P (x))) {
MAY_ASSERT (may_recompute_hash (x) == MAY_HASH (x));
return x;
}
/***** The big switch ******/
switch (MAY_TYPE (x)) {
/* The nums */
case MAY_INT_T:
y = may_mpz_simplify (x);
break;
case MAY_RAT_T:
y = may_mpq_simplify (x, MAY_RAT (x));
break;
case MAY_FLOAT_T:
y = may_mpfr_simplify (x);
break;
case MAY_COMPLEX_T:
y = may_cx_simplify (x);
break;
/* The other atomics */
case MAY_STRING_T:
y = x;
MAY_CLOSE_C (y, MAY_EVAL_F, may_string_hash (MAY_NAME (x)));
break;
case MAY_DATA_T:
y = x;
MAY_CLOSE_C (y, MAY_EVAL_F,
may_data_hash ((const char*)MAY_DATA (x).data,
MAY_DATA (x).size));
break;
case MAY_INDIRECT_T:
MAY_ASSERT (MAY_INDIRECT (x) != x);
x = MAY_INDIRECT (x);
MAY_ASSERT (MAY_EVAL_P (x) && MAY_TYPE (x) != MAY_INDIRECT_T);
return x; /* It is forbidden to go down: return immediately */
/* The most importants types: eval must be fast for them */
case MAY_SUM_T:
case MAY_SUM_RESERVE_T:
y = may_eval_sum (x);
break;
case MAY_FACTOR_T:
case MAY_PRODUCT_T:
case MAY_PRODUCT_RESERVE_T:
y = may_eval_product (x);
break;
case MAY_POW_T:
y = may_eval_pow (x);
break;
case MAY_RANGE_T:
y = may_eval_range (x);
break;
case MAY_EXP_T:
MAY_ASSERT (MAY_NODE_SIZE(x) == 1);
y = may_eval_exp (eval_outside_intmod(MAY_AT (x, 0)), x);
break;
case MAY_LOG_T:
MAY_ASSERT (MAY_NODE_SIZE(x) == 1);
y = may_eval_log (eval_outside_intmod(MAY_AT (x, 0)), x);
break;
case MAY_COS_T:
MAY_ASSERT (MAY_NODE_SIZE(x) == 1);
y = may_eval_cos (eval_outside_intmod(MAY_AT (x, 0)), x);
break;
case MAY_SIN_T:
MAY_ASSERT (MAY_NODE_SIZE(x) == 1);
y = may_eval_sin (eval_outside_intmod(MAY_AT (x, 0)), x);
break;
case MAY_TAN_T:
MAY_ASSERT (MAY_NODE_SIZE(x) == 1);
y = may_eval_tan (eval_outside_intmod(MAY_AT (x, 0)), x);
break;
case MAY_ASIN_T:
MAY_ASSERT (MAY_NODE_SIZE(x) == 1);
y = may_eval_asin (eval_outside_intmod(MAY_AT (x, 0)), x);
break;
case MAY_ACOS_T:
MAY_ASSERT (MAY_NODE_SIZE(x) == 1);
y = may_eval_acos (eval_outside_intmod(MAY_AT (x, 0)), x);
break;
case MAY_ATAN_T:
MAY_ASSERT (MAY_NODE_SIZE(x) == 1);
y = may_eval_atan (eval_outside_intmod(MAY_AT (x, 0)), x);
break;
case MAY_COSH_T:
MAY_ASSERT (MAY_NODE_SIZE(x) == 1);
y = may_eval_cosh (eval_outside_intmod(MAY_AT (x, 0)), x);
break;
case MAY_SINH_T:
MAY_ASSERT (MAY_NODE_SIZE(x) == 1);
y = may_eval_sinh (eval_outside_intmod(MAY_AT (x, 0)), x);
break;
case MAY_TANH_T:
MAY_ASSERT (MAY_NODE_SIZE(x) == 1);
y = may_eval_tanh (eval_outside_intmod(MAY_AT (x, 0)), x);
break;
case MAY_ASINH_T:
MAY_ASSERT (MAY_NODE_SIZE(x) == 1);
y = may_eval_asinh (eval_outside_intmod(MAY_AT (x, 0)), x);
break;
case MAY_ACOSH_T:
MAY_ASSERT (MAY_NODE_SIZE(x) == 1);
y = may_eval_acosh (eval_outside_intmod(MAY_AT (x, 0)), x);
break;
case MAY_ATANH_T:
MAY_ASSERT (MAY_NODE_SIZE(x) == 1);
y = may_eval_atanh (eval_outside_intmod(MAY_AT (x, 0)), x);
break;
case MAY_FLOOR_T:
MAY_ASSERT (MAY_NODE_SIZE(x) == 1);
y = may_eval_floor (may_eval (MAY_AT (x, 0)), x);
break;
case MAY_SIGN_T:
MAY_ASSERT (MAY_NODE_SIZE(x) == 1);
y = may_eval_sign (eval_outside_intmod(MAY_AT (x, 0)), x);
break;
case MAY_GAMMA_T:
MAY_ASSERT (MAY_NODE_SIZE(x) == 1);
y = may_eval_gamma (eval_outside_intmod(MAY_AT (x, 0)), x);
break;
case MAY_CONJ_T:
MAY_ASSERT (MAY_NODE_SIZE(x) == 1);
y = may_eval_conj (may_eval (MAY_AT (x, 0)), x);
break;
case MAY_REAL_T:
MAY_ASSERT (MAY_NODE_SIZE(x) == 1);
y = may_eval_real (may_eval (MAY_AT (x, 0)), x);
break;
case MAY_IMAG_T:
MAY_ASSERT (MAY_NODE_SIZE(x) == 1);
y = may_eval_imag (may_eval (MAY_AT (x, 0)), x);
break;
case MAY_ARGUMENT_T:
MAY_ASSERT (MAY_NODE_SIZE(x) == 1);
y = may_eval_argument (eval_outside_intmod(MAY_AT (x, 0)), x);
break;
case MAY_ABS_T:
MAY_ASSERT (MAY_NODE_SIZE(x) == 1);
{
may_t gcd, z = may_eval (MAY_AT (x, 0));
int s;
y = NULL;
/* If Pure Numerical, do it. For complex, we may go outside purenum,
so we can't use may_num_simplify. */
if (MAY_PURENUM_P (z))
y = may_num_pos_p (z) ? z
: may_eval (may_num_abs (MAY_DUMMY, z));
/* If Num(x), try to eval the sign */
else if (MAY_NUM_P (z) && (s = may_compute_sign (z)) != 0)
y = (s&4) ? may_neg (z) : z;
/* if Product, expand it -- FIXME: Good idea? */
else if (MAY_TYPE (z) == MAY_FACTOR_T
|| MAY_TYPE (z) == MAY_PRODUCT_T)
y = may_eval (may_map_c (z, may_abs_c));
else if (MAY_TYPE (z) == MAY_POW_T
&& MAY_TYPE (MAY_AT (z, 1)) == MAY_INT_T) {
y = may_pow_c (may_abs_c (MAY_AT (z, 0)), MAY_AT (z, 1));
y = may_eval (y);
}
else if (MAY_TYPE (z) == MAY_SUM_T
&& (gcd = extract_constant_coefficient_from_sum (z))
!= MAY_ONE) {
y = fix_sum_after_extracting_coefficient (z, gcd);
y = may_mul_c (may_abs_c (gcd), may_abs_c (y));
y = may_eval (y);
}
/* Support of generalised expressions through predicates */
else if (may_real_p (z)) {
if (may_nonneg_p (z))
y = z;
else if (may_nonpos_p (z))
y = may_neg (z);
}
/* Else Rebuild it */
if (y == NULL) {
y = x;
MAY_SET_AT (y, 0, z);
MAY_CLOSE_C (y, MAY_FLAGS (z), MAY_HASH (z));
}
break;
}
case MAY_DIFF_T:
/* Always reeval */
y = may_eval_diff (x);
break;
case MAY_FUNC_T:
MAY_ASSERT (MAY_NODE_SIZE(x) == 2);
{
may_t func = MAY_AT (x, 0); /*FIXME: Shouldn't we eval it? */
may_t z = eval_outside_intmod(MAY_AT (x, 1));
/* Rebuild it */
y = x;
MAY_SET_AT (y, 0, func);
MAY_SET_AT (y, 1, z);
MAY_CLOSE_C (y, MAY_EVAL_F, MAY_NEW_HASH2 (func, z));
}
break;
case MAY_GCD_T:
MAY_ASSERT (MAY_NODE_SIZE(x) == 2);
{
may_t temp[2];
temp[0] = may_eval (MAY_AT (x, 0));
temp[1] = may_eval (MAY_AT (x, 1));
y = may_gcd (2, temp);
/* We return y*gcd(a/y,b/y) except if we know gcd(a/y,b/y) is one */
if (y != MAY_ONE) {
temp[0] = may_divexact (temp[0], y);
temp[1] = may_divexact (temp[1], y);
}
if (temp[0] != MAY_ONE && temp[1] != MAY_ONE
&& !(MAY_PURENUM_P (temp[0]) && MAY_PURENUM_P (temp[1])))
y = may_mul (y, may_hold (may_gcd_c (temp[0], temp[1])));
}
break;
case MAY_MOD_T:
MAY_ASSERT (MAY_NODE_SIZE(x) == 2);
{
may_t z1 = may_eval (MAY_AT (x, 0)),
z2 = may_eval (MAY_AT (x, 1));
/* Rebuild it */
y = x;
MAY_SET_AT (y, 0, z1);
MAY_SET_AT (y, 1, z2);
MAY_CLOSE_C (y, MAY_EVAL_F, MAY_NEW_HASH2 (z1, z2));
}
break;
default:
/* Check if this is an extension */
if (MAY_LIKELY (MAY_EXT_P (x)))
y = may_eval_extension (x);
else
/* Not a registered type. Not a fatal error (assert) since
it may come due to registering an extension, building an expression,
unregistering the extension, and reevaluating the expression... */
MAY_THROW (MAY_INVALID_TOKEN_ERR);
}
MAY_ASSERT (MAY_EVAL_P (y));
MAY_ASSERT (may_recompute_hash (y) == MAY_HASH (y));
/* Cache the evaluation by modifing the input if possible */
if (MAY_UNLIKELY (x != y))
MAY_SET_INDIRECT (x, y);
return y;
}
/* Check the order of a sum or a product */
#ifdef MAY_WANT_ASSERT
static int
check_sum_order (may_t x)
{
may_size_t i, n;
may_t a, b;
MAY_ASSERT (MAY_TYPE (x) == MAY_SUM_T);
n = MAY_NODE_SIZE(x);
//printf ("Start %d\n", n);
for (i=1+MAY_PURENUM_P (MAY_AT (x, 0)); i<n; i++) {
a = MAY_AT (x, i-1);
b = MAY_AT (x, i);
a = MAY_TYPE (a) == MAY_FACTOR_T ? MAY_AT (a, 1) : a;
b = MAY_TYPE (b) == MAY_FACTOR_T ? MAY_AT (b, 1) : b;
//printf ("%04X ", (unsigned int) MAY_HASH (a)); may_dump (a);
if (may_identical (a, b) >= 0) {
//printf ("%04X ", (unsigned int) MAY_HASH (b)); may_dump (b);
printf ("!!! Error in sorting !!!\n");
return 0;
}
}
//printf ("%04X ", (unsigned int) MAY_HASH (MAY_AT (x, n-1))); may_dump (MAY_AT (x, n-1));
return 1;
}
#else
#define check_sum_order(x) 1
#endif
/* From begin to end, sum all the num values */
static MAY_REGPARM may_t
may_SumNum (may_pair_t *begin, may_pair_t *end)
{
may_pair_t *it;
may_t sum;
/* Compute trivial case */
it = begin;
MAY_ASSERT (it < end);
if (MAY_LIKELY (it+1 == end))
return it->first;
sum = may_num_add (MAY_DUMMY, it->first, (it+1)->first);
for (it += 2; it != end; it++)
sum = may_num_add (sum, sum, it->first);
return may_num_simplify (sum);
}
#define CMP(i,j) cmp_pair (&tab[i], &tab[j])
#define SWAP(i, j) do {may_pair_t tmp = tab[i]; tab[i] = tab[j]; tab[j]=tmp;} while (0)
#define ROLL(i,j,k) do {may_pair_t tmp = tab[i]; tab[i] = tab[j]; tab[j] = tab[k]; tab[k] = tmp;} while (0)
MAY_INLINE int
cmp_pair (may_pair_t *x, may_pair_t *y)
{
int i;
may_t xs = x->second, ys = y->second;
if (MAY_UNLIKELY (xs == ys))
return 0;
i = may_identical (xs, ys);
/* If the expressions were identical, then we speed up next computing */
if (MAY_UNLIKELY (i == 0)) {
if ( (void*) xs < (void*) ys)
y->second = xs;
else
x->second = ys;
}
return i;
}
static void
sort_pair2 (may_pair_t *tab, may_size_t size, may_pair_t *t)
{
/* If small enought, do an inline sort */
if (MAY_LIKELY (size <= 4)) {
if (MAY_UNLIKELY (size < 2))
return;
else if (size == 2) {
if (CMP (0, 1) > 0)
SWAP (0, 1);
return;
} else if (size == 3) {
if (CMP (0, 1) > 0)
SWAP (0 ,1);
if (CMP (1, 2) <= 0)
return;
if (CMP (0, 2) > 0)
ROLL (2, 1, 0);
else
SWAP (1, 2);
return;
} else {
MAY_ASSERT (size == 4);
#if 1
if (CMP (0, 1) > 0)
SWAP (0 ,1);
if (CMP (2, 3) > 0)
SWAP (2, 3);
if (CMP (1, 2) <= 0)
return;
if (CMP (0, 2) <= 0) {
SWAP (1, 2);
if (CMP (2, 3) > 0)
SWAP (2, 3);
} else if (CMP (0, 3) <= 0) {
ROLL (2, 1, 0);
if (CMP (2, 3) > 0)
SWAP (2, 3);
} else {
SWAP (0, 2);
SWAP (1, 3);
}
return;
#else
if (CMP (0, 1) > 0)
SWAP (0 ,1);
if (CMP (2, 3) > 0)
SWAP (2, 3);
if (CMP (0, 2) > 0) {
SWAP (0, 2);
SWAP (1, 3);
}
if (CMP (1, 2) <= 0)
return;
if (CMP (1, 3) > 0)
ROLL (1, 2, 3);
else
SWAP (1, 2);
return;
#endif
}
MAY_ASSERT (0);
}
/* Do a merge sort */
may_pair_t *tmp, *tab1, *tab2;
may_size_t n1, n2;
n1 = size / 2;
n2 = size - n1;
tab1 = tab;
tab2 = tab + n1;
sort_pair2 (tab1, n1, t);
sort_pair2 (tab2, n2, t);
tmp = t;
for (;;) {
if (cmp_pair (tab1, tab2) <= 0) {
*tmp++ = *tab1++;
if (MAY_UNLIKELY (-- n1 == 0))
break;
} else {
*tmp++ = *tab2++;
if (MAY_UNLIKELY (-- n2 == 0)) {
if (n1 > 0)
memcpy (tmp, tab1, n1 * sizeof (may_pair_t));
break;
}
}
}
memcpy (tab, t, (size - n2) * sizeof (may_pair_t));
}
#if 0
static void
merge_pairs (may_pair_t *tab, may_size_t n1, may_size_t n2)
{
may_pair_t *tmp, *t, *tab1, *tab2;
may_size_t size = n1+n2;
if (n1 == 0 || n2 == 0)
return;
MAY_RECORD ();
tmp = t = may_alloc (size * sizeof * tmp);
tab1 = tab;
tab2 = &tab[n1];
for (;;) {
if (cmp_pair (tab1, tab2) <= 0) {
*tmp++ = *tab1++;
if (MAY_UNLIKELY (-- n1 == 0))
break;
} else {
*tmp++ = *tab2++;
if (MAY_UNLIKELY (-- n2 == 0)) {
if (n1 > 0)
memcpy (tmp, tab1, n1 * sizeof (may_pair_t));
break;
}
}
}
memcpy (tab, t, (size - n2) * sizeof (may_pair_t));
MAY_CLEANUP ();
}
#endif
// Note: see how to perform a radix like sort?
static void
sort_pair (may_pair_t *tab, may_size_t size)
{
may_pair_t *it, *t;
may_size_t i, m, rank;
may_hash_t previous;
/* Alloc the temporary table inside MAY stack to avoid
a system stack overflow (MAY tries to enlarge it if need is) */
MAY_RECORD ();
t = MAY_ALLOC (size * sizeof *t);
if (MAY_LIKELY (size <= MAY_SORT_THRESHOLD1)
|| (size >= MAY_SORT_THRESHOLD2 && size <= MAY_SORT_THRESHOLD3)) {
sort_pair2 (tab, size, t);
MAY_CLEANUP ();
return;
}
MAY_ASSERT ((MAY_HASH_MAX & (MAY_HASH_MAX-1)) == 0);
/* Do a count sort first */
unsigned int count_tab[MAY_HASH_MAX];
size_t size_hash_tab = MAY_SIZE_IN_BITS (size);
size_hash_tab = MIN ( (2U << size_hash_tab) - 1, MAY_HASH_MAX-1);
int shift_hash = MAY_SIZE_IN_BITS (MAY_HASH_MAX-1)
- MAY_SIZE_IN_BITS (size_hash_tab);
memset (count_tab, 0, (size_hash_tab+1) * sizeof (unsigned int));
if (MAY_UNLIKELY (shift_hash <= 0)) {
/* No shift for the HASH table. Huge table ==> inline the code */
for (it = tab, m = size; m -- > 0; )
count_tab[(unsigned int) MAY_HASH ((*it++).second)] ++;
for (i = 1; i <= size_hash_tab; i++)
count_tab[i] += count_tab[i-1];
for (it = tab, m = size; m -- > 0; ) {
rank = --count_tab[(unsigned int)MAY_HASH (it->second)];
t[rank] = *it++;
}
/* Merge the remaining items */
previous = MAY_HASH (t[0].second);
for (i = 1; i < size; i++) {
MAY_ASSERT (previous <= (MAY_HASH (t[i].second)));
if (previous == MAY_HASH (t[i].second)) {
may_size_t j;
for (j = i+1; j < size
&& MAY_HASH (t[j].second) == previous; j++);
sort_pair2 (&t[i-1], j-i+1, &tab[i-1]); /* Hick... slow down 2x*/
if (MAY_UNLIKELY (j == size))
break;
i = j;
}
previous = MAY_HASH (t[i].second);
}
} else {
for (it = tab, m = size; m -- > 0; )
count_tab[(unsigned int) MAY_HASH ((*it++).second) >> shift_hash] ++;
for (i = 1; i <= size_hash_tab; i++)
count_tab[i] += count_tab[i-1];
for (it = tab, m = size; m -- > 0; ) {
rank = --count_tab[(unsigned int)MAY_HASH (it->second) >> shift_hash];
t[rank] = *it++;
}
/* Merge the remaining items */
previous = MAY_HASH (t[0].second) >> shift_hash;
for (i = 1; i < size; i++) {
MAY_ASSERT (previous <= (MAY_HASH (t[i].second)>>shift_hash));
if (previous == (MAY_HASH (t[i].second) >> shift_hash)) {