blob: b9e64da492bdacfb5e624740549ebc7fc2ca8db0 [file] [log] [blame]
Edward O'Callaghan2bf62722009-08-05 04:02:56 +00001/* ===-- clzdi2.c - Implement __clzdi2 -------------------------------------===
2 *
Anton Korobeynikov1c5f89b2011-04-19 17:52:09 +00003 * The LLVM Compiler Infrastructure
Edward O'Callaghan2bf62722009-08-05 04:02:56 +00004 *
Howard Hinnant9ad441f2010-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'Callaghan2bf62722009-08-05 04:02:56 +00007 *
8 * ===----------------------------------------------------------------------===
9 *
10 * This file implements __clzdi2 for the compiler_rt library.
11 *
12 * ===----------------------------------------------------------------------===
13 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000014
15#include "int_lib.h"
16
Edward O'Callaghan2bf62722009-08-05 04:02:56 +000017/* Returns: the number of leading 0-bits */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000018
Edward O'Callaghan2bf62722009-08-05 04:02:56 +000019/* Precondition: a != 0 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000020
Anton Korobeynikov1c5f89b2011-04-19 17:52:09 +000021COMPILER_RT_ABI si_int
Daniel Dunbarb3a69012009-06-26 16:47:03 +000022__clzdi2(di_int a)
23{
24 dwords x;
25 x.all = a;
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +000026 const si_int f = -(x.s.high == 0);
27 return __builtin_clz((x.s.high & ~f) | (x.s.low & f)) +
Daniel Dunbarb3a69012009-06-26 16:47:03 +000028 (f & ((si_int)(sizeof(si_int) * CHAR_BIT)));
29}