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