?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. /*-------------------------------------------------------------------------
  2.    _modsint.c - routine for signed int (16 bit) modulus
  3.  
  4.    Copyright (C) 1999, Sandeep Dutta . sandeep.dutta@usa.net
  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.  
  30. #include <sdcc-lib.h>
  31.  
  32. #if _SDCC_MANGLES_SUPPORT_FUNS
  33. unsigned unsigned _moduint (unsigned a, unsigned b);
  34. #endif
  35.  
  36. /*   Assembler-functions are provided for:
  37.      mcs51 small
  38.      mcs51 small stack-auto
  39. */
  40.  
  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
  52.  
  53. #if defined _MODSINT_ASM_SMALL
  54.  
  55. static void
  56. _modsint_dummy (void) __naked
  57. {
  58.         __asm
  59.  
  60.         #define a0      dpl
  61.         #define a1      dph
  62.  
  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)
  76.  
  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
  81.  
  82.         setb    F0
  83.  
  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
  91.  
  92. a_not_negative:
  93.  
  94.         mov     a,b1
  95.         jnb     acc.7,b_not_negative
  96.  
  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
  104.  
  105. b_not_negative:
  106.  
  107.         lcall   __moduint
  108.  
  109.         jnb     F0,not_negative
  110.  
  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
  118.  
  119. not_negative:
  120.         ret
  121.  
  122.         __endasm;
  123. }
  124.  
  125. #elif defined _MODSINT_ASM_SMALL_AUTO
  126.  
  127. static void
  128. _modsint_dummy (void) __naked
  129. {
  130.         __asm
  131.  
  132.         #define a0      dpl
  133.         #define a1      dph
  134.  
  135.         ar0 = 0                 ; BUG register set is not considered
  136.         ar1 = 1
  137.  
  138.         .globl __modsint
  139.  
  140. __modsint:
  141.  
  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
  146.  
  147.         setb    F0
  148.  
  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
  156.  
  157. a_not_negative:
  158.  
  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
  163.  
  164.         jnb     acc.7,b_not_negative
  165.  
  166.         dec     r0
  167.  
  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
  176.  
  177. b_not_negative:
  178.  
  179.         mov     ar1,@r0         ; b1
  180.         dec     r0
  181.         mov     ar0,@r0         ; b0
  182.  
  183.         lcall   __modint
  184.  
  185.         jnb     F0,not_negative
  186.  
  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
  194.  
  195. not_negative:
  196.         ret
  197.  
  198.         __endasm;
  199. }
  200.  
  201. #else  // _MODSINT_ASM_
  202.  
  203. int _modsint (int a, int b)
  204. {
  205.   register int r;
  206.  
  207.   r = (unsigned)(a < 0 ? -a : a) % (unsigned)(b < 0 ? -b : b);
  208.  
  209.   if (a < 0)
  210.     return -r;
  211.   else
  212.     return r;
  213. }
  214.  
  215. #endif  // _MODSINT_ASM_
  216.