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