?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /*-------------------------------------------------------------------------
  2.    _divulong.c - routine for division of 32 bit unsigned long
  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) && !defined(__SDCC_PARMS_IN_BANK1)
  40. #        define _DIVULONG_ASM_SMALL_AUTO
  41. #      else
  42. #        define _DIVULONG_ASM_SMALL
  43. #      endif
  44. #    endif
  45. #  endif
  46. #endif
  47.  
  48. #if defined _DIVULONG_ASM_SMALL
  49.  
  50. static void
  51. _divlong_dummy (void) __naked
  52. {
  53.         __asm
  54.  
  55.         .globl __divulong
  56.  
  57. __divulong:
  58.  
  59.         #define count   r2
  60.  
  61.         #define x0      dpl
  62.         #define x1      dph
  63.         #define x2      b
  64.         #define x3      r3
  65.  
  66.         #define reste0  r4
  67.         #define reste1  r5
  68.         #define reste2  r6
  69.         #define reste3  r7
  70. #if !defined(__SDCC_PARMS_IN_BANK1)
  71.  
  72. #if defined(__SDCC_NOOVERLAY)
  73.         .area DSEG    (DATA)
  74. #else
  75.         .area OSEG    (OVR,DATA)
  76. #endif
  77.  
  78.         .globl __divulong_PARM_2
  79.         .globl __divslong_PARM_2
  80.  
  81. __divulong_PARM_2:
  82. __divslong_PARM_2:
  83.         .ds     4
  84.  
  85.         .area CSEG    (CODE)
  86.  
  87.         #define y0      (__divulong_PARM_2)
  88.         #define y1      (__divulong_PARM_2 + 1)
  89.         #define y2      (__divulong_PARM_2 + 2)
  90.         #define y3      (__divulong_PARM_2 + 3)
  91. #else
  92.         #define y0      (b1_0)
  93.         #define y1      (b1_1)
  94.         #define y2      (b1_2)
  95.         #define y3      (b1_3)
  96. #endif // !__SDCC_PARMS_IN_BANK1
  97.                                 ; parameter x comes in a, b, dph, dpl
  98.         mov     x3,a            ; save parameter x3
  99.  
  100.         mov     count,#32
  101.         clr     a
  102.         mov     reste0,a
  103.         mov     reste1,a
  104.         mov     reste2,a
  105.         mov     reste3,a
  106.  
  107.         ; optimization  loop in lp0 until the first bit is shifted into rest
  108.  
  109. lp0:    mov     a,x0            ; x <<= 1
  110.         add     a,x0
  111.         mov     x0,a
  112.         mov     a,x1
  113.         rlc     a
  114.         mov     x1,a
  115.         mov     a,x2
  116.         rlc     a
  117.         mov     x2,a
  118.         mov     a,x3
  119.         rlc     a
  120.         mov     x3,a
  121.  
  122.         jc      in_lp
  123.         djnz    count,lp0
  124.  
  125.         sjmp    exit
  126.  
  127. loop:   mov     a,x0            ; x <<= 1
  128.         add     a,x0
  129.         mov     x0,a
  130.         mov     a,x1
  131.         rlc     a
  132.         mov     x1,a
  133.         mov     a,x2
  134.         rlc     a
  135.         mov     x2,a
  136.         mov     a,x3
  137.         rlc     a
  138.         mov     x3,a
  139.  
  140. in_lp:  mov     a,reste0        ; reste <<= 1
  141.         rlc     a               ; feed in carry
  142.         mov     reste0,a
  143.         mov     a,reste1
  144.         rlc     a
  145.         mov     reste1,a
  146.         mov     a,reste2
  147.         rlc     a
  148.         mov     reste2,a
  149.         mov     a,reste3
  150.         rlc     a
  151.         mov     reste3,a
  152.  
  153.         mov     a,reste0        ; reste - y
  154.         subb    a,y0            ; carry is always clear here, because
  155.                                         ; reste <<= 1 never overflows
  156.         mov     a,reste1
  157.         subb    a,y1
  158.         mov     a,reste2
  159.         subb    a,y2
  160.         mov     a,reste3
  161.         subb    a,y3
  162.  
  163.         jc      minus           ; reste >= y?
  164.  
  165.                                 ; -> yes;  reste -= y;
  166.         mov     a,reste0
  167.         subb    a,y0            ; carry is always clear here (jc)
  168.         mov     reste0,a
  169.         mov     a,reste1
  170.         subb    a,y1
  171.         mov     reste1,a
  172.         mov     a,reste2
  173.         subb    a,y2
  174.         mov     reste2,a
  175.         mov     a,reste3
  176.         subb    a,y3
  177.         mov     reste3,a
  178.  
  179.         orl     x0,#1
  180.  
  181. minus:  djnz    count,loop      ; -> no
  182.  
  183. exit:   mov     a,x3            ; prepare the return value
  184.         ret
  185.  
  186.         __endasm;
  187. }
  188.  
  189. #elif defined _DIVULONG_ASM_SMALL_AUTO
  190.  
  191. static void
  192. _divlong_dummy (void) __naked
  193. {
  194.         __asm
  195.  
  196.         .globl __divulong
  197.  
  198. __divulong:
  199.  
  200.         #define count   r2
  201.  
  202.         #define x0      dpl
  203.         #define x1      dph
  204.         #define x2      b
  205.         #define x3      r3
  206.  
  207.         #define reste0  r4
  208.         #define reste1  r5
  209.         #define reste2  r6
  210.         #define reste3  r7
  211.  
  212.         .globl __divlong        ; entry point for __divslong
  213.  
  214.         #define y0      r1
  215.  
  216.                                 ; parameter x comes in a, b, dph, dpl
  217.         mov     x3,a            ; save parameter x3
  218.  
  219.         mov     a,sp
  220.         add     a,#-2-3         ; 2 bytes return address, 3 bytes param y
  221.         mov     r0,a            ; r0 points to y0
  222.  
  223. __divlong:                      ; entry point for __divslong
  224.  
  225.         mov     a,@r0           ; load y0
  226.         mov     y0,a
  227.         inc     r0              ; r0 points to y1
  228.  
  229.         mov     count,#32
  230.         clr     a
  231.         mov     reste0,a
  232.         mov     reste1,a
  233.         mov     reste2,a
  234.         mov     reste3,a
  235.  
  236.         ; optimization  loop in lp0 until the first bit is shifted into rest
  237.  
  238. lp0:    mov     a,x0            ; x <<= 1
  239.         add     a,x0
  240.         mov     x0,a
  241.         mov     a,x1
  242.         rlc     a
  243.         mov     x1,a
  244.         mov     a,x2
  245.         rlc     a
  246.         mov     x2,a
  247.         mov     a,x3
  248.         rlc     a
  249.         mov     x3,a
  250.  
  251.         jc      in_lp
  252.         djnz    count,lp0
  253.  
  254.         sjmp    exit
  255.  
  256. loop:   mov     a,x0            ; x <<= 1
  257.         add     a,x0
  258.         mov     x0,a
  259.         mov     a,x1
  260.         rlc     a
  261.         mov     x1,a
  262.         mov     a,x2
  263.         rlc     a
  264.         mov     x2,a
  265.         mov     a,x3
  266.         rlc     a
  267.         mov     x3,a
  268.  
  269. in_lp:  mov     a,reste0        ; reste <<= 1
  270.         rlc     a               ; feed in carry
  271.         mov     reste0,a
  272.         mov     a,reste1
  273.         rlc     a
  274.         mov     reste1,a
  275.         mov     a,reste2
  276.         rlc     a
  277.         mov     reste2,a
  278.         mov     a,reste3
  279.         rlc     a
  280.         mov     reste3,a
  281.  
  282.         mov     a,reste0        ; reste - y
  283.         subb    a,y0            ; carry is always clear here, because
  284.                                 ; reste <<= 1 never overflows
  285.         mov     a,reste1
  286.         subb    a,@r0           ; y1
  287.         mov     a,reste2
  288.         inc     r0
  289.         subb    a,@r0           ; y2
  290.         mov     a,reste3
  291.         inc     r0
  292.         subb    a,@r0           ; y3
  293.         dec     r0
  294.         dec     r0
  295.  
  296.         jc      minus           ; reste >= y?
  297.  
  298.                                 ; -> yes;  reste -= y;
  299.         mov     a,reste0
  300.         subb    a,y0            ; carry is always clear here (jc)
  301.         mov     reste0,a
  302.         mov     a,reste1
  303.         subb    a,@r0           ; y1
  304.         mov     reste1,a
  305.         mov     a,reste2
  306.         inc     r0
  307.         subb    a,@r0           ; y2
  308.         mov     reste2,a
  309.         mov     a,reste3
  310.         inc     r0
  311.         subb    a,@r0           ; y3
  312.         mov     reste3,a
  313.         dec     r0
  314.         dec     r0
  315.  
  316.         orl     x0,#1
  317.  
  318. minus:  djnz    count,loop      ; -> no
  319.  
  320. exit:   mov     a,x3            ; prepare the return value
  321.         ret
  322.  
  323.         __endasm;
  324. }
  325.  
  326. #else // _DIVULONG_ASM
  327.  
  328. #define MSB_SET(x) ((x >> (8*sizeof(x)-1)) & 1)
  329.  
  330. unsigned long
  331. _divulong (unsigned long x, unsigned long y)
  332. {
  333.   unsigned long reste = 0L;
  334.   unsigned char count = 32;
  335.   bool c;
  336.  
  337.   do
  338.   {
  339.     // reste: x <- 0;
  340.     c = MSB_SET(x);
  341.     x <<= 1;
  342.     reste <<= 1;
  343.     if (c)
  344.       reste |= 1L;
  345.  
  346.     if (reste >= y)
  347.     {
  348.       reste -= y;
  349.       // x <- (result = 1)
  350.       x |= 1L;
  351.     }
  352.   }
  353.   while (--count);
  354.   return x;
  355. }
  356.  
  357. #endif // _DIVULONG_ASM
  358.