1 /*-------------------------------------------------------------------------
2    _fsreturnval.c - Floating point library in optimized assembly for 8051
3 
4    Copyright (c) 2004, Paul Stoffregen, paul@pjrc.com
5 
6    This library is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by the
8    Free Software Foundation; either version 2, or (at your option) any
9    later version.
10 
11    This library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this library; see the file COPYING. If not, write to the
18    Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
19    MA 02110-1301, USA.
20 
21    As a special exception, if you link this library with other files,
22    some of which are compiled with SDCC, to produce an executable,
23    this library does not by itself cause the resulting executable to
24    be covered by the GNU General Public License. This exception does
25    not however invalidate any other reasons why the executable file
26    might be covered by the GNU General Public License.
27 -------------------------------------------------------------------------*/
28 
29 
30 
31 #define __SDCC_FLOAT_LIB
32 #include <float.h>
33 
34 
35 #ifdef FLOAT_ASM_MCS51
36 
dummy(void)37 static void dummy(void) __naked
38 {
39 	__asm
40 
41 	.globl	fs_round_and_return
42 fs_round_and_return:
43 #ifdef FLOAT_FULL_ACCURACY
44 	// discard the extra 8 bits of precision we kept around in r1
45 	cjne	r1, #128, 00001$
46 	mov	a, r2
47 	rrc	a
48 	cpl	c
49 00001$:
50 	jc	fs_zerocheck_return
51 	mov	a, r2
52 	add	a, #1
53 	mov	r2, a
54 	clr	a
55 	addc	a, r3
56 	mov	r3, a
57 	clr	a
58 	addc	a, r4
59 	mov	r4, a
60 	jnc	fs_zerocheck_return
61 	mov	r4, #0x80
62 	inc	exp_a
63 #endif
64 
65 	.globl	fs_zerocheck_return
66 fs_zerocheck_return:
67 	// zero output is a special case
68 	cjne	r4, #0, fs_direct_return
69 	cjne	r3, #0, fs_direct_return
70 	cjne	r2, #0, fs_direct_return
71 
72 	.globl	fs_return_zero
73 fs_return_zero:
74 	clr	a
75 	mov	b, a
76 	mov	dph, a
77 	mov	dpl, a
78 	ret
79 
80 	.globl	fs_direct_return
81 fs_direct_return:
82 	// collect all pieces and return
83 	mov	c, sign_a
84 	mov	a, exp_a
85 	rrc	a
86 	mov	b, r4
87 	mov	b.7, c
88 	mov	dph, r3
89 	mov	dpl, r2
90 	ret
91 
92 	.globl	fs_return_inf
93 fs_return_inf:
94 	clr	a
95 	mov	dph, a
96 	mov	dpl, a
97 	mov	b, #0x80
98 	cpl	a
99 	mov	c, sign_a
100 	rrc	a
101 	ret
102 
103 	.globl	fs_return_nan
104 fs_return_nan:
105 	clr	a
106 	mov	dph, a
107 	mov	dpl, a
108 	mov	b, #0xC0
109 	mov	a, #0x7F
110 	ret
111 
112 	__endasm;
113 }
114 
115 #endif
116