1 /*-------------------------------------------------------------------------
2 _divuint.c - routine for unsigned int (16 bit) division
4 Copyright (C) 1999, Jean-Louis Vern <jlvern AT gmail.com>
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
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,
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 -------------------------------------------------------------------------*/
29 /* Assembler-functions are provided for:
31 mcs51 small stack-auto
38 #if !defined(__SDCC_USE_XSTACK) && !defined(_SDCC_NO_ASM_LIB_FUNCS)
39 # if defined(__SDCC_mcs51)
40 # if defined(__SDCC_MODEL_SMALL)
41 # if defined(__SDCC_STACK_AUTO)
42 # define _DIVUINT_ASM_SMALL_AUTO
44 # define _DIVUINT_ASM_SMALL
50 #if defined _DIVUINT_ASM_SMALL || defined _DIVUINT_ASM_SMALL_AUTO
53 _divuint_dummy (void) __naked
67 #if defined(__SDCC_PARMS_IN_BANK1)
70 #else // __SDCC_PARMS_IN_BANK1
71 #if defined(__SDCC_STACK_AUTO)
76 add a
,#-2 ; 2 bytes return address
77 mov r0
,a
; r0 points to yh
87 __divint
: ; entry point
for __divsint
90 #else // __SDCC_STACK_AUTO
92 #if defined(__SDCC_NOOVERLAY)
98 .globl __divuint_PARM_2
99 .globl __divsint_PARM_2
107 #define yl (__divuint_PARM_2)
108 #define yh (__divuint_PARM_2 + 1)
110 #endif // __SDCC_STACK_AUTO
111 #endif // __SDCC_PARMS_IN_BANK1
126 mov a
,reste_l
; reste
<<= 1
127 rlc a
; feed in carry
133 mov a
,reste_l
; reste
- y
134 subb a
,yl
; here carry is always clear
, because
135 ; reste
<<= 1 never overflows
140 jc smaller
; reste
>= y
?
142 mov reste_h
,a
; -> yes
; reste
= reste
- y
;
152 #else // defined _DIVUINT_ASM_SMALL || defined _DIVUINT_ASM_SMALL_AUTO
154 #define MSB_SET(x) ((x >> (8*sizeof(x)-1)) & 1)
157 _divuint (unsigned int x
, unsigned int y
) __SDCC_NONBANKED
159 unsigned int reste
= 0;
160 unsigned char count
= 16;
183 #endif // defined _DIVUINT_ASM_SMALL || defined _DIVUINT_ASM_SMALL_AUTO