1/* libgcc routines for MeP.
2   Copyright (C) 2001-2015 Free Software Foundation, Inc.
3
4This file is free software; you can redistribute it and/or modify it
5under the terms of the GNU General Public License as published by the
6Free Software Foundation; either version 3 of the License, or (at your
7option) any later version.
8
9This file is distributed in the hope that it will be useful, but
10WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12General Public License for more details.
13
14Under Section 7 of GPL version 3, you are granted additional
15permissions described in the GCC Runtime Library Exception, version
163.1, as published by the Free Software Foundation.
17
18You should have received a copy of the GNU General Public License and
19a copy of the GCC Runtime Library Exception along with this program;
20see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
21<http://www.gnu.org/licenses/>.  */
22
23typedef		 int SItype		__attribute__ ((mode (SI)));
24typedef unsigned int USItype		__attribute__ ((mode (SI)));
25
26typedef int word_type			__attribute__ ((mode (__word__)));
27
28USItype
29__mulsi3 (USItype a, USItype b)
30{
31  USItype c = 0;
32
33  while (a != 0)
34    {
35      if (a & 1)
36	c += b;
37      a >>= 1;
38      b <<= 1;
39    }
40
41  return c;
42}
43
44
45
46USItype
47udivmodsi4(USItype num, USItype den, word_type modwanted)
48{
49  USItype bit = 1;
50  USItype res = 0;
51
52  while (den < num && bit && !(den & (1L<<31)))
53    {
54      den <<=1;
55      bit <<=1;
56    }
57  while (bit)
58    {
59      if (num >= den)
60	{
61	  num -= den;
62	  res |= bit;
63	}
64      bit >>=1;
65      den >>=1;
66    }
67  if (modwanted) return num;
68  return res;
69}
70
71
72
73SItype
74__divsi3 (SItype a, SItype b)
75{
76  word_type neg = 0;
77  SItype res;
78
79  if (a < 0)
80    {
81      a = -a;
82      neg = !neg;
83    }
84
85  if (b < 0)
86    {
87      b = -b;
88      neg = !neg;
89    }
90
91  res = udivmodsi4 (a, b, 0);
92
93  if (neg)
94    res = -res;
95
96  return res;
97}
98
99
100
101SItype
102__modsi3 (SItype a, SItype b)
103{
104  word_type neg = 0;
105  SItype res;
106
107  if (a < 0)
108    {
109      a = -a;
110      neg = 1;
111    }
112
113  if (b < 0)
114    b = -b;
115
116  res = udivmodsi4 (a, b, 1);
117
118  if (neg)
119    res = -res;
120
121  return res;
122}
123
124
125
126
127SItype
128__udivsi3 (SItype a, SItype b)
129{
130  return udivmodsi4 (a, b, 0);
131}
132
133
134
135SItype
136__umodsi3 (SItype a, SItype b)
137{
138  return udivmodsi4 (a, b, 1);
139}
140