aboutsummaryrefslogtreecommitdiffstats
path: root/arch/m68k/fpsp040/bindec.S
blob: f2e795231046049bd41950bd642178f81065c819 (plain) (blame)
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
|
|	bindec.sa 3.4 1/3/91
|
|	bindec
|
|	Description:
|		Converts an input in extended precision format
|		to bcd format.
|
|	Input:
|		a0 points to the input extended precision value
|		value in memory; d0 contains the k-factor sign-extended
|		to 32-bits.  The input may be either normalized,
|		unnormalized, or denormalized.
|
|	Output:	result in the FP_SCR1 space on the stack.
|
|	Saves and Modifies: D2-D7,A2,FP2
|
|	Algorithm:
|
|	A1.	Set RM and size ext;  Set SIGMA = sign of input.
|		The k-factor is saved for use in d7. Clear the
|		BINDEC_FLG for separating normalized/denormalized
|		input.  If input is unnormalized or denormalized,
|		normalize it.
|
|	A2.	Set X = abs(input).
|
|	A3.	Compute ILOG.
|		ILOG is the log base 10 of the input value.  It is
|		approximated by adding e + 0.f when the original
|		value is viewed as 2^^e * 1.f in extended precision.
|		This value is stored in d6.
|
|	A4.	Clr INEX bit.
|		The operation in A3 above may have set INEX2.
|
|	A5.	Set ICTR = 0;
|		ICTR is a flag used in A13.  It must be set before the
|		loop entry A6.
|
|	A6.	Calculate LEN.
|		LEN is the number of digits to be displayed.  The
|		k-factor can dictate either the total number of digits,
|		if it is a positive number, or the number of digits
|		after the decimal point which are to be included as
|		significant.  See the 68882 manual for examples.
|		If LEN is computed to be greater than 17, set OPERR in
|		USER_FPSR.  LEN is stored in d4.
|
|	A7.	Calculate SCALE.
|		SCALE is equal to 10^ISCALE, where ISCALE is the number
|		of decimal places needed to insure LEN integer digits
|		in the output before conversion to bcd. LAMBDA is the
|		sign of ISCALE, used in A9. Fp1 contains
|		10^^(abs(ISCALE)) using a rounding mode which is a
|		function of the original rounding mode and the signs
|		of ISCALE and X.  A table is given in the code.
|
|	A8.	Clr INEX; Force RZ.
|		The operation in A3 above may have set INEX2.
|		RZ mode is forced for the scaling operation to insure
|		only one rounding error.  The grs bits are collected in
|		the INEX flag for use in A10.
|
|	A9.	Scale X -> Y.
|		The mantissa is scaled to the desired number of
|		significant digits.  The excess digits are collected
|		in INEX2.
|
|	A10.	Or in INEX.
|		If INEX is set, round error occurred.  This is
|		compensated for by 'or-ing' in the INEX2 flag to
|		the lsb of Y.
|
|	A11.	Restore original FPCR; set size ext.
|		Perform FINT operation in the user's rounding mode.
|		Keep the size to extended.
|
|	A12.	Calculate YINT = FINT(Y) according to user's rounding
|		mode.  The FPSP routine sintd0 is used.  The output
|		is in fp0.
|
|	A13.	Check for LEN digits.
|		If the int operation results in more than LEN digits,
|		or less than LEN -1 digits, adjust ILOG and repeat from
|		A6.  This test occurs only on the first pass.  If the
|		result is exactly 10^LEN, decrement ILOG and divide
|		the mantissa by 10.
|
|	A14.	Convert the mantissa to bcd.
|		The binstr routine is used to convert the LEN digit
|		mantissa to bcd in memory.  The input to binstr is
|		to be a fraction; i.e. (mantissa)/10^LEN and adjusted
|		such that the decimal point is to the left of bit 63.
|		The bcd digits are stored in the correct position in
|		the final string area in memory.
|
|	A15.	Convert the exponent to bcd.
|		As in A14 above, the exp is converted to bcd and the
|		digits are stored in the final string.
|		Test the length of the final exponent string.  If the
|		length is 4, set operr.
|
|	A16.	Write sign bits to final string.
|
|	Implementation Notes:
|
|	The registers are used as follows:
|
|		d0: scratch; LEN input to binstr
|		d1: scratch
|		d2: upper 32-bits of mantissa for binstr
|		d3: scratch;lower 32-bits of mantissa for binstr
|		d4: LEN
|		d5: LAMBDA/ICTR
|		d6: ILOG
|		d7: k-factor
|		a0: ptr for original operand/final result
|		a1: scratch pointer
|		a2: pointer to FP_X; abs(original value) in ext
|		fp0: scratch
|		fp1: scratch
|		fp2: scratch
|		F_SCR1:
|		F_SCR2:
|		L_SCR1:
|		L_SCR2:

|		Copyright (C) Motorola, Inc. 1990
|			All Rights Reserved
|
|       For details on the license for this file, please see the
|       file, README, in this same directory.

|BINDEC    idnt    2,1 | Motorola 040 Floating Point Software Package

#include "fpsp.h"

	|section	8

| Constants in extended precision
LOG2:	.long	0x3FFD0000,0x9A209A84,0xFBCFF798,0x00000000
LOG2UP1:	.long	0x3FFD0000,0x9A209A84,0xFBCFF799,0x00000000

| Constants in single precision
FONE:	.long	0x3F800000,0x00000000,0x00000000,0x00000000
FTWO:	.long	0x40000000,0x00000000,0x00000000,0x00000000
FTEN:	.long	0x41200000,0x00000000,0x00000000,0x00000000
F4933:	.long	0x459A2800,0x00000000,0x00000000,0x00000000

RBDTBL:	.byte	0,0,0,0
	.byte	3,3,2,2
	.byte	3,2,2,3
	.byte	2,3,3,2

	|xref	binstr
	|xref	sintdo
	|xref	ptenrn,ptenrm,ptenrp

	.global	bindec
	.global	sc_mul
bindec:
	moveml	%d2-%d7/%a2,-(%a7)
	fmovemx %fp0-%fp2,-(%a7)

| A1. Set RM and size ext. Set SIGMA = sign input;
|     The k-factor is saved for use in d7.  Clear BINDEC_FLG for
|     separating  normalized/denormalized input.  If the input
|     is a denormalized number, set the BINDEC_FLG memory word
|     to signal denorm.  If the input is unnormalized, normalize
|     the input and test for denormalized result.
|
	fmovel	#rm_mode,%FPCR	|set RM and ext
	movel	(%a0),L_SCR2(%a6)	|save exponent for sign check
	movel	%d0,%d7		|move k-factor to d7
	clrb	BINDEC_FLG(%a6)	|clr norm/denorm flag
	movew	STAG(%a6),%d0	|get stag
	andiw	#0xe000,%d0	|isolate stag bits
	beq	A2_str		|if zero, input is norm
|
| Normalize the denorm
|
un_de_norm:
	movew	(%a0),%d0
	andiw	#0x7fff,%d0	|strip sign of normalized exp
	movel	4(%a0),%d1
	movel	8(%a0),%d2
norm_loop:
	subw	#1,%d0
	lsll	#1,%d2
	roxll	#1,%d1
	tstl	%d1
	bges	norm_loop
|
| Test if the normalized input is denormalized
|
	tstw	%d0
	bgts	pos_exp		|if greater than zero, it is a norm
	st	BINDEC_FLG(%a6)	|set flag for denorm
pos_exp:
	andiw	#0x7fff,%d0	|strip sign of normalized exp
	movew	%d0,(%a0)
	movel	%d1,4(%a0)
	movel	%d2,8(%a0)

| A2. Set X = abs(input).
|
A2_str:
	movel	(%a0),FP_SCR2(%a6) | move input to work space
	movel	4(%a0),FP_SCR2+4(%a6) | move input to work space
	movel	8(%a0),FP_SCR2+8(%a6) | move input to work space
	andil	#0x7fffffff,FP_SCR2(%a6) |create abs(X)

| A3. Compute ILOG.
|     ILOG is the log base 10 of the input value.  It is approx-
|     imated by adding e + 0.f when the original value is viewed
|     as 2^^e * 1.f in extended precision.  This value is stored
|     in d6.
|
| Register usage:
|	Input/Output
|	d0: k-factor/exponent
|	d2: x/x
|	d3: x/x
|	d4: x/x
|	d5: x/x
|	d6: x/ILOG
|	d7: k-factor/Unchanged
|	a0: ptr for original operand/final result
|	a1: x/x
|	a2: x/x
|	fp0: x/float(ILOG)
|	fp1: x/x
|	fp2: x/x
|	F_SCR1:x/x
|	F_SCR2:Abs(X)/Abs(X) with $3fff exponent
|	L_SCR1:x/x
|	L_SCR2:first word of X packed/Unchanged

	tstb	BINDEC_FLG(%a6)	|check for denorm
	beqs	A3_cont		|if clr, continue with norm
	movel	#-4933,%d6	|force ILOG = -4933
	bras	A4_str
A3_cont:
	movew	FP_SCR2(%a6),%d0	|move exp to d0
	movew	#0x3fff,FP_SCR2(%a6) |replace exponent with 0x3fff
	fmovex	FP_SCR2(%a6),%fp0	|now fp0 has 1.f
	subw	#0x3fff,%d0	|strip off bias
	faddw	%d0,%fp0		|add in exp
	fsubs	FONE,%fp0	|subtract off 1.0
	fbge	pos_res		|if pos, branch
	fmulx	LOG2UP1,%fp0	|if neg, mul by LOG2UP1
	fmovel	%fp0,%d6		|put ILOG in d6 as a lword
	bras	A4_str		|go move out ILOG
pos_res:
	fmulx	LOG2,%fp0	|if pos, mul by LOG2
	fmovel	%fp0,%d6		|put ILOG in d6 as a lword


| A4. Clr INEX bit.
|     The operation in A3 above may have set INEX2.

A4_str:
	fmovel	#0,%FPSR		|zero all of fpsr - nothing needed


| A5. Set ICTR = 0;
|     ICTR is a flag used in A13.  It must be set before the
|     loop entry A6. The lower word of d5 is used for ICTR.

	clrw	%d5		|clear ICTR


| A6. Calculate LEN.
|     LEN is the number of digits to be displayed.  The k-factor
|     can dictate either the total number of digits, if it is
|     a positive number, or the number of digits after the
|     original decimal point which are to be included as
|     significant.  See the 68882 manual for examples.
|     If LEN is computed to be greater than 17, set OPERR in
|     USER_FPSR.  LEN is stored in d4.
|
| Register usage:
|	Input/Output
|	d0: exponent/Unchanged
|	d2: x/x/scratch
|	d3: x/x
|	d4: exc picture/LEN
|	d5: ICTR/Unchanged
|	d6: ILOG/Unchanged
|	d7: k-factor/Unchanged
|	a0: ptr for original operand/final result
|	a1: x/x
|	a2: x/x
|	fp0: float(ILOG)/Unchanged
|	fp1: x/x
|	fp2: x/x
|	F_SCR1:x/x
|	F_SCR2:Abs(X) with $3fff exponent/Unchanged
|	L_SCR1:x/x
|	L_SCR2:first word of X packed/Unchanged

A6_str:
	tstl	%d7		|branch on sign of k
	bles	k_neg		|if k <= 0, LEN = ILOG + 1 - k
	movel	%d7,%d4		|if k > 0, LEN = k
	bras	len_ck		|skip to LEN check
k_neg:
	movel	%d6,%d4		|first load ILOG to d4
	subl	%d7,%d4		|subtract off k
	addql	#1,%d4		|add in the 1
len_ck:
	tstl	%d4		|LEN check: branch on sign of LEN
	bles	LEN_ng		|if neg, set LEN = 1
	cmpl	#17,%d4		|test if LEN > 17
	bles	A7_str		|if not, forget it
	movel	#17,%d4		|set max LEN = 17
	tstl	%d7		|if negative, never set OPERR
	bles	A7_str		|if positive, continue
	orl	#opaop_mask,USER_FPSR(%a6) |set OPERR & AIOP in USER_FPSR
	bras	A7_str		|finished here
LEN_ng:
	moveql	#1,%d4		|min LEN is 1


| A7. Calculate SCALE.
|     SCALE is equal to 10^ISCALE, where ISCALE is the number
|     of decimal places needed to insure LEN integer digits
|     in the output before conversion to bcd. LAMBDA is the sign
|     of ISCALE, used in A9.  Fp1 contains 10^^(abs(ISCALE)) using
|     the rounding mode as given in the following table (see
|     Coonen, p. 7.23 as ref.; however, the SCALE variable is
|     of opposite sign in bindec.sa from Coonen).
|
|	Initial					USE
|	FPCR[6:5]	LAMBDA	SIGN(X)		FPCR[6:5]
|	----------------------------------------------
|	 RN	00	   0	   0		00/0	RN
|	 RN	00	   0	   1		00/0	RN
|	 RN	00	   1	   0		00/0	RN
|	 RN	00	   1	   1		00/0	RN
|	 RZ	01	   0	   0		11/3	RP
|	 RZ	01	   0	   1		11/3	RP
|	 RZ	01	   1	   0		10/2	RM
|	 RZ	01	   1	   1		10/2	RM
|	 RM	10	   0	   0		11/3	RP
|	 RM	10	   0	   1		10/2	RM
|	 RM	10	   1	   0		10/2	RM
|	 RM	10	   1	   1		11/3	RP
|	 RP	11	   0	   0		10/2	RM
|	 RP	11	   0	   1		11/3	RP
|	 RP	11	   1	   0		11/3	RP
|	 RP	11	   1	   1		10/2	RM
|
| Register usage:
|	Input/Output
|	d0: exponent/scratch - final is 0
|	d2: x/0 or 24 for A9
|	d3: x/scratch - offset ptr into PTENRM array
|	d4: LEN/Unchanged
|	d5: 0/ICTR:LAMBDA
|	d6: ILOG/ILOG or k if ((k<=0)&(ILOG<k))
|	d7: k-factor/Unchanged
|	a0: ptr for original operand/final result
|	a1: x/ptr to PTENRM array
|	a2: x/x
|	fp0: float(ILOG)/Unchanged
|	fp1: x/10^ISCALE
|	fp2: x/x
|	F_SCR1:x/x
|	F_SCR2:Abs(X) with $3fff exponent/Unchanged
|	L_SCR1:x/x
|	L_SCR2:first word of X packed/Unchanged

A7_str:
	tstl	%d7		|test sign of k
	bgts	k_pos		|if pos and > 0, skip this
	cmpl	%d6,%d7		|test k - ILOG
	blts	k_pos		|if ILOG >= k, skip this
	movel	%d7,%d6		|if ((k<0) & (ILOG < k)) ILOG = k
k_pos:
	movel	%d6,%d0		|calc ILOG + 1 - LEN in d0
	addql	#1,%d0		|add the 1
	subl	%d4,%d0		|sub off LEN
	swap	%d5		|use upper word of d5 for LAMBDA
	clrw	%d5		|set it zero initially
	clrw	%d2		|set up d2 for very small case
	tstl	%d0		|test sign of ISCALE
	bges	iscale		|if pos, skip next inst
	addqw	#1,%d5		|if neg, set LAMBDA true
	cmpl	#0xffffecd4,%d0	|test iscale <= -4908
	bgts	no_inf		|if false, skip rest
	addil	#24,%d0		|add in 24 to iscale
	movel	#24,%d2		|put 24 in d2 for A9
no_inf:
	negl	%d0		|and take abs of ISCALE
iscale:
	fmoves	FONE,%fp1	|init fp1 to 1
	bfextu	USER_FPCR(%a6){#26:#2},%d1 |get initial rmode bits
	lslw	#1,%d1		|put them in bits 2:1
	addw	%d5,%d1		|add in LAMBDA
	lslw	#1,%d1		|put them in bits 3:1
	tstl	L_SCR2(%a6)	|test sign of original x
	bges	x_pos		|if pos, don't set bit 0
	addql	#1,%d1		|if neg, set bit 0
x_pos:
	leal	RBDTBL,%a2	|load rbdtbl base
	moveb	(%a2,%d1),%d3	|load d3 with new rmode
	lsll	#4,%d3		|put bits in proper position
	fmovel	%d3,%fpcr		|load bits into fpu
	lsrl	#4,%d3		|put bits in proper position
	tstb	%d3		|decode new rmode for pten table
	bnes	not_rn		|if zero, it is RN