/
Double.spin2
1083 lines (931 loc) · 22.3 KB
/
Double.spin2
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
{{
Double -- double (and single) precision floating point routines for
the Propeller
Copyright (c) 2012-2019 Total Spectrum Software Inc.
Released under the MIT License (see the end of this file for details)
}}
'#define DEBUG
''
'' define EXACT if results should be exactly correct; otherwise they may be
'' wrong in the last digit (up to 0.5 ulp error)
''
#define EXACT
CON
FLAG_SIGN = $1
FLAG_ZERO = $2
FLAG_INF = $4
FLAG_NAN = $8
FLAG_STICKY = $10
FLAG_DOUBLE = $20
FBIAS_EXP = 127
FMAX_EXP = 255
DBIAS_EXP = 1023
DMAX_EXP = $7ff
'' mask for double significand (high word)
DMANTMASK = $000FFFFF
'' mask for float significand
FMANTMASK = $007FFFFF
one_4_28 = $1000_0000
NAN_32 = $7fc0_0000
DAT
'----------------------------
' Assembly language routines
'----------------------------
'----------------------------
' Main control loop
'----------------------------
#define r0 x10
#define r1 x11
#define r2 x12
#define r3 x13
org $1d0
A res 1
Alo res 1
Aflag res 1
Aexp res 1
B res 1
Blo res 1
Bflag res 1
Bexp res 1
C res 1
Clo res 1
Cflag res 1
Cexp res 1
Alo2 res 1
Alo3 res 1
tmp0 res 1
tmp1 res 1
tmp2 res 1
count res 1
fit $1f0
orgh
''
'' code to unpack a double in A, Alo
'' the IEEE format is 1 bit sign, 11 bit exponent,
'' then 52 bit significand
'' The unpacked significand is two longs, 4.28 + 0.32
'' Exponent goes in Aexp, flags in Aflag
''
DUnpack
mov Aflag, #FLAG_DOUBLE
mov Aexp, A
shl Aexp, #1 wc
if_c or Aflag, #FLAG_SIGN
and A, ##DMANTMASK ' mask off exponent and sign bit
shr Aexp, #21 wz ' extract exponent
if_z jmp #_Ddenorm ' zero or denormal
cmp Aexp, ##DMAX_EXP wz
sub Aexp, ##DBIAS_EXP ' remove bias
if_z jmp #_Dnan ' NaN or Infinity
'' now shift up to 4.28 to give head room
'' we start with 1.20
mov tmp0, Alo
shl A, #8
shl Alo, #8
shr tmp0, #24
or A, tmp0
or A, ##one_4_28 '' or in implied one
DUnpack_ret
ret
'' normalize a denormalized number
_Ddenorm
sub Aexp, ##DBIAS_EXP
'' adjust for converting from 1.52 to 1.60
add Aexp, #(1+8)
'' check for all 0
mov pa, A
or pa, Alo wz
if_z sub Aexp, #64
if_z or Aflag, #FLAG_ZERO
if_z ret
'' not all 0, renormalize
jmp #Normalize
'' handle NaN or Infinity
_Dnan
mov Aexp, ##DMAX_EXP
mov pa, A
or pa, Alo wz '' check for infinity
if_z or Aflag, #FLAG_INF
if_z mov A, ##one_4_28
if_nz or Aflag, #FLAG_NAN
if_nz add Aexp, Aexp
ret
''
'' code to unpack a single precision float in A
'' the IEEE format is 1 bit sign, 8 bit exponent,
'' then 23 bit significand
''
FUnpack
mov Alo, #0
mov Aflag, #0
mov Aexp, A
shl Aexp, #1 wc
if_c or Aflag, #FLAG_SIGN
and A, ##FMANTMASK ' mask off exponent and sign bit
shr Aexp, #24 wz ' extract exponent
if_z jmp #_Fdenorm ' zero or denormal
cmp Aexp, #FMAX_EXP wz
sub Aexp, #FBIAS_EXP ' remove bias
if_z jmp #_Fnan ' NaN or Infinity
'' now shift up to 4.28 to give head room
'' we start with 1.23
shl A, #5
_ret_ or A, ##one_4_28 '' or in implied one
'' handle NaN or Infinity
_Fnan
mov Aexp, #FMAX_EXP
mov pa, A
or pa, Alo wz
if_z or Aflag, #FLAG_INF
if_z mov A, ##one_4_28
if_nz or Aflag, #FLAG_NAN
if_nz add Aexp, Aexp
ret
'' normalize a denormalized number
_Fdenorm
sub Aexp, #(FBIAS_EXP-1)
'' adjust for converting from 1.23 to 1.28
'' and check for all 0
shl A, #5 wz
if_z sub Aexp, #511
if_z or Aflag, #FLAG_ZERO
if_z ret
'' not all 0, renormalize
jmp #Normalize
''
'' re-normalize A to 4.28 format
''
Normalize
'' check for 0
mov pa, A
or pa, Alo wz
if_z or Aflag, #FLAG_ZERO
if_z ret
'' shift down if necessary
_down
test A, ##$E000_0000 wz
if_z jmp #_up
add Aexp, #1
shr A, #1 wc
rcr Alo, #1 wc
if_c or Aflag, #FLAG_STICKY ' remember we lost bits
jmp #_down
_up
test A, ##one_4_28 wz
if_nz ret
shl Alo, #1 wc
rcl A, #1
sub Aexp, #1
jmp #_up
Normalize_ret
ret
''
'' pack a 4.60 number in A,Alo back to an IEEE double
'' in r1,r0
''
'' need to handle rounding and such!
''
'' input is assumed to be normalized
''
DPack
test Aflag, #(FLAG_INF|FLAG_NAN|FLAG_ZERO) wz
if_nz jmp #dpack_excep
call #Normalize
'' fix up exponent
add Aexp, ##DBIAS_EXP
fles Aexp, ##DMAX_EXP-1 wc
if_c mov A, #0
if_c or Aflag, #(FLAG_INF)
if_c jmp #dpack_excep
cmps Aexp, #0 wcz
if_be call #dpack_denorm
'' round here
'' we clear the implied one first, and allow the
'' rounding to propagate up to it
andn A, ##one_4_28
test Aflag, #FLAG_STICKY wz
'' we have 4.60, we want to round to 4.52
'' half of the lsb is therefore 0x80
'' we also want to round to nearest even, so
'' add a sticky bit if the lsb is set
test Alo, #$100 wc
if_nz_or_c or Alo, #1
add Alo, #$7f wc
addx A, #0
dpack_exp
'' now shift down to 12.52
shr Alo,#8
mov tmp0,A
shr A,#8
shl tmp0,#24
or Alo,tmp0
shl Aexp, #20
mov r0, Alo
mov r1, A
add r1, Aexp
shl Aflag, #31
or r1, Aflag
DPack_ret
ret
''
'' exponent is <=0, so we have to create an IEEE denormalized
'' number
dpack_denorm
abs Aexp, Aexp
add Aexp, #1
_ddlp
shr A, #1 wc
rcr Alo, #1 wc
if_c or Aflag, #FLAG_STICKY
djnz Aexp, #_ddlp
ret
dpack_excep
mov A, #0
mov Alo, #0
mov Aexp, ##DMAX_EXP
test Aflag, #FLAG_NAN wz
if_nz mov A, ##one_4_28
if_nz shr A, #1
if_nz jmp #dpack_exp
test Aflag, #FLAG_ZERO wz
if_nz mov Aexp, #0
jmp #dpack_exp
''
'' unpack (r1,r0) into A and (r3,r2) into B
''
DUnpack2
mov A,r3
mov Alo,r2
call #DUnpack
mov B,A
mov Blo,Alo
mov Bflag,Aflag
mov Bexp,Aexp
mov A,r1
mov Alo,r0
jmp #DUnpack
''
'' pack a 4.60 number in A back to an IEEE float
'' in r0
''
'' need to handle rounding and such!
''
'' input is assumed to be normalized
''
FPack
call #Normalize
test Aflag, #(FLAG_INF|FLAG_NAN|FLAG_ZERO) wz
if_nz jmp #fpack_excep
'' fix up exponent
add Aexp, #FBIAS_EXP
fles Aexp, #FMAX_EXP-1 wc
if_c or Aflag, #FLAG_INF
if_c jmp #fpack_excep
cmps Aexp, #0 wcz
if_be call #fpack_denorm
'' round here
'' we clear the implied one first, and allow the
'' rounding to propagate up to it
andn A, ##one_4_28
cmp Alo,#0 wz
if_nz or Aflag, #FLAG_STICKY
test Aflag, #FLAG_STICKY wz
'' we have 4.28, we want to round to 4.23
'' half of the lsb is therefore 0x10
'' we also round to nearest even, so add a sticky
'' bit if lsb is set
test A, #$20 wc
if_nz_or_c or A, #1
add A, #$f
fpack_exp
'' now shift down to 9.23
shr A,#5 wz
shl Aexp, #23
mov r0, A
add r0, Aexp
shl Aflag, #31
_ret_ or r0, Aflag
''
'' exponent is <=0, so we have to create an IEEE denormalized
'' number
fpack_denorm
abs Aexp, Aexp
add Aexp, #1 ' shift one extra space
_fdlp
shr A, #1 wcz
if_c or Aflag, #FLAG_STICKY
if_z mov Aexp, #0
if_nz djnz Aexp, #_fdlp
fpack_denorm_ret
ret
fpack_excep
mov A, #0
mov Aexp, #FMAX_EXP
test Aflag, #FLAG_NAN wz
if_nz mov r0, ##NAN_32
if_nz ret
test Aflag, #FLAG_ZERO wz
if_z jmp #fpack_exp
' A is zero here
mov r0, Aflag
_ret_ shl r0, #31 ' relies on FLAG_SIGN being in lowest bit!
''
'' unpack 2 floats in r0,r1 into A,B
''
FUnpack2
mov A, r1
call #FUnpack
mov Blo,Alo
mov B, A
mov Bflag,Aflag
mov Bexp,Aexp
mov A, r0
jmp #FUnpack
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' Actual commands start here
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' addition and subtraction
DSub
xor r3, ##$80000000
'' fall through
DAdd
call #DUnpack2
call #_Add
jmp #DPack
DMul
call #DUnpack2
call #_Mul
jmp #DPack
DDiv
call #DUnpack2
call #_Div
jmp #DPack
DSqrt
mov A, r1
mov Alo, r0
call #DUnpack
call #_Sqrt
jmp #DPack
'' single precision operations
FSub
xor r1, ##$80000000
'' fall through
FAdd
call #FUnpack2
call #_Add
jmp #FPack
FMul
call #FUnpack2
call #_Mul
jmp #FPack
FDiv
call #FUnpack2
call #_DivSmall
jmp #FPack
FSqrtx
mov A, r0
call #FUnpack
call #_Sqrt
jmp #FPack
'' conversion operations
'' single to double
FToD
mov A, r0
call #FUnpack
jmp #DPack
'' double to single
DToF
mov A, r1
mov Alo, r0
call #DUnpack
jmp #FPack
'' 32 bit signed integer to float
IToF
abs A, r0 wc, wz
mov Aflag, #0
if_c or Aflag, #FLAG_SIGN
mov r2, #0 '' single precision
doint
if_z mov r1, #0
if_z ret '' 0 -> 0
mov Alo, #0
mov Aexp,#28 '' set the exponent
cmp r2, #0 wz
if_nz jmp #dblprec
jmp #FPack
dblprec
jmp #DPack
'' 32 bit unsigned integer to float
UIToF
mov A, r0 wcz
mov Aflag, #0
mov r2, #0 '' single precision
jmp #doint
'' 32 bit signed integer to double
IToD
abs A, r0 wcz
mov Aflag, #0
if_c or Aflag, #FLAG_SIGN
mov r2, #1 '' double precision
jmp #doint
'' 32 bit unsigned integer to double
UIToD
mov A, r0 wz
mov Aflag, #0
mov r2, #1 '' double precision
jmp #doint
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' Utility functions go here
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''
'' the actual add routine
''
_Add
'' swap so magnitude of A is bigger than that of B
'' NOTE: we are assuming here that infinity is given
'' a big Aexp, and 0 a very tiny one
cmps Aexp, Bexp wcz
if_a jmp #_addnoswap
if_b jmp #_addswap
cmp Alo,Blo wcz
cmpx A,B wcz
if_ae jmp #_addnoswap
_addswap
mov tmp0,Aflag
mov tmp1,Aexp
mov Aflag,Bflag
mov Aexp, Bexp
mov Bflag,tmp0
mov Bexp, tmp1
mov tmp0, A
mov tmp1, Alo
mov A, B
mov Alo, Blo
mov B, tmp0
mov Blo, tmp1
_addnoswap
'' shift B down as necessary
'' if we are shifting more than 63 then
'' we just return the original value
mov tmp0,Aexp
sub tmp0,Bexp wz
if_z jmp #_doadd
fle tmp0,#63 wc
'' FIXME: should raise inexact here?
if_c ret
'' check for short shift
cmp tmp0,#32 wcz
if_b jmp #_addshift
'' shifting B by more than 32
cmp Blo,#0 wz
if_nz or Aflag, #FLAG_STICKY
mov Blo, B
mov B, #0
sub tmp0,#32 wz
_addshift
'' now shift B, Blo by tmp0 which is < 32
'' (B,Blo) >> tmp0 = (B>>tmp0), (B<<tmp1)|(Blo>>tmp0)
mov tmp1,#32
sub tmp1,tmp0
mov tmp2,Blo
shl tmp2,tmp1 wz
if_nz or Aflag, #FLAG_STICKY
shr Blo, tmp0
mov tmp2, B
shr B, tmp0
shl tmp2, tmp1
or Blo, tmp2
_doadd
'' now perform the addition
mov tmp0, Aflag
xor tmp0, Bflag
test tmp0, #FLAG_SIGN wz
if_nz jmp #_dosub
add Alo, Blo wc
addx A, B
ret
_dosub
'' check for INF - INF
'' note that if B is INF, then A is NAN or INF, so
'' in either case NAN is appropriate to return
test Bflag, #FLAG_INF wz
if_nz or Aflag, #FLAG_NAN
test Aflag, #FLAG_STICKY wc
subx Alo, Blo wcz
subx A, B
mov Blo, Alo
or Blo, A wz
if_z andn Aflag, #FLAG_SIGN
if_z or Aflag, #FLAG_ZERO
ret
'' MulAcc128:
'' multiply B,Blo * C,Clo
'' and accumulate result into A,Alo,Alo2,Alo3
_MulAcc128
qmul Blo, Clo
mov rs1, B
mov rs2, C
call #imp_mulhu ' rs1 = low word, rd = high word
add Alo, rs1 wc
addx A, rd
getqx tmp0
getqy tmp1
add Alo3, tmp0 wc
addx Alo2, tmp1 wc
addx Alo, #0 wc
addx A, #0 wc
mov tmp0, Blo
or tmp0, Clo wz
if_z jmp #_mulacc128_shift_short
'' now do the cross products
cmp Clo, #0 wz
if_nz qmul B, Clo
mov rs1, Blo wz
if_z mov rd, #0
mov rs2, C
if_nz call #imp_mulhu ' rs1 = low word, rd = high word
add Alo2, rs1 wc
addx Alo, rd wc
addx A, #0
cmp Clo, #0 wz
if_nz getqx tmp0
if_nz getqy tmp1
if_nz add Alo2, tmp0 wc
if_nz addx Alo, tmp1 wc
if_nz addx A, #0
_mulacc128_shift
'' now we have the result as an 8.120 bit number in A, Alo, Alo2, Alo3
'' shift it up by 4 to get 4.128
shl A, #4
getnib tmp0, Alo, #7
or A, tmp0
shl Alo, #4
getnib tmp0, Alo2, #7
or Alo, tmp0
shl Alo2, #4
getnib tmp0, Alo3, #7
or Alo2, tmp0
_ret_ shl Alo3, #4
_mulacc128_shift_short
'' now we have the result as an 8.120 bit number in A, Alo, Alo2, Alo3
'' shift it up by 4 to get 4.128
shl A, #4
getnib tmp0, Alo, #7
or A, tmp0
_ret_ shl Alo, #4
'' the actual multiply routine
_Mul
mov tmp0,Aflag
or tmp0,Bflag
test tmp0,#(FLAG_INF|FLAG_NAN) wz
if_nz jmp #_mul_excep
'' regular multiply
add Aexp,Bexp
'' calculate (A,Alo) * (B, Blo)
'' both are 4.60 numbers
mov C, A wz
if_z jmp #_mul_sign
mov Clo, Alo
mov A, #0
mov Alo, #0
mov Alo2, #0
mov Alo3, #0
cmp B, #0 wz
if_z jmp #_mul_sign
call #_MulAcc128
or Alo2, Alo3 wz
if_nz or Aflag, #FLAG_STICKY
_mul_sign
and Bflag, #FLAG_SIGN ' adjust sign of result
_ret_ xor Aflag, Bflag
'' special cases for zero, inf, NaN
_mul_excep
'' if we get here, we know that either the
'' NAN or INF bit is set
'' if 0 is set as well, we have an illegal condition
'' NAN*anything = NAN
'' 0*inf == NAN
test tmp0, #(FLAG_NAN|FLAG_ZERO) wz
if_nz or Aflag,#FLAG_NAN
if_z or Aflag,#FLAG_INF
jmp #_mul_sign
''
'' the actual division routine
''
''
'' (A, Alo) / (B, Blo)
''
'' DivSmall is for when Alo and Blo are both 0
''
_DivSmall
'' start div, assuming we will need it
shr A, #4 ' bottom bits of A are 0
qfrac A, B
'' set sign of result
mov tmp0, Aflag
xor tmp0, Bflag
test tmp0, #FLAG_SIGN wz
muxnz Aflag, #FLAG_SIGN
mov tmp0, Aflag
or tmp0, Bflag
'' check for divide by infinity or NAN
test tmp0, #(FLAG_INF|FLAG_NAN) wz
if_nz jmp #_div_excep
'' check for divide by 0
test Bflag, #FLAG_ZERO wz
if_nz jmp #_div_by_zero
sub Aexp, Bexp
getqx A ' quotient
getqy tmp0 ' remainder
cmp tmp0, #0 wz
if_nz or Aflag, #FLAG_STICKY
ret
''
'' perform 4.60 / 4.60 division
''
_Div
mov count,#61
_doDiv
'' set the sign of the result
mov tmp0, Aflag
xor tmp0, Bflag
test tmp0, #FLAG_SIGN wz
muxnz Aflag,#FLAG_SIGN
mov tmp0, Aflag
or tmp0, Bflag
'' check for divide by infinity or NAN
test tmp0, #(FLAG_INF|FLAG_NAN) wz
if_nz jmp #_div_excep
'' check for divide by 0
test Bflag, #FLAG_ZERO wz
if_nz jmp #_div_by_zero
'' regular divide loop here
sub Aexp, Bexp
mov tmp0, Alo
mov tmp1, A
'' initialize quotient
mov A, #0
mov Alo, #0
_divloop
cmp tmp0, Blo wcz
cmpx tmp1, B wcz
if_b jmp #_div_skip_sub
sub tmp0, Blo wcz
subx tmp1, B
shl Alo, #1 wc
or Alo, #1
jmp #_div_next
_div_skip_sub
shl Alo, #1 wc
_div_next
rcl A, #1
shl tmp0, #1 wc
rcl tmp1, #1
djnz count, #_divloop
'' set sticky bit if necessary
or tmp0,tmp1 wz
if_nz or Aflag, #FLAG_STICKY
_Div_ret
_DivSmall_ret
ret
_div_by_zero
test Aflag, #(FLAG_NAN|FLAG_INF|FLAG_ZERO) wz
if_nz or Aflag, #FLAG_NAN
if_z or Aflag, #FLAG_INF
jmp #_Div_ret
''
'' if some number is infinity or NaN, come here
''
_div_excep
test tmp0, #FLAG_NAN wz
_div_nan
if_nz or Aflag, #FLAG_NAN
if_nz jmp #_Div_ret
test Aflag, #FLAG_INF wz
if_z jmp #_a_finite
'' infinity/x
test Bflag, #(FLAG_INF) wz
if_nz jmp #_div_nan
jmp #_Div_ret
'' x/infinity
_a_finite
or Aflag, #FLAG_ZERO
mov A, #0
mov Alo, #0
jmp #_Div_ret
''
'' square root calculation
''
_Sqrt
test Aflag, #(FLAG_NAN|FLAG_ZERO) wz
if_nz ret
'' sqrt(-x) -> NaN
test Aflag, #(FLAG_SIGN) wz
if_nz jmp #sqrt_excep
test Aflag, #FLAG_INF wz ' sqrt(inf) == inf
if_nz ret
mov Alo2, #0
' originally our mantissa is set up with 1 <= A < 2
' and in 4.60 format
' convert to 2.62 format
' note that 1 * 2^60 = 4 * 2^58
shl Alo, #1 wc
rcl A, #1
shl Alo, #1 wc
rcl A, #1
sub Aexp, #2
test Aexp, #1 wz ' make Aexp even if necessary
if_nz add Aexp, #1
if_nz shr A, #1 wc
if_nz rcr Alo, #1
'' get square root
#ifdef DEBUG
mov uart_char, #">"
call #ser_tx
mov uart_num, A
call #ser_hex
mov uart_num, Alo
call #ser_hex
mov uart_char, #"^"
mov uart_num, Aexp
call #ser_hex
call #ser_nl
#endif
qsqrt Alo, A
'' save original value of A
mov Cexp, Aexp
mov C, A
mov Clo, Alo
mov Cflag, Aflag
' do square root
sar Aexp, #1
getqx A ' A has square root in 1.31 format
test Cflag, #FLAG_DOUBLE wz
if_nz jmp #sqrt_heron
cmps Cexp, ##-FBIAS_EXP wcz
if_be jmp #sqrt_heron
'' convert to 4.28
mov Alo, A
shl Alo, #30
shr A, #2
'' FIXME: should check here for exact squares,
'' for which FLAG_STICKY is not appropriate
_ret_ or Aflag, #FLAG_STICKY
sqrt_heron
'' have to do an iteration of Heron's algorithm to get
'' the low bits
'' we have C = origA (2.30)
'' A = x0 (1.31)
'' calculate x1 = 1/2 * (x0 + origA / x0)
''
mov Alo, #0
'' x0 is 1.31
''
'' compute origA / x0
''
'' we know 0.5 <= x0 < 2
'' and 0.5 <= origA < 4
''
'' Q = 2^32
'' S = 2^60
'' T = 2^62
''
'' origA = (C*Q + Clo) / T
'' x0 = B / R, where R = 2^31
''
'' so T * origA / x0 = T * ((C*Q + Clo) / T) * (R / A)
'' = T * ((C*Q + Clo) / A) * (R / T)
'' = ((C*Q + Clo) / A) * R
''
'' we're calculating 2^32 * C / A
'' what we really want is 2^31 * C / A
'' so our value is twice as big as it should be
setq C
qdiv Clo, A
getqx B ' B is the quotient
getqy tmp1 ' tmp1 is the remainder
qfrac tmp1, A
getqx Blo
getqy tmp1
'' so here B,Blo = 2*(origA / x0) as a 2.30 number
'' or, equivalently, origA / x0 as a 1.31 number
'' now make A = (x0 + (origA / x0)) as a 1.31 number
add Alo, Blo wc
addx A, B wc
'' finally divide by 2
rcr A, #1 wc
rcr Alo, #1
'' we have a 1.63 number here, but want 4.60
shr A, #1 wc
rcr Alo, #1
shr A, #1 wc
rcr Alo, #1
#ifdef DEBUG
mov uart_char, #"i"
call #ser_tx
mov uart_num, A
call #ser_hex
mov uart_num, Alo
call #ser_hex
call #ser_nl
#endif
#ifdef EXACT
'' OK, let's calculate the error A*A - origA
'' origA is in C
'' so set B = A*A
qmul A, Alo
mov rs1, A
mov rs2, A
call #imp_mulhu ' result in rd, rs1
mov B, rd
mov Blo, rs1
mov rs1, Alo
mov rs2, Alo
call #imp_mulhu ' result in rd, rs1
'' so now partial result is:
'' B, Blo, rd, rs1
getqx tmp1
getqy tmp0
'' double (tmp0, tmp1)
add tmp1, tmp1 wc
addx tmp0, tmp0 wc
addx B, #0
'' B, Blo + tmp0, rd + tmp1, rs1
add tmp1, rd wc
addx Blo, tmp0 wc
addx B, #0
'' partials are in 8.120 format
'' need to shift up to 4.124