1 /* Copyright (C) 2008-2015 Free Software Foundation, Inc.
3 This file is free software; you can redistribute it and/or modify it under
4 the terms of the GNU General Public License as published by the Free
5 Software Foundation; either version 3 of the License, or (at your option)
8 This file is distributed in the hope that it will be useful, but WITHOUT
9 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 Under Section 7 of GPL version 3, you are granted additional
14 permissions described in the GCC Runtime Library Exception, version
15 3.1, as published by the Free Software Foundation.
17 You should have received a copy of the GNU General Public License and
18 a copy of the GCC Runtime Library Exception along with this program;
19 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
20 <http://www.gnu.org/licenses/>. */
22 #include <spu_intrinsics.h>
24 typedef unsigned int UTItype
__attribute__ ((mode (TI
)));
25 typedef int TItype
__attribute__ ((mode (TI
)));
26 TItype
__divti3 (TItype u
, TItype v
);
27 TItype
__modti3 (TItype u
, TItype v
);
28 UTItype
__udivti3 (UTItype u
, UTItype v
);
29 UTItype
__umodti3 (UTItype u
, UTItype v
);
30 UTItype
__udivmodti4 (UTItype u
, UTItype v
, UTItype
*w
);
39 si_from_UTItype (UTItype t
)
41 union qword_UTItype u
;
47 si_to_UTItype (qword q
)
49 union qword_UTItype u
;
54 inline static unsigned int
55 count_leading_zeros (UTItype x
)
57 qword c
= si_clz (*(qword
*) & x
);
58 qword cmp0
= si_cgti (c
, 31);
59 qword cmp1
= si_and (cmp0
, si_shlqbyi (cmp0
, 4));
60 qword cmp2
= si_and (cmp1
, si_shlqbyi (cmp0
, 8));
61 qword s
= si_a (c
, si_and (cmp0
, si_shlqbyi (c
, 4)));
62 s
= si_a (s
, si_and (cmp1
, si_shlqbyi (c
, 8)));
63 s
= si_a (s
, si_and (cmp2
, si_shlqbyi (c
, 12)));
64 return si_to_uint (s
);
67 /* Based on implementation of udivmodsi4, which is essentially
68 * an optimized version of libgcc/udivmodsi4.c
88 __udivmodti4 (UTItype num
, UTItype den
, UTItype
* rp
)
91 si_from_uint (count_leading_zeros (den
) - count_leading_zeros (num
));
92 qword n0
= si_from_UTItype (num
);
93 qword d0
= si_from_UTItype (den
);
94 qword bit
= si_andi (si_fsmbi (1), 1);
96 qword m1
= si_fsmbi (0x000f);
99 d0
= si_shlqbybi (si_shlqbi (d0
, shift
), shift
);
100 bit
= si_shlqbybi (si_shlqbi (bit
, shift
), shift
);
104 r1
= si_or (r0
, bit
);
106 // n1 = n0 - d0 in TImode
108 n1
= si_shlqbyi (n1
, 4);
110 n1
= si_bgx (d0
, n0
, n1
);
111 n1
= si_shlqbyi (n1
, 4);
113 n1
= si_bgx (d0
, n0
, n1
);
114 n1
= si_shlqbyi (n1
, 4);
116 n1
= si_sfx (d0
, n0
, n1
);
118 mask
= si_fsm (si_cgti (n1
, -1));
119 r0
= si_selb (r0
, r1
, mask
);
120 n0
= si_selb (n0
, n1
, mask
);
121 bit
= si_rotqmbii (bit
, -1);
122 d0
= si_rotqmbii (d0
, -1);
124 while (si_to_uint (si_orx (bit
)));
126 *rp
= si_to_UTItype (n0
);
127 return si_to_UTItype (r0
);
131 __udivti3 (UTItype n
, UTItype d
)
133 return __udivmodti4 (n
, d
, (UTItype
*)0);
137 __umodti3 (UTItype n
, UTItype d
)
140 __udivmodti4 (n
, d
, &w
);
145 __divti3 (TItype n
, TItype d
)
161 w
= __udivmodti4 (n
, d
, (UTItype
*)0);
168 __modti3 (TItype n
, TItype d
)
184 __udivmodti4 (n
, d
, (UTItype
*) &w
);