GCN libgcc.
[official-gcc.git] / libgcc / config / gcn / lib2-divmod.c
blobb5acbeaa9de78a3ea0f97efda24825bfab87c3bc
1 /* Copyright (C) 2012-2019 Free Software Foundation, Inc.
2 Contributed by Altera and Mentor Graphics, Inc.
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.
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.
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.
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/>. */
23 #include "lib2-gcn.h"
25 /* 32-bit SI divide and modulo as used in gcn. */
27 static USItype
28 udivmodsi4 (USItype num, USItype den, word_type modwanted)
30 USItype bit = 1;
31 USItype res = 0;
33 while (den < num && bit && !(den & (1L<<31)))
35 den <<=1;
36 bit <<=1;
38 while (bit)
40 if (num >= den)
42 num -= den;
43 res |= bit;
45 bit >>=1;
46 den >>=1;
48 if (modwanted)
49 return num;
50 return res;
54 SItype
55 __divsi3 (SItype a, SItype b)
57 word_type neg = 0;
58 SItype res;
60 if (a < 0)
62 a = -a;
63 neg = !neg;
66 if (b < 0)
68 b = -b;
69 neg = !neg;
72 res = udivmodsi4 (a, b, 0);
74 if (neg)
75 res = -res;
77 return res;
81 SItype
82 __modsi3 (SItype a, SItype b)
84 word_type neg = 0;
85 SItype res;
87 if (a < 0)
89 a = -a;
90 neg = 1;
93 if (b < 0)
94 b = -b;
96 res = udivmodsi4 (a, b, 1);
98 if (neg)
99 res = -res;
101 return res;
105 SItype
106 __udivsi3 (SItype a, SItype b)
108 return udivmodsi4 (a, b, 0);
112 SItype
113 __umodsi3 (SItype a, SItype b)
115 return udivmodsi4 (a, b, 1);