10b57cec5SDimitry Andric //===-- ctzdi2.c - Implement __ctzdi2 -------------------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file implements __ctzdi2 for the compiler_rt library. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "int_lib.h" 140b57cec5SDimitry Andric 150b57cec5SDimitry Andric // Returns: the number of trailing 0-bits 160b57cec5SDimitry Andric 170b57cec5SDimitry Andric #if !defined(__clang__) && \ 180b57cec5SDimitry Andric ((defined(__sparc__) && defined(__arch64__)) || defined(__mips64) || \ 190b57cec5SDimitry Andric (defined(__riscv) && __SIZEOF_POINTER__ >= 8)) 200b57cec5SDimitry Andric // On 64-bit architectures with neither a native clz instruction nor a native 210b57cec5SDimitry Andric // ctz instruction, gcc resolves __builtin_ctz to __ctzdi2 rather than 220b57cec5SDimitry Andric // __ctzsi2, leading to infinite recursion. 230b57cec5SDimitry Andric #define __builtin_ctz(a) __ctzsi2(a) 24*5ffd83dbSDimitry Andric extern int __ctzsi2(si_int); 250b57cec5SDimitry Andric #endif 260b57cec5SDimitry Andric 270b57cec5SDimitry Andric // Precondition: a != 0 280b57cec5SDimitry Andric __ctzdi2(di_int a)29*5ffd83dbSDimitry AndricCOMPILER_RT_ABI int __ctzdi2(di_int a) { 300b57cec5SDimitry Andric dwords x; 310b57cec5SDimitry Andric x.all = a; 320b57cec5SDimitry Andric const si_int f = -(x.s.low == 0); 33*5ffd83dbSDimitry Andric return ctzsi((x.s.high & f) | (x.s.low & ~f)) + 340b57cec5SDimitry Andric (f & ((si_int)(sizeof(si_int) * CHAR_BIT))); 350b57cec5SDimitry Andric } 36