Drizzled Public API Documentation

my_bit.h
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008, 2009 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 /*
00021   Some useful bit functions
00022 */
00023 
00024 #pragma once
00025 
00026 namespace drizzled
00027 {
00028 namespace internal
00029 {
00030 
00031 extern const char _my_bits_nbits[256];
00032 extern const unsigned char _my_bits_reverse_table[256];
00033 
00034 /*
00035   Find smallest X in 2^X >= value
00036   This can be used to divide a number with value by doing a shift instead
00037 */
00038 
00039 static inline uint32_t my_bit_log2(uint32_t value)
00040 {
00041   uint32_t bit;
00042   for (bit=0 ; value > 1 ; value>>=1, bit++) ;
00043   return bit;
00044 }
00045 
00046 static inline uint32_t my_count_bits(uint64_t v)
00047 {
00048   /* The following code is a bit faster on 16 bit machines than if we would
00049      only shift v */
00050   uint32_t v2=(uint32_t) (v >> 32);
00051   return (uint32_t) (unsigned char) (_my_bits_nbits[(unsigned char)  v] +
00052                          _my_bits_nbits[(unsigned char) (v >> 8)] +
00053                          _my_bits_nbits[(unsigned char) (v >> 16)] +
00054                          _my_bits_nbits[(unsigned char) (v >> 24)] +
00055                          _my_bits_nbits[(unsigned char) (v2)] +
00056                          _my_bits_nbits[(unsigned char) (v2 >> 8)] +
00057                          _my_bits_nbits[(unsigned char) (v2 >> 16)] +
00058                          _my_bits_nbits[(unsigned char) (v2 >> 24)]);
00059 }
00060 
00061 static inline uint32_t my_count_bits_uint16(uint16_t v)
00062 {
00063   return _my_bits_nbits[v];
00064 }
00065 
00066 
00067 static inline uint32_t my_clear_highest_bit(uint32_t v)
00068 {
00069   uint32_t w=v >> 1;
00070   w|= w >> 1;
00071   w|= w >> 2;
00072   w|= w >> 4;
00073   w|= w >> 8;
00074   w|= w >> 16;
00075   return v & w;
00076 }
00077 
00078 static inline uint32_t my_reverse_bits(uint32_t key)
00079 {
00080   return
00081     (_my_bits_reverse_table[ key      & 255] << 24) |
00082     (_my_bits_reverse_table[(key>> 8) & 255] << 16) |
00083     (_my_bits_reverse_table[(key>>16) & 255] <<  8) |
00084      _my_bits_reverse_table[(key>>24)      ];
00085 }
00086 
00087 } /* namespace internal */
00088 } /* namespace drizzled */
00089