svn-gvsig-desktop / branches / gvSIG_19_ext3D_osgVP_2_2_0 / libraries / libjni-potrace / src / main / native / jpotrace / bitops.h @ 31855
History | View | Annotate | Download (1.75 KB)
1 | 31791 | rgaitan | /* Copyright (C) 2001-2007 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 | /* $Id: bitops.h 147 2007-04-09 00:44:09Z selinger $ */
|
||
6 | |||
7 | /* bits.h: this file defines some macros for bit manipulations. We
|
||
8 | provide a generic implementation, as well as machine- and
|
||
9 | compiler-specific fast implementations */
|
||
10 | |||
11 | /* lobit: return the position of the rightmost "1" bit of an int, or
|
||
12 | 32 if none. hibit: return 1 + the position of the leftmost "1" bit
|
||
13 | of an int, or 0 if none. Note: these functions work on 32-bit
|
||
14 | integers. */
|
||
15 | |||
16 | #ifndef BITOPS_H
|
||
17 | #define BITOPS_H
|
||
18 | |||
19 | #ifdef HAVE_CONFIG_H
|
||
20 | #include "config.h" |
||
21 | #endif
|
||
22 | |||
23 | /* ---------------------------------------------------------------------- */
|
||
24 | /* machine specific macros */
|
||
25 | |||
26 | #if defined(HAVE_I386)
|
||
27 | |||
28 | static inline unsigned int lobit(unsigned int x) { |
||
29 | unsigned int res; |
||
30 | asm ("bsf %1,%0\n\t" |
||
31 | "jnz 0f\n\t"
|
||
32 | "movl $32,%0\n"
|
||
33 | "0:"
|
||
34 | : "=r" (res)
|
||
35 | : "r" (x));
|
||
36 | return res;
|
||
37 | } |
||
38 | |||
39 | static inline unsigned int hibit(unsigned int x) { |
||
40 | unsigned int res; |
||
41 | |||
42 | asm ("bsr %1,%0\n\t" |
||
43 | "jnz 0f\n\t"
|
||
44 | "movl $-1,%0\n"
|
||
45 | "0:"
|
||
46 | : "=r" (res)
|
||
47 | : "r" (x));
|
||
48 | return res+1; |
||
49 | } |
||
50 | |||
51 | /* ---------------------------------------------------------------------- */
|
||
52 | #else /* generic macros */ |
||
53 | |||
54 | static inline unsigned int lobit(unsigned int x) { |
||
55 | unsigned int res = 32; |
||
56 | while (x & 0xffffff) { |
||
57 | x <<= 8;
|
||
58 | res -= 8;
|
||
59 | } |
||
60 | while (x) {
|
||
61 | x <<= 1;
|
||
62 | res -= 1;
|
||
63 | } |
||
64 | return res;
|
||
65 | } |
||
66 | |||
67 | static inline unsigned int hibit(unsigned int x) { |
||
68 | unsigned int res = 0; |
||
69 | while (x > 0xff) { |
||
70 | x >>= 8;
|
||
71 | res += 8;
|
||
72 | } |
||
73 | while (x) {
|
||
74 | x >>= 1;
|
||
75 | res += 1;
|
||
76 | } |
||
77 | return res;
|
||
78 | } |
||
79 | |||
80 | #endif
|
||
81 | |||
82 | #endif /* BITOPS_H */ |