1156cd587Sjoerg /* ===-- ctzdi2.c - Implement __ctzdi2 -------------------------------------=== 2156cd587Sjoerg * 3156cd587Sjoerg * The LLVM Compiler Infrastructure 4156cd587Sjoerg * 5156cd587Sjoerg * This file is dual licensed under the MIT and the University of Illinois Open 6156cd587Sjoerg * Source Licenses. See LICENSE.TXT for details. 7156cd587Sjoerg * 8156cd587Sjoerg * ===----------------------------------------------------------------------=== 9156cd587Sjoerg * 10156cd587Sjoerg * This file implements __ctzdi2 for the compiler_rt library. 11156cd587Sjoerg * 12156cd587Sjoerg * ===----------------------------------------------------------------------=== 13156cd587Sjoerg */ 14156cd587Sjoerg 15156cd587Sjoerg #include "int_lib.h" 16156cd587Sjoerg 17156cd587Sjoerg /* Returns: the number of trailing 0-bits */ 18156cd587Sjoerg 19*75b842b8Sskrll #if !defined(__clang__) && (defined(__sparc64__) || defined(__mips64) || defined(__riscv__)) 209710b25dSchristos /* gcc resolves __builtin_ctz -> __ctzdi2 leading to infinite recursion */ 219710b25dSchristos #define __builtin_ctz(a) __ctzsi2(a) 229710b25dSchristos extern si_int __ctzsi2(si_int); 239710b25dSchristos #endif 249710b25dSchristos 25156cd587Sjoerg /* Precondition: a != 0 */ 26156cd587Sjoerg 27156cd587Sjoerg COMPILER_RT_ABI si_int __ctzdi2(di_int a)28156cd587Sjoerg__ctzdi2(di_int a) 29156cd587Sjoerg { 30156cd587Sjoerg dwords x; 31156cd587Sjoerg x.all = a; 32156cd587Sjoerg const si_int f = -(x.s.low == 0); 33156cd587Sjoerg return __builtin_ctz((x.s.high & f) | (x.s.low & ~f)) + 34156cd587Sjoerg (f & ((si_int)(sizeof(si_int) * CHAR_BIT))); 35156cd587Sjoerg } 36