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