10ba22f51SPetr Hosek //===-- clzdi2.c - Implement __clzdi2 -------------------------------------===// 20ba22f51SPetr Hosek // 30ba22f51SPetr Hosek // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40ba22f51SPetr Hosek // See https://llvm.org/LICENSE.txt for license information. 50ba22f51SPetr Hosek // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60ba22f51SPetr Hosek // 70ba22f51SPetr Hosek //===----------------------------------------------------------------------===// 80ba22f51SPetr Hosek // 90ba22f51SPetr Hosek // This file implements __clzdi2 for the compiler_rt library. 100ba22f51SPetr Hosek // 110ba22f51SPetr Hosek //===----------------------------------------------------------------------===// 12a6b264b5SAlexey Samsonov 13a6b264b5SAlexey Samsonov #include "int_lib.h" 14a6b264b5SAlexey Samsonov 150ba22f51SPetr Hosek // Returns: the number of leading 0-bits 16a6b264b5SAlexey Samsonov 1722db6965SKristina Brooks #if !defined(__clang__) && \ 18082b89b2SPetr Hosek ((defined(__sparc__) && defined(__arch64__)) || defined(__mips64) || \ 1922db6965SKristina Brooks (defined(__riscv) && __SIZEOF_POINTER__ >= 8)) 200ba22f51SPetr Hosek // On 64-bit architectures with neither a native clz instruction nor a native 210ba22f51SPetr Hosek // ctz instruction, gcc resolves __builtin_clz to __clzdi2 rather than 220ba22f51SPetr Hosek // __clzsi2, leading to infinite recursion. 231db3ca9aSJonas Devlieghere #define __builtin_clz(a) __clzsi2(a) 24*0ee439b7SAnatoly Trosinenko extern int __clzsi2(si_int); 251db3ca9aSJonas Devlieghere #endif 261db3ca9aSJonas Devlieghere 270ba22f51SPetr Hosek // Precondition: a != 0 28a6b264b5SAlexey Samsonov __clzdi2(di_int a)29*0ee439b7SAnatoly TrosinenkoCOMPILER_RT_ABI int __clzdi2(di_int a) { 30a6b264b5SAlexey Samsonov dwords x; 31a6b264b5SAlexey Samsonov x.all = a; 32a6b264b5SAlexey Samsonov const si_int f = -(x.s.high == 0); 334d41df64SAyke van Laethem return clzsi((x.s.high & ~f) | (x.s.low & f)) + 34a6b264b5SAlexey Samsonov (f & ((si_int)(sizeof(si_int) * CHAR_BIT))); 35a6b264b5SAlexey Samsonov } 36