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