Xonotic
bits.qh File Reference
#include "log.qh"
+ Include dependency graph for bits.qh:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define BIT(n)   (1 << (n))
 Only ever assign into the first 24 bits in QC (so max is BIT(23)). More...
 
#define BITS(n)   (BIT(n) - 1)
 
#define BITSET(var, mask, flag)   (flag ? (var) | (mask) : (var) & ~(mask))
 

Enumerations

enum  {
  OP_SET, OP_MIN, OP_MAX, OP_PLUS,
  OP_MINUS
}
 

Functions

ERASEABLE bool GiveBit (entity e,.int fld, int bit, int op, int val)
 
ERASEABLE bool GiveValue (entity e,.int fld, int op, int val)
 
ERASEABLE int lowestbit (int f)
 
ERASEABLE int randombit (int bits)
 
ERASEABLE int randombits (int bits, int k, bool error_return)
 

Macro Definition Documentation

◆ BIT

◆ BITS

◆ BITSET

#define BITSET (   var,
  mask,
  flag 
)    (flag ? (var) | (mask) : (var) & ~(mask))

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
OP_SET 
OP_MIN 
OP_MAX 
OP_PLUS 
OP_MINUS 

Definition at line 76 of file bits.qh.

76  {
77  OP_SET,
78  OP_MIN,
79  OP_MAX,
80  OP_PLUS,
81  OP_MINUS
82 };
Definition: bits.qh:79
Definition: bits.qh:78
Definition: bits.qh:77
Definition: bits.qh:80
Definition: bits.qh:81

Function Documentation

◆ GiveBit()

ERASEABLE bool GiveBit ( entity  e,
.int  fld,
int  bit,
int  op,
int  val 
)

Definition at line 85 of file bits.qh.

References ERASEABLE, OP_MAX, OP_MIN, OP_MINUS, OP_PLUS, and OP_SET.

Referenced by GiveItems().

86 {
87  int v0 = (e.(fld) & bit);
88  switch (op)
89  {
90  case OP_SET:
91  if (val > 0) e.(fld) |= bit;
92  else e.(fld) &= ~bit;
93  break;
94  case OP_MIN:
95  case OP_PLUS:
96  if (val > 0) e.(fld) |= bit;
97  break;
98  case OP_MAX:
99  if (val <= 0) e.(fld) &= ~bit;
100  break;
101  case OP_MINUS:
102  if (val > 0) e.(fld) &= ~bit;
103  break;
104  }
105  int v1 = (e.(fld) & bit);
106  return v0 != v1;
107 }
Definition: bits.qh:79
Definition: bits.qh:78
Definition: bits.qh:77
Definition: bits.qh:80
Definition: bits.qh:81
+ Here is the caller graph for this function:

◆ GiveValue()

ERASEABLE bool GiveValue ( entity  e,
.int  fld,
int  op,
int  val 
)

Definition at line 110 of file bits.qh.

References max(), min(), OP_MAX, OP_MIN, OP_MINUS, OP_PLUS, and OP_SET.

111 {
112  int v0 = e.(fld);
113  switch (op)
114  {
115  case OP_SET:
116  e.(fld) = val;
117  break;
118  case OP_MIN:
119  e.(fld) = max(e.(fld), val); // min 100 cells = at least 100 cells
120  break;
121  case OP_MAX:
122  e.(fld) = min(e.(fld), val);
123  break;
124  case OP_PLUS:
125  e.(fld) += val;
126  break;
127  case OP_MINUS:
128  e.(fld) -= val;
129  break;
130  }
131  int v1 = e.(fld);
132  return v0 != v1;
133 }
Definition: bits.qh:79
Definition: bits.qh:78
Definition: bits.qh:77
Definition: bits.qh:80
Definition: bits.qh:81
+ Here is the call graph for this function:

◆ lowestbit()

ERASEABLE int lowestbit ( int  f)

Definition at line 17 of file bits.qh.

References ERASEABLE.

18 {
19  f &= ~(f << 1);
20  f &= ~(f << 2);
21  f &= ~(f << 4);
22  f &= ~(f << 8);
23  f &= ~(f << 16);
24  return f;
25 }

◆ randombit()

ERASEABLE int randombit ( int  bits)

Definition at line 28 of file bits.qh.

References ERASEABLE, and random().

Referenced by randombits().

29 {
30  if (!(bits & (bits - 1))) // this ONLY holds for powers of two!
31  return bits;
32 
33  int r = random();
34  int b = 0;
35  int n = 0;
36 
37  for (int f = 1; f <= bits; f *= 2)
38  {
39  if (bits & f)
40  {
41  ++n;
42  r *= n;
43  if (r <= 1) b = f;
44  else r = (r - 1) / (n - 1);
45  }
46  }
47  return b;
48 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ randombits()

ERASEABLE int randombits ( int  bits,
int  k,
bool  error_return 
)

Definition at line 51 of file bits.qh.

References randombit().

52 {
53  int r = 0;
54  while (k > 0 && bits != r)
55  {
56  r += randombit(bits - r);
57  --k;
58  }
59  if (error_return)
60  if (k > 0) return -1;
61  // all
62  return r;
63 }
ERASEABLE int randombit(int bits)
Definition: bits.qh:28
+ Here is the call graph for this function: