87 lines · plain
1//===-- clzdi2.c - Implement __clzdi2 -------------------------------------===//2//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-exception6//7//===----------------------------------------------------------------------===//8//9// This file implements count leading zeros for 64bit arguments.10//11//===----------------------------------------------------------------------===//12 13#include "../assembly.h"14 15 .syntax unified16 .text17 DEFINE_CODE_STATE18 19 .p2align 220DEFINE_COMPILERRT_FUNCTION(__clzdi2)21#ifdef __ARM_FEATURE_CLZ22#ifdef __ARMEB__23 cmp r0, 024 itee ne25 clzne r0, r026 clzeq r0, r127 addeq r0, r0, 3228#else29 cmp r1, 030 itee ne31 clzne r0, r132 clzeq r0, r033 addeq r0, r0, 3234#endif35 JMP(lr)36#else37 // Assumption: n != 038 39 // r0: n40 // r1: upper half of n, overwritten after check41 // r1: count of leading zeros in n + 142 // r2: scratch register for shifted r043#ifdef __ARMEB__44 cmp r0, 045 moveq r0, r146#else47 cmp r1, 048 movne r0, r149#endif50 movne r1, 151 moveq r1, 3352 53 // Basic block:54 // if ((r0 >> SHIFT) == 0)55 // r1 += SHIFT;56 // else57 // r0 >>= SHIFT;58 // for descending powers of two as SHIFT.59#define BLOCK(shift) \60 lsrs r2, r0, shift; \61 movne r0, r2; \62 addeq r1, shift \63 64 BLOCK(16)65 BLOCK(8)66 BLOCK(4)67 BLOCK(2)68 69 // The basic block invariants at this point are (r0 >> 2) == 0 and70 // r0 != 0. This means 1 <= r0 <= 3 and 0 <= (r0 >> 1) <= 1.71 //72 // r0 | (r0 >> 1) == 0 | (r0 >> 1) == 1 | -(r0 >> 1) | 1 - (r0 >> 1)73 // ---+----------------+----------------+------------+--------------74 // 1 | 1 | 0 | 0 | 175 // 2 | 0 | 1 | -1 | 076 // 3 | 0 | 1 | -1 | 077 //78 // The r1's initial value of 1 compensates for the 1 here.79 sub r0, r1, r0, lsr #180 81 JMP(lr)82#endif // __ARM_FEATURE_CLZ83END_COMPILERRT_FUNCTION(__clzdi2)84 85NO_EXEC_STACK_DIRECTIVE86 87