1 /* 2 * University of Illinois/NCSA Open Source License 3 * 4 * Copyright (c) 2003-2012 University of Illinois at Urbana-Champaign. 5 * All rights reserved. 6 * 7 * Developed by: 8 * 9 * LLVM Team 10 * 11 * University of Illinois at Urbana-Champaign 12 * 13 * http://llvm.org 14 * 15 * Permission is hereby granted, free of charge, to any person obtaining a copy 16 * of this software and associated documentation files (the "Software"), to deal 17 * with the Software without restriction, including without limitation the 18 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 19 * sell copies of the Software, and to permit persons to whom the Software is 20 * furnished to do so, subject to the following conditions: 21 * 22 * * Redistributions of source code must retain the above copyright notice, 23 * this list of conditions and the following disclaimers. 24 * 25 * * Redistributions in binary form must reproduce the above copyright 26 * notice, this list of conditions and the following disclaimers in the 27 * documentation and/or other materials provided with the distribution. 28 * 29 * * Neither the names of the LLVM Team, University of Illinois at 30 * Urbana-Champaign, nor the names of its contributors may be used to 31 * endorse or promote products derived from this Software without specific 32 * prior written permission. 33 * 34 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 35 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 36 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 37 * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 38 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 39 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH 40 * THE SOFTWARE. 41 */ 42 43 /* 44 * C compiler runtime logic for __ctzdi2() related builtins taken from LLVM. 45 */ 46 47 #include <sys/isa_defs.h> 48 #include <limits.h> 49 50 typedef union { 51 long long all; 52 struct { 53 #ifdef _LITTLE_ENDIAN 54 unsigned low; 55 int high; 56 #else 57 int high; 58 unsigned low; 59 #endif /* _LITTLE_ENDIAN */ 60 } s; 61 } dwords; 62 63 int 64 __ctzdi2(unsigned long long val) 65 { 66 dwords x; 67 x.all = val; 68 const int f = -(x.s.low == 0); 69 return (__builtin_ctz((x.s.high & f) | (x.s.low & ~f)) + 70 (f & ((int)(sizeof (int) * CHAR_BIT)))); 71 } 72