uboot/lib_nios2/divmod.c
<<
>>
Prefs
   1/*
   2 * This file is part of GNU CC.
   3 *
   4 * GNU CC is free software; you can redistribute it and/or modify
   5 * it under the terms of the GNU General Public License as published
   6 * by the Free Software Foundation; either version 2, or (at your
   7 * option) any later version.
   8 *
   9 * GNU CC is distributed in the hope that it will be useful,
  10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12 * GNU General Public License for more details.
  13 *
  14 * You should have received a copy of the GNU General Public
  15 * License along with GNU CC; see the file COPYING.  If not, write
  16 * to the Free Software Foundation, 59 Temple Place - Suite 330,
  17 * Boston, MA 02111-1307, USA.
  18 */
  19
  20
  21#include "math.h"
  22
  23USItype udivmodsi4 (USItype num, USItype den, word_type modwanted)
  24{
  25        USItype bit = 1;
  26        USItype res = 0;
  27
  28        while (den < num && bit && !(den & (1L << 31))) {
  29                den <<= 1;
  30                bit <<= 1;
  31        }
  32        while (bit) {
  33                if (num >= den) {
  34                        num -= den;
  35                        res |= bit;
  36                }
  37                bit >>= 1;
  38                den >>= 1;
  39        }
  40        if (modwanted)
  41                return num;
  42        return res;
  43}
  44
  45
  46SItype __divsi3 (SItype a, SItype b)
  47{
  48        word_type neg = 0;
  49        SItype res;
  50
  51        if (a < 0) {
  52                a = -a;
  53                neg = !neg;
  54        }
  55
  56        if (b < 0) {
  57                b = -b;
  58                neg = !neg;
  59        }
  60
  61        res = udivmodsi4 (a, b, 0);
  62
  63        if (neg)
  64                res = -res;
  65
  66        return res;
  67}
  68
  69
  70SItype __modsi3 (SItype a, SItype b)
  71{
  72        word_type neg = 0;
  73        SItype res;
  74
  75        if (a < 0) {
  76                a = -a;
  77                neg = 1;
  78        }
  79
  80        if (b < 0)
  81                b = -b;
  82
  83        res = udivmodsi4 (a, b, 1);
  84
  85        if (neg)
  86                res = -res;
  87
  88        return res;
  89}
  90
  91
  92SItype __udivsi3 (SItype a, SItype b)
  93{
  94        return udivmodsi4 (a, b, 0);
  95}
  96
  97
  98SItype __umodsi3 (SItype a, SItype b)
  99{
 100        return udivmodsi4 (a, b, 1);
 101}
 102