1 /* Copyright (C) 2008 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 2 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 You should have received a copy of the GNU General Public License
14 along with this file; see the file COPYING. If not, write to the Free
15 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
18 /* As a special exception, if you link this library with files compiled with
19 GCC to produce an executable, this does not cause the resulting executable
20 to be covered by the GNU General Public License. The exception does not
21 however invalidate any other reasons why the executable file might be covered
22 by the GNU General Public License. */
24 #include <spu_intrinsics.h>
26 typedef unsigned int UTItype __attribute__ ((mode (TI)));
27 typedef int TItype __attribute__ ((mode (TI)));
28 TItype __divti3 (TItype u, TItype v);
29 TItype __modti3 (TItype u, TItype v);
30 UTItype __udivti3 (UTItype u, UTItype v);
31 UTItype __umodti3 (UTItype u, UTItype v);
32 UTItype __udivmodti4 (UTItype u, UTItype v, UTItype *w);
34 inline static unsigned int
35 count_leading_zeros (UTItype x)
37 qword c = si_clz (*(qword *) & x);
38 qword cmp0 = si_cgti (c, 31);
39 qword cmp1 = si_and (cmp0, si_shlqbyi (cmp0, 4));
40 qword cmp2 = si_and (cmp1, si_shlqbyi (cmp0, 8));
41 qword s = si_a (c, si_and (cmp0, si_shlqbyi (c, 4)));
42 s = si_a (s, si_and (cmp1, si_shlqbyi (c, 8)));
43 s = si_a (s, si_and (cmp2, si_shlqbyi (c, 12)));
44 return si_to_uint (s);
47 /* Based on implementation of udivmodsi4, which is essentially
48 * an optimized version of gcc/config/udivmodsi4.c
68 __udivmodti4 (UTItype num, UTItype den, UTItype * rp)
71 si_from_uint (count_leading_zeros (den) - count_leading_zeros (num));
72 qword n0 = *(qword *) & num;
73 qword d0 = *(qword *) & den;
74 qword bit = si_andi (si_fsmbi (1), 1);
76 qword m1 = si_fsmbi (0x000f);
79 d0 = si_shlqbybi (si_shlqbi (d0, shift), shift);
80 bit = si_shlqbybi (si_shlqbi (bit, shift), shift);
86 // n1 = n0 - d0 in TImode
88 n1 = si_shlqbyi (n1, 4);
90 n1 = si_bgx (d0, n0, n1);
91 n1 = si_shlqbyi (n1, 4);
93 n1 = si_bgx (d0, n0, n1);
94 n1 = si_shlqbyi (n1, 4);
96 n1 = si_sfx (d0, n0, n1);
98 mask = si_fsm (si_cgti (n1, -1));
99 r0 = si_selb (r0, r1, mask);
100 n0 = si_selb (n0, n1, mask);
101 bit = si_rotqmbii (bit, -1);
102 d0 = si_rotqmbii (d0, -1);
104 while (si_to_uint (si_orx (bit)));
106 *rp = *(UTItype *) & n0;
107 return *(UTItype *) & r0;
111 __udivti3 (UTItype n, UTItype d)
113 return __udivmodti4 (n, d, (UTItype *)0);
117 __umodti3 (UTItype n, UTItype d)
120 __udivmodti4 (n, d, &w);
125 __divti3 (TItype n, TItype d)
141 w = __udivmodti4 (n, d, (UTItype *)0);
148 __modti3 (TItype n, TItype d)
164 __udivmodti4 (n, d, (UTItype *) &w);