1 /* Copyright (C) 2008, 2009 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);
32 inline static unsigned int
33 count_leading_zeros (UTItype x)
35 qword c = si_clz (*(qword *) & x);
36 qword cmp0 = si_cgti (c, 31);
37 qword cmp1 = si_and (cmp0, si_shlqbyi (cmp0, 4));
38 qword cmp2 = si_and (cmp1, si_shlqbyi (cmp0, 8));
39 qword s = si_a (c, si_and (cmp0, si_shlqbyi (c, 4)));
40 s = si_a (s, si_and (cmp1, si_shlqbyi (c, 8)));
41 s = si_a (s, si_and (cmp2, si_shlqbyi (c, 12)));
42 return si_to_uint (s);
45 /* Based on implementation of udivmodsi4, which is essentially
46 * an optimized version of gcc/config/udivmodsi4.c
66 __udivmodti4 (UTItype num, UTItype den, UTItype * rp)
69 si_from_uint (count_leading_zeros (den) - count_leading_zeros (num));
70 qword n0 = *(qword *) & num;
71 qword d0 = *(qword *) & den;
72 qword bit = si_andi (si_fsmbi (1), 1);
74 qword m1 = si_fsmbi (0x000f);
77 d0 = si_shlqbybi (si_shlqbi (d0, shift), shift);
78 bit = si_shlqbybi (si_shlqbi (bit, shift), shift);
84 // n1 = n0 - d0 in TImode
86 n1 = si_shlqbyi (n1, 4);
88 n1 = si_bgx (d0, n0, n1);
89 n1 = si_shlqbyi (n1, 4);
91 n1 = si_bgx (d0, n0, n1);
92 n1 = si_shlqbyi (n1, 4);
94 n1 = si_sfx (d0, n0, n1);
96 mask = si_fsm (si_cgti (n1, -1));
97 r0 = si_selb (r0, r1, mask);
98 n0 = si_selb (n0, n1, mask);
99 bit = si_rotqmbii (bit, -1);
100 d0 = si_rotqmbii (d0, -1);
102 while (si_to_uint (si_orx (bit)));
104 *rp = *(UTItype *) & n0;
105 return *(UTItype *) & r0;
109 __udivti3 (UTItype n, UTItype d)
111 return __udivmodti4 (n, d, (UTItype *)0);
115 __umodti3 (UTItype n, UTItype d)
118 __udivmodti4 (n, d, &w);
123 __divti3 (TItype n, TItype d)
139 w = __udivmodti4 (n, d, (UTItype *)0);
146 __modti3 (TItype n, TItype d)
162 __udivmodti4 (n, d, (UTItype *) &w);