Imported Upstream version 2.9.0
[debian/cc1111] / device / lib / _fs2ulong.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 // unsigned long __fs2ulong (float x)
27 static void dummy(void) __naked
28 {
29         __asm
30         .globl  ___fs2ulong
31 ___fs2ulong:
32         mov     r7, #158
33         .globl  fs2ulong_begin
34 fs2ulong_begin:
35         lcall   fsgetarg
36         jnb     sign_a, fs2ulong_int
37         ljmp    fs_return_zero
38
39 fs2ulong_int:
40         clr     c
41         mov     a, r7
42         subb    a, exp_a
43         jnc     fs2ulong_int_ok
44         // if we get here, x >= 2^32
45         mov     a, #0xFF
46         mov     b, a
47         mov     dph, a
48         mov     dpl, a
49         ret
50
51 fs2ulong_int_ok:
52         mov     r1, #0
53         lcall   fs_rshift_a
54
55 fs2ulong_done:
56         mov     dpl, r1
57         mov     dph, r2
58         mov     b, r3
59         mov     a, r4
60         ret
61         __endasm;
62 }
63
64 #else
65
66 /*
67 ** libgcc support for software floating point.
68 ** Copyright (C) 1991 by Pipeline Associates, Inc.  All rights reserved.
69 ** Permission is granted to do *anything* you want with this file,
70 ** commercial or otherwise, provided this message remains intact.  So there!
71 ** I would appreciate receiving any updates/patches/changes that anyone
72 ** makes, and am willing to be the repository for said changes (am I
73 ** making a big mistake?).
74 **
75 ** Pat Wood
76 ** Pipeline Associates, Inc.
77 ** pipeline!phw@motown.com or
78 ** sun!pipeline!phw or
79 ** uunet!motown!pipeline!phw
80 */
81
82 /* (c)2000/2001: hacked a little by johan.knol@iduna.nl for sdcc */
83
84
85 union float_long
86 {
87   float f;
88   long l;
89 };
90
91 /* convert float to unsigned long */
92 unsigned long 
93 __fs2ulong (float a1)
94 {
95   volatile union float_long fl1;
96   volatile int exp;
97   volatile long l;
98   
99   fl1.f = a1;
100   
101   if (!fl1.l || SIGN(fl1.l))
102     return (0);
103
104   exp = EXP (fl1.l) - EXCESS - 24;
105   l = MANT (fl1.l);
106   
107   l >>= -exp;
108
109   return l;
110 }
111
112 #endif