blob: b3d37d01b0c62d277aa874b06abce9814a60f973 [file] [log] [blame]
Edward O'Callaghan55836322009-08-07 20:30:09 +00001/* ===-- ctzdi2.c - Implement __ctzdi2 -------------------------------------===
2 *
3 * The LLVM Compiler Infrastructure
4 *
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'Callaghan55836322009-08-07 20:30:09 +00007 *
8 * ===----------------------------------------------------------------------===
9 *
10 * This file implements __ctzdi2 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'Callaghan55836322009-08-07 20:30:09 +000018/* Returns: the number of trailing 0-bits */
Daniel Dunbarfd089992009-06-26 16:47:03 +000019
Edward O'Callaghan55836322009-08-07 20:30:09 +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__ctzdi2(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.low == 0);
28 return __builtin_ctz((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}