KiCad PCB EDA Suite
bitops.h
Go to the documentation of this file.
1 /* Copyright (C) 2001-2015 Peter Selinger.
2  * This file is part of Potrace. It is free software and it is covered
3  * by the GNU General Public License. See the file COPYING for details. */
4 
5 
6 /* bits.h: this file defines some macros for bit manipulations. We
7  * provide a generic implementation, as well as machine- and
8  * compiler-specific fast implementations */
9 
10 /* lobit: return the position of the rightmost "1" bit of an int, or
11  * 32 if none. hibit: return 1 + the position of the leftmost "1" bit
12  * of an int, or 0 if none. Note: these functions work on 32-bit
13  * integers. */
14 
15 #ifndef BITOPS_H
16 #define BITOPS_H
17 
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
21 
22 /* ---------------------------------------------------------------------- */
23 /* machine specific macros */
24 
25 #if defined(HAVE_I386)
26 
27 static inline unsigned int lobit( unsigned int x )
28 {
29  unsigned int res;
30 
31  asm ("bsf %1,%0\n\t"
32  "jnz 0f\n\t"
33  "movl $32,%0\n"
34  "0:"
35  : "=r" (res)
36  : "r" (x)
37  : "cc");
38  return res;
39 }
40 
41 
42 static inline unsigned int hibit( unsigned int x )
43 {
44  unsigned int res;
45 
46  asm ("bsr %1,%0\n\t"
47  "jnz 0f\n\t"
48  "movl $-1,%0\n"
49  "0:"
50  : "=r" (res)
51  : "r" (x)
52  : "cc");
53  return res + 1;
54 }
55 
56 
57 /* ---------------------------------------------------------------------- */
58 #else /* generic macros */
59 
60 static inline unsigned int lobit( unsigned int x )
61 {
62  unsigned int res = 32;
63 
64  while( x & 0xffffff )
65  {
66  x <<= 8;
67  res -= 8;
68  }
69 
70  while( x )
71  {
72  x <<= 1;
73  res -= 1;
74  }
75 
76  return res;
77 }
78 
79 
80 static inline unsigned int hibit( unsigned int x )
81 {
82  unsigned int res = 0;
83 
84  while( x > 0xff )
85  {
86  x >>= 8;
87  res += 8;
88  }
89 
90  while( x )
91  {
92  x >>= 1;
93  res += 1;
94  }
95 
96  return res;
97 }
98 
99 
100 #endif
101 
102 #endif /* BITOPS_H */
static unsigned int lobit(unsigned int x)
Definition: bitops.h:60
static unsigned int hibit(unsigned int x)
Definition: bitops.h:80