Imported Upstream version 2.9.0
[debian/cc1111] / device / lib / _fs2slong.c
1 /* Floating point library in optimized assembly for 8051
2  * Copyright (c) 2004, Paul Stoffregen, paul@pjrc.com
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  */
18
19
20 #define SDCC_FLOAT_LIB
21 #include <float.h>
22
23
24 #ifdef FLOAT_ASM_MCS51
25
26 // long __fs2slong (float x)
27 static void dummy(void) __naked
28 {
29         __asm
30         .globl  ___fs2slong
31 ___fs2slong:
32         lcall   fsgetarg
33         clr     c
34         mov     a, #158
35         subb    a, exp_a
36         jc      fs2slong_maxval         //  |x| >= 2^32
37 fs2slong_int_ok:
38         mov     r1, #0
39         lcall   fs_rshift_a
40         jnb     sign_a, fs2slong_pos
41 fs2slong_neg:
42         mov     a, r1
43         cpl     a
44         add     a, #1
45         mov     dpl, a
46         mov     a, r2
47         cpl     a
48         addc    a, #0
49         mov     dph, a
50         mov     a, r3
51         cpl     a
52         addc    a, #0
53         mov     b, a
54         mov     a, r4
55         cpl     a
56         addc    a, #0
57         //Check for zero
58         jnz fs2slong_not_zero
59         mov a, dpl
60         orl a, dph
61         orl a, b
62         jnz fs2slong_clr_a
63         ret
64 fs2slong_clr_a:
65         clr a
66 fs2slong_not_zero:
67         jnb acc.7, fs2slong_maxval_neg  // x < -0x80000000
68         ret
69 fs2slong_pos:
70         mov     a, r4
71         jb      acc.7, fs2slong_maxval_pos  //  x > 0x7FFFFFFF
72         mov     dpl, r1
73         mov     dph, r2
74         mov     b, r3
75         ret
76 fs2slong_maxval:
77         jnb     sign_a, fs2slong_maxval_pos
78 fs2slong_maxval_neg:
79         clr     a
80         mov     dpl, a
81         mov     dph, a
82         mov     b, a
83         mov     a, #0x80
84         ret
85 fs2slong_maxval_pos:
86         mov     a, #0xFF
87         mov     dpl, a
88         mov     dph, a
89         mov     b, a
90         mov     a, #0x7F
91         ret
92         __endasm;
93 }
94
95 #else
96
97 /* convert float to signed long */
98 signed long __fs2slong (float f)
99 {
100
101   if (!f)
102     return 0;
103
104   if (f<0) {
105     return -__fs2ulong(-f);
106   } else {
107     return __fs2ulong(f);
108   }
109 }
110
111 #endif