?login_element?

Subversion Repositories NedoOS

Rev

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

  1. /*-------------------------------------------------------------------------
  2.    _divslonglong.c - routine for divsion of 64 bit unsigned long long
  3.  
  4.    Copyright (C) 1999, Jean-Louis Vern <jlvern AT gmail.com>
  5.    Copyright (C) 2012, Philipp Klaus Krause . pkk@spth.de
  6.  
  7.    This library is free software; you can redistribute it and/or modify it
  8.    under the terms of the GNU General Public License as published by the
  9.    Free Software Foundation; either version 2, or (at your option) any
  10.    later version.
  11.  
  12.    This library is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License
  18.    along with this library; see the file COPYING. If not, write to the
  19.    Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
  20.    MA 02110-1301, USA.
  21.  
  22.    As a special exception, if you link this library with other files,
  23.    some of which are compiled with SDCC, to produce an executable,
  24.    this library does not by itself cause the resulting executable to
  25.    be covered by the GNU General Public License. This exception does
  26.    not however invalidate any other reasons why the executable file
  27.    might be covered by the GNU General Public License.
  28. -------------------------------------------------------------------------*/
  29.  
  30. #pragma std_c99
  31.  
  32. #include <stdint.h>
  33. #include <stdbool.h>
  34.  
  35. #ifdef __SDCC_LONGLONG
  36. #define MSB_SET(x) ((x >> (8*sizeof(x)-1)) & 1)
  37.  
  38. unsigned long long
  39. _divulonglong (unsigned long long x, unsigned long long y)
  40. {
  41.   unsigned long long reste = 0L;
  42.   unsigned char count = 64;
  43.   bool c;
  44.  
  45.   do
  46.   {
  47.     // reste: x <- 0;
  48.     c = MSB_SET(x);
  49.     x <<= 1;
  50.     reste <<= 1;
  51.     if (c)
  52.       reste |= 1L;
  53.  
  54.     if (reste >= y)
  55.     {
  56.       reste -= y;
  57.       // x <- (result = 1)
  58.       x |= 1L;
  59.     }
  60.   }
  61.   while (--count);
  62.   return x;
  63. }
  64. #endif
  65.  
  66.