forked from vermaseren/form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloat.c
2559 lines (2385 loc) · 67.5 KB
/
float.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
/** @file float.c
*
* This file contains numerical routines that deal with floating point numbers.
* We use the GMP for arbitrary precision floating point arithmetic.
* The functions of the type sin, cos, ln, sqrt etc are handled by the
* mpfr library. The reason that for MZV's and the general notation we use
* the GMP (mpf_) is because the contents of the structs have been frozen
* and can be used for storage in a Form function float_. With mpfr_ this
* is not safely possible. All mpfr_ related code is in the file evaluate.c.
*/
/* #[ License : */
/*
* Copyright (C) 1984-2023 J.A.M. Vermaseren
* When using this file you are requested to refer to the publication
* J.A.M.Vermaseren "New features of FORM" math-ph/0010025
* This is considered a matter of courtesy as the development was paid
* for by FOM the Dutch physics granting agency and we would like to
* be able to track its scientific use to convince FOM of its value
* for the community.
*
* This file is part of FORM.
*
* FORM is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* FORM 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 General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along
* with FORM. If not, see <http://www.gnu.org/licenses/>.
*/
/* #] License : */
/*
#[ Includes : float.c
*/
#include "form3.h"
#include <math.h>
#include <gmp.h>
#define WITHCUTOFF
#define GMPSPREAD (GMP_LIMB_BITS/BITSINWORD)
void Form_mpf_init(mpf_t t);
void Form_mpf_clear(mpf_t t);
void Form_mpf_set_prec_raw(mpf_t t,ULONG newprec);
void FormtoZ(mpz_t z,UWORD *a,WORD na);
void ZtoForm(UWORD *a,WORD *na,mpz_t z);
long FloatToInteger(UWORD *out, mpf_t floatin, long *bitsused);
void IntegerToFloat(mpf_t result, UWORD *formlong, int longsize);
int FloatToRat(UWORD *ratout, WORD *nratout, mpf_t floatin);
int SetFloatPrecision(WORD prec);
int AddFloats(PHEAD WORD *fun3, WORD *fun1, WORD *fun2);
int MulFloats(PHEAD WORD *fun3, WORD *fun1, WORD *fun2);
int DivFloats(PHEAD WORD *fun3, WORD *fun1, WORD *fun2);
int AddRatToFloat(PHEAD WORD *outfun, WORD *infun, UWORD *formrat, WORD nrat);
int MulRatToFloat(PHEAD WORD *outfun, WORD *infun, UWORD *formrat, WORD nrat);
void SimpleDelta(mpf_t sum, int m);
void SimpleDeltaC(mpf_t sum, int m);
void SingleTable(mpf_t *tabl, int N, int m, int pow);
void DoubleTable(mpf_t *tabout, mpf_t *tabin, int N, int m, int pow);
void EndTable(mpf_t sum, mpf_t *tabin, int N, int m, int pow);
void deltaMZV(mpf_t, int *, int);
void deltaEuler(mpf_t, int *, int);
void deltaEulerC(mpf_t, int *, int);
void CalculateMZVhalf(mpf_t, int *, int);
void CalculateMZV(mpf_t, int *, int);
void CalculateEuler(mpf_t, int *, int);
int ExpandMZV(WORD *term, WORD level);
int ExpandEuler(WORD *term, WORD level);
int PackFloat(WORD *,mpf_t);
int UnpackFloat(mpf_t, WORD *);
void RatToFloat(mpf_t result, UWORD *formrat, int ratsize);
/*
#] Includes :
#[ Some GMP float info :
From gmp.h:
typedef struct
{
int _mp_prec; Max precision, in number of `mp_limb_t's.
Set by mpf_init and modified by
mpf_set_prec. The area pointed to by the
_mp_d field contains `prec' + 1 limbs.
int _mp_size; abs(_mp_size) is the number of limbs the
last field points to. If _mp_size is
negative this is a negative number.
mp_exp_t _mp_exp; Exponent, in the base of `mp_limb_t'.
mp_limb_t *_mp_d; Pointer to the limbs.
} __mpf_struct;
typedef __mpf_struct mpf_t[1];
Currently:
sizeof(int) = 4
sizeof(mpf_t) = 24
sizeof(mp_limb_t) = 8,
sizeof(mp_exp_t) = 8,
sizeof a pointer is 8.
If any of this changes in the future we would have to change the
PackFloat and UnpackFloat routines.
Our float_ function is packed with the 4 arguments:
-SNUMBER _mp_prec
-SNUMBER _mp_size
exponent which can be -SNUMBER or a regular term
the limbs as n/1 in regular term format, or just an -SNUMBER.
During normalization the sign is taken away from the second argument
and put as the sign of the complete term. This is easier for the
WriteInnerTerm routine in sch.c
Wildcarding of functions excludes matches with float_
Float_ always ends up inside the bracket, just like poly(rat)fun.
From mpfr.h
The formats here are different. This has, among others, to do with
the rounding. The result is that we need different aux variables
when working with mpfr and we need a different conversion routine
to float. It also means that we need to treat the mzv_ etc functions
completely separately. For now we try to develop that in the file
Evaluate.c. At a later stage we may merge the two files.
Originally we had the sqrt in float.c but it seems better to put
it in Evaluate.c
#] Some GMP float info :
#[ Low Level :
In the low level routines we interact directly with the content
of the GMP structs. This can be done safely, because their
layout is documented. We pay particular attention to the init
and clear routines, because they involve malloc/free calls.
#[ Explanations :
We need a number of facilities inside Form to deal with floating point
numbers, taking into account that they are only meant for sporadic use.
1: We need a special function float_ who's arguments contain a limb
representation of the mpf_t of the gmp.
2: Some functions may return a floating point number. This is then in
the form of an occurrence of the function float_.
3: We need a print routine to print this float_.
4: We need routines for conversions:
a: (long) int to float.
b: rat to float.
c: float to rat (if possible)
d: float to integer (with rounding toward zero).
5: We need calculational operations for add, sub, mul, div, exp.
6: We need service routines to pack and unpack the function float_.
7: The coefficient should be pulled inside float_.
8: Float_ cannot occur inside PolyRatFun.
9: We need to be able to treat float_ as the coefficient during sorting.
10: For now, we need the functions mzvf_ and eulerf_ for the evaluation
of mzv's and euler sums.
11: We need a setting for the default precision.
12: We need a special flag for the dirty field of arguments to avoid
the normalization of the argument with the limbs.
Alternatively, the float_ should be not a regular function, but
get a status < FUNCTION. Just like SNUMBER, LNUMBER etc.
Note that we can keep this thread-safe. The only routine that is not
thread-safe is the print routine, but that is in accordance with all
other print routines in Form. When called from MesPrint it needs to
be protected by a lock, as is done standard inside Form.
#] Explanations :
#[ Form_mpf_init :
*/
void Form_mpf_init(mpf_t t)
{
mp_limb_t *d;
int i, prec;
if ( t->_mp_d ) { M_free(t->_mp_d,"Form_mpf_init"); t->_mp_d = 0; }
prec = (AC.DefaultPrecision + 8*sizeof(mp_limb_t)-1)/(8*sizeof(mp_limb_t)) + 1;
t->_mp_prec = prec;
t->_mp_size = 0;
t->_mp_exp = 0;
d = (mp_limb_t *)Malloc1(prec*sizeof(mp_limb_t),"Form_mpf_init");
if ( d == 0 ) {
MesPrint("Fatal error in Malloc1 call in Form_mpf_init. Trying to allocate %ld bytes.",
prec*sizeof(mp_limb_t));
Terminate(-1);
}
t->_mp_d = d;
for ( i = 0; i < prec; i++ ) d[i] = 0;
}
/*
#] Form_mpf_init :
#[ Form_mpf_clear :
*/
void Form_mpf_clear(mpf_t t)
{
if ( t->_mp_d ) { M_free(t->_mp_d,"Form_mpf_init"); t->_mp_d = 0; }
t->_mp_prec = 0;
t->_mp_size = 0;
t->_mp_exp = 0;
}
/*
#] Form_mpf_clear :
#[ Form_mpf_empty :
*/
void Form_mpf_empty(mpf_t t)
{
int i;
for ( i = 0; i < t->_mp_prec; i++ ) t->_mp_d[i] = 0;
t->_mp_size = 0;
t->_mp_exp = 0;
}
/*
#] Form_mpf_empty :
#[ Form_mpf_set_prec_raw :
*/
void Form_mpf_set_prec_raw(mpf_t t,ULONG newprec)
{
ULONG newpr = (newprec + 8*sizeof(mp_limb_t)-1)/(8*sizeof(mp_limb_t)) + 1;
if ( newpr <= (ULONG)(t->_mp_prec) ) {
int i, used = ABS(t->_mp_size) ;
if ( newpr > (ULONG)used ) {
for ( i = used; i < (int)newpr; i++ ) t->_mp_d[i] = 0;
}
if ( t->_mp_size < 0 ) newpr = -newpr;
t->_mp_size = newpr;
}
else {
/*
We can choose here between "forget about it" or making a new malloc.
If the program is well designed, we should never get here though.
For now we choose the "forget it" option.
*/
MesPrint("Trying too big a precision %ld in Form_mpf_set_prec_raw.",newprec);
MesPrint("Old maximal precision is %ld.",
(size_t)(t->_mp_prec-1)*sizeof(mp_limb_t)*8);
Terminate(-1);
}
}
/*
#] Form_mpf_set_prec_raw :
#[ PackFloat :
Puts an object of type mpf_t inside a function float_
float_(prec, nlimbs, exp, limbs);
Complication: the limbs and the exponent are long's. That means
two times the size of a Form (U)WORD.
To save space we could split the limbs in two because Form stores
coefficients as rationals with equal space for numerator and denominator.
Hence for each limb we could put the most significant UWORD in the
numerator and the least significant limb in the denominator.
This gives a problem with the compilation and subsequent potential
normalization of the 'fraction'. Hence for now we take the wasteful
approach. At a later stage we can try to veto normalization of FLOATFUN.
Caution: gmp/fmp is little endian and Form is big endian.
Zero is represented by zero limbs (and zero exponent).
*/
int PackFloat(WORD *fun,mpf_t infloat)
{
WORD *t, nlimbs, num, nnum;
mp_limb_t *d = infloat->_mp_d; /* Pointer to the limbs. */
int i;
long e = infloat->_mp_exp;
t = fun;
*t++ = FLOATFUN;
t++;
FILLFUN(t);
*t++ = -SNUMBER;
*t++ = infloat->_mp_prec;
*t++ = -SNUMBER;
*t++ = infloat->_mp_size;
/*
Now the exponent which is a signed long
*/
if ( e < 0 ) {
e = -e;
if ( ( e >> (BITSINWORD-1) ) == 0 ) {
*t++ = -SNUMBER; *t++ = -e;
}
else {
*t++ = ARGHEAD+6; *t++ = 0; FILLARG(t);
*t++ = 6;
*t++ = (UWORD)e;
*t++ = (UWORD)(e >> BITSINWORD);
*t++ = 1; *t++ = 0; *t++ = -5;
}
}
else {
if ( ( e >> (BITSINWORD-1) ) == 0 ) {
*t++ = -SNUMBER; *t++ = e;
}
else {
*t++ = ARGHEAD+6; *t++ = 0; FILLARG(t);
*t++ = 6;
*t++ = (UWORD)e;
*t++ = (UWORD)(e >> BITSINWORD);
*t++ = 1; *t++ = 0; *t++ = 5;
}
}
/*
and now the limbs. Note that the number of active limbs could be less
than prec+1 in which case we copy the smaller number.
*/
nlimbs = infloat->_mp_size < 0 ? -infloat->_mp_size: infloat->_mp_size;
if ( nlimbs == 0 ) {
}
else if ( nlimbs == 1 && (ULONG)(*d) < ((ULONG)1)<<(BITSINWORD-1) ) {
*t++ = -SNUMBER;
*t++ = (UWORD)(*d);
}
else {
if ( d[nlimbs-1] < ((ULONG)1)<<BITSINWORD ) {
num = 2*nlimbs-1; /* number of UWORDS needed */
}
else {
num = 2*nlimbs; /* number of UWORDS needed */
}
nnum = num;
*t++ = 2*num+2+ARGHEAD;
*t++ = 0;
FILLARG(t);
*t++ = 2*num+2;
while ( num > 1 ) {
*t++ = (UWORD)(*d); *t++ = (UWORD)(((ULONG)(*d))>>BITSINWORD); d++;
num -= 2;
}
if ( num == 1 ) { *t++ = (UWORD)(*d); }
*t++ = 1;
for ( i = 1; i < nnum; i++ ) *t++ = 0;
*t++ = 2*nnum+1; /* the sign is hidden in infloat->_mp_size */
}
fun[1] = t-fun;
return(fun[1]);
}
/*
#] PackFloat :
#[ UnpackFloat :
Takes the arguments of a function float_ and puts it inside an mpf_t.
For notation see commentary for PutInFloat.
We should make a new allocation for the limbs if the precision exceeds
the present precision of outfloat, or when outfloat has not been
initialized yet.
The return value is -1 if the contents of the FLOATFUN cannot be converted.
Otherwise the return value is the number of limbs.
*/
int UnpackFloat(mpf_t outfloat,WORD *fun)
{
UWORD *t;
WORD *f;
int num, i;
mp_limb_t *d;
/*
Very first step: check whether we can use float_:
*/
GETIDENTITY
if ( AT.aux_ == 0 ) {
MesPrint("Illegal attempt at using a float_ function without proper startup.");
MesPrint("Please use %#StartFloat <options> first.");
Terminate(-1);
}
/*
Check first the number and types of the arguments
There should be 4. -SNUMBER,-SNUMBER,-SNUMBER or a long number.
+ the limbs, either -SNUMBER or Long number in the form of a Form rational.
*/
if ( TestFloat(fun) == 0 ) goto Incorrect;
f = fun + FUNHEAD + 2;
if ( ABS(f[1]) > f[-1]+1 ) goto Incorrect;
if ( f[1] > outfloat->_mp_prec+1
|| outfloat->_mp_d == 0 ) {
/*
We need to make a new allocation.
Unfortunately we cannot use Malloc1 because that is not
recognised by gmp and hence we cannot clear with mpf_clear.
Maybe we can do something about it by making our own
mpf_init and mpf_clear?
*/
if ( outfloat->_mp_d != 0 ) free(outfloat->_mp_d);
outfloat->_mp_d = (mp_limb_t *)malloc((f[1]+1)*sizeof(mp_limb_t));
}
num = f[1];
outfloat->_mp_size = num;
if ( num < 0 ) { num = -num; }
f += 2;
if ( *f == -SNUMBER ) {
outfloat->_mp_exp = (mp_exp_t)(f[1]);
f += 2;
}
else {
f += ARGHEAD+6;
if ( f[-1] == -5 ) {
outfloat->_mp_exp =
-(mp_exp_t)((((ULONG)(f[-4]))<<BITSINWORD)+f[-5]);
}
else if ( f[-1] == 5 ) {
outfloat->_mp_exp =
(mp_exp_t)((((ULONG)(f[-4]))<<BITSINWORD)+f[-5]);
}
}
/*
And now the limbs if needed
*/
d = outfloat->_mp_d;
if ( outfloat->_mp_size == 0 ) {
for ( i = 0; i < outfloat->_mp_prec; i++ ) *d++ = 0;
return(0);
}
else if ( *f == -SNUMBER ) {
*d++ = (mp_limb_t)f[1];
for ( i = 0; i < outfloat->_mp_prec; i++ ) *d++ = 0;
return(0);
}
num = (*f-ARGHEAD-2)/2; /* 2*number of limbs in the argument */
t = (UWORD *)(f+ARGHEAD+1);
while ( num > 1 ) {
*d++ = (mp_limb_t)((((ULONG)(t[1]))<<BITSINWORD)+t[0]);
t += 2; num -= 2;
}
if ( num == 1 ) *d++ = (mp_limb_t)((UWORD)(*t));
for ( i = d-outfloat->_mp_d; i <= outfloat->_mp_prec; i++ ) *d++ = 0;
return(0);
Incorrect:
return(-1);
}
/*
#] UnpackFloat :
#[ TestFloat :
Tells whether the function (with its arguments) is a legal float_.
We assume, as in the whole float library that one limb is 2 WORDs.
*/
int TestFloat(WORD *fun)
{
WORD *fstop, *f, num, nnum, i;
fstop = fun+fun[1];
f = fun + FUNHEAD;
num = 0;
/*
Count arguments
*/
while ( f < fstop ) { num++; NEXTARG(f); }
if ( num != 4 ) return(0);
f = fun + FUNHEAD;
if ( *f != -SNUMBER ) return(0);
if ( f[1] < 0 ) return(0);
f += 2;
if ( *f != -SNUMBER ) return(0);
num = ABS(f[1]); /* number of limbs */
f += 2;
if ( *f == -SNUMBER ) { f += 2; }
else {
if ( *f != ARGHEAD+6 ) return(0);
if ( ABS(f[ARGHEAD+5]) != 5 ) return(0);
if ( f[ARGHEAD+3] != 1 ) return(0);
if ( f[ARGHEAD+4] != 0 ) return(0);
f += *f;
}
if ( num == 0 ) return(1);
if ( *f == -SNUMBER ) { if ( num != 1 ) return(0); }
else {
nnum = (ABS(f[*f-1])-1)/2;
if ( ( *f != 4*num+2+ARGHEAD ) && ( *f != 4*num+ARGHEAD ) ) return(0);
if ( ( nnum != 2*num ) && ( nnum != 2*num-1 ) ) return(0);
f += ARGHEAD+nnum+1;
if ( f[0] != 1 ) return(0);
for ( i = 1; i < nnum; i++ ) {
if ( f[i] != 0 ) return(0);
}
}
return(1);
}
/*
#] TestFloat :
#[ FormtoZ :
Converts a Form long integer to a GMP long integer.
typedef struct
{
int _mp_alloc; Number of *limbs* allocated and pointed
to by the _mp_d field.
int _mp_size; abs(_mp_size) is the number of limbs the
last field points to. If _mp_size is
negative this is a negative number.
mp_limb_t *_mp_d;Pointer to the limbs.
} __mpz_struct;
typedef __mpz_struct mpz_t[1];
*/
void FormtoZ(mpz_t z,UWORD *a,WORD na)
{
int nlimbs, sgn = 1;
mp_limb_t *d;
UWORD *b = a;
WORD nb = na;
if ( nb == 0 ) {
z->_mp_size = 0;
z->_mp_d[0] = 0;
return;
}
if ( nb < 0 ) { sgn = -1; nb = -nb; }
nlimbs = (nb+1)/2;
if ( mpz_cmp_si(z,0L) <= 1 ) {
mpz_set_ui(z,10000000);
}
if ( z->_mp_alloc <= nlimbs ) {
mpz_t zz;
mpz_init(zz);
while ( z->_mp_alloc <= nlimbs ) {
mpz_mul(zz,z,z);
mpz_set(z,zz);
}
mpz_clear(zz);
}
z->_mp_size = sgn*nlimbs;
d = z->_mp_d;
while ( nb > 1 ) {
*d++ = (((mp_limb_t)(b[1]))<<BITSINWORD)+(mp_limb_t)(b[0]);
b += 2; nb -= 2;
}
if ( nb == 1 ) { *d = (mp_limb_t)(*b); }
}
/*
#] FormtoZ :
#[ ZtoForm :
Converts a GMP long integer to a Form long integer.
Mainly an exercise of going from little endian ULONGs.
to big endian UWORDs
*/
void ZtoForm(UWORD *a,WORD *na,mpz_t z)
{
mp_limb_t *d = z->_mp_d;
int nlimbs = ABS(z->_mp_size), i;
UWORD j;
if ( nlimbs == 0 ) { *na = 0; return; }
*na = 0;
for ( i = 0; i < nlimbs-1; i++ ) {
*a++ = (UWORD)(*d);
*a++ = (UWORD)((*d++)>>BITSINWORD);
*na += 2;
}
*na += 1;
*a++ = (UWORD)(*d);
j = (UWORD)((*d)>>BITSINWORD);
if ( j != 0 ) { *a = j; *na += 1; }
if ( z->_mp_size < 0 ) *na = -*na;
}
/*
#] ZtoForm :
#[ FloatToInteger :
Converts a GMP float to a long Form integer.
*/
long FloatToInteger(UWORD *out, mpf_t floatin, long *bitsused)
{
mpz_t z;
WORD nout, nx;
UWORD x;
mpz_init(z);
mpz_set_f(z,floatin);
ZtoForm(out,&nout,z);
mpz_clear(z);
x = out[0]; nx = 0;
while ( x ) { nx++; x >>= 1; }
*bitsused = (nout-1)*BITSINWORD + nx;
return(nout);
}
/*
#] FloatToInteger :
#[ IntegerToFloat :
Converts a Form long integer to a gmp float of default size.
We assume that sizeof(unsigned long int) = 2*sizeof(UWORD).
*/
void IntegerToFloat(mpf_t result, UWORD *formlong, int longsize)
{
mpz_t z;
mpz_init(z);
FormtoZ(z,formlong,longsize);
mpf_set_z(result,z);
mpz_clear(z);
}
/*
#] IntegerToFloat :
#[ RatToFloat :
Converts a Form rational to a gmp float of default size.
*/
void RatToFloat(mpf_t result, UWORD *formrat, int ratsize)
{
GETIDENTITY
int nnum, nden;
UWORD *num, *den;
int sgn = 0;
if ( ratsize < 0 ) { ratsize = -ratsize; sgn = 1; }
nnum = nden = (ratsize-1)/2;
num = formrat; den = formrat+nnum;
while ( num[nnum-1] == 0 ) { nnum--; }
while ( den[nden-1] == 0 ) { nden--; }
IntegerToFloat(aux2,num,nnum);
IntegerToFloat(aux3,den,nden);
mpf_div(result,aux2,aux3);
if ( sgn > 0 ) mpf_neg(result,result);
}
/*
#] RatToFloat :
#[ FloatFunToRat :
*/
WORD FloatFunToRat(PHEAD UWORD *ratout,WORD *in)
{
WORD oldin = in[0], nratout;
in[0] = FLOATFUN;
UnpackFloat(aux4,in);
FloatToRat(ratout,&nratout,aux4);
in[0] = oldin;
return(nratout);
}
/*
#] FloatFunToRat :
#[ FloatToRat :
Converts a gmp float to a Form rational.
The algorithm we use is 'repeated fractions' of the style
n0+1/(n1+1/(n2+1/(n3+.....)))
The moment we get an n that is very large we should have the proper fraction
Main problem: what if the sequence keeps going?
(there are always rounding errors)
We need a cutoff with an error condition.
Basically the product n0*n1*n2*... is an underlimit on the number of
digits in the answer. We can put a limit on this.
A return value of zero means that everything went fine.
*/
int FloatToRat(UWORD *ratout, WORD *nratout, mpf_t floatin)
{
GETIDENTITY
WORD *out = AT.WorkPointer;
WORD s; /* the sign. */
UWORD *a, *b, *c, *d, *mc;
WORD na, nb, nc, nd, i;
int nout;
LONG oldpWorkPointer = AT.pWorkPointer;
long bitsused = 0, totalbitsused = 0, totalbits = AC.DefaultPrecision;
int retval = 0, startnul;
WantAddPointers(AC.DefaultPrecision); /* Horrible overkill */
AT.pWorkSpace[AT.pWorkPointer++] = out;
a = NumberMalloc("FloatToRat");
b = NumberMalloc("FloatToRat");
s = mpf_sgn(floatin);
if ( s < 0 ) mpf_neg(floatin,floatin);
Form_mpf_empty(aux1);
Form_mpf_empty(aux2);
Form_mpf_empty(aux3);
mpf_trunc(aux1,floatin); /* This should now be an integer */
mpf_sub(aux2,floatin,aux1); /* and this >= 0 */
if ( mpf_sgn(aux1) == 0 ) { *out++ = 0; startnul = 1; }
else {
nout = FloatToInteger((UWORD *)out,aux1,&totalbitsused);
out += nout;
startnul = 0;
}
AT.pWorkSpace[AT.pWorkPointer++] = out;
if ( mpf_sgn(aux2) ) {
for(;;) {
mpf_ui_div(aux3,1,aux2);
mpf_trunc(aux1,aux3);
mpf_sub(aux2,aux3,aux1);
if ( mpf_sgn(aux1) == 0 ) { *out++ = 0; }
else {
nout = FloatToInteger((UWORD *)out,aux1,&bitsused);
out += nout;
totalbitsused += bitsused;
}
if ( bitsused > (totalbits-totalbitsused)/2 ) { break; }
if ( mpf_sgn(aux2) == 0 ) {
/*if ( startnul == 1 )*/ AT.pWorkSpace[AT.pWorkPointer++] = out;
break;
}
AT.pWorkSpace[AT.pWorkPointer++] = out;
}
/*
At this point we have the function with the repeated fraction.
Now we should work out the fraction. Form code would be:
repeat id dum_(?a,x1?,x2?) = dum_(?a,x1+1/x2);
id dum_(x?) = x;
We have to work backwards. This is why we made a list of pointers
in AT.pWorkSpace
Now we need the long integers a and b.
Note that by construction there should never be a GCD!
*/
out = (WORD *)(AT.pWorkSpace[--AT.pWorkPointer]);
na = 1; a[0] = 1;
c = (UWORD *)(AT.pWorkSpace[--AT.pWorkPointer]);
nc = nb = ((UWORD *)out)-c;
if ( nc > 10 ) {
mc = c;
c = (UWORD *)(AT.pWorkSpace[--AT.pWorkPointer]);
nc = nb = ((UWORD *)mc)-c;
}
for ( i = 0; i < nb; i++ ) b[i] = c[i];
mc = c = NumberMalloc("FloatToRat");
while ( AT.pWorkPointer > oldpWorkPointer ) {
d = (UWORD *)(AT.pWorkSpace[--AT.pWorkPointer]);
nd = (UWORD *)(AT.pWorkSpace[AT.pWorkPointer+1])-d; /* This is the x1 in the formula */
if ( nd == 1 && *d == 0 ) break;
c = a; a = b; b = c; nc = na; na = nb; nb = nc; /* 1/x2 */
MulLong(d,nd,a,na,mc,&nc);
AddLong(mc,nc,b,nb,b,&nb);
}
NumberFree(mc,"FloatToRat");
if ( startnul == 0 ) {
c = a; a = b; b = c; nc = na; na = nb; nb = nc;
}
}
else {
c = (UWORD *)(AT.pWorkSpace[oldpWorkPointer]);
na = (UWORD *)out - c;
for ( i = 0; i < na; i++ ) a[i] = c[i];
b[0] = 1; nb = 1;
}
/*
Now we have the fraction in a/b. Create the rational and add the sign.
*/
d = ratout;
if ( na == nb ) {
*nratout = (2*na+1)*s;
for ( i = 0; i < na; i++ ) *d++ = a[i];
for ( i = 0; i < nb; i++ ) *d++ = b[i];
}
else if ( na < nb ) {
*nratout = (2*nb+1)*s;
for ( i = 0; i < na; i++ ) *d++ = a[i];
for ( ; i < nb; i++ ) *d++ = 0;
for ( i = 0; i < nb; i++ ) *d++ = b[i];
}
else {
*nratout = (2*na+1)*s;
for ( i = 0; i < na; i++ ) *d++ = a[i];
for ( i = 0; i < nb; i++ ) *d++ = b[i];
for ( ; i < na; i++ ) *d++ = 0;
}
*d = (UWORD)(*nratout);
NumberFree(b,"FloatToRat");
NumberFree(a,"FloatToRat");
AT.pWorkPointer = oldpWorkPointer;
/*
Just for check we go back to float
*/
if ( s < 0 ) mpf_neg(floatin,floatin);
/*
{
WORD n = *ratout;
RatToFloat(aux1,ratout,n);
mpf_sub(aux2,floatin,aux1);
gmp_printf("Diff is %.*Fe\n",40,aux2);
}
*/
return(retval);
}
/*
#] FloatToRat :
#[ ZeroTable :
*/
void ZeroTable(mpf_t *tab, int N)
{
int i, j;
for ( i = 0; i < N; i++ ) {
for ( j = 0; j < tab[i]->_mp_prec; j++ ) tab[i]->_mp_d[j] = 0;
}
}
/*
#] ZeroTable :
#[ ReadFloat :
Used by the compiler. It reads a floating point number and
outputs it at AT.WorkPointer as if it were a float_ function
with its arguments.
The call enters with s[-1] == TFLOAT.
*/
SBYTE *ReadFloat(SBYTE *s)
{
GETIDENTITY
SBYTE *ss, c;
ss = s;
while ( *ss > 0 ) ss++;
c = *ss; *ss = 0;
gmp_sscanf((char *)s,"%Ff",aux1);
if ( AT.WorkPointer > AT.WorkTop ) {
MLOCK(ErrorMessageLock);
MesWork();
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
PackFloat(AT.WorkPointer,aux1);
*ss = c;
return(ss);
}
/*
#] ReadFloat :
#[ CheckFloat :
For the tokenizer. Tests whether the input string can be a float.
*/
UBYTE *CheckFloat(UBYTE *ss, int *spec)
{
GETIDENTITY
UBYTE *s = ss;
int zero = 1, gotdot = 0;
while ( FG.cTable[s[-1]] == 1 ) s--;
*spec = 0;
if ( FG.cTable[*s] == 1 ) {
while ( *s == '0' ) s++;
if ( FG.cTable[*s] == 1 ) {
s++;
while ( FG.cTable[*s] == 1 ) s++;
zero = 0;
}
if ( *s == '.' ) { goto dot; }
}
else if ( *s == '.' ) {
dot:
gotdot = 1;
s++;
if ( FG.cTable[*s] != 1 && zero == 1 ) return(ss);
while ( *s == '0' ) s++;
if ( FG.cTable[*s] == 1 ) {
s++;
while ( FG.cTable[*s] == 1 ) s++;
zero = 0;
}
}
else return(ss);
/*
Now we have had the mantissa part, which may be zero.
Check for an exponent.
*/
if ( *s == 'e' || *s == 'E' ) {
s++;
if ( *s == '-' || *s == '+' ) s++;
if ( FG.cTable[*s] == 1 ) {
s++;
while ( FG.cTable[*s] == 1 ) s++;
}
else { return(ss); }
}
else if ( gotdot == 0 ) return(ss);
if ( AT.aux_ == 0 ) { /* no floating point system */
*spec = -1;
return(s);
}
if ( zero ) *spec = 1;
return(s);
}
/*
#] CheckFloat :
#] Low Level :
#[ Float Routines :
#[ SetFloatPrecision :
We set the default precision of the floats and allocate
space for an output string if we want to write the float.
Space needed: exponent: up to 12 chars.
mantissa 2+10*prec/33 + a little bit extra.
*/
int SetFloatPrecision(WORD prec)
{
if ( prec <= 0 ) {
MesPrint("&Illegal value for number of bits for floating point constants: %d",prec);
return(-1);
}
else {
AC.DefaultPrecision = prec;
if ( AO.floatspace != 0 ) M_free(AO.floatspace,"floatspace");
AO.floatsize = ((10*prec)/33+20)*sizeof(char);
AO.floatspace = (UBYTE *)Malloc1(AO.floatsize,"floatspace");
mpf_set_default_prec(prec);
return(0);
}
}
/*
#] SetFloatPrecision :
#[ PrintFloat :
Print the float_ function with its arguments as a floating point
number with numdigits digits.
If however we use rawfloat as a format option it prints it as a
regular Form function and it should never come here because the
print routines in sch.c should intercept it.
The buffer AO.floatspace is allocated when the precision of the
floats is set in the routine SetupMZVTables.
*/
int PrintFloat(WORD *fun,int numdigits)
{
GETIDENTITY
int n = 0;
int prec = (AC.DefaultPrecision-AC.MaxWeight-1)*log10(2.0);
if ( numdigits > prec || numdigits == 0 ) {
numdigits = prec;
}
if ( UnpackFloat(aux4,fun) == 0 )
n = gmp_snprintf((char *)(AO.floatspace),AO.floatsize,"%.*Fe",numdigits,aux4);
if ( numdigits == prec ) {
UBYTE *s1, *s2;
int n1, n2 = n;
s1 = AO.floatspace+n;
while ( s1 > AO.floatspace && s1[-1] != 'e'
&& s1[-1] != 'E' && s1[-1] != '.' ) { s1--; n2--; }
if ( s1 > AO.floatspace && s1[-1] != '.' ) {
s1--; n2--;
s2 = s1; n1 = n2;
while ( s1[-1] == '0' ) { s1--; n1--; }
if ( s1[-1] == '.' ) { s1++; n1++; }
n -= (n2-n1);
while ( n1 < n2 ) { *s1++ = *s2++; n1++; }
*s1 = 0;
}
}
return(n);
}
/*
#] PrintFloat :
#[ AddFloats :
*/
int AddFloats(PHEAD WORD *fun3, WORD *fun1, WORD *fun2)
{
int retval = 0;
if ( UnpackFloat(aux1,fun1) == 0 && UnpackFloat(aux2,fun2) == 0 ) {
mpf_add(aux1,aux1,aux2);
PackFloat(fun3,aux1);
}
else { retval = -1; }
return(retval);
}
/*
#] AddFloats :
#[ MulFloats :
*/
int MulFloats(PHEAD WORD *fun3, WORD *fun1, WORD *fun2)
{
int retval = 0;
if ( UnpackFloat(aux1,fun1) == 0 && UnpackFloat(aux2,fun2) == 0 ) {
mpf_mul(aux1,aux1,aux2);
PackFloat(fun3,aux1);
}
else { retval = -1; }
return(retval);
}