blob: c281945ffdff11157394d2eaf959bfbab527009e [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 */
Anton Korobeynikove63da932011-04-19 17:52:09 +000014#include "abi.h"
Daniel Dunbarfd089992009-06-26 16:47:03 +000015
16#include "int_lib.h"
17
Edward O'Callaghan4856eef2009-08-05 04:02:56 +000018/* Returns: the number of leading 0-bits */
Daniel Dunbarfd089992009-06-26 16:47:03 +000019
Edward O'Callaghan4856eef2009-08-05 04:02:56 +000020/* Precondition: a != 0 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000021
Anton Korobeynikove63da932011-04-19 17:52:09 +000022COMPILER_RT_ABI si_int
Daniel Dunbarfd089992009-06-26 16:47:03 +000023__clzdi2(di_int a)
24{
25 dwords x;
26 x.all = a;
Edward O'Callaghanccf48132009-08-09 18:41:02 +000027 const si_int f = -(x.s.high == 0);
28 return __builtin_clz((x.s.high & ~f) | (x.s.low & f)) +
Daniel Dunbarfd089992009-06-26 16:47:03 +000029 (f & ((si_int)(sizeof(si_int) * CHAR_BIT)));
30}