OSDN Git Service

Add NIOS2 support. Code from SourceyG++.
[pf3gnuchains/gcc-fork.git] / libgcc / config / nios2 / lib2-divmod.c
1 /* Copyright (C) 2012 Free Software Foundation, Inc.
2    Contributed by Altera and Mentor Graphics, Inc.
3
4 This file is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 3, or (at your option) any
7 later version.
8
9 This file is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 General Public License for more details.
13
14 Under Section 7 of GPL version 3, you are granted additional
15 permissions described in the GCC Runtime Library Exception, version
16 3.1, as published by the Free Software Foundation.
17
18 You should have received a copy of the GNU General Public License and
19 a copy of the GCC Runtime Library Exception along with this program;
20 see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
21 <http://www.gnu.org/licenses/>.  */
22
23 #include "lib2-nios2.h"
24
25 /* 32-bit SI divide and modulo as used in Nios II.  */
26
27 static USItype
28 udivmodsi4 (USItype num, USItype den, word_type modwanted)
29 {
30   USItype bit = 1;
31   USItype res = 0;
32
33   while (den < num && bit && !(den & (1L<<31)))
34     {
35       den <<=1;
36       bit <<=1;
37     }
38   while (bit)
39     {
40       if (num >= den)
41         {
42           num -= den;
43           res |= bit;
44         }
45       bit >>=1;
46       den >>=1;
47     }
48   if (modwanted) return num;
49   return res;
50 }
51
52
53 SItype
54 __divsi3 (SItype a, SItype b)
55 {
56   word_type neg = 0;
57   SItype res;
58
59   if (a < 0)
60     {
61       a = -a;
62       neg = !neg;
63     }
64
65   if (b < 0)
66     {
67       b = -b;
68       neg = !neg;
69     }
70
71   res = udivmodsi4 (a, b, 0);
72
73   if (neg)
74     res = -res;
75
76   return res;
77 }
78
79
80 SItype
81 __modsi3 (SItype a, SItype b)
82 {
83   word_type neg = 0;
84   SItype res;
85
86   if (a < 0)
87     {
88       a = -a;
89       neg = 1;
90     }
91
92   if (b < 0)
93     b = -b;
94
95   res = udivmodsi4 (a, b, 1);
96
97   if (neg)
98     res = -res;
99
100   return res;
101 }
102
103
104 SItype
105 __udivsi3 (SItype a, SItype b)
106 {
107   return udivmodsi4 (a, b, 0);
108 }
109
110
111 SItype
112 __umodsi3 (SItype a, SItype b)
113 {
114   return udivmodsi4 (a, b, 1);
115 }
116