intmacros.h
Go to the documentation of this file.
1 #ifndef IZ_INTMACROS_H
2 #define IZ_INTMACROS_H
3 
4 #include <climits>
5 
6 namespace IZ
7 {
8 // Shift to replicate the sign bit into all bits
9 #define SS (sizeof(int) * CHAR_BIT - 1)
10 
11  static inline unsigned int bitMask(unsigned int bitCount)
12  {
13  // return pow(2, bitCount) - 1
14  return (1U << bitCount) - 1;
15  }
16 
17 // return signBit
18  static inline unsigned int signBit(int v)
19  {
20 // return v < 0 ? 1 : 0;
21  return (unsigned int) (v) >> SS;
22  }
23 
24 // return -1, if exactly one of v1 and v2 is negative, 0 otherwise
25  static inline int oppositeSign(int v1, int v2)
26  {
27 // return (v1 ^ v2) < 0 ? -1 : 0;
28  return (v1 ^ v2) >> SS;
29  }
30 
31 // return v0 if s is 0, return v1 if s is -1, otherwise the behavior is undefined
32  static inline int selectVal(int s, int v0, int v1)
33  {
34 // return !s ? v0 : v1;
35  return (~s & v0) | (s & v1);
36  }
37 
38 // clamp to a maximum value
39  static inline int clampMax(int v, int max)
40  {
41 // return v > max ? max : v;
42  return max + ((v - max) & ((v - max) >> SS));
43  }
44 
45 // clamp to a minimum value
46  static inline int clampMin(int v, int min)
47  {
48 // return v < min ? min : v;
49  return min - ((min - v) & ((min - v) >> SS));
50  }
51 
52 // clamp to zero
53  static inline int clamp0(int v)
54  {
55 // return v < 0 ? 0 : v;
56  return v & (-v >> SS);
57  }
58 
59 // clamp to unsigned byte (0 ... 255)
60  static inline int clampByte(int v)
61  {
62 // return v < 0 ? 0 : v > 255 ? 255 : v;
63  return clampMax(clamp0(v), 255);
64  }
65 
66 // cancel the value, if the condition value is not positive
67  static inline int cancelValue(int v, int v0)
68  {
69 // return v0 > 0 ? v : 0;
70  return v & (-v0 >> SS);
71  }
72 
73 // return absolute value
74  static inline unsigned int absValue(int v)
75  {
76 // return v < 0 ? -v : v;
77  return (v + (v >> SS)) ^ (v >> SS);
78  }
79 
80 // bit scan reverse
81  static inline unsigned int bsr(unsigned int w)
82  {
83 // asm ("bsr %1,%0" : "=r" (w) : "rm" (w)); return w;
84 // int k = -1; while (w > 0) { w >>= 1; ++k; } return k;
85  return __builtin_clz(w) ^ SS;
86  }
87 
88 // number of bits required for unsigned storage (0->0, 1->1, 2...3->2, 4...7->3, etc.
89  static inline unsigned int numBits(unsigned int v)
90  {
91 // return v > 0 ? 1 + log2(v) : 0;
92 // unsigned int k = 0; while (v > 0) { v >>= 1; ++k; } return k;
93  return cancelValue(1 + bsr(v), v);
94  }
95 
96 // signed to unsigned storage
97  static inline unsigned int s2u(int s)
98  {
99 // return s > 0 ? (s << 1) : (-s << 1) - 1;
100  return (s << 1) ^ (s >> SS);
101  }
102 
103 // unsigned storage to signed
104  static inline int u2s(unsigned int u)
105  {
106 // return u & 1 ? -(u >> 1) - 1 : (u >> 1);
107  return (u >> 1) ^ (-(u & 1));
108  }
109 }
110 #endif
static unsigned int numBits(unsigned int v)
Definition: intmacros.h:89
static int selectVal(int s, int v0, int v1)
Definition: intmacros.h:32
static int u2s(unsigned int u)
Definition: intmacros.h:104
static unsigned int absValue(int v)
Definition: intmacros.h:74
Definition: bitcoder.h:6
static unsigned int signBit(int v)
Definition: intmacros.h:18
static unsigned int bsr(unsigned int w)
Definition: intmacros.h:81
#define SS
Definition: intmacros.h:9
static int cancelValue(int v, int v0)
Definition: intmacros.h:67
static int clamp0(int v)
Definition: intmacros.h:53
static int clampByte(int v)
Definition: intmacros.h:60
static int clampMin(int v, int min)
Definition: intmacros.h:46
static unsigned int bitMask(unsigned int bitCount)
Definition: intmacros.h:11
static int oppositeSign(int v1, int v2)
Definition: intmacros.h:25
static int clampMax(int v, int max)
Definition: intmacros.h:39
static unsigned int s2u(int s)
Definition: intmacros.h:97


imagezero
Author(s):
autogenerated on Mon Jun 10 2019 13:37:22