1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
/* Copyright (C) 2008-2018 Free Software Foundation, Inc.
This file is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your option)
any later version.
This file is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
<http://www.gnu.org/licenses/>. */
#include <spu_intrinsics.h>
typedef unsigned int UTItype __attribute__ ((mode (TI)));
typedef int TItype __attribute__ ((mode (TI)));
TItype __divti3 (TItype u, TItype v);
TItype __modti3 (TItype u, TItype v);
UTItype __udivti3 (UTItype u, UTItype v);
UTItype __umodti3 (UTItype u, UTItype v);
UTItype __udivmodti4 (UTItype u, UTItype v, UTItype *w);
union qword_UTItype
{
qword q;
UTItype t;
};
inline static qword
si_from_UTItype (UTItype t)
{
union qword_UTItype u;
u.t = t;
return u.q;
}
inline static UTItype
si_to_UTItype (qword q)
{
union qword_UTItype u;
u.q = q;
return u.t;
}
inline static unsigned int
count_leading_zeros (UTItype x)
{
qword c = si_clz (*(qword *) & x);
qword cmp0 = si_cgti (c, 31);
qword cmp1 = si_and (cmp0, si_shlqbyi (cmp0, 4));
qword cmp2 = si_and (cmp1, si_shlqbyi (cmp0, 8));
qword s = si_a (c, si_and (cmp0, si_shlqbyi (c, 4)));
s = si_a (s, si_and (cmp1, si_shlqbyi (c, 8)));
s = si_a (s, si_and (cmp2, si_shlqbyi (c, 12)));
return si_to_uint (s);
}
/* Based on implementation of udivmodsi4, which is essentially
* an optimized version of libgcc/udivmodsi4.c
clz %7,%2
clz %4,%1
il %5,1
fsmbi %0,0
sf %7,%4,%7
ori %3,%1,0
shl %5,%5,%7
shl %4,%2,%7
1: or %8,%0,%5
rotmi %5,%5,-1
clgt %6,%4,%3
sf %7,%4,%3
rotmi %4,%4,-1
selb %0,%8,%0,%6
selb %3,%7,%3,%6
3: brnz %5,1b
*/
UTItype
__udivmodti4 (UTItype num, UTItype den, UTItype * rp)
{
qword shift =
si_from_uint (count_leading_zeros (den) - count_leading_zeros (num));
qword n0 = si_from_UTItype (num);
qword d0 = si_from_UTItype (den);
qword bit = si_andi (si_fsmbi (1), 1);
qword r0 = si_il (0);
qword m1 = si_fsmbi (0x000f);
qword mask, r1, n1;
d0 = si_shlqbybi (si_shlqbi (d0, shift), shift);
bit = si_shlqbybi (si_shlqbi (bit, shift), shift);
do
{
r1 = si_or (r0, bit);
// n1 = n0 - d0 in TImode
n1 = si_bg (d0, n0);
n1 = si_shlqbyi (n1, 4);
n1 = si_sf (m1, n1);
n1 = si_bgx (d0, n0, n1);
n1 = si_shlqbyi (n1, 4);
n1 = si_sf (m1, n1);
n1 = si_bgx (d0, n0, n1);
n1 = si_shlqbyi (n1, 4);
n1 = si_sf (m1, n1);
n1 = si_sfx (d0, n0, n1);
mask = si_fsm (si_cgti (n1, -1));
r0 = si_selb (r0, r1, mask);
n0 = si_selb (n0, n1, mask);
bit = si_rotqmbii (bit, -1);
d0 = si_rotqmbii (d0, -1);
}
while (si_to_uint (si_orx (bit)));
if (rp)
*rp = si_to_UTItype (n0);
return si_to_UTItype (r0);
}
UTItype
__udivti3 (UTItype n, UTItype d)
{
return __udivmodti4 (n, d, (UTItype *)0);
}
UTItype
__umodti3 (UTItype n, UTItype d)
{
UTItype w;
__udivmodti4 (n, d, &w);
return w;
}
TItype
__divti3 (TItype n, TItype d)
{
int c = 0;
TItype w;
if (n < 0)
{
c = ~c;
n = -n;
}
if (d < 0)
{
c = ~c;
d = -d;
}
w = __udivmodti4 (n, d, (UTItype *)0);
if (c)
w = -w;
return w;
}
TItype
__modti3 (TItype n, TItype d)
{
int c = 0;
TItype w;
if (n < 0)
{
c = ~c;
n = -n;
}
if (d < 0)
{
c = ~c;
d = -d;
}
__udivmodti4 (n, d, (UTItype *) &w);
if (c)
w = -w;
return w;
}
|