1353358Sdim//===-- ffsti2.c - Implement __ffsti2 -------------------------------------===//
2353358Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6353358Sdim//
7353358Sdim//===----------------------------------------------------------------------===//
8353358Sdim//
9353358Sdim// This file implements __ffsti2 for the compiler_rt library.
10353358Sdim//
11353358Sdim//===----------------------------------------------------------------------===//
12276789Sdim
13276789Sdim#include "int_lib.h"
14276789Sdim
15276789Sdim#ifdef CRT_HAS_128BIT
16276789Sdim
17353358Sdim// Returns: the index of the least significant 1-bit in a, or
18353358Sdim// the value zero if a is zero. The least significant bit is index one.
19276789Sdim
20353358SdimCOMPILER_RT_ABI si_int __ffsti2(ti_int a) {
21353358Sdim  twords x;
22353358Sdim  x.all = a;
23353358Sdim  if (x.s.low == 0) {
24353358Sdim    if (x.s.high == 0)
25353358Sdim      return 0;
26353358Sdim    return __builtin_ctzll(x.s.high) + (1 + sizeof(di_int) * CHAR_BIT);
27353358Sdim  }
28353358Sdim  return __builtin_ctzll(x.s.low) + 1;
29276789Sdim}
30276789Sdim
31353358Sdim#endif // CRT_HAS_128BIT
32