github.com/goccy/go-jit@v0.0.0-20200514131505-ff78d45cf6af/internal/ccall/jit-bitset.c (about)

     1  /*
     2   * jit-bitset.h - Bitset routines for the JIT.
     3   *
     4   * Copyright (C) 2006  Southern Storm Software, Pty Ltd.
     5   *
     6   * This file is part of the libjit library.
     7   *
     8   * The libjit library is free software: you can redistribute it and/or
     9   * modify it under the terms of the GNU Lesser General Public License
    10   * as published by the Free Software Foundation, either version 2.1 of
    11   * the License, or (at your option) any later version.
    12   *
    13   * The libjit library is distributed in the hope that it will be useful,
    14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    16   * Lesser General Public License for more details.
    17   *
    18   * You should have received a copy of the GNU Lesser General Public
    19   * License along with the libjit library.  If not, see
    20   * <http://www.gnu.org/licenses/>.
    21   */
    22  
    23  #include "jit-internal.h"
    24  #include "jit-bitset.h"
    25  
    26  void
    27  _jit_bitset_init(_jit_bitset_t *bs)
    28  {
    29  	bs->size = 0;
    30  	bs->bits = 0;
    31  }
    32  
    33  int
    34  _jit_bitset_allocate(_jit_bitset_t *bs, int size)
    35  {
    36  	bs->size = size;
    37  	if(size > 0)
    38  	{
    39  		size = (size + _JIT_BITSET_WORD_BITS - 1) / _JIT_BITSET_WORD_BITS;
    40  		bs->bits = jit_calloc(size, sizeof(_jit_bitset_word_t));
    41  		if(!bs->bits)
    42  		{
    43  			jit_free(bs);
    44  			return 0;
    45  		}
    46  	}
    47  	else
    48  	{
    49  		bs->bits = 0;
    50  	}
    51  	return 1;
    52  }
    53  
    54  int
    55   _jit_bitset_is_allocated(_jit_bitset_t *bs)
    56  {
    57  	return (bs->bits != 0);
    58  }
    59  
    60  void
    61  _jit_bitset_free(_jit_bitset_t *bs)
    62  {
    63  	if(bs->bits)
    64  	{
    65  		jit_free(bs->bits);
    66  		bs->size = 0;
    67  		bs->bits = 0;
    68  	}
    69  }
    70  
    71  void
    72  _jit_bitset_set_bit(_jit_bitset_t *bs, int bit)
    73  {
    74  	int word;
    75  	word = bit / _JIT_BITSET_WORD_BITS;
    76  	bit = bit % _JIT_BITSET_WORD_BITS;
    77  	bs->bits[word] |= bit;
    78  }
    79  
    80  void
    81  _jit_bitset_clear_bit(_jit_bitset_t *bs, int bit)
    82  {
    83  	int word;
    84  	word = bit / _JIT_BITSET_WORD_BITS;
    85  	bit = bit % _JIT_BITSET_WORD_BITS;
    86  	bs->bits[word] &= ~bit;
    87  }
    88  
    89  int
    90  _jit_bitset_test_bit(_jit_bitset_t *bs, int bit)
    91  {
    92  	int word;
    93  	word = bit / _JIT_BITSET_WORD_BITS;
    94  	bit = bit % _JIT_BITSET_WORD_BITS;
    95  	return (bs->bits[word] & bit) != 0;
    96  }
    97  
    98  void
    99  _jit_bitset_clear(_jit_bitset_t *bs)
   100  {
   101  	int i;
   102  	for(i = 0; i < bs->size; i++)
   103  	{
   104  		bs->bits[i] = 0;
   105  	}
   106  }
   107  
   108  int
   109  _jit_bitset_empty(_jit_bitset_t *bs)
   110  {
   111  	int i;
   112  	for(i = 0; i < bs->size; i++)
   113  	{
   114  		if(bs->bits[i])
   115  		{
   116  			return 0;
   117  		}
   118  	}
   119  	return 1;
   120  }
   121  
   122  void
   123  _jit_bitset_add(_jit_bitset_t *dest, _jit_bitset_t *src)
   124  {
   125  	int i;
   126  	for(i = 0; i < dest->size; i++)
   127  	{
   128  		dest->bits[i] |= src->bits[i];
   129  	}
   130  }
   131  
   132  void
   133  _jit_bitset_sub(_jit_bitset_t *dest, _jit_bitset_t *src)
   134  {
   135  	int i;
   136  	for(i = 0; i < dest->size; i++)
   137  	{
   138  		dest->bits[i] &= ~src->bits[i];
   139  	}
   140  }
   141  
   142  int
   143  _jit_bitset_copy(_jit_bitset_t *dest, _jit_bitset_t *src)
   144  {
   145  	int i;
   146  	int changed;
   147  
   148  	changed = 0;
   149  	for(i = 0; i < dest->size; i++)
   150  	{
   151  		if(dest->bits[i] != src->bits[i])
   152  		{
   153  			dest->bits[i] = src->bits[i];
   154  			changed = 1;
   155  		}
   156  	}
   157  	return changed;
   158  }
   159  
   160  int
   161  _jit_bitset_equal(_jit_bitset_t *bs1, _jit_bitset_t *bs2)
   162  {
   163  	int i;
   164  	for(i = 0; i < bs1->size; i++)
   165  	{
   166  		if(bs1->bits[i] != bs2->bits[i])
   167  		{
   168  			return 0;
   169  		}
   170  	}
   171  	return 1;
   172  }