source: trunk/ccl/lisp-kernel/bits.h @ 6

Last change on this file since 6 was 6, checked in by gb, 16 years ago

Initial revision

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.9 KB
Line 
1/*
2   Copyright (C) 1994-2001 Digitool, Inc
3   This file is part of OpenMCL. 
4
5   OpenMCL is licensed under the terms of the Lisp Lesser GNU Public
6   License , known as the LLGPL and distributed with OpenMCL as the
7   file "LICENSE".  The LLGPL consists of a preamble and the LGPL,
8   which is distributed with OpenMCL as the file "LGPL".  Where these
9   conflict, the preamble takes precedence. 
10
11   OpenMCL is referenced in the preamble as the "LIBRARY."
12
13   The LLGPL is also available online at
14   http://opensource.franz.com/preamble.html
15*/
16
17/*
18  12/26/95  gb  lots o macros.
19*/
20
21
22#ifndef __bits_h__
23#define __bits_h__ 1
24
25#include <string.h>
26
27typedef unsigned *bitvector;
28
29
30static inline int
31set_bit(bitvector bits,unsigned bitnum)
32{
33  unsigned 
34    windex = bitnum>>5, 
35    old = bits[windex],
36    new = old | (0x80000000 >> (bitnum & 0x1f));
37  if (new == old) {
38    return 1;                   /* Was set */
39  } else {
40    bits[windex] = new;
41    return 0;                   /* Was clear */
42  }
43}
44
45void set_n_bits(bitvector,unsigned,unsigned);
46
47static inline int
48clr_bit(bitvector bits, unsigned bitnum)
49{
50  unsigned 
51    windex = bitnum>>5, 
52    old = bits[windex],
53    new = old & ~(0x80000000 >> (bitnum & 0x1f));
54  if (new == old) {
55    return 0;   /* Was clear */
56  } else {
57    bits[windex] = new;
58    return 1;   /* Was set */
59  }
60}
61
62
63static inline unsigned
64ref_bit(bitvector bits,unsigned bitnum)
65{
66  return ((bits[bitnum>>5] & (0x80000000 >> (bitnum & 0x1f))) != 0);
67}
68
69bitvector new_bitvector(unsigned);
70void zero_bits(bitvector, unsigned);
71void ior_bits(bitvector,bitvector,unsigned);
72
73#define BIT0_MASK 0x80000000U
74#define bits_word_index(bitnum) (((unsigned)(bitnum)) >> 5)
75#define bits_bit_index(bitnum) (((unsigned)(bitnum)) & 0x1f)
76#define bits_word_ptr(bits,bitnum) \
77  ((unsigned *) (((unsigned *) bits) + ((unsigned) (bits_word_index(bitnum)))))
78#define bits_word_mask(bitnum) ((BIT0_MASK) >> bits_bit_index(bitnum))
79#define bits_indexed_word(bitv,indexw) ((((unsigned *)(bitv))[indexw]))
80#define bits_word(bitv,bitnum) bits_indexed_word(bits,bits_word_index(bitnum))
81
82/* Evaluates some arguments twice */
83
84#define set_bits_vars(BITVvar,BITNUMvar,BITPvar,BITWvar,MASKvar) \
85{ BITPvar = bits_word_ptr(BITVvar,BITNUMvar); BITWvar = *BITPvar; MASKvar = bits_word_mask(BITNUMvar); }
86
87#define set_bitidx_vars(BITVvar,BITNUMvar,BITPvar,BITWvar,BITIDXvar) \
88{ BITPvar = bits_word_ptr(BITVvar,BITNUMvar); BITIDXvar = bits_bit_index(BITNUMvar); \
89    BITWvar = (*BITPvar << BITIDXvar) >> BITIDXvar; }
90
91/* The MetroWerks compiler open codes this, but doing two machine
92   instructions out-of-line won't kill us. */
93
94#ifdef __MWERKS__
95#define count_leading_zeros(x) __cntlzw(x)
96#else
97#ifdef __GNUC__
98static __inline__ int
99count_leading_zeros(unsigned w)
100{
101  unsigned lz;
102  __asm__  ("cntlzw %0,%1" : "=r" (lz) : "r" (w));
103  return lz;
104}
105#else
106unsigned
107count_leading_zeros(unsigned);
108#endif
109#endif
110
111
112
113                                       
114#endif /* __bits_h__ */
Note: See TracBrowser for help on using the repository browser.