github.com/akaros/go-akaros@v0.0.0-20181004170632-85005d477eab/src/cmd/gc/bits.c (about)

     1  // Inferno utils/cc/bits.c
     2  // http://code.google.com/p/inferno-os/source/browse/utils/cc/bits.c
     3  //
     4  //	Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
     5  //	Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
     6  //	Portions Copyright © 1997-1999 Vita Nuova Limited
     7  //	Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
     8  //	Portions Copyright © 2004,2006 Bruce Ellis
     9  //	Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
    10  //	Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
    11  //	Portions Copyright © 2009 The Go Authors.  All rights reserved.
    12  //
    13  // Permission is hereby granted, free of charge, to any person obtaining a copy
    14  // of this software and associated documentation files (the "Software"), to deal
    15  // in the Software without restriction, including without limitation the rights
    16  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    17  // copies of the Software, and to permit persons to whom the Software is
    18  // furnished to do so, subject to the following conditions:
    19  //
    20  // The above copyright notice and this permission notice shall be included in
    21  // all copies or substantial portions of the Software.
    22  //
    23  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    24  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    25  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    26  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    27  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    28  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    29  // THE SOFTWARE.
    30  
    31  #include <u.h>
    32  #include <libc.h>
    33  #include "go.h"
    34  
    35  /*
    36  Bits
    37  bor(Bits a, Bits b)
    38  {
    39  	Bits c;
    40  	int i;
    41  
    42  	for(i=0; i<BITS; i++)
    43  		c.b[i] = a.b[i] | b.b[i];
    44  	return c;
    45  }
    46  
    47  Bits
    48  band(Bits a, Bits b)
    49  {
    50  	Bits c;
    51  	int i;
    52  
    53  	for(i=0; i<BITS; i++)
    54  		c.b[i] = a.b[i] & b.b[i];
    55  	return c;
    56  }
    57  
    58  Bits
    59  bnot(Bits a)
    60  {
    61  	Bits c;
    62  	int i;
    63  
    64  	for(i=0; i<BITS; i++)
    65  		c.b[i] = ~a.b[i];
    66  	return c;
    67  }
    68  */
    69  
    70  int
    71  bany(Bits *a)
    72  {
    73  	int i;
    74  
    75  	for(i=0; i<BITS; i++)
    76  		if(a->b[i])
    77  			return 1;
    78  	return 0;
    79  }
    80  
    81  /*
    82  int
    83  beq(Bits a, Bits b)
    84  {
    85  	int i;
    86  
    87  	for(i=0; i<BITS; i++)
    88  		if(a.b[i] != b.b[i])
    89  			return 0;
    90  	return 1;
    91  }
    92  */
    93  
    94  int
    95  bnum(Bits a)
    96  {
    97  	int i;
    98  	int32 b;
    99  
   100  	for(i=0; i<BITS; i++)
   101  		if(b = a.b[i])
   102  			return 32*i + bitno(b);
   103  	fatal("bad in bnum");
   104  	return 0;
   105  }
   106  
   107  Bits
   108  blsh(uint n)
   109  {
   110  	Bits c;
   111  
   112  	c = zbits;
   113  	c.b[n/32] = 1L << (n%32);
   114  	return c;
   115  }
   116  
   117  /*
   118  int
   119  bset(Bits a, uint n)
   120  {
   121  	if(a.b[n/32] & (1L << (n%32)))
   122  		return 1;
   123  	return 0;
   124  }
   125  */
   126  
   127  int
   128  bitno(int32 b)
   129  {
   130  	int i;
   131  
   132  	for(i=0; i<32; i++)
   133  		if(b & (1L<<i))
   134  			return i;
   135  	fatal("bad in bitno");
   136  	return 0;
   137  }
   138  
   139  int
   140  Qconv(Fmt *fp)
   141  {
   142  	Bits bits;
   143  	int i, first;
   144  
   145  	first = 1;
   146  	bits = va_arg(fp->args, Bits);
   147  	while(bany(&bits)) {
   148  		i = bnum(bits);
   149  		if(first)
   150  			first = 0;
   151  		else
   152  			fmtprint(fp, " ");
   153  		if(var[i].node == N || var[i].node->sym == S)
   154  			fmtprint(fp, "$%d", i);
   155  		else {
   156  			fmtprint(fp, "%s(%d)", var[i].node->sym->name, i);
   157  			if(var[i].offset != 0)
   158  				fmtprint(fp, "%+lld", (vlong)var[i].offset);
   159  		}
   160  		bits.b[i/32] &= ~(1L << (i%32));
   161  	}
   162  	return 0;
   163  }