]> git.ipfire.org Git - thirdparty/gcc.git/blame - libgcc/config/gcn/lib2-divmod-di.c
Update copyright years.
[thirdparty/gcc.git] / libgcc / config / gcn / lib2-divmod-di.c
CommitLineData
a945c346 1/* Copyright (C) 2021-2024 Free Software Foundation, Inc.
a8a730cd
JB
2 Contributed by Mentor Graphics, 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, or (at your option) any
7later 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
23#include "lib2-gcn.h"
24
d9d67745 25/* 64-bit SI divide and modulo as used in gcn. */
a8a730cd 26
d9d67745
AS
27union pack {
28 UTItype ti;
29 struct {DItype quot, rem;} pair;
30};
31union upack {
32 UTItype ti;
33 struct {UDItype quot, rem;} pair;
34};
35
36UTItype
37__udivmoddi4 (UDItype num, UDItype den)
38{
39 UDItype bit = 1;
40 union upack res = {0};
41
42 while (den < num && bit && !(den & (1L<<63)))
43 {
44 den <<=1;
45 bit <<=1;
46 }
47 while (bit)
48 {
49 if (num >= den)
50 {
51 num -= den;
52 res.pair.quot |= bit;
53 }
54 bit >>=1;
55 den >>=1;
56 }
57 res.pair.rem = num;
58 return res.ti;
59}
60
61UTItype
62__divmoddi4 (DItype a, DItype b)
63{
64 word_type nega = 0, negb = 0;
65 union pack res;
66
67 if (a < 0)
68 {
69 a = -a;
70 nega = 1;
71 }
72
73 if (b < 0)
74 {
75 b = -b;
76 negb = 1;
77 }
78
79 res.ti = __udivmoddi4 (a, b);
80
81 if (nega)
82 res.pair.rem = -res.pair.rem;
83 if (nega ^ negb)
84 res.pair.quot = -res.pair.quot;
85
86 return res.ti;
87}
88
89
90DItype
91__divdi3 (DItype a, DItype b)
92{
93 union pack u;
94 u.ti = __divmoddi4 (a, b);
95 return u.pair.quot;
96}
97
98DItype
99__moddi3 (DItype a, DItype b)
100{
101 union pack u;
102 u.ti = __divmoddi4 (a, b);
103 return u.pair.rem;
104}
105
106
107UDItype
108__udivdi3 (UDItype a, UDItype b)
109{
110 union pack u;
111 u.ti = __udivmoddi4 (a, b);
112 return u.pair.quot;
113}
114
115UDItype
116__umoddi3 (UDItype a, UDItype b)
117{
118 union pack u;
119 u.ti = __udivmoddi4 (a, b);
120 return u.pair.rem;
121}
a8a730cd 122