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 #if __ARM_ARCH_ISA_THUMB == 2 19 .thumb 20 #endif 21 22 .p2align 2 23 #if __ARM_ARCH_ISA_THUMB == 2 24 DEFINE_COMPILERRT_THUMB_FUNCTION(__clzsi2) 25 #else 26 DEFINE_COMPILERRT_FUNCTION(__clzsi2) 27 #endif 28 #ifdef __ARM_FEATURE_CLZ 29 clz r0, r0 30 JMP(lr) 31 #else 32 /* Assumption: n != 0 */ 33 34 /* 35 * r0: n 36 * r1: count of leading zeros in n + 1 37 * r2: scratch register for shifted r0 38 */ 39 mov r1, 1 40 41 /* 42 * Basic block: 43 * if ((r0 >> SHIFT) == 0) 44 * r1 += SHIFT; 45 * else 46 * r0 >>= SHIFT; 47 * for descending powers of two as SHIFT. 48 */ 49 50 #define BLOCK(shift) \ 51 lsrs r2, r0, shift; \ 52 movne r0, r2; \ 53 addeq r1, shift \ 54 55 BLOCK(16) 56 BLOCK(8) 57 BLOCK(4) 58 BLOCK(2) 59 60 /* 61 * The basic block invariants at this point are (r0 >> 2) == 0 and 62 * r0 != 0. This means 1 <= r0 <= 3 and 0 <= (r0 >> 1) <= 1. 63 * 64 * r0 | (r0 >> 1) == 0 | (r0 >> 1) == 1 | -(r0 >> 1) | 1 - (r0 >> 1) 65 * ---+----------------+----------------+------------+-------------- 66 * 1 | 1 | 0 | 0 | 1 67 * 2 | 0 | 1 | -1 | 0 68 * 3 | 0 | 1 | -1 | 0 69 * 70 * The r1's initial value of 1 compensates for the 1 here. 71 */ 72 sub r0, r1, r0, lsr #1 73 74 JMP(lr) 75 #endif // __ARM_FEATURE_CLZ 76 END_COMPILERRT_FUNCTION(__clzsi2) 77