* src/SDCCval.c, src/SDCCmain.c, device/include/limits.h,
[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         jnz     fs2schar_not_zero
35         mov     a, dpl
36         orl     a, dph
37         orl     a, b
38         jnz     fs2schar_clr_a
39         ret
40 fs2schar_clr_a:
41         clr a
42 fs2schar_not_zero:
43         jnb     sign_a, fs2schar_pos
44 fs2schar_neg:
45         cpl     a
46         jnz     fs2schar_maxval_neg
47         mov     a, b
48         cpl     a
49         jnz     fs2schar_maxval_neg
50         mov     a, dph
51         cpl     a
52         jnz     fs2schar_maxval_neg
53         mov     a, dpl
54         jnb     acc.7, fs2schar_maxval_neg
55         ret
56 fs2schar_maxval_neg:
57         mov     dpl, #0x80
58         ret
59 fs2schar_pos:
60         jnz     fs2schar_maxval_pos
61         mov     a, b
62         jnz     fs2schar_maxval_pos
63         mov     a, dph
64         jnz     fs2schar_maxval_pos
65         mov     a, dpl
66         jb      acc.7, fs2schar_maxval_pos
67         ret
68 fs2schar_maxval_pos:
69         mov     dpl, #0x7F
70         ret
71         __endasm;
72 }
73
74 #else
75
76 /* convert float to signed char */
77 signed char __fs2schar (float f)
78 {
79   signed long sl=__fs2slong(f);
80   if (sl>=SCHAR_MAX)
81     return SCHAR_MAX;
82   if (sl<=SCHAR_MIN)
83     return -SCHAR_MIN;
84   return sl;
85 }
86
87 #endif