1234353Sdim/* Copyright (C) 2012-2022 Free Software Foundation, Inc.
2226584Sdim   Contributed by Altera and Mentor Graphics, Inc.
3226584Sdim
4226584SdimThis file is free software; you can redistribute it and/or modify it
5226584Sdimunder the terms of the GNU General Public License as published by the
6226584SdimFree Software Foundation; either version 3, or (at your option) any
7226584Sdimlater version.
8226584Sdim
9226584SdimThis file is distributed in the hope that it will be useful, but
10226584SdimWITHOUT ANY WARRANTY; without even the implied warranty of
11226584SdimMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12226584SdimGeneral Public License for more details.
13226584Sdim
14226584SdimUnder Section 7 of GPL version 3, you are granted additional
15226584Sdimpermissions described in the GCC Runtime Library Exception, version
16226584Sdim3.1, as published by the Free Software Foundation.
17226584Sdim
18226584SdimYou should have received a copy of the GNU General Public License and
19234353Sdima copy of the GCC Runtime Library Exception along with this program;
20226584Sdimsee the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
21226584Sdim<http://www.gnu.org/licenses/>.  */
22226584Sdim
23226584Sdim#include "lib2-gcn.h"
24226584Sdim
25226584Sdim/* 32-bit SI divide and modulo as used in gcn.  */
26226584Sdim
27226584Sdimstatic USItype
28226584Sdimudivmodsi4 (USItype num, USItype den, word_type modwanted)
29226584Sdim{
30226584Sdim  USItype bit = 1;
31226584Sdim  USItype res = 0;
32226584Sdim
33226584Sdim  while (den < num && bit && !(den & (1L<<31)))
34226584Sdim    {
35226584Sdim      den <<=1;
36226584Sdim      bit <<=1;
37226584Sdim    }
38226584Sdim  while (bit)
39226584Sdim    {
40226584Sdim      if (num >= den)
41226584Sdim	{
42226584Sdim	  num -= den;
43226584Sdim	  res |= bit;
44226584Sdim	}
45226584Sdim      bit >>=1;
46226584Sdim      den >>=1;
47234353Sdim    }
48226584Sdim  if (modwanted)
49226584Sdim    return num;
50226584Sdim  return res;
51226584Sdim}
52226584Sdim
53226584Sdim
54226584SdimSItype
55226584Sdim__divsi3 (SItype a, SItype b)
56226584Sdim{
57226584Sdim  word_type neg = 0;
58234353Sdim  SItype res;
59226584Sdim
60226584Sdim  if (a < 0)
61226584Sdim    {
62226584Sdim      a = -a;
63226584Sdim      neg = !neg;
64226584Sdim    }
65226584Sdim
66226584Sdim  if (b < 0)
67226584Sdim    {
68226584Sdim      b = -b;
69226584Sdim      neg = !neg;
70226584Sdim    }
71226584Sdim
72226584Sdim  res = udivmodsi4 (a, b, 0);
73226584Sdim
74226584Sdim  if (neg)
75226584Sdim    res = -res;
76234353Sdim
77226584Sdim  return res;
78226584Sdim}
79226584Sdim
80226584Sdim
81226584SdimSItype
82226584Sdim__modsi3 (SItype a, SItype b)
83226584Sdim{
84226584Sdim  word_type neg = 0;
85226584Sdim  SItype res;
86226584Sdim
87226584Sdim  if (a < 0)
88226584Sdim    {
89226584Sdim      a = -a;
90226584Sdim      neg = 1;
91226584Sdim    }
92226584Sdim
93226584Sdim  if (b < 0)
94226584Sdim    b = -b;
95226584Sdim
96226584Sdim  res = udivmodsi4 (a, b, 1);
97226584Sdim
98226584Sdim  if (neg)
99226584Sdim    res = -res;
100226584Sdim
101226584Sdim  return res;
102226584Sdim}
103226584Sdim
104226584Sdim
105226584SdimUSItype
106226584Sdim__udivsi3 (USItype a, USItype b)
107226584Sdim{
108226584Sdim  return udivmodsi4 (a, b, 0);
109226584Sdim}
110226584Sdim
111226584Sdim
112226584SdimUSItype
113226584Sdim__umodsi3 (USItype a, USItype b)
114226584Sdim{
115226584Sdim  return udivmodsi4 (a, b, 1);
116226584Sdim}
117226584Sdim
118226584Sdim