File indexing completed on 2024-12-22 04:04:10
0001 /* Copyright (C) 2001-2019 Peter Selinger. 0002 This file is part of Potrace. It is free software and it is covered 0003 by the GNU General Public License. See the file COPYING for details. */ 0004 0005 0006 /* bits.h: this file defines some macros for bit manipulations. We 0007 provide a generic implementation, as well as machine- and 0008 compiler-specific fast implementations */ 0009 0010 /* lobit: return the position of the rightmost "1" bit of an int, or 0011 32 if none. hibit: return 1 + the position of the leftmost "1" bit 0012 of an int, or 0 if none. Note: these functions work on 32-bit 0013 integers. */ 0014 0015 #ifndef BITOPS_H 0016 #define BITOPS_H 0017 0018 #ifdef HAVE_CONFIG_H 0019 #include <config.h> 0020 #endif 0021 0022 /* ---------------------------------------------------------------------- */ 0023 /* machine specific macros */ 0024 0025 #if defined(HAVE_I386) 0026 0027 static inline unsigned int lobit(unsigned int x) { 0028 unsigned int res; 0029 asm ("bsf %1,%0\n\t" 0030 "jnz 0f\n\t" 0031 "movl $32,%0\n" 0032 "0:" 0033 : "=r" (res) 0034 : "r" (x) 0035 : "cc"); 0036 return res; 0037 } 0038 0039 static inline unsigned int hibit(unsigned int x) { 0040 unsigned int res; 0041 0042 asm ("bsr %1,%0\n\t" 0043 "jnz 0f\n\t" 0044 "movl $-1,%0\n" 0045 "0:" 0046 : "=r" (res) 0047 : "r" (x) 0048 : "cc"); 0049 return res+1; 0050 } 0051 0052 /* ---------------------------------------------------------------------- */ 0053 #else /* generic macros */ 0054 0055 static inline unsigned int lobit(unsigned int x) { 0056 unsigned int res = 32; 0057 while (x & 0xffffff) { 0058 x <<= 8; 0059 res -= 8; 0060 } 0061 while (x) { 0062 x <<= 1; 0063 res -= 1; 0064 } 0065 return res; 0066 } 0067 0068 static inline unsigned int hibit(unsigned int x) { 0069 unsigned int res = 0; 0070 while (x > 0xff) { 0071 x >>= 8; 0072 res += 8; 0073 } 0074 while (x) { 0075 x >>= 1; 0076 res += 1; 0077 } 0078 return res; 0079 } 0080 0081 #endif 0082 0083 #endif /* BITOPS_H */