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