]>
git.proxmox.com Git - mirror_edk2.git/blob - ArmPkg/Library/CompilerIntrinsicsLib/Arm/udivmoddi4.c
2 Compiler intrinsic for 64-bit compare, ported from LLVM code.
4 Copyright (c) 2008-2009, Apple Inc. All rights reserved.<BR>
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
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.
16 University of Illinois/NCSA
19 Copyright (c) 2003-2008 University of Illinois at Urbana-Champaign.
26 University of Illinois at Urbana-Champaign
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:
37 * Redistributions of source code must retain the above copyright notice,
38 this list of conditions and the following disclaimers.
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.
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.
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
59 #include "Llvm_int_lib.h"
61 // Effects: if rem != 0, *rem = a % b
64 // Translated from Figure 3-40 of The PowerPC Compiler Writer's Guide
67 __udivmoddi4 (UINT64 a
, UINT64 b
, UINT64
* rem
)
69 const unsigned n_uword_bits
= sizeof(UINT32
) * CHAR_BIT
;
70 const unsigned n_udword_bits
= sizeof(UINT64
) * CHAR_BIT
;
84 // special cases, X is unknown, K != 0
112 *rem
= n
.high
% d
.low
;
113 return n
.high
/ d
.low
;
123 r
.high
= n
.high
% d
.high
;
127 return n
.high
/ d
.high
;
132 if ((d
.high
& (d
.high
- 1)) == 0) // if d is a power of 2
137 r
.high
= n
.high
& (d
.high
- 1);
140 return n
.high
>> COUNT_TRAILING_ZEROS(d
.high
);
145 sr
= COUNT_LEADING_ZEROS(d
.high
) - COUNT_LEADING_ZEROS(n
.high
);
146 // 0 <= sr <= n_uword_bits - 2 or sr large
147 if (sr
> n_uword_bits
- 2)
154 // 1 <= sr <= n_uword_bits - 1
155 // q.all = n.all << (n_udword_bits - sr);
157 q
.high
= n
.low
<< (n_uword_bits
- sr
);
158 // r.all = n.all >> sr;
159 r
.high
= n
.high
>> sr
;
160 r
.low
= (n
.high
<< (n_uword_bits
- sr
)) | (n
.low
>> sr
);
169 if ((d
.low
& (d
.low
- 1)) == 0) // if d is a power of 2
172 *rem
= n
.low
& (d
.low
- 1);
175 unsigned sr
= COUNT_TRAILING_ZEROS(d
.low
);
176 q
.high
= n
.high
>> sr
;
177 q
.low
= (n
.high
<< (n_uword_bits
- sr
)) | (n
.low
>> sr
);
183 sr
= 1 + n_uword_bits
+ COUNT_LEADING_ZEROS(d
.low
) - COUNT_LEADING_ZEROS(n
.high
);
184 // 2 <= sr <= n_udword_bits - 1
185 // q.all = n.all << (n_udword_bits - sr);
186 // r.all = n.all >> sr;
187 // if (sr == n_uword_bits)
194 // else if (sr < n_uword_bits) // 2 <= sr <= n_uword_bits - 1
197 // q.high = n.low << (n_uword_bits - sr);
198 // r.high = n.high >> sr;
199 // r.low = (n.high << (n_uword_bits - sr)) | (n.low >> sr);
201 // else // n_uword_bits + 1 <= sr <= n_udword_bits - 1
203 // q.low = n.low << (n_udword_bits - sr);
204 // q.high = (n.high << (n_udword_bits - sr)) |
205 // (n.low >> (sr - n_uword_bits));
207 // r.low = n.high >> (sr - n_uword_bits);
209 q
.low
= (n
.low
<< (n_udword_bits
- sr
)) &
210 ((INT32
)(n_uword_bits
- sr
) >> (n_uword_bits
-1));
211 q
.high
= ((n
.low
<< ( n_uword_bits
- sr
)) &
212 ((INT32
)(sr
- n_uword_bits
- 1) >> (n_uword_bits
-1))) |
213 (((n
.high
<< (n_udword_bits
- sr
)) |
214 (n
.low
>> (sr
- n_uword_bits
))) &
215 ((INT32
)(n_uword_bits
- sr
) >> (n_uword_bits
-1)));
216 r
.high
= (n
.high
>> sr
) &
217 ((INT32
)(sr
- n_uword_bits
) >> (n_uword_bits
-1));
218 r
.low
= ((n
.high
>> (sr
- n_uword_bits
)) &
219 ((INT32
)(n_uword_bits
- sr
- 1) >> (n_uword_bits
-1))) |
220 (((n
.high
<< (n_uword_bits
- sr
)) |
222 ((INT32
)(sr
- n_uword_bits
) >> (n_uword_bits
-1)));
229 sr
= COUNT_LEADING_ZEROS(d
.high
) - COUNT_LEADING_ZEROS(n
.high
);
230 // 0 <= sr <= n_uword_bits - 1 or sr large
231 if (sr
> n_uword_bits
- 1)
238 // 1 <= sr <= n_uword_bits
239 // q.all = n.all << (n_udword_bits - sr);
241 q
.high
= n
.low
<< (n_uword_bits
- sr
);
242 // r.all = n.all >> sr;
243 // if (sr < n_uword_bits)
245 // r.high = n.high >> sr;
246 // r.low = (n.high << (n_uword_bits - sr)) | (n.low >> sr);
253 r
.high
= (n
.high
>> sr
) &
254 ((INT32
)(sr
- n_uword_bits
) >> (n_uword_bits
-1));
255 r
.low
= (n
.high
<< (n_uword_bits
- sr
)) |
257 ((INT32
)(sr
- n_uword_bits
) >> (n_uword_bits
-1)));
260 // Not a special case
261 // q and r are initialized with:
262 // q.all = n.all << (n_udword_bits - sr);
263 // r.all = n.all >> sr;
264 // 1 <= sr <= n_udword_bits - 1
268 // r:q = ((r:q) << 1) | carry
269 r
.high
= (r
.high
<< 1) | (r
.low
>> (n_uword_bits
- 1));
270 r
.low
= (r
.low
<< 1) | (q
.high
>> (n_uword_bits
- 1));
271 q
.high
= (q
.high
<< 1) | (q
.low
>> (n_uword_bits
- 1));
272 q
.low
= (q
.low
<< 1) | carry
;
274 // if (r.all >= d.all)
279 const INT64 s
= (INT64
)(d
.all
- r
.all
- 1) >> (n_udword_bits
- 1);
283 q
.all
= (q
.all
<< 1) | carry
;