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