Repository navigation
Expand file tree
/
Copy pathmath2.asm
More file actions
4257 lines (3716 loc) · 102 KB
/
Copy pathmath2.asm
File metadata and controls
4257 lines (3716 loc) · 102 KB
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
keep obj/math2
mcopy math2.macros
case on
****************************************************************
*
* Math2 - additional math routines
*
* This code provides additional functions from <math.h>
* (including internal helper functions used by macros),
* supplementing the ones in math.asm.
*
****************************************************************
math2 private dummy segment
copy equates.asm
end
INVALID gequ $0001 exceptions
UNDERFLOW gequ $0002
OVERFLOW gequ $0004
DIVBYZERO gequ $0008
INEXACT gequ $0010
TONEAREST gequ 0 rounding directions
UPWARD gequ 1
DOWNWARD gequ 2
TOWARDZERO gequ 3
****************************************************************
*
* MathCommon2 - common work areas for the math library
*
****************************************************************
*
MathCommon2 privdata
;
; temporary work space/return value
;
t1 ds 10
end
****************************************************************
*
* int __fpclassifyf(float x);
*
* Classify a float value
*
* Inputs:
* val - the number to classify
*
* Outputs:
* one of the FP_* classification values
*
****************************************************************
*
__fpclassifyf start
csubroutine (10:val),0
tdc
clc
adc #val
ldy #0
phy
pha
phy
pha
phy
pha
FX2S
FCLASSS
txa
and #$00FF
cmp #$00FC
bne lb1
inc a
lb1 sta val
creturn 2:val
end
****************************************************************
*
* int __fpclassifyd(double x);
*
* Classify a double value
*
* Inputs:
* val - the number to classify
*
* Outputs:
* one of the FP_* classification values
*
****************************************************************
*
__fpclassifyd start
csubroutine (10:val),0
tdc
clc
adc #val
ldy #0
phy
pha
phy
pha
phy
pha
FX2D
FCLASSD
txa
and #$00FF
cmp #$00FC
bne lb1
inc a
lb1 sta val
creturn 2:val
end
****************************************************************
*
* int __fpclassifyl(long double x);
*
* Classify a long double value
*
* Inputs:
* val - the number to classify
*
* Outputs:
* one of the FP_* classification values
*
****************************************************************
*
__fpclassifyl start
csubroutine (10:val),0
tdc
clc
adc #val
pea 0
pha
FCLASSX
txa
and #$00FF
cmp #$00FC
bne lb1
inc a
lb1 sta val
creturn 2:val
end
****************************************************************
*
* int __signbit(long double x);
*
* Get the sign bit of a floating-point value
*
* Inputs:
* val - the number
*
* Outputs:
* 0 if positive, non-zero if negative
*
****************************************************************
*
__signbit start
csubroutine (10:val),0
lda val+8
and #$8000
sta val
creturn 2:val
end
****************************************************************
*
* int __fpcompare(long double x, long double y, short mask);
*
* Compare two floating-point values, not signaling invalid
* if they are unordered.
*
* Inputs:
* x,y - values to compare
* mask - mask of bits as returned in X register from FCMP
*
* Outputs:
* 1 if x and y have one of the relations specified by mask
* 0 otherwise
*
****************************************************************
*
__fpcompare start
csubroutine (10:x,10:y,2:mask),0
tdc
clc
adc #x
pea 0
pha
tdc
clc
adc #y
pea 0
pha
FCMPX
txa
and mask
beq lb1
lda #1
lb1 sta mask
creturn 2:mask
end
****************************************************************
*
* double acosh(double x);
*
* Returns the inverse hyperbolic cosine of x.
*
****************************************************************
*
acosh start
acoshf entry
acoshl entry
using MathCommon2
csubroutine (10:x),0
phb
phk
plb
pha save env & set to default
tsc
inc a
pea 0
pha
FPROCENTRY
lda x y = sqrt(x-1)
sta y
lda x+2
sta y+2
lda x+4
sta y+4
lda x+6
sta y+6
lda x+8
sta y+8
ph4 #one
ph4 #y
FSUBI
ph4 #y
FSQRTX
lda x t1 = sqrt(x+1)
sta t1
lda x+2
sta t1+2
lda x+4
sta t1+4
lda x+6
sta t1+6
lda x+8
sta t1+8
ph4 #one
ph4 #t1
FADDI
ph4 #t1
FSQRTX
ph4 #y t1 = ln(1+y*(y+t1))
ph4 #t1
FADDX
ph4 #y
ph4 #t1
FMULX
ph4 #t1
FLN1X
lda t1+8 if t1 = +inf
cmp #32767
bne ret
lda t1+6
asl a
ora t1+4
ora t1+2
ora t1
bne ret
pea 0 clear exceptions
FSETENV
lda x t1 = ln(x) + ln(2)
sta t1
lda x+2
sta t1+2
lda x+4
sta t1+4
lda x+6
sta t1+6
lda x+8
sta t1+8
ph4 #t1
FLNX
ph4 #ln2
ph4 #t1
FADDX
ret FPROCEXIT restore env & raise any new exceptions
plb
creturn 10:t1 return t1
y ds 10 temporary variable
one dc i'1' constants
ln2 dc e'0.69314718055994530942'
end
****************************************************************
*
* double asinh(double x);
*
* Returns the inverse hyperbolic sine of x.
*
****************************************************************
*
asinh start
asinhf entry
asinhl entry
using MathCommon2
csubroutine (10:x),0
phb
phk
plb
pha save env & set to default
tsc
inc a
pea 0
pha
FPROCENTRY
pei x+8 save sign of x
asl x+8 x = abs(x)
lsr x+8
lda x t1 = y = z = x
sta y
sta z
sta t1
lda x+2
sta y+2
sta z+2
sta t1+2
lda x+4
sta y+4
sta z+4
sta t1+4
lda x+6
sta y+6
sta z+6
sta t1+6
lda x+8
sta y+8
sta z+8
sta t1+8
lda x if value is zero (or typical inf)
ora x+2
ora x+4
ora x+6
beq skipcalc return the input value
lda x+8 else if x is very small
cmp #-33+16383
bge calc
pea INEXACT raise "inexact" exception
FSETXCP
skipcalc brl setsign return the input value
calc cmp #16383/2+16383 else if x is very large (or nan)
blt notbig
ph4 #z z = ln(x) + ln(2)
FLNX
ph4 #ln2
ph4 #z
FADDX
brl setsign else
notbig pea -2 t1 = 1 / (t1 * t1)
ph4 #t1
FXPWRI
ph4 #one t1 = 1 + t1
ph4 #t1
FADDI
ph4 #t1 t1 = sqrt(t1)
FSQRTX
pea -1 y = 1 / y
ph4 #y
FXPWRI
ph4 #y t1 = t1 + y
ph4 #t1
FADDX
ph4 #t1 z = z / t1
ph4 #z
FDIVX
tdc z = z + x
clc
adc #x
pea 0
pha
ph4 #z
FADDX
ph4 #z z = ln(1+z)
FLN1X
setsign asl z+8 sign of z = original sign of x
pla
asl a
ror z+8
FPROCEXIT restore env & raise any new exceptions
plb
creturn 10:z return z
y ds 10 temporary variables
z ds 10
one dc i'1' constants
ln2 dc e'0.69314718055994530942'
end
****************************************************************
*
* double atanh(double x);
*
* Returns the inverse hyperbolic tangent of x.
*
****************************************************************
*
atanh start
atanhf entry
atanhl entry
using MathCommon2
csubroutine (10:x),0
phb
phk
plb
pha save env & set to default
tsc
inc a
pea 0
pha
FPROCENTRY
pei x+8 save sign of x
asl x+8 x = abs(x)
lsr x+8
lda x t1 = x
sta t1
lda x+2
sta t1+2
lda x+4
sta t1+4
lda x+6
sta t1+6
lda x+8
sta t1+8
lda x+8 if x is very small
cmp #-33+16383
bge calc
lda x if value is not zero
ora x+2
ora x+4
ora x+6
beq skipcalc
pea INEXACT raise "inexact" exception
FSETXCP
skipcalc bra setsign skip next steps (return input value)
calc ph4 #one x = x - 1
tdc
clc
adc #x
pea 0
pha
FSUBI
tdc t1 = t1 / x
clc
adc #x
pea 0
pha
ph4 #t1
FDIVX
lda t1+8 if t1 is inf/nan
asl a
cmp #32767*2
beq setsign skip next steps (so atanh(1) = +inf)
ph4 #minustwo t1 = t1 * -2
ph4 #t1
FMULI
ph4 #t1 t1 = ln(1+t1)
FLN1X
ph4 #minustwo t1 = t1 / -2
ph4 #t1
FDIVI
setsign asl t1+8 sign of t1 = original sign of x
pla
asl a
ror t1+8
FPROCEXIT restore env & raise any new exceptions
plb
creturn 10:t1 return t1
one dc i'1' constants
minustwo dc i'-2'
end
****************************************************************
*
* double cbrt(double x);
*
* Returns x^(1/3) (the cube root of x).
*
****************************************************************
*
cbrt start
cbrtf entry
cbrtl entry
using MathCommon2
scale equ 1
csubroutine (10:x),2
phb
phk
plb
stz scale scale by 0 by default (for inf/nan)
lda x+8
pha save original sign
and #$7FFF
sta x+8 force sign to +
cmp #32767 skip scaling for inf/nan
beq do_calc
ldx x+6 if number is denormalized
bmi div_exp
bne normaliz
ldx x+4
bne normaliz
ldx x+2
bne normaliz
ldx x
beq div_exp
normaliz dec a normalize it and adjust exponent
asl x
rol x+2
rol x+4
rol x+6
bpl normaliz
div_exp pha calculate exponent/3
pha
pha
pea 3
_SDivide
pla a = quotient
plx x = remainder
cpx #2 adjust remainder of 2 to -1
bne setscale
ldx #-1
inc a
setscale sec calculate amount to scale result by
sbc #16383/3
sta scale
txa use remainder as exponent for calc.
clc
adc #16383
do_calc sta t1+8
lda x place mantissa in work area
sta t1
lda x+2
sta t1+2
lda x+4
sta t1+4
lda x+6
sta t1+6
ph4 #onethird compute val^(1/3)
ph4 #t1
FXPWRY
clc apply scaling
lda t1+8
adc scale
sta t1+8
asl t1+8 set sign of result to orig. sign of x
pla
asl a
ror t1+8
plb
creturn 10:t1 return t1
onethird dc e'0.33333333333333333333'
end
****************************************************************
*
* double copysign(double x, double y);
*
* Returns a value with the magnitude of x and the sign of y.
*
****************************************************************
*
copysign start
copysignf entry
copysignl entry
using MathCommon2
phb place x in a work area...
plx
ply
phk
plb
pla
sta t1
pla
sta t1+2
pla
sta t1+4
pla
sta t1+6
pla
asl a ...with the sign bit shifted off
sta t1+8
pla remove y
pla
pla
pla
pla
asl a get sign bit of y
ror t1+8 give return value that sign
phy
phx
plb
ldx #^t1 return a pointer to the result
lda #t1
rtl
end
****************************************************************
*
* double erf(double x);
*
* Returns the error function of x.
*
* double erfc(double x);
*
* Returns the complementary error function of x, 1 - erf(x).
*
* This implementation is based on W. J. Cody's article
* "Rational Chebyshev Approximations for the Error Function."
*
****************************************************************
*
erf start
erff entry
erfl entry
using MathCommon2
x_offset equ 9 stack offset of x (in most of the code)
clc
bra lb1
erfc entry
erfcf entry
erfcl entry
sec
lb1 phb save & set data bank
phk
plb
pha make space for saved SANE environment
lda x_offset-2+8,s
ror a save erf/erfc flag (high bit)
pha and original sign (next bit)
rol a t1 := |x|
rol a
lsr a
sta t1+8
lda x_offset+6,s
sta t1+6
lda x_offset+4,s
sta t1+4
lda x_offset+2,s
sta t1+2
lda x_offset,s
sta t1
tsc save env & set to default
clc
adc #3
pea 0
pha
FPROCENTRY
;
; Computation using approximation of erf, for small enough x values
;
ph4 #threshold if |x| <= 0.5 then
ph4 #t1
FCMPS
jmi use_erfc
ph4 #t1 t1 := x^2
ph4 #t1
FMULX
ph4 #y y := P1(t1)
pea 4
ph4 #P1
jsl poly
ph4 #z z := Q1(t1)
pea 4
ph4 #Q1
jsl poly
ph4 #z y := y/z
ph4 #y
FDIVX
tsc y := x * y
clc
adc #x_offset
pea 0
pha
ph4 #y
FMULX
pla
jpl clearxcp if computing erfc then
ph4 #one y := y - 1
ph4 #y
FSUBX
brl flipsign y := -y
;
; Computation using approximations of erfc, for larger x values
;
use_erfc ph4 #four else
ph4 #t1
FCMPI
jmi big_erfc if |x| <= 4 then
ph4 #y y := P2(t1)
pea 8
ph4 #P2
jsl poly
ph4 #z z := Q2(t1)
pea 8
ph4 #Q2
jsl poly
ph4 #z y := y/z
ph4 #y
FDIVX
ph4 #t1 t1 := e^(-x^2)
ph4 #t1
FMULX
lda t1+8
eor #$8000
sta t1+8
ph4 #t1
FEXPX
ph4 #t1 y := t1 * y
ph4 #y
FMULX
brl end_erfc else (if |x| > 4 or NAN)
big_erfc pea -2 t1 := 1 / x^2
ph4 #t1
FXPWRI
ph4 #y y := P3(t1)
pea 5
ph4 #P3
jsl poly
ph4 #z z := Q3(t1)
pea 5
ph4 #Q3
jsl poly
ph4 #z y := y/z
ph4 #y
FDIVX
ph4 #t1 y := t1 * y
ph4 #y
FMULX
ph4 #one_over_sqrt_pi y := 1/sqrt(pi) + y
ph4 #y
FADDX
lda x_offset+8,s y := y / |x|
and #$7fff
sta x_offset+8,s
tsc
clc
adc #x_offset
ldx #0
phx (push operands of below calls)
pha
phx
pha
phx
pha
phx
pha
phx
pha
ph4 #y
FDIVX
FMULX y := e^(-x^2) * y
lda x_offset+8+8,s
eor #$8000
sta x_offset+8+8,s
FEXPX
ph4 #y
FMULX
end_erfc pla
bpl erf_from_erfc if computing erfc then
ldx #$1300 (set allowed exception mask)
asl a
bpl rstr_env if x < 0
ph4 #two y := y - 2
ph4 #y
FSUBI
bra flipsign y := -y
erf_from_erfc anop
pha
ph4 #one if computing erf then
ph4 #y
FSUBX y := y - 1
pla
asl a
bmi clearxcp if x > 0 then
flipsign lda y+8 y := -y
eor #$8000
sta y+8
clearxcp ldx #$1100 ignore overflow, div-by-zero
rstr_env stx z (& underflow unless doing erfc for x>.5)
FGETENV
txa
and z
ora 1,s
sta 1,s
FSETENV unless computing erfc for x > 4
pla clean up stack
sta 9,s
pla
sta 9,s
tsc
clc
adc #6
tcs
plb
ldx #^y return a pointer to the result
lda #y
rtl
threshold dc f'0.5' threshold for computing erf or erfc
; constants
two dc i2'2'
four dc i2'4'
one_over_sqrt_pi dc e'0.564189583547756286924'
; coefficients for erf calculation, |x| <= .5
P1 dc e'1.857777061846031526730e-1'
dc e'3.161123743870565596947e+0'
dc e'1.138641541510501556495e+2'
dc e'3.774852376853020208137e+2'
dc e'3.209377589138469472562e+3'
one anop
Q1 dc e'1.0'
dc e'2.360129095234412093499e+1'
dc e'2.440246379344441733056e+2'
dc e'1.282616526077372275645e+3'
dc e'2.844236833439170622273e+3'
; coefficients for erfc calculation, .46875 <= x <= 4
P2 dc e'2.15311535474403846343e-8'
dc e'5.64188496988670089180e-1'
dc e'8.88314979438837594118e+0'
dc e'6.61191906371416294775e+1'
dc e'2.98635138197400131132e+2'
dc e'8.81952221241769090411e+2'
dc e'1.71204761263407058314e+3'
dc e'2.05107837782607146532e+3'
dc e'1.23033935479799725272e+3'
Q2 dc e'1.0'
dc e'1.57449261107098347253e+1'
dc e'1.17693950891312499305e+2'
dc e'5.37181101862009857509e+2'
dc e'1.62138957456669018874e+3'
dc e'3.29079923573345962678e+3'
dc e'4.36261909014324715820e+3'
dc e'3.43936767414372163696e+3'
dc e'1.23033935480374942043e+3'
; coefficients for erfc calculation, x >= 4
P3 dc e'-1.63153871373020978498e-2'
dc e'-3.05326634961232344035e-1'
dc e'-3.60344899949804439429e-1'
dc e'-1.25781726111229246204e-1'
dc e'-1.60837851487422766278e-2'
dc e'-6.58749161529837803157e-4'
Q3 dc e'1.0'
dc e'2.56852019228982242072e+0'
dc e'1.87295284992346047209e+0'
dc e'5.27905102951428412248e-1'
dc e'6.05183413124413191178e-2'
dc e'2.33520497626869185443e-3'
; temporaries / return values
y ds 10
z ds 10
end
****************************************************************
*
* double exp2(double x);
*
* Returns 2^x.
*
****************************************************************
*
exp2 start
exp2f entry
exp2l entry
using MathCommon2
phb place the number in a work area
plx
ply
phk
plb