1 //===-- clzsi2.c - Implement __clzsi2 -------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements count leading zeros for 32bit arguments.
11 //===----------------------------------------------------------------------===//
13 #include "../assembly.h"
20 DEFINE_COMPILERRT_FUNCTION(__clzsi2)
21 #ifdef __ARM_FEATURE_CLZ
28 // r1: count of leading zeros in n + 1
29 // r2: scratch register for shifted r0
33 // if ((r0 >> SHIFT) == 0)
37 // for descending powers of two as SHIFT.
39 #define BLOCK(shift) \
49 // The basic block invariants at this point are (r0 >> 2) == 0 and
50 // r0 != 0. This means 1 <= r0 <= 3 and 0 <= (r0 >> 1) <= 1.
52 // r0 | (r0 >> 1) == 0 | (r0 >> 1) == 1 | -(r0 >> 1) | 1 - (r0 >> 1)
53 // ---+----------------+----------------+------------+--------------
58 // The r1's initial value of 1 compensates for the 1 here.
59 sub r0, r1, r0, lsr #1
62 #endif // __ARM_FEATURE_CLZ
63 END_COMPILERRT_FUNCTION(__clzsi2)
65 NO_EXEC_STACK_DIRECTIVE