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