Added mcs51 assembly versions for the 12 float/integer conversion functions.
[fw/sdcc] / 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         jnb     sign_a, fs2sint_pos
34 fs2sint_neg:
35         cpl     a
36         jnz     fs2sint_maxval_neg
37         mov     a, b
38         cpl     a
39         jnz     fs2sint_maxval_neg
40         mov     a, dph
41         jnb     acc.7, fs2sint_maxval_neg
42         ret
43 fs2sint_maxval_neg:
44         mov     dptr, #0x8000
45         ret
46 fs2sint_pos:
47         jnz     fs2sint_maxval_pos
48         mov     a, b
49         jnz     fs2sint_maxval_pos
50         mov     a, dph
51         jb      acc.7, fs2sint_maxval_pos
52         ret
53 fs2sint_maxval_pos:
54         mov     dptr, #0x7FFF
55         ret
56         _endasm;
57 }
58
59
60 #else
61
62
63
64 /* convert float to signed int */
65 signed int __fs2sint (float f) {
66   signed long sl=__fs2slong(f);
67   if (sl>=INT_MAX)
68     return INT_MAX;
69   if (sl<=INT_MIN) 
70     return -INT_MIN;
71   return sl;
72 }
73
74 #endif
75