]>
Commit | Line | Data |
---|---|---|
1e57a462 | 1 | /** @file\r |
2 | Compiler intrinsic for 32-bit unsigned div, ported from LLVM code.\r | |
3 | \r | |
4 | Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r | |
3402aac7 | 5 | \r |
1e57a462 | 6 | This program and the accompanying materials\r |
7 | are licensed and made available under the terms and conditions of the BSD License\r | |
8 | which accompanies this distribution. The full text of the license may be found at\r | |
9 | http://opensource.org/licenses/bsd-license.php\r | |
10 | \r | |
11 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
12 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
13 | \r | |
14 | **/\r | |
15 | /**\r | |
16 | University of Illinois/NCSA\r | |
17 | Open Source License\r | |
3402aac7 | 18 | \r |
1e57a462 | 19 | Copyright (c) 2003-2008 University of Illinois at Urbana-Champaign.\r |
20 | All rights reserved.\r | |
3402aac7 | 21 | \r |
1e57a462 | 22 | Developed by:\r |
3402aac7 | 23 | \r |
1e57a462 | 24 | LLVM Team\r |
3402aac7 | 25 | \r |
1e57a462 | 26 | University of Illinois at Urbana-Champaign\r |
3402aac7 | 27 | \r |
1e57a462 | 28 | http://llvm.org\r |
3402aac7 | 29 | \r |
1e57a462 | 30 | Permission is hereby granted, free of charge, to any person obtaining a copy of\r |
31 | this software and associated documentation files (the "Software"), to deal with\r | |
32 | the Software without restriction, including without limitation the rights to\r | |
33 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies\r | |
34 | of the Software, and to permit persons to whom the Software is furnished to do\r | |
35 | so, subject to the following conditions:\r | |
3402aac7 | 36 | \r |
1e57a462 | 37 | * Redistributions of source code must retain the above copyright notice,\r |
38 | this list of conditions and the following disclaimers.\r | |
3402aac7 | 39 | \r |
1e57a462 | 40 | * Redistributions in binary form must reproduce the above copyright notice,\r |
41 | this list of conditions and the following disclaimers in the\r | |
42 | documentation and/or other materials provided with the distribution.\r | |
3402aac7 | 43 | \r |
1e57a462 | 44 | * Neither the names of the LLVM Team, University of Illinois at\r |
45 | Urbana-Champaign, nor the names of its contributors may be used to\r | |
46 | endorse or promote products derived from this Software without specific\r | |
47 | prior written permission.\r | |
3402aac7 | 48 | \r |
1e57a462 | 49 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r |
50 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r | |
51 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r | |
52 | CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r | |
53 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r | |
54 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE\r | |
55 | SOFTWARE.\r | |
56 | **/\r | |
57 | \r | |
58 | \r | |
59 | #include "Llvm_int_lib.h"\r | |
60 | \r | |
61 | \r | |
62 | // Returns: n / d\r | |
63 | \r | |
64 | // Translated from Figure 3-40 of The PowerPC Compiler Writer's Guide\r | |
65 | \r | |
66 | UINT32\r | |
67 | __udivsi3(UINT32 n, UINT32 d)\r | |
68 | {\r | |
69 | const unsigned n_uword_bits = sizeof(UINT32) * CHAR_BIT;\r | |
70 | UINT32 q;\r | |
71 | UINT32 r;\r | |
72 | unsigned sr;\r | |
73 | \r | |
74 | // special cases\r | |
75 | if (d == 0) {\r | |
76 | // ASSERT (FALSE);\r | |
77 | return 0; // ?!\r | |
78 | }\r | |
79 | if (n == 0)\r | |
80 | return 0;\r | |
81 | \r | |
82 | sr = COUNT_LEADING_ZEROS(d) - COUNT_LEADING_ZEROS(n);\r | |
83 | // 0 <= sr <= n_uword_bits - 1 or sr large\r | |
84 | if (sr > n_uword_bits - 1) // d > r\r | |
85 | return 0;\r | |
86 | if (sr == n_uword_bits - 1) // d == 1\r | |
87 | return n;\r | |
88 | ++sr;\r | |
89 | // 1 <= sr <= n_uword_bits - 1\r | |
90 | // Not a special case\r | |
91 | q = n << (n_uword_bits - sr);\r | |
92 | r = n >> sr;\r | |
93 | UINT32 carry = 0;\r | |
94 | for (; sr > 0; --sr)\r | |
95 | {\r | |
96 | // r:q = ((r:q) << 1) | carry\r | |
97 | r = (r << 1) | (q >> (n_uword_bits - 1));\r | |
98 | q = (q << 1) | carry;\r | |
99 | // carry = 0;\r | |
100 | // if (r.all >= d.all)\r | |
101 | // {\r | |
102 | // r.all -= d.all;\r | |
103 | // carry = 1;\r | |
104 | // }\r | |
105 | const INT32 s = (INT32)(d - r - 1) >> (n_uword_bits - 1);\r | |
106 | carry = s & 1;\r | |
107 | r -= d & s;\r | |
108 | }\r | |
109 | q = (q << 1) | carry;\r | |
110 | return q;\r | |
111 | }\r |