Go to the documentation of this file.00001 #ifndef IZ_INTMACROS_H
00002 #define IZ_INTMACROS_H
00003
00004 #include <climits>
00005
00006 namespace IZ
00007 {
00008
00009 #define SS (sizeof(int) * CHAR_BIT - 1)
00010
00011 static inline unsigned int bitMask(unsigned int bitCount)
00012 {
00013
00014 return (1U << bitCount) - 1;
00015 }
00016
00017
00018 static inline unsigned int signBit(int v)
00019 {
00020
00021 return (unsigned int) (v) >> SS;
00022 }
00023
00024
00025 static inline int oppositeSign(int v1, int v2)
00026 {
00027
00028 return (v1 ^ v2) >> SS;
00029 }
00030
00031
00032 static inline int selectVal(int s, int v0, int v1)
00033 {
00034
00035 return (~s & v0) | (s & v1);
00036 }
00037
00038
00039 static inline int clampMax(int v, int max)
00040 {
00041
00042 return max + ((v - max) & ((v - max) >> SS));
00043 }
00044
00045
00046 static inline int clampMin(int v, int min)
00047 {
00048
00049 return min - ((min - v) & ((min - v) >> SS));
00050 }
00051
00052
00053 static inline int clamp0(int v)
00054 {
00055
00056 return v & (-v >> SS);
00057 }
00058
00059
00060 static inline int clampByte(int v)
00061 {
00062
00063 return clampMax(clamp0(v), 255);
00064 }
00065
00066
00067 static inline int cancelValue(int v, int v0)
00068 {
00069
00070 return v & (-v0 >> SS);
00071 }
00072
00073
00074 static inline unsigned int absValue(int v)
00075 {
00076
00077 return (v + (v >> SS)) ^ (v >> SS);
00078 }
00079
00080
00081 static inline unsigned int bsr(unsigned int w)
00082 {
00083
00084
00085 return __builtin_clz(w) ^ SS;
00086 }
00087
00088
00089 static inline unsigned int numBits(unsigned int v)
00090 {
00091
00092
00093 return cancelValue(1 + bsr(v), v);
00094 }
00095
00096
00097 static inline unsigned int s2u(int s)
00098 {
00099
00100 return (s << 1) ^ (s >> SS);
00101 }
00102
00103
00104 static inline int u2s(unsigned int u)
00105 {
00106
00107 return (u >> 1) ^ (-(u & 1));
00108 }
00109 }
00110 #endif