1214152Sed/* ===-- ffsdi2.c - Implement __ffsdi2 -------------------------------------===
2214152Sed *
3214152Sed *                     The LLVM Compiler Infrastructure
4214152Sed *
5222656Sed * This file is dual licensed under the MIT and the University of Illinois Open
6222656Sed * Source Licenses. See LICENSE.TXT for details.
7214152Sed *
8214152Sed * ===----------------------------------------------------------------------===
9214152Sed *
10214152Sed * This file implements __ffsdi2 for the compiler_rt library.
11214152Sed *
12214152Sed * ===----------------------------------------------------------------------===
13214152Sed */
14214152Sed
15214152Sed#include "int_lib.h"
16214152Sed
17214152Sed/* Returns: the index of the least significant 1-bit in a, or
18214152Sed * the value zero if a is zero. The least significant bit is index one.
19214152Sed */
20214152Sed
21222656SedCOMPILER_RT_ABI si_int
22214152Sed__ffsdi2(di_int a)
23214152Sed{
24214152Sed    dwords x;
25214152Sed    x.all = a;
26214152Sed    if (x.s.low == 0)
27214152Sed    {
28214152Sed        if (x.s.high == 0)
29214152Sed            return 0;
30214152Sed        return __builtin_ctz(x.s.high) + (1 + sizeof(si_int) * CHAR_BIT);
31214152Sed    }
32214152Sed    return __builtin_ctz(x.s.low) + 1;
33214152Sed}
34