1276789Sdim/* ===-- clzdi2.c - Implement __clzdi2 -------------------------------------===
2276789Sdim *
3276789Sdim *               The LLVM Compiler Infrastructure
4276789Sdim *
5276789Sdim * This file is dual licensed under the MIT and the University of Illinois Open
6276789Sdim * Source Licenses. See LICENSE.TXT for details.
7276789Sdim *
8276789Sdim * ===----------------------------------------------------------------------===
9276789Sdim *
10276789Sdim * This file implements __clzdi2 for the compiler_rt library.
11276789Sdim *
12276789Sdim * ===----------------------------------------------------------------------===
13276789Sdim */
14276789Sdim
15276789Sdim#include "int_lib.h"
16276789Sdim
17276789Sdim/* Returns: the number of leading 0-bits */
18276789Sdim
19276789Sdim/* Precondition: a != 0 */
20276789Sdim
21276789SdimCOMPILER_RT_ABI si_int
22276789Sdim__clzdi2(di_int a)
23276789Sdim{
24276789Sdim    dwords x;
25276789Sdim    x.all = a;
26276789Sdim    const si_int f = -(x.s.high == 0);
27276789Sdim    return __builtin_clz((x.s.high & ~f) | (x.s.low & f)) +
28276789Sdim           (f & ((si_int)(sizeof(si_int) * CHAR_BIT)));
29276789Sdim}
30