Back to index

cell-binutils  2.17cvs20070401
cgen-bitset.c
Go to the documentation of this file.
00001 /* CGEN generic opcode support.
00002 
00003    Copyright 2002, 2005
00004    Free Software Foundation, Inc.
00005 
00006    This file is part of the GNU Binutils and GDB, the GNU debugger.
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2, or (at your option)
00011    any later version.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License along
00019    with this program; if not, write to the Free Software Foundation, Inc.,
00020    51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00021 
00022 /* Functions for manipulating CGEN_BITSET.  */
00023 
00024 #include "libiberty.h"
00025 #include "opcode/cgen-bitset.h"
00026 #include <string.h>
00027 
00028 /* Create a bit mask.  */
00029 CGEN_BITSET *
00030 cgen_bitset_create (unsigned bit_count)
00031 {
00032   CGEN_BITSET * mask = xmalloc (sizeof (* mask));
00033   cgen_bitset_init (mask, bit_count);
00034   return mask;
00035 }
00036 
00037 /* Initialize an existing bit mask.  */
00038 
00039 void
00040 cgen_bitset_init (CGEN_BITSET * mask, unsigned bit_count)
00041 {
00042   if (! mask)
00043     return;
00044   mask->length = (bit_count / 8) + 1;
00045   mask->bits = xmalloc (mask->length);
00046   cgen_bitset_clear (mask);
00047 }
00048 
00049 /* Clear the bits of a bit mask.  */
00050 
00051 void
00052 cgen_bitset_clear (CGEN_BITSET * mask)
00053 {
00054   unsigned i;
00055 
00056   if (! mask)
00057     return;
00058 
00059   for (i = 0; i < mask->length; ++i)
00060     mask->bits[i] = 0;
00061 }
00062 
00063 /* Add a bit to a bit mask.  */
00064 
00065 void
00066 cgen_bitset_add (CGEN_BITSET * mask, unsigned bit_num)
00067 {
00068   int byte_ix, bit_ix;
00069   int bit_mask;
00070 
00071   if (! mask)
00072     return;
00073   byte_ix = bit_num / 8;
00074   bit_ix = bit_num % 8;
00075   bit_mask = 1 << (7 - bit_ix);
00076   mask->bits[byte_ix] |= bit_mask;
00077 }
00078 
00079 /* Set a bit mask.  */
00080 
00081 void
00082 cgen_bitset_set (CGEN_BITSET * mask, unsigned bit_num)
00083 {
00084   if (! mask)
00085     return;
00086   cgen_bitset_clear (mask);
00087   cgen_bitset_add (mask, bit_num);
00088 }
00089 
00090 /* Test for a bit in a bit mask.
00091    Returns 1 if the bit is found  */
00092 
00093 int
00094 cgen_bitset_contains (CGEN_BITSET * mask, unsigned bit_num)
00095 {
00096   int byte_ix, bit_ix;
00097   int bit_mask;
00098 
00099   if (! mask)
00100     return 1; /* No bit restrictions.  */
00101 
00102   byte_ix = bit_num / 8;
00103   bit_ix = 7 - (bit_num % 8);
00104   bit_mask = 1 << bit_ix;
00105   return (mask->bits[byte_ix] & bit_mask) >> bit_ix;
00106 }
00107 
00108 /* Compare two bit masks for equality.
00109    Returns 0 if they are equal.  */
00110 
00111 int
00112 cgen_bitset_compare (CGEN_BITSET * mask1, CGEN_BITSET * mask2)
00113 {
00114   if (mask1 == mask2)
00115     return 0;
00116   if (! mask1 || ! mask2)
00117     return 1;
00118   if (mask1->length != mask2->length)
00119     return 1;
00120   return memcmp (mask1->bits, mask2->bits, mask1->length);
00121 }
00122 
00123 /* Test two bit masks for common bits.
00124    Returns 1 if a common bit is found.  */
00125 
00126 int
00127 cgen_bitset_intersect_p (CGEN_BITSET * mask1, CGEN_BITSET * mask2)
00128 {
00129   unsigned i, limit;
00130 
00131   if (mask1 == mask2)
00132     return 1;
00133   if (! mask1 || ! mask2)
00134     return 0;
00135   limit = mask1->length < mask2->length ? mask1->length : mask2->length;
00136 
00137   for (i = 0; i < limit; ++i)
00138     if ((mask1->bits[i] & mask2->bits[i]))
00139       return 1;
00140 
00141   return 0;
00142 }
00143 
00144 /* Make a copy of a bit mask.  */
00145 
00146 CGEN_BITSET *
00147 cgen_bitset_copy (CGEN_BITSET * mask)
00148 {
00149   CGEN_BITSET* newmask;
00150 
00151   if (! mask)
00152     return NULL;
00153   newmask = cgen_bitset_create ((mask->length * 8) - 1);
00154   memcpy (newmask->bits, mask->bits, mask->length);
00155   return newmask;
00156 }
00157 
00158 /* Combine two bit masks.  */
00159 
00160 void
00161 cgen_bitset_union (CGEN_BITSET * mask1, CGEN_BITSET * mask2,
00162                  CGEN_BITSET * result)
00163 {
00164   unsigned i;
00165 
00166   if (! mask1 || ! mask2 || ! result
00167       || mask1->length != mask2->length
00168       || mask1->length != result->length)
00169     return;
00170 
00171   for (i = 0; i < result->length; ++i)
00172     result->bits[i] = mask1->bits[i] | mask2->bits[i];
00173 }