]>
Commit | Line | Data |
---|---|---|
2ef2b01e A |
1 | /** @file |
2 | Compiler intrinsic to return the number of leading zeros, ported from LLVM code. | |
3 | ||
d6ebcab7 | 4 | Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR> |
2ef2b01e | 5 | |
d6ebcab7 | 6 | This program and the accompanying materials |
2ef2b01e A |
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 | // Returns: the number of leading 0-bits | |
62 | ||
63 | // Precondition: a != 0 | |
64 | ||
65 | INT32 | |
66 | __clzsi2(INT32 a) | |
67 | { | |
68 | UINT32 x = (UINT32)a; | |
69 | INT32 t = ((x & 0xFFFF0000) == 0) << 4; // if (x is small) t = 16 else 0 | |
70 | x >>= 16 - t; // x = [0 - 0xFFFF] | |
71 | UINT32 r = t; // r = [0, 16] | |
72 | // return r + clz(x) | |
73 | t = ((x & 0xFF00) == 0) << 3; | |
74 | x >>= 8 - t; // x = [0 - 0xFF] | |
75 | r += t; // r = [0, 8, 16, 24] | |
76 | // return r + clz(x) | |
77 | t = ((x & 0xF0) == 0) << 2; | |
78 | x >>= 4 - t; // x = [0 - 0xF] | |
79 | r += t; // r = [0, 4, 8, 12, 16, 20, 24, 28] | |
80 | // return r + clz(x) | |
81 | t = ((x & 0xC) == 0) << 1; | |
82 | x >>= 2 - t; // x = [0 - 3] | |
83 | r += t; // r = [0 - 30] and is even | |
84 | // return r + clz(x) | |
85 | // switch (x) | |
86 | // { | |
87 | // case 0: | |
88 | // return r + 2; | |
89 | // case 1: | |
90 | // return r + 1; | |
91 | // case 2: | |
92 | // case 3: | |
93 | // return r; | |
94 | // } | |
95 | return r + ((2 - x) & -((x & 2) == 0)); | |
96 | } |