?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. /*-------------------------------------------------------------------------
  2.    _divuint.c - routine for unsigned int (16 bit) division
  3.  
  4.    Copyright (C) 1999, Jean-Louis Vern <jlvern AT gmail.com>
  5.  
  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.
  10.  
  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.
  15.  
  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.
  20.  
  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. -------------------------------------------------------------------------*/
  28.  
  29. /*   Assembler-functions are provided for:
  30.      mcs51 small
  31.      mcs51 small stack-auto
  32. */
  33.  
  34. #include <stdbool.h>
  35.  
  36. #if !defined(__SDCC_USE_XSTACK) && !defined(_SDCC_NO_ASM_LIB_FUNCS)
  37. #  if defined(__SDCC_mcs51)
  38. #    if defined(__SDCC_MODEL_SMALL)
  39. #      if defined(__SDCC_STACK_AUTO)
  40. #        define _DIVUINT_ASM_SMALL_AUTO
  41. #      else
  42. #        define _DIVUINT_ASM_SMALL
  43. #      endif
  44. #    endif
  45. #  endif
  46. #endif
  47.  
  48. #if defined _DIVUINT_ASM_SMALL || defined _DIVUINT_ASM_SMALL_AUTO
  49.  
  50. static void
  51. _divuint_dummy (void) __naked
  52. {
  53.         __asm
  54.  
  55.         .globl __divuint
  56.  
  57. __divuint:
  58.  
  59.         #define count   r2
  60.         #define reste_l r3
  61.         #define reste_h r4
  62.         #define xl      dpl
  63.         #define xh      dph
  64.  
  65. #if defined(__SDCC_PARMS_IN_BANK1)
  66.         #define yl      (b1_0)
  67.         #define yh      (b1_1)
  68. #else // __SDCC_PARMS_IN_BANK1
  69.   #if defined(__SDCC_STACK_AUTO)
  70.  
  71.         .globl __divint
  72.  
  73.         mov     a,sp
  74.         add     a,#-2           ; 2 bytes return address
  75.         mov     r0,a            ; r0 points to yh
  76.         mov     a,@r0           ; load yh
  77.         mov     r1,a
  78.         dec     r0
  79.         mov     a,@r0           ; load yl
  80.         mov     r0,a
  81.  
  82.         #define yl      r0
  83.         #define yh      r1
  84.  
  85. __divint:                       ; entry point for __divsint
  86.  
  87.  
  88.   #else // __SDCC_STACK_AUTO
  89.  
  90.     #if defined(__SDCC_NOOVERLAY)
  91.         .area DSEG    (DATA)
  92.     #else
  93.         .area OSEG    (OVR,DATA)
  94.     #endif
  95.  
  96.         .globl __divuint_PARM_2
  97.         .globl __divsint_PARM_2
  98.  
  99. __divuint_PARM_2:
  100. __divsint_PARM_2:
  101.         .ds     2
  102.  
  103.         .area CSEG    (CODE)
  104.  
  105.         #define yl      (__divuint_PARM_2)
  106.         #define yh      (__divuint_PARM_2 + 1)
  107.  
  108.   #endif // __SDCC_STACK_AUTO
  109. #endif // __SDCC_PARMS_IN_BANK1
  110.  
  111.         mov     count,#16
  112.         clr     a
  113.         mov     reste_l,a
  114.         mov     reste_h,a
  115.  
  116. loop:
  117.         mov     a,xl            ; x <<= 1
  118.         add     a,acc
  119.         mov     xl,a
  120.         mov     a,xh
  121.         rlc     a
  122.         mov     xh,a
  123.  
  124.         mov     a,reste_l       ; reste <<= 1
  125.         rlc     a               ;   feed in carry
  126.         mov     reste_l,a
  127.         mov     a,reste_h
  128.         rlc     a
  129.         mov     reste_h,a
  130.  
  131.         mov     a,reste_l       ; reste - y
  132.         subb    a,yl            ; here carry is always clear, because
  133.                                         ; reste <<= 1 never overflows
  134.         mov     b,a
  135.         mov     a,reste_h
  136.         subb    a,yh
  137.  
  138.         jc      smaller         ; reste >= y?
  139.  
  140.         mov     reste_h,a       ; -> yes;  reste = reste - y;
  141.         mov     reste_l,b
  142.         orl     xl,#1
  143. smaller:                        ; -> no
  144.         djnz    count,loop
  145.         ret
  146.  
  147.         __endasm;
  148. }
  149.  
  150. #else  // defined _DIVUINT_ASM_SMALL || defined _DIVUINT_ASM_SMALL_AUTO
  151.  
  152. #define MSB_SET(x) ((x >> (8*sizeof(x)-1)) & 1)
  153.  
  154. unsigned int
  155. _divuint (unsigned int x, unsigned int y)
  156. {
  157.   unsigned int reste = 0;
  158.   unsigned char count = 16;
  159.   bool c;
  160.  
  161.   do
  162.   {
  163.     // reste: x <- 0;
  164.     c = MSB_SET(x);
  165.     x <<= 1;
  166.     reste <<= 1;
  167.     if (c)
  168.       reste |= 1;
  169.  
  170.     if (reste >= y)
  171.     {
  172.       reste -= y;
  173.       // x <- (result = 1)
  174.       x |= 1;
  175.     }
  176.   }
  177.   while (--count);
  178.   return x;
  179. }
  180.  
  181. #endif  // defined _DIVUINT_ASM_SMALL || defined _DIVUINT_ASM_SMALL_AUTO
  182.