00001 /* Part of the culibs project, <http://www.eideticdew.org/culibs/>. 00002 * Copyright (C) 2009 Petter Urkedal <urkedal@nbi.dk> 00003 * 00004 * This program is free software: you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License as published by 00006 * the Free Software Foundation, either version 3 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 */ 00017 00018 #ifndef CU_WORD_H 00019 #define CU_WORD_H 00020 00021 #include <cu/fwd.h> 00022 #include <cu/bool.h> 00023 #include <cu/int.h> 00024 00025 CU_BEGIN_DECLARATIONS 00026 /** \defgroup cu_word_h cu/word.h: Functions on Machine Words 00027 ** @{ \ingroup cu_type_mod 00028 ** 00029 ** The following is a collection of functions on unsigned integers of the 00030 ** native word size. This may be useful when dealing with data which can be 00031 ** processed in chunks of a fixed number of bits, such as \ref 00032 ** cucon_bitarray_h "bit arrays". For higher level algorithms, the \ref 00033 ** cu_int_h "integer" variants may be more suitable. 00034 ** 00035 ** \see cu_int_h 00036 ** \see cu_size_h 00037 ** \see cu_wordarr_h 00038 **/ 00039 00040 /** \copydoc cu_uint8_dcover */ 00041 CU_SINLINE cu_word_t cu_word_dcover(cu_word_t x) 00042 { return CUP_WORD_NAME(cu_,dcover)(x); } 00043 00044 /** \copydoc cu_uint_ucover */ 00045 CU_SINLINE cu_word_t 00046 cu_word_ucover(unsigned long x) { return x | ~(x - CU_WORD_C(1)); } 00047 00048 /** \copydoc cu_uint8_exp2_ceil_log2 */ 00049 CU_SINLINE cu_word_t 00050 cu_word_exp2_ceil_log2(cu_word_t x) 00051 { return cu_word_dcover(x - CU_WORD_C(1)) + CU_WORD_C(1); } 00052 00053 /** \copydoc cu_uint8_bit_count */ 00054 CU_SINLINE cu_word_t cu_word_bit_count(cu_word_t x) 00055 { return CUP_WORD_NAME(cu_,bit_count)(x); } 00056 00057 /** \copydoc cu_uint8_floor_log2*/ 00058 CU_SINLINE unsigned int cu_word_floor_log2(cu_word_t x) 00059 { return CUP_WORD_NAME(cu_,floor_log2)(x); } 00060 00061 /** \copydoc cu_uint8_log2_lowbit*/ 00062 CU_SINLINE unsigned int 00063 cu_word_log2_lowbit(cu_word_t x) 00064 { return CUP_WORD_NAME(cu_,log2_lowbit)(x); } 00065 00066 /** Returns -1, 0, or 1 if after reversing the bits \a w0 is less, equal, or 00067 ** greater than \a w1, respectively. */ 00068 CU_SINLINE int 00069 cu_word_rcmp(cu_word_t w0, cu_word_t w1) 00070 { 00071 cu_word_t d = w0 ^ w1; 00072 cu_word_t m = (d - 1) ^ d; 00073 w0 &= m; w1 &= m; 00074 return w0 < w1 ? -1 : w0 > w1 ? 1 : 0; 00075 } 00076 00077 /** The bitwise image of \a x under \a f. That is, the word where bit number 00078 ** \e i is the result of applying \a f to bit number \e i of \a x. */ 00079 CU_SINLINE cu_word_t 00080 cu_word_bitimg(cu_bool1f_t f, cu_word_t x) 00081 { 00082 return f & 1 ? (f & 2 ? ~CU_WORD_C(0) : ~x) 00083 : (f & 2 ? x : CU_WORD_C(0)); 00084 } 00085 00086 /** The image under \a f of pairs of bits drawn from respective positions of \a 00087 ** x and \a y. */ 00088 cu_word_t cu_word_bitimg2(cu_bool2f_t f, cu_word_t x, cu_word_t y); 00089 00090 /** @} */ 00091 CU_END_DECLARATIONS 00092 00093 #endif