blob: 1819e6be436b0b849152f8800d84b88989d6d035 [file] [log] [blame]
Edward O'Callaghan4856eef2009-08-05 04:02:56 +00001/* ===-- clzdi2.c - Implement __clzdi2 -------------------------------------===
2 *
Anton Korobeynikove63da932011-04-19 17:52:09 +00003 * The LLVM Compiler Infrastructure
Edward O'Callaghan4856eef2009-08-05 04:02:56 +00004 *
Howard Hinnant5b791f62010-11-16 22:13:33 +00005 * This file is dual licensed under the MIT and the University of Illinois Open
6 * Source Licenses. See LICENSE.TXT for details.
Edward O'Callaghan4856eef2009-08-05 04:02:56 +00007 *
8 * ===----------------------------------------------------------------------===
9 *
10 * This file implements __clzdi2 for the compiler_rt library.
11 *
12 * ===----------------------------------------------------------------------===
13 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000014
15#include "int_lib.h"
16
Edward O'Callaghan4856eef2009-08-05 04:02:56 +000017/* Returns: the number of leading 0-bits */
Daniel Dunbarfd089992009-06-26 16:47:03 +000018
Kristina Brooks22db6962018-09-18 18:56:52 +000019#if !defined(__clang__) && \
20 ((defined(__sparc__) && defined(__arch64__)) || \
21 defined(__mips64) || \
22 (defined(__riscv) && __SIZEOF_POINTER__ >= 8))
23/* On 64-bit architectures with neither a native clz instruction nor a native
24 * ctz instruction, gcc resolves __builtin_clz to __clzdi2 rather than
25 * __clzsi2, leading to infinite recursion. */
Jonas Devlieghere1db3ca92018-02-08 11:14:11 +000026#define __builtin_clz(a) __clzsi2(a)
27extern si_int __clzsi2(si_int);
28#endif
29
Edward O'Callaghan4856eef2009-08-05 04:02:56 +000030/* Precondition: a != 0 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000031
Anton Korobeynikove63da932011-04-19 17:52:09 +000032COMPILER_RT_ABI si_int
Daniel Dunbarfd089992009-06-26 16:47:03 +000033__clzdi2(di_int a)
34{
35 dwords x;
36 x.all = a;
Edward O'Callaghanccf48132009-08-09 18:41:02 +000037 const si_int f = -(x.s.high == 0);
38 return __builtin_clz((x.s.high & ~f) | (x.s.low & f)) +
Daniel Dunbarfd089992009-06-26 16:47:03 +000039 (f & ((si_int)(sizeof(si_int) * CHAR_BIT)));
40}