2009-08-05 12:02:56 +08:00
|
|
|
/* ===-- clzdi2.c - Implement __clzdi2 -------------------------------------===
|
|
|
|
*
|
2011-04-20 01:52:09 +08:00
|
|
|
* The LLVM Compiler Infrastructure
|
2009-08-05 12:02:56 +08:00
|
|
|
*
|
2010-11-17 06:13:33 +08:00
|
|
|
* This file is dual licensed under the MIT and the University of Illinois Open
|
|
|
|
* Source Licenses. See LICENSE.TXT for details.
|
2009-08-05 12:02:56 +08:00
|
|
|
*
|
|
|
|
* ===----------------------------------------------------------------------===
|
|
|
|
*
|
|
|
|
* This file implements __clzdi2 for the compiler_rt library.
|
|
|
|
*
|
|
|
|
* ===----------------------------------------------------------------------===
|
|
|
|
*/
|
2009-06-27 00:47:03 +08:00
|
|
|
|
|
|
|
#include "int_lib.h"
|
|
|
|
|
2009-08-05 12:02:56 +08:00
|
|
|
/* Returns: the number of leading 0-bits */
|
2009-06-27 00:47:03 +08:00
|
|
|
|
2018-09-19 02:56:52 +08:00
|
|
|
#if !defined(__clang__) && \
|
|
|
|
((defined(__sparc__) && defined(__arch64__)) || \
|
|
|
|
defined(__mips64) || \
|
|
|
|
(defined(__riscv) && __SIZEOF_POINTER__ >= 8))
|
|
|
|
/* On 64-bit architectures with neither a native clz instruction nor a native
|
|
|
|
* ctz instruction, gcc resolves __builtin_clz to __clzdi2 rather than
|
|
|
|
* __clzsi2, leading to infinite recursion. */
|
2018-02-08 19:14:11 +08:00
|
|
|
#define __builtin_clz(a) __clzsi2(a)
|
|
|
|
extern si_int __clzsi2(si_int);
|
|
|
|
#endif
|
|
|
|
|
2009-08-05 12:02:56 +08:00
|
|
|
/* Precondition: a != 0 */
|
2009-06-27 00:47:03 +08:00
|
|
|
|
2011-04-20 01:52:09 +08:00
|
|
|
COMPILER_RT_ABI si_int
|
2009-06-27 00:47:03 +08:00
|
|
|
__clzdi2(di_int a)
|
|
|
|
{
|
|
|
|
dwords x;
|
|
|
|
x.all = a;
|
2009-08-10 02:41:02 +08:00
|
|
|
const si_int f = -(x.s.high == 0);
|
|
|
|
return __builtin_clz((x.s.high & ~f) | (x.s.low & f)) +
|
2009-06-27 00:47:03 +08:00
|
|
|
(f & ((si_int)(sizeof(si_int) * CHAR_BIT)));
|
|
|
|
}
|