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