bitops.h
Go to the documentation of this file.
00001 /* Copyright (C) 2001-2007 Peter Selinger.
00002    This file is part of Potrace. It is free software and it is covered
00003    by the GNU General Public License. See the file COPYING for details. */
00004 
00005 /* $Id: bitops.h 147 2007-04-09 00:44:09Z selinger $ */
00006 
00007 /* bits.h: this file defines some macros for bit manipulations. We
00008    provide a generic implementation, as well as machine- and
00009    compiler-specific fast implementations */
00010 
00011 /* lobit: return the position of the rightmost "1" bit of an int, or
00012    32 if none. hibit: return 1 + the position of the leftmost "1" bit
00013    of an int, or 0 if none. Note: these functions work on 32-bit
00014    integers. */
00015 
00016 #ifndef BITOPS_H
00017 #define BITOPS_H
00018 
00019 #ifdef HAVE_CONFIG_H
00020 #include "config.h"
00021 #endif
00022 
00023 /* ---------------------------------------------------------------------- */
00024 /* machine specific macros */
00025 
00026 #if defined(HAVE_I386)
00027 
00028 static inline unsigned int lobit(unsigned int x) {
00029   unsigned int res;
00030   asm ("bsf     %1,%0\n\t"
00031        "jnz     0f\n\t"
00032        "movl    $32,%0\n"
00033        "0:"
00034        : "=r" (res)
00035        : "r" (x));
00036   return res;
00037 }
00038 
00039 static inline unsigned int hibit(unsigned int x) {
00040   unsigned int res;                                     
00041 
00042   asm ("bsr     %1,%0\n\t"
00043        "jnz     0f\n\t"
00044        "movl    $-1,%0\n"
00045        "0:"
00046        : "=r" (res)
00047        : "r" (x));
00048   return res+1;
00049 }
00050 
00051 /* ---------------------------------------------------------------------- */
00052 #else /* generic macros */
00053 
00054 static inline unsigned int lobit(unsigned int x) {
00055   unsigned int res = 32;
00056   while (x & 0xffffff) {
00057     x <<= 8;
00058     res -= 8;
00059   }
00060   while (x) {
00061     x <<= 1;
00062     res -= 1;
00063   }
00064   return res;
00065 }
00066 
00067 static inline unsigned int hibit(unsigned int x) {
00068   unsigned int res = 0;
00069   while (x > 0xff) {
00070     x >>= 8;
00071     res += 8;
00072   }
00073   while (x) {
00074     x >>= 1;
00075     res += 1;
00076   }
00077   return res;
00078 }
00079 
00080 #endif 
00081 
00082 #endif /* BITOPS_H */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines


portrait_painter
Author(s): Niklas Meinzer, Ina Baumgarten
autogenerated on Wed Dec 26 2012 16:00:43