-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpfun90.f90
8038 lines (6470 loc) · 180 KB
/
mpfun90.f90
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
!*****************************************************************************
! MPFUN: A MULTIPLE PRECISION FLOATING POINT COMPUTATION PACKAGE
! IEEE Fortran-90 version
! Version Date: 2010-08-25
! Author:
! David H. Bailey
! NERSC, Lawrence Berkeley Lab
! Mail Stop 50B-2239
! Berkeley, CA 94720
! Email: dhbailey@lbl.gov
! This work was supported by the Director, Office of Science, Division
! of Mathematical, Information, and Computational Sciences of the
! U.S. Department of Energy under contract number DE-AC03-76SF00098.
! See README file accompanying this software for other legal details.
! A detailed description of this package, and instructions for compiling
! and testing this program on various specific systems are included in the
! README file that accompanies this file.
!*****************************************************************************
module mpfuna
! This section initializes global parameters and arrays default values.
double precision:: mpbbx, mpbdx, mpbx2, mprbx, mprdx, mprx2, mprxx
parameter (mpkdp = kind (0.d0))
!> To disable the need for "allocate" operations in normal precision
! usage: (1) comment out the next two statements; (2) uncomment the
! three lines that immediately follow them; (3) comment out the two
! "allocate" operations in subroutine mpiniq below; and (4) adjust
! mpdigits if necessary, so that mpdigits (here) = mpipl (in mpmod90.f)
! and mpwords (here) = mpwds (in mpmod90.f).
allocatable mpcosq, mpsinq
real*4 mpcosq(:,:), mpsinq(:,:)
! integer mpwords, mpdigits
! parameter (mpdigits = 2005, mpwords = mpdigitsdigits / 7.224719896d0 + 2.d0)
! real*4 mpcosq(mpwords+4,0:8), mpsinq(mpwords+4,0:8)
complex (mpkdp), allocatable:: mpuu1(:), mpuu2(:)
! The parameters MPBBX, MPNBT, MPNPR and MPMCRX depend on system word size.
! On IEEE systems and most other systems, set MPBBX = 4096.D0,
! MPNBT = 24, MPNPR = 32, and MPMCRX = 7. The parameters
! MPNROW, MPNSP1 and MPNSP2 are spacing parameters to avoid bank and cache
! conflict performance problems in the FFT routines. MPNROW = 16,
! MPNSP1 = 2 and MPNSP2 = 9 appear to work well on most systems.
parameter (mpbbx = 4096.d0, mpnbt = 24, mpnpr = 32, mpmcrx = 7, &
mpbdx = mpbbx ** 2, mpbx2 = mpbdx ** 2, mprbx = 1.d0 / mpbbx, &
mprdx = mprbx ** 2, mprx2 = mprdx ** 2, mprxx = 16.d0 * mprx2, &
mpnrow = 16, mpnsp1 = 2, mpnsp2 = 9)
dimension mpker(73)
data mpidb, mpldb, mpndb, mpier, mpmcr, mpird / 0, 6, 22, 99, mpmcrx, 1/
data mpker /73 * 2/
contains
subroutine mpabrt
!>
! This routine terminates execution. Users may wish to replace the
! default STOP with a call to a system routine that provides a traceback.
! Examples of code that produce traceback are included here (commented out)
! for some systems.
if (mpier .eq. 99) then
write (mpldb, 1)
1 format ('*** The MPFUN library has not been initialized. If you are using'/&
'the Fortran-90 translation modules, you must insert the following line'/ &
'at the start of execution in your main program:'/ ' '/ 'CALL MPINIT')
else
write (mpldb, 2) mpier
2 format ('*** MPABRT: Execution terminated, error code =',i4)
endif
stop
end subroutine
end module
module mpfunb
! This module defines the 'dp' routines, i.e. routines that operate on
! numbers of the DPE (double precision plus exponent) datatype.
use mpfuna
contains
subroutine dpadd (a, na, b, nb, c, nc)
! This adds the DPE numbers (A, NA) and (B, NB) to yield the sum (C, NC).
implicit double precision (a-h, o-z)
dimension pt(64)
save pt
data pt/ 64 * 0.d0/
if (mpier .ne. 0) then
if (mpier .eq. 99) call mpabrt
c = 0.d0
nc = 0
return
endif
! If this is the first call to DPADD, initialize the PT table.
if (pt(1) .eq. 0.d0) then
pt(1) = 0.5d0
do i = 2, 64
pt(i) = 0.5d0 * pt(i-1)
enddo
endif
! This operation reduces to five cases.
if (b .eq. 0.d0) then
c = a
nc = na
else if (a .eq. 0.d0) then
c = b
nc = nb
else if (na .eq. nb) then
c = a + b
nc = na
else if (na .gt. nb) then
k = na - nb
nc = na
if (k .gt. 64) then
c = a
else
c = a + b * pt(k)
endif
else
k = nb - na
nc = nb
if (k .gt. 64) then
c = b
else
c = b + a * pt(k)
endif
endif
if (c .eq. 0.d0) then
nc = 0
goto 130
endif
! Normalize the result to a decent range if it is not.
110 if (abs (c) .ge. mpbdx) then
c = mprdx * c
nc = nc + mpnbt
goto 110
endif
120 if (abs (c) .lt. 1.d0) then
c = mpbdx * c
nc = nc - mpnbt
goto 120
endif
130 return
end subroutine
subroutine dpdec (a, na, b, nb)
! This converts the DPE number (A, NA) to decimal form, i.e. B * 10^NB,
! where |B| is between 1 and 10.
implicit double precision (a-h, o-z)
parameter (xlt = 0.3010299956639812d0)
if (a .ne. 0.d0) then
t1 = xlt * na + log10 (abs (a))
nb = t1
if (t1 .lt. 0.d0) nb = nb - 1
b = sign (10.d0 ** (t1 - nb), a)
else
b = 0.d0
nb = 0
endif
return
end subroutine
subroutine dpdiv (a, na, b, nb, c, nc)
! This divides the DPE number (A, NA) by (B, NB) to yield the quotient
! (C, NC).
implicit double precision (a-h, o-z)
if (mpier .ne. 0) then
if (mpier .eq. 99) call mpabrt
c = 0.d0
nc = 0
return
endif
if (b .eq. 0.d0) then
if (mpker(1) .ne. 0) then
write (mpldb, 1)
1 format ('*** DPDIV: Divisor is zero.')
mpier = 1
if (mpker(mpier) .eq. 2) call mpabrt
endif
return
endif
! Divide A by B and subtract exponents, unless A is zero.
if (a .eq. 0.d0) then
c = 0.d0
nc = 0
goto 120
else
c = a / b
nc = na - nb
endif
! Normalize the result to a decent range if it is not.
100 if (abs (c) .ge. mpbdx) then
c = mprdx * c
nc = nc + mpnbt
goto 100
endif
110 if (abs (c) .lt. 1.d0) then
c = mpbdx * c
nc = nc - mpnbt
goto 110
endif
120 return
end subroutine
subroutine dpmul (a, na, b, nb, c, nc)
! This multiplies the DPE number (A, NA) by (B, NB) to yield the product
! (C, NC).
implicit double precision (a-h, o-z)
if (mpier .ne. 0) then
if (mpier .eq. 99) call mpabrt
c = 0.d0
nc = 0
return
endif
! Multiply A by B and add exponents, unless either is zero.
if (a .eq. 0.d0 .or. b .eq. 0.d0) then
c = 0.d0
nc = 0
goto 120
else
c = a * b
nc = na + nb
endif
! Normalize the result to a decent range if it is not.
100 if (abs (c) .ge. mpbdx) then
c = mprdx * c
nc = nc + mpnbt
goto 100
endif
110 if (abs (c) .lt. 1.d0) then
c = mpbdx * c
nc = nc - mpnbt
goto 110
endif
120 return
end subroutine
subroutine dppwr (a, na, b, nb, c, nc)
! This raises the DPE number (A, NA) to the (B, NB) power and places the
! result in (C, NC).
implicit double precision (a-h, o-z)
parameter (cl2 = 1.4426950408889633d0)
if (mpier .ne. 0) then
if (mpier .eq. 99) call mpabrt
c = 0.d0
nc = 0
return
endif
if (a .le. 0.d0) then
if (mpker(2) .ne. 0) then
write (mpldb, 1)
1 format ('*** DPPWR: Argument is less than or equal to zero.')
mpier = 2
if (mpker(mpier) .eq. 2) call mpabrt
endif
return
endif
if (b .eq. 0.d0) then
c = 1.d0
nc = 0
goto 120
endif
if (b .eq. 1.d0 .and. nb .eq. 0) then
c = a
nc = na
goto 120
endif
! Compute the base 2 logarithm of A and multiply by B.
al = cl2 * log (a) + na
call dpmul (al, 0, b, nb, t1, n1)
! Check for possible overflow or underflow.
if (n1 .gt. 6) then
if (t1 .gt. 0.d0) then
if (mpker(3) .ne. 0) then
write (mpldb, 2)
2 format ('*** DPPWR: Overflow')
mpier = 3
if (mpker(mpier) .eq. 2) call mpabrt
endif
return
else
c = 0.d0
nc = 0
goto 120
endif
endif
! Compute 2 raised to the power B * Log_2 (A).
t1 = t1 * 2.d0 ** n1
nc = int (t1)
c = 2.d0 ** (t1 - nc)
! Normalize the result to a decent range if it is not.
100 if (abs (c) .ge. mpbdx) then
c = mprdx * c
nc = nc + mpnbt
goto 100
endif
110 if (abs (c) .lt. 1.d0) then
c = mpbdx * c
nc = nc - mpnbt
goto 110
endif
120 return
end subroutine
subroutine dpsqrt (a, na, b, nb)
! This computes the square root of the DPE number (A, NA) and places the
! result in (B, NB).
implicit double precision (a-h, o-z)
if (mpier .ne. 0) then
if (mpier .eq. 99) call mpabrt
b = 0.d0
nb = 0
return
endif
if (a .lt. 0.d0) then
if (mpker(4) .ne. 0) then
write (mpldb, 1)
1 format ('*** DPSQRT: Argument is negative.')
mpier = 4
if (mpker(mpier) .eq. 2) call mpabrt
endif
return
endif
if (a .eq. 0.d0) then
b = 0.d0
nb = 0
goto 120
endif
! Divide the exponent of A by two and then take the square root of A. If
! NA is not an even number, then we have to multiply A by 10 before taking
! the square root.
nb = na / 2
if (na .eq. 2 * nb) then
b = sqrt (a)
else
b = sqrt (2.d0 * a)
if (na .lt. 0) nb = nb - 1
endif
! Normalize the result to a decent range if it is not.
100 if (abs (b) .ge. mpbdx) then
b = mprdx * b
nb = nb + mpnbt
goto 100
endif
110 if (abs (b) .lt. 1.d0) then
b = mpbdx * b
nb = nb - mpnbt
goto 110
endif
120 return
end subroutine
subroutine dpsub (a, na, b, nb, c, nc)
! This subtracts the DPE number (B, NB) from (A, NA) to yield the difference
! (C, NC).
implicit double precision (a-h, o-z)
if (mpier .ne. 0) then
if (mpier .eq. 99) call mpabrt
c = 0.d0
nc = 0
return
endif
bb = -b
call dpadd (a, na, bb, nb, c, nc)
return
end subroutine
end module
module mpfunc
! This module defines basic arithmetic routines.
use mpfuna
contains
subroutine mpadd (a, b, c, mpnw)
! This routine adds MP numbers A and B to yield the MP sum C. It attempts
! to include all significance of A and B in the result, up to the maximum
! mantissa length MPNW. Debug output starts with MPIDB = 9. This is a new
! simplified version.
! Max SP space for C: MPNW + 4 cells.
double precision d(mpnw+5), db
dimension a(mpnw+2), b(mpnw+2), c(mpnw+4)
if (mpier .ne. 0) then
if (mpier .eq. 99) call mpabrt
c(1) = 0.
c(2) = 0.
return
endif
if (mpidb .ge. 9) then
no = min (int (abs (a(1))), mpndb) + 2
write (mpldb, 1) (a(i), i = 1, no)
1 format ('MPADD I'/(6f12.0))
no = min (int (abs (b(1))), mpndb) + 2
write (mpldb, 1) (b(i), i = 1, no)
endif
ia = sign (1., a(1))
ib = sign (1., b(1))
na = min (int (abs (a(1))), mpnw)
nb = min (int (abs (b(1))), mpnw)
! Check for zero inputs.
if (na .eq. 0) then
! A is zero -- the result is B.
c(1) = sign (nb, ib)
do i = 2, nb + 2
c(i) = b(i)
enddo
goto 100
elseif (nb .eq. 0) then
! B is zero -- the result is A.
c(1) = sign (na, ia)
do i = 2, na + 2
c(i) = a(i)
enddo
goto 100
endif
if (ia .eq. ib) then
db = 1.d0
else
db = -1.d0
endif
ixa = a(2)
ixb = b(2)
ish = ixa - ixb
if (mpidb .ge. 9) write (6, *) 'ish =', ish
if (ish .ge. 0) then
! A has greater exponent than B, so B must be shifted to the right.
m1 = min (na, ish)
m2 = min (na, nb + ish)
m3 = na
m4 = min (max (na, ish), mpnw + 1)
m5 = min (max (na, nb + ish), mpnw + 1)
d(1) = 0.d0
d(2) = 0.d0
do i = 1, m1
d(i+2) = a(i+2)
enddo
do i = m1 + 1, m2
d(i+2) = a(i+2) + db * b(i+2-ish)
enddo
do i = m2 + 1, m3
d(i+2) = a(i+2)
enddo
do i = m3 + 1, m4
d(i+2) = 0.d0
enddo
do i = m4 + 1, m5
d(i+2) = db * b(i+2-ish)
enddo
nd = m5
ixd = ixa
d(nd+3) = 0.d0
d(nd+4) = 0.d0
else
! B has greater exponent than A, so A must be shifted to the right.
nsh = - ish
m1 = min (nb, nsh)
m2 = min (nb, na + nsh)
m3 = nb
m4 = min (max (nb, nsh), mpnw + 1)
m5 = min (max (nb, na + nsh), mpnw + 1)
d(1) = 0.d0
d(2) = 0.d0
do i = 1, m1
d(i+2) = db * b(i+2)
enddo
do i = m1 + 1, m2
d(i+2) = a(i+2-nsh) + db * b(i+2)
enddo
do i = m2 + 1, m3
d(i+2) = db * b(i+2)
enddo
do i = m3 + 1, m4
d(i+2) = 0.d0
enddo
do i = m4 + 1, m5
d(i+2) = a(i+2-nsh)
enddo
nd = m5
ixd = ixb
d(nd+3) = 0.d0
d(nd+4) = 0.d0
endif
! Call mpnorm to fix up result and store in c.
d(1) = sign (nd, ia)
d(2) = ixd
call mpnorm (d, c, mpnw)
100 continue
if (mpidb .ge. 9) then
no = min (int (abs (c(1))), mpndb) + 2
write (mpldb, 2) (c(i), i = 1, no)
2 format ('MPADD O'/(6f12.0))
endif
return
end subroutine
subroutine mpcbrt (a, b, mpnw)
! This computes the cube root of the MP number A and returns the MP result
! in B. For extra high levels of precision, use MPCBRX. Debug output
! starts with MPIDB = 7.
! Max SP space for B: MPNW + 4 cells.
! This subroutine employs the following Newton-Raphson iteration, which
! converges to A ^ (-2/3):
! X_{k+1} = X_k + (1 - X_k^3 * A^2) * X_k / 3
! where the muliplication () * X_k is performed with only half of the
! normal level of precision. These iterations are performed with a
! maximum precision level MPNW that is dynamically changed, doubling with
! each iteration. The final iteration is performed as follows (this is
! due to A. Karp):
! Cbrt(A) = (A * X_n) + [A - (A * X_n)^3] * X_n / 3 (approx.)
! where the multiplications A * X_n and [] * X_n are performed with only
! half of the final level of precision. See the comment about the parameter
! NIT in MPDIVX.
double precision cl2, t1, t2
parameter (cl2 = 1.4426950408889633d0, nit = 3)
dimension a(mpnw+2), b(mpnw+4), f(8), s(3*mpnw+15)
if (mpier .ne. 0) then
if (mpier .eq. 99) call mpabrt
b(1) = 0.
b(2) = 0.
return
endif
if (mpidb .ge. 7) then
no = min (int (abs (a(1))), mpndb) + 2
write (mpldb, 1) (a(i), i = 1, no)
1 format ('MPCBRT I'/(6f12.0))
endif
ia = sign (1., a(1))
na = min (int (abs (a(1))), mpnw)
if (na .eq. 0) then
b(1) = 0.
b(2) = 0.
goto 120
endif
if (ia .lt. 0.d0) then
if (mpker(13) .ne. 0) then
write (mpldb, 2)
2 format ('*** MPCBRT: Argument is negative.')
mpier = 13
if (mpker(mpier) .eq. 2) call mpabrt
endif
return
endif
n5 = mpnw + 5
k0 = 1
k1 = k0 + n5
k2 = k1 + n5
nws = mpnw
! Determine the least integer MQ such that 2 ^ MQ .GE. MPNW.
t1 = mpnw
mq = cl2 * log (t1) + 1.d0 - mprxx
! Compute A^2 outside of the iteration loop.
mpnw = nws + 1
call mpmul (a, a, s(k0), mpnw)
! Compute the initial approximation of A ^ (-2/3).
call mpmdc (a, t1, n)
n3 = - 2 * n / 3
t2 = (t1 * 2.d0 ** (n + 3.d0 * n3 / 2.d0)) ** (-2.d0 / 3.d0)
call mpdmc (t2, n3, b)
f(1) = 1.
f(2) = 0.
f(3) = 1.
f(4) = 0.
mpnw = 3
iq = 0
! Perform the Newton-Raphson iteration described above with a dynamically
! changing precision level MPNW (one greater than powers of two).
do k = 2, mq - 1
nw1 = mpnw
mpnw = min (2 * mpnw - 2, nws) + 1
nw2 = mpnw
100 continue
call mpmul (b, b, s(k1), mpnw)
call mpmul (b, s(k1), s(k2), mpnw)
call mpmul (s(k0), s(k2), s(k1), mpnw)
call mpsub (f, s(k1), s(k2), mpnw)
mpnw = nw1
call mpmul (b, s(k2), s(k1), mpnw)
call mpdivd (s(k1), 3.d0, 0, s(k2), mpnw)
mpnw = nw2
call mpadd (b, s(k2), s(k1), mpnw)
call mpeq (s(k1), b, mpnw)
if (k .eq. mq - nit .and. iq .eq. 0) then
iq = 1
goto 100
endif
enddo
! Perform last iteration using Karp's trick.
call mpmul (a, b, s(k0), mpnw)
nw1 = mpnw
mpnw = min (2 * mpnw - 2, nws) + 1
nw2 = mpnw
call mpmul (s(k0), s(k0), s(k1), mpnw)
call mpmul (s(k0), s(k1), s(k2), mpnw)
call mpsub (a, s(k2), s(k1), mpnw)
mpnw = nw1
call mpmul (s(k1), b, s(k2), mpnw)
call mpdivd (s(k2), 3.d0, 0, s(k1), mpnw)
mpnw = nw2
call mpadd (s(k0), s(k1), s(k2), mpnw)
call mpeq (s(k2), b, mpnw)
! Restore original precision level.
mpnw = nws
call mproun (b, mpnw)
120 if (mpidb .ge. 7) then
no = min (int (abs (b(1))), mpndb) + 2
write (mpldb, 3) (b(i), i = 1, no)
3 format ('MPCBRT O'/(6f12.0))
endif
return
end subroutine
subroutine mpcpr (a, b, ic, mpnw)
! This routine compares the MP numbers A and B and returns in IC the value
! -1, 0, or 1 depending on whether A < B, A = B, or A > B. It is faster
! than merely subtracting A and B and looking at the sign of the result.
! Debug output begins with MPIDB = 9.
dimension a(mpnw+4), b(mpnw+4)
if (mpier .ne. 0) then
if (mpier .eq. 99) call mpabrt
ic = 0
return
endif
if (mpidb .ge. 9) then
no = min (int (abs (a(1))), mpndb) + 2
write (mpldb, 1) (a(i), i = 1, no)
1 format ('MPCPR I'/(6f12.0))
no = min (int (abs (b(1))), mpndb) + 2
write (mpldb, 1) (b(i), i = 1, no)
endif
ia = sign (1., a(1))
if (a(1) .eq. 0.) ia = 0
ib = sign (1., b(1))
if (b(1) .eq. 0.) ib = 0
! Compare signs.
if (ia .ne. ib) then
ic = sign (1, ia - ib)
goto 110
endif
! The signs are the same. Compare exponents.
ma = a(2)
mb = b(2)
if (ma .ne. mb) then
ic = ia * sign (1, ma - mb)
goto 110
endif
! The signs and the exponents are the same. Compare mantissas.
na = min (int (abs (a(1))), mpnw)
nb = min (int (abs (b(1))), mpnw)
do i = 3, min (na, nb) + 2
if (a(i) .ne. b(i)) then
ic = ia * sign (1., a(i) - b(i))
goto 110
endif
enddo
! The mantissas are the same to the common length. Compare lengths.
if (na .ne. nb) then
ic = ia * sign (1, na - nb)
goto 110
endif
! The signs, exponents, mantissas and lengths are the same. Thus A = B.
ic = 0
110 if (mpidb .ge. 9) write (mpldb, 2) ic
2 format ('MPCPR O',i4)
return
end subroutine
subroutine mpdotdx (n, isa, a, isb, db, c, mpnw)
! This routine computes the dot product of the MP vector A with the DP
! vector DB, returning the MP result in C. This routine is used in the
! author's customized PSLQ routine, resulting in substantial speedup.
! The length of both the A and DB vectors is N, and ISA and ISB are the
! skip distances between successive elements of A and DB, measured in
! single precision words, and in DP words, respectively. ISA must be
! at least MPNW + 4. The DP values in DB must be whole numbers, so for
! example they cannot be larger than 2^53 on IEEE systems. Debug output
! begins with MPIDB = 8.
double precision d1(mpnw+5), d2(mpnw+5), db(isb*n), dmax, &
dt0, dt1, dt2, dt3, dt4
! Set DMAX to the largest DP whole number that can be represented exactly:
! 2.d0 ** 53 on IEEE systems.
parameter (dmax = 2.d0 ** 53)
real a(isa*n), c(mpnw+4), s1(mpnw+5)
if (mpier .ne. 0) then
if (mpier .eq. 99) call mpabrt
c(1) = 0.
c(2) = 0.
return
endif
if (mpidb .ge. 8) then
write (6, 1) n, isa, isb
1 format ('mpdotd input:',3i8)
do k = 1, n
ka = (k - 1) * isa
kb = (k - 1) * isb + 1
call mpmdc (a(1+ka), dt1, n1)
dt1 = dt1 * 2.d0 ** n1
write (6, '(i4,1p,2d25.16)') k, dt1, db(kb)
enddo
endif
do i = 1, 4
d1(i) = 0.d0
d2(i) = 0.d0
enddo
! ND is the length of D1, IXD is the exponent (as in ordinary mpfun format).
! In the code below ND + 1 mantissa words are maintained whenever possible.
nd = 0
ixd = 0
nrel = 0
! Loop over the n input data pairs.
do k = 1, n
ka = (k - 1) * isa
kb = (k - 1) * isb + 1
do kk = 1, mpnw + 4
s1(kk) = a(ka+kk)
enddo
na = min (int (abs (s1(1))), mpnw)
dt0 = db(kb)
if (na .eq. 0 .or. dt0 .eq. 0.d0) goto 100
! Check to make sure the input DP value satisfies the requirements.
if (abs (dt0) .ge. dmax .or. mod (dt0, 1.d0) .ne. 0.d0) then
if (mpker(73) .ne. 0) then
write (6, 2) k, dt0
2 format ('mpdotd: improper dp value:',i4,1p,d25.15)
mpier = 73
if (mpker(mpier) .eq. 2) call mpabrt
endif
return
endif
! Save the two initial and the two final words of A, then zero these words.
ia1 = s1(1)
ia2 = s1(2)
ia3 = s1(na+3)
ia4 = s1(na+4)
s1(1) = 0.
s1(2) = 0.
s1(na+3) = 0.
s1(na+4) = 0.
if (ia1 .lt. 0) dt0 = - dt0
! Split the input DP value into high-order, mid-order and a low-order values.
dt1 = int (mprx2 * dt0)
dt0 = dt0 - mpbx2 * dt1
dt2 = int (mprdx * dt0)
dt3 = dt0 - mpbdx * dt2
if (dt1 .eq. 0.d0 .and. dt2 .eq. 0.d0) then
! Only the low-order part of the input DP value is nonzero.
ish = ia2 - ixd
if (nd .eq. 0) ish = 0
if (ish .ge. 0) then
! The product a(k) * db(k) has greater exponent than the cumulative sum.
! Thus the cumulative sum must be shifted to the right by ish words.
m1 = min (na, ish)
m2 = min (na, nd + ish)
m3 = na
m4 = min (max (na, ish), mpnw + 1)
m5 = min (max (na, nd + ish), mpnw + 1)
d2(1) = 0.d0
d2(2) = 0.d0
do i = 1, m1
d2(i+2) = dt3 * s1(i+2)
enddo
do i = m1 + 1, m2
d2(i+2) = d1(i+2-ish) + dt3 * s1(i+2)
enddo
do i = m2 + 1, m3
d2(i+2) = dt3 * s1(i+2)
enddo
do i = m3 + 1, m4
d2(i+2) = 0.d0
enddo
do i = m4 + 1, m5
d2(i+2) = d1(i+2-ish)
enddo
! Copy d2 back to d1.
do i = 1, m5 + 2
d1(i) = d2(i)
enddo
nd = m5
ixd = ia2
d1(nd+3) = 0.d0
d1(nd+4) = 0.d0
else
! The product a(k) * db(k) has smaller exponent than the cumulative sum.
! Thus the product must be shifted to the right by -ish words.
nsh = - ish
m1 = min (nd, nsh)
m2 = min (nd, na + nsh)
m3 = nd
m4 = min (max (nd, nsh), mpnw + 1)
m5 = min (max (nd, na + nsh), mpnw + 1)
do i = m1 + 1, m2
d1(i+2) = d1(i+2) + dt3 * s1(i+2-nsh)
enddo
do i = m3 + 1, m4
d1(i+2) = 0.d0
enddo
do i = m4 + 1, m5
d1(i+2) = dt3 * s1(i+2-nsh)
enddo
nd = m5
d1(nd+3) = 0.d0
d1(nd+4) = 0.d0
endif