]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/builtins/arm/clzsi2.S
Merge compiler-rt trunk r321017 to contrib/compiler-rt.
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / builtins / arm / clzsi2.S
1 /* ===-- clzsi2.c - Implement __clzsi2 -------------------------------------===
2  *
3  *               The LLVM Compiler Infrastructure
4  *
5  * This file is dual licensed under the MIT and the University of Illinois Open
6  * Source Licenses. See LICENSE.TXT for details.
7  *
8  * ===----------------------------------------------------------------------===
9  *
10  * This file implements count leading zeros for 32bit arguments.
11  *
12  * ===----------------------------------------------------------------------===
13  */
14 #include "../assembly.h"
15
16         .syntax unified
17         .text
18         DEFINE_CODE_STATE
19
20         .p2align        2
21 DEFINE_COMPILERRT_FUNCTION(__clzsi2)
22 #ifdef __ARM_FEATURE_CLZ
23         clz     r0, r0
24         JMP(lr)
25 #else
26         /* Assumption: n != 0 */
27
28         /*
29          * r0: n
30          * r1: count of leading zeros in n + 1
31          * r2: scratch register for shifted r0
32          */
33         mov     r1, 1
34
35         /*
36          * Basic block:
37          * if ((r0 >> SHIFT) == 0)
38          *   r1 += SHIFT;
39          * else
40          *   r0 >>= SHIFT;
41          * for descending powers of two as SHIFT.
42          */
43
44 #define BLOCK(shift) \
45         lsrs    r2, r0, shift; \
46         movne   r0, r2; \
47         addeq   r1, shift \
48
49         BLOCK(16)
50         BLOCK(8)
51         BLOCK(4)
52         BLOCK(2)
53
54         /*
55          * The basic block invariants at this point are (r0 >> 2) == 0 and
56          * r0 != 0. This means 1 <= r0 <= 3 and 0 <= (r0 >> 1) <= 1.
57          *
58          * r0 | (r0 >> 1) == 0 | (r0 >> 1) == 1 | -(r0 >> 1) | 1 - (r0 >> 1)
59          * ---+----------------+----------------+------------+--------------
60          * 1  | 1              | 0              | 0          | 1
61          * 2  | 0              | 1              | -1         | 0
62          * 3  | 0              | 1              | -1         | 0
63          *
64          * The r1's initial value of 1 compensates for the 1 here.
65          */
66         sub     r0, r1, r0, lsr #1
67
68         JMP(lr)
69 #endif // __ARM_FEATURE_CLZ
70 END_COMPILERRT_FUNCTION(__clzsi2)
71
72 NO_EXEC_STACK_DIRECTIVE
73