github.com/yanyiwu/go@v0.0.0-20150106053140-03d6637dbb7f/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  	uint64 b;
    99  
   100  	for(i=0; i<BITS; i++){
   101  		b = a.b[i];
   102  		if(b)
   103  			return 64*i + bitno(b);
   104  	}
   105  	fatal("bad in bnum");
   106  	return 0;
   107  }
   108  
   109  Bits
   110  blsh(uint n)
   111  {
   112  	Bits c;
   113  
   114  	c = zbits;
   115  	c.b[n/64] = 1LL << (n%64);
   116  	return c;
   117  }
   118  
   119  int
   120  btest(Bits *a, uint n)
   121  {
   122  	return (a->b[n/64] & (1LL << (n%64))) != 0;
   123  }
   124  
   125  void
   126  biset(Bits *a, uint n)
   127  {
   128  	a->b[n/64] |= 1LL << (n%64);
   129  }
   130  
   131  void
   132  biclr(Bits *a, uint n)
   133  {
   134  	a->b[n/64] &= ~(1LL << (n%64));
   135  }
   136  
   137  int
   138  bitno(uint64 b)
   139  {
   140  	int i;
   141  
   142  	for(i=0; i<64; i++)
   143  		if(b & (1LL<<i))
   144  			return i;
   145  	fatal("bad in bitno");
   146  	return 0;
   147  }
   148  
   149  int
   150  Qconv(Fmt *fp)
   151  {
   152  	Bits bits;
   153  	int i, first;
   154  
   155  	first = 1;
   156  	bits = va_arg(fp->args, Bits);
   157  	while(bany(&bits)) {
   158  		i = bnum(bits);
   159  		if(first)
   160  			first = 0;
   161  		else
   162  			fmtprint(fp, " ");
   163  		if(var[i].node == N || var[i].node->sym == S)
   164  			fmtprint(fp, "$%d", i);
   165  		else {
   166  			fmtprint(fp, "%s(%d)", var[i].node->sym->name, i);
   167  			if(var[i].offset != 0)
   168  				fmtprint(fp, "%+lld", (vlong)var[i].offset);
   169  		}
   170  		biclr(&bits, i);
   171  	}
   172  	return 0;
   173  }