1156cd587Sjoerg /* ===-- clzdi2.c - Implement __clzdi2 -------------------------------------=== 2156cd587Sjoerg * 3156cd587Sjoerg * The LLVM Compiler Infrastructure 4156cd587Sjoerg * 5156cd587Sjoerg * This file is dual licensed under the MIT and the University of Illinois Open 6156cd587Sjoerg * Source Licenses. See LICENSE.TXT for details. 7156cd587Sjoerg * 8156cd587Sjoerg * ===----------------------------------------------------------------------=== 9156cd587Sjoerg * 10156cd587Sjoerg * This file implements __clzdi2 for the compiler_rt library. 11156cd587Sjoerg * 12156cd587Sjoerg * ===----------------------------------------------------------------------=== 13156cd587Sjoerg */ 14156cd587Sjoerg 15156cd587Sjoerg #include "int_lib.h" 16156cd587Sjoerg 17156cd587Sjoerg /* Returns: the number of leading 0-bits */ 18156cd587Sjoerg 19*75b842b8Sskrll #if !defined(__clang__) && (defined(__sparc64__) || defined(__mips64) || defined(__riscv__)) 209710b25dSchristos /* gcc resolves __builtin_clz -> __clzdi2 leading to infinite recursion */ 219710b25dSchristos #define __builtin_clz(a) __clzsi2(a) 229710b25dSchristos extern si_int __clzsi2(si_int); 239710b25dSchristos #endif 249710b25dSchristos 25156cd587Sjoerg /* Precondition: a != 0 */ 26156cd587Sjoerg 27156cd587Sjoerg COMPILER_RT_ABI si_int __clzdi2(di_int a)28156cd587Sjoerg__clzdi2(di_int a) 29156cd587Sjoerg { 30156cd587Sjoerg dwords x; 31156cd587Sjoerg x.all = a; 32156cd587Sjoerg const si_int f = -(x.s.high == 0); 33156cd587Sjoerg return __builtin_clz((x.s.high & ~f) | (x.s.low & f)) + 34156cd587Sjoerg (f & ((si_int)(sizeof(si_int) * CHAR_BIT))); 35156cd587Sjoerg } 36