1/* libgcc routines for R8C/M16C/M32C
2   Copyright (C) 2005
3   Free Software Foundation, Inc.
4   Contributed by Red Hat.
5
6   This file is part of GCC.
7
8   GCC is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published
10   by the Free Software Foundation; either version 2, or (at your
11   option) any later version.
12
13   In addition to the permissions in the GNU General Public License,
14   the Free Software Foundation gives you unlimited permission to link
15   the compiled version of this file into combinations with other
16   programs, and to distribute those combinations without any
17   restriction coming from the use of this file.  (The General Public
18   License restrictions do apply in other respects; for example, they
19   cover modification of the file, and distribution when not linked
20   into a combine executable.)
21
22   GCC is distributed in the hope that it will be useful, but WITHOUT
23   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
25   License for more details.
26
27   You should have received a copy of the GNU General Public License
28   along with GCC; see the file COPYING.  If not, write to the Free
29   Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
30   02110-1301, USA.  */
31
32typedef          int  HItype __attribute__ ((mode (HI)));
33typedef unsigned int UHItype __attribute__ ((mode (HI)));
34typedef          int  SItype __attribute__ ((mode (SI)));
35typedef unsigned int USItype __attribute__ ((mode (SI)));
36
37typedef int word_type __attribute__ ((mode (__word__)));
38
39USItype udivmodsi4 (USItype num, USItype den, word_type modwanted);
40SItype __divsi3 (SItype a, SItype b);
41SItype __modsi3 (SItype a, SItype b);
42SItype __udivsi3 (SItype a, SItype b);
43SItype __umodsi3 (SItype a, SItype b);
44
45USItype
46udivmodsi4 (USItype num, USItype den, word_type modwanted)
47{
48  USItype bit = 1;
49  USItype res = 0;
50
51  while (den < num && bit && !(den & (1L << 31)))
52    {
53      den <<= 1;
54      bit <<= 1;
55    }
56  while (bit)
57    {
58      if (num >= den)
59	{
60	  num -= den;
61	  res |= bit;
62	}
63      bit >>= 1;
64      den >>= 1;
65    }
66  if (modwanted)
67    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