Modified patch originates from Andy Green <andy@openmoko.com>
[u-boot-openmoko/mini2440.git] / lib_nios2 / divmod.c
blob3c7e71e9757a6d49535f62a2a91315e3b472329f
1 /*
2 * This file is part of GNU CC.
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.
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.
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.
21 #include "math.h"
23 USItype udivmodsi4 (USItype num, USItype den, word_type modwanted)
25 USItype bit = 1;
26 USItype res = 0;
28 while (den < num && bit && !(den & (1L << 31))) {
29 den <<= 1;
30 bit <<= 1;
32 while (bit) {
33 if (num >= den) {
34 num -= den;
35 res |= bit;
37 bit >>= 1;
38 den >>= 1;
40 if (modwanted)
41 return num;
42 return res;
46 SItype __divsi3 (SItype a, SItype b)
48 word_type neg = 0;
49 SItype res;
51 if (a < 0) {
52 a = -a;
53 neg = !neg;
56 if (b < 0) {
57 b = -b;
58 neg = !neg;
61 res = udivmodsi4 (a, b, 0);
63 if (neg)
64 res = -res;
66 return res;
70 SItype __modsi3 (SItype a, SItype b)
72 word_type neg = 0;
73 SItype res;
75 if (a < 0) {
76 a = -a;
77 neg = 1;
80 if (b < 0)
81 b = -b;
83 res = udivmodsi4 (a, b, 1);
85 if (neg)
86 res = -res;
88 return res;
92 SItype __udivsi3 (SItype a, SItype b)
94 return udivmodsi4 (a, b, 0);
98 SItype __umodsi3 (SItype a, SItype b)
100 return udivmodsi4 (a, b, 1);