Better handling of floats between -1.0 and 0.0
[fw/sdcc] / device / lib / _fs2schar.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
25 #ifdef FLOAT_ASM_MCS51
26
27 // char __fs2schar (float x)
28 static void dummy(void) _naked
29 {
30         _asm
31         .globl  ___fs2schar
32 ___fs2schar:
33         lcall   ___fs2slong
34         push acc
35         jnz fs2schar_not_zero
36         mov a, dpl
37         jnz fs2schar_not_zero
38         mov a, dph
39         jnz fs2schar_not_zero
40         mov a, b
41         jnz fs2schar_not_zero
42         pop acc
43         ret
44 fs2schar_not_zero:
45     pop acc
46         jnb     sign_a, fs2schar_pos
47 fs2schar_neg:
48         cpl     a
49         jnz     fs2schar_maxval_neg
50         mov     a, b
51         cpl     a
52         jnz     fs2schar_maxval_neg
53         mov     a, dph
54         cpl     a
55         jnz     fs2schar_maxval_neg
56         mov     a, dpl
57         jnb     acc.7, fs2schar_maxval_neg
58         ret
59 fs2schar_maxval_neg:
60         mov     dpl, #0x80
61         ret
62 fs2schar_pos:
63         jnz     fs2schar_maxval_pos
64         mov     a, b
65         jnz     fs2schar_maxval_pos
66         mov     a, dph
67         jnz     fs2schar_maxval_pos
68         mov     a, dpl
69         jb      acc.7, fs2schar_maxval_pos
70         ret
71 fs2schar_maxval_pos:
72         mov     dpl, #0x7F
73         ret
74         _endasm;
75 }
76
77
78 #else
79
80
81 /* convert float to signed char */
82 signed char __fs2schar (float f) {
83   signed long sl=__fs2slong(f);
84   if (sl>=CHAR_MAX)
85     return CHAR_MAX;
86   if (sl<=CHAR_MIN) 
87     return -CHAR_MIN;
88   return sl;
89 }
90
91 #endif
92