Path: blob/master/libs/compiler-rt/lib/builtins/ctzdi2.c
12346 views
/* ===-- ctzdi2.c - Implement __ctzdi2 -------------------------------------===1*2* The LLVM Compiler Infrastructure3*4* This file is dual licensed under the MIT and the University of Illinois Open5* Source Licenses. See LICENSE.TXT for details.6*7* ===----------------------------------------------------------------------===8*9* This file implements __ctzdi2 for the compiler_rt library.10*11* ===----------------------------------------------------------------------===12*/1314#include "int_lib.h"1516/* Returns: the number of trailing 0-bits */1718#if !defined(__clang__) && \19((defined(__sparc__) && defined(__arch64__)) || \20defined(__mips64) || \21(defined(__riscv) && __SIZEOF_POINTER__ >= 8))22/* On 64-bit architectures with neither a native clz instruction nor a native23* ctz instruction, gcc resolves __builtin_ctz to __ctzdi2 rather than24* __ctzsi2, leading to infinite recursion. */25#define __builtin_ctz(a) __ctzsi2(a)26extern si_int __ctzsi2(si_int);27#endif2829/* Precondition: a != 0 */3031COMPILER_RT_ABI si_int32__ctzdi2(di_int a)33{34dwords x;35x.all = a;36const si_int f = -(x.s.low == 0);37return __builtin_ctz((x.s.high & f) | (x.s.low & ~f)) +38(f & ((si_int)(sizeof(si_int) * CHAR_BIT)));39}404142