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