ca9754a7f5770dfe44a8535ad5f55c8d67f79402
[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         push acc
34         jnz fs2sint_not_zero
35         mov a, dpl
36         jnz fs2sint_not_zero
37         mov a, dph
38         jnz fs2sint_not_zero
39         mov a, b
40         jnz fs2sint_not_zero
41         pop acc
42         ret
43 fs2sint_not_zero:
44     pop acc
45         jnb     sign_a, fs2sint_pos
46 fs2sint_neg:
47         cpl     a
48         jnz     fs2sint_maxval_neg
49         mov     a, b
50         cpl     a
51         jnz     fs2sint_maxval_neg
52         mov     a, dph
53         jnb     acc.7, fs2sint_maxval_neg
54         ret
55 fs2sint_maxval_neg:
56         mov     dptr, #0x8000
57         ret
58 fs2sint_pos:
59         jnz     fs2sint_maxval_pos
60         mov     a, b
61         jnz     fs2sint_maxval_pos
62         mov     a, dph
63         jb      acc.7, fs2sint_maxval_pos
64         ret
65 fs2sint_maxval_pos:
66         mov     dptr, #0x7FFF
67         ret
68         _endasm;
69 }
70
71
72 #else
73
74
75
76 /* convert float to signed int */
77 signed int __fs2sint (float f) {
78   signed long sl=__fs2slong(f);
79   if (sl>=INT_MAX)
80     return INT_MAX;
81   if (sl<=INT_MIN) 
82     return -INT_MIN;
83   return sl;
84 }
85
86 #endif
87