1;----------------------------------------------------------------------
2; Floating Point Library for 6502
3;----------------------------------------------------------------------
4; (C)1978 Microsoft
5
6	lda #'-'	;exponent is negative.
7fout14
8	sta fbuffr+1,y	;store sign of exponent.
9	lda #'E'
10	sta fbuffr,y	;store the "e" character.
11	txa
12	ldx #$2f
13	sec
14fout15
15	inx		;move closer to output value.
16	sbc #$0a	;subtract 10.
17	bcs fout15	;not negative yet.
18	adc #$3a	;get second output character.
19	sta fbuffr+3,y	;store high digit.
20	txa
21	sta fbuffr+2,y	;store low digit.
22	lda #0		;put in terminator.
23	sta fbuffr+4,y
24	beq fout20	;return, (always branches).
25fout19
26	sta fbuffr-1,y	;store the character.
27fout17
28	lda #0		;a terminator.
29	sta fbuffr,y
30fout20
31	lda #<fbuffr
32	ldy #>fbuffr
33	rts		;all done.
34
35				;1/2
36fhalf	.byt $80,$00
37zero	.byt $00,$00,$00
38
39foutbl				;powers of 10
40	.byt $fa,$0a,$1f,$00	;-100,000,000
41	.byt $00,$98,$96,$80	;  10,000,000
42	.byt $ff,$f0,$bd,$c0	;  -1,000,000
43	.byt $00,$01,$86,$a0	;     100,000
44	.byt $ff,$ff,$d8,$f0	;     -10,000
45	.byt $00,$00,$03,$e8	;       1,000
46	.byt $ff,$ff,$ff,$9c	;        -100
47	.byt $00,$00,$00,$0a	;          10
48	.byt $ff,$ff,$ff,$ff	;          -1
49fdcend
50
51			;exponentiation --- x^y.
52			;n.b. 0^0=1
53			;first check if y=0. if so, the result is one.
54			;next check if x=0. if so the result is zero.
55			;then check if x>0. if not check that y is an integer.
56			;if so, negate x, so that lg doesn't give fcerr.
57			;if x is negative and y is odd, negate the result
58			;returned by exp.
59			;to compute the result use x^y=exp((y*log(x)).
60
61fpwrt
62	beq exp		;if fac=0, just exponentiate taht.
63	lda argexp	;is x=0?
64	bne fpwrt1
65	jmp zerof1	;zero fac.
66
67fpwrt1
68	ldx #<tempf3	;save it for later in a temp.
69	ldy #>tempf3
70	jsr movmf
71			;y=0 already. good; in case no one calls int.
72	lda argsgn
73	bpl fpwr1	;no problems if x>0.
74	jsr int		;integerize the fac.
75	lda #<tempf3	;get addr of comperand.
76	ldy #>tempf3
77	jsr fcomp	;equal?
78	bne fpwr1	;leave x neg. log will blow him out.
79			;a=-1 and y is irrelavant.
80	tya		;negative x. make positive.
81	ldy integr	;get evenness.
82fpwr1
83	jsr movfa1	;alternate entry point
84	tya
85	pha		;save evenness for later.
86	jsr log	        ;find log
87	lda #<tempf3	;multiply fac times log(x).
88	ldy #>tempf3
89	jsr fmult
90	jsr exp		;exponentiate the fac.
91	pla
92	lsr a		;is it even?
93	bcc negrts	;yes. or x>0.
94			;negate the number in fac.
95negop
96	lda facexp
97	beq negrts
98	lda facsgn
99	eor #$ff
100	sta facsgn
101negrts
102	rts
103
104