Imported Upstream version 2.9.0
[debian/cc1111] / device / lib / _fs2sint.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 // int __fs2sint (float x)
27 static void dummy(void) __naked
28 {
29         __asm
30         .globl  ___fs2sint
31 ___fs2sint:
32         lcall   ___fs2slong
33         jnz fs2sint_not_zero
34         mov a, dpl
35         orl a, dph
36         orl a, b
37         jnz fs2sint_clr_a
38         ret
39 fs2sint_clr_a:
40         clr a
41 fs2sint_not_zero:
42         jnb     sign_a, fs2sint_pos
43 fs2sint_neg:
44         cpl     a
45         jnz     fs2sint_maxval_neg
46         mov     a, b
47         cpl     a
48         jnz     fs2sint_maxval_neg
49         mov     a, dph
50         jnb     acc.7, fs2sint_maxval_neg
51         ret
52 fs2sint_maxval_neg:
53         mov     dptr, #0x8000
54         ret
55 fs2sint_pos:
56         jnz     fs2sint_maxval_pos
57         mov     a, b
58         jnz     fs2sint_maxval_pos
59         mov     a, dph
60         jb      acc.7, fs2sint_maxval_pos
61         ret
62 fs2sint_maxval_pos:
63         mov     dptr, #0x7FFF
64         ret
65         __endasm;
66 }
67
68 #else
69
70 /* convert float to signed int */
71 signed int __fs2sint (float f)
72 {
73   signed long sl=__fs2slong(f);
74   if (sl>=INT_MAX)
75     return INT_MAX;
76   if (sl<=INT_MIN)
77     return -INT_MIN;
78   return sl;
79 }
80
81 #endif