xref: /netbsd-src/sys/external/bsd/compiler_rt/dist/lib/builtins/clzti2.c (revision f7f78b3373aafafa211473d7733a8806c0e402c0)
1156cd587Sjoerg /* ===-- clzti2.c - Implement __clzti2 -------------------------------------===
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 __clzti2 for the compiler_rt library.
11156cd587Sjoerg  *
12156cd587Sjoerg  * ===----------------------------------------------------------------------===
13156cd587Sjoerg  */
14156cd587Sjoerg 
15156cd587Sjoerg #include "int_lib.h"
16156cd587Sjoerg 
17156cd587Sjoerg #ifdef CRT_HAS_128BIT
18156cd587Sjoerg 
19156cd587Sjoerg /* Returns: the number of leading 0-bits */
20156cd587Sjoerg 
21156cd587Sjoerg /* Precondition: a != 0 */
22156cd587Sjoerg 
23*f7f78b33Sjoerg COMPILER_RT_ABI si_int
__clzti2(ti_int a)24156cd587Sjoerg __clzti2(ti_int a)
25156cd587Sjoerg {
26156cd587Sjoerg     twords x;
27156cd587Sjoerg     x.all = a;
28156cd587Sjoerg     const di_int f = -(x.s.high == 0);
29156cd587Sjoerg     return __builtin_clzll((x.s.high & ~f) | (x.s.low & f)) +
30156cd587Sjoerg            ((si_int)f & ((si_int)(sizeof(di_int) * CHAR_BIT)));
31156cd587Sjoerg }
32156cd587Sjoerg 
33156cd587Sjoerg #endif /* CRT_HAS_128BIT */
34