struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / device / lib / _modsint.c
blobcffc77d7d410dae9854a0261d69fc44be9166f40
1 /*-------------------------------------------------------------------------
2 _modsint.c - routine for signed int (16 bit) modulus
4 Copyright (C) 1999, Sandeep Dutta . sandeep.dutta@usa.net
6 This library is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this library; see the file COPYING. If not, write to the
18 Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
19 MA 02110-1301, USA.
21 As a special exception, if you link this library with other files,
22 some of which are compiled with SDCC, to produce an executable,
23 this library does not by itself cause the resulting executable to
24 be covered by the GNU General Public License. This exception does
25 not however invalidate any other reasons why the executable file
26 might be covered by the GNU General Public License.
27 -------------------------------------------------------------------------*/
30 #include <sdcc-lib.h>
32 #if _SDCC_MANGLES_SUPPORT_FUNS
33 unsigned unsigned _moduint (unsigned a, unsigned b);
34 #endif
36 /* Assembler-functions are provided for:
37 mcs51 small
38 mcs51 small stack-auto
41 #if !defined(__SDCC_USE_XSTACK) && !defined(_SDCC_NO_ASM_LIB_FUNCS)
42 # if defined(__SDCC_mcs51)
43 # if defined(__SDCC_MODEL_SMALL)
44 # if defined(__SDCC_STACK_AUTO) && !defined(__SDCC_PARMS_IN_BANK1)
45 # define _MODSINT_ASM_SMALL_AUTO
46 # else
47 # define _MODSINT_ASM_SMALL
48 # endif
49 # endif
50 # endif
51 #endif
53 #if defined _MODSINT_ASM_SMALL
55 static void
56 _modsint_dummy (void) __naked
58 __asm
60 #define a0 dpl
61 #define a1 dph
63 .globl __modsint
64 #if defined(__SDCC_PARMS_IN_BANK1)
65 #define b0 (b1_0)
66 #define b1 (b1_1)
67 #else
68 // _modsint_PARM_2 shares the same memory with _moduint_PARM_2
69 // and is defined in _moduint.c
70 #define b0 (__modsint_PARM_2)
71 #define b1 (__modsint_PARM_2 + 1)
72 #endif
73 __modsint:
74 ; a1 in dph
75 ; b1 in (__modsint_PARM_2 + 1)
77 clr F0 ; Flag 0 in PSW
78 ; available to user for general purpose
79 mov a,a1
80 jnb acc.7,a_not_negative
82 setb F0
84 clr a
85 clr c
86 subb a,a0
87 mov a0,a
88 clr a
89 subb a,a1
90 mov a1,a
92 a_not_negative:
94 mov a,b1
95 jnb acc.7,b_not_negative
97 clr a
98 clr c
99 subb a,b0
100 mov b0,a
101 clr a
102 subb a,b1
103 mov b1,a
105 b_not_negative:
107 lcall __moduint
109 jnb F0,not_negative
111 clr a
112 clr c
113 subb a,a0
114 mov a0,a
115 clr a
116 subb a,a1
117 mov a1,a
119 not_negative:
122 __endasm;
125 #elif defined _MODSINT_ASM_SMALL_AUTO
127 static void
128 _modsint_dummy (void) __naked
130 __asm
132 #define a0 dpl
133 #define a1 dph
135 ar0 = 0 ; BUG register set is not considered
136 ar1 = 1
138 .globl __modsint
140 __modsint:
142 clr F0 ; Flag 0 in PSW
143 ; available to user for general purpose
144 mov a,a1
145 jnb acc.7,a_not_negative
147 setb F0
149 clr a
150 clr c
151 subb a,a0
152 mov a0,a
153 clr a
154 subb a,a1
155 mov a1,a
157 a_not_negative:
159 mov a,sp
160 add a,#-2 ; 2 bytes return address
161 mov r0,a ; r0 points to b1
162 mov a,@r0 ; b1
164 jnb acc.7,b_not_negative
166 dec r0
168 clr a
169 clr c
170 subb a,@r0 ; b0
171 mov @r0,a
172 clr a
173 inc r0
174 subb a,@r0 ; b1
175 mov @r0,a
177 b_not_negative:
179 mov ar1,@r0 ; b1
180 dec r0
181 mov ar0,@r0 ; b0
183 lcall __modint
185 jnb F0,not_negative
187 clr a
188 clr c
189 subb a,a0
190 mov a0,a
191 clr a
192 subb a,a1
193 mov a1,a
195 not_negative:
198 __endasm;
201 #else // _MODSINT_ASM_
203 int _modsint (int a, int b) __SDCC_NONBANKED
205 register int r;
207 r = (unsigned)(a < 0 ? -a : a) % (unsigned)(b < 0 ? -b : b);
209 if (a < 0)
210 return -r;
211 else
212 return r;
215 #endif // _MODSINT_ASM_