intmacros.h
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 // Shift to replicate the sign bit into all bits
00009 #define SS (sizeof(int) * CHAR_BIT - 1)
00010 
00011   static inline unsigned int bitMask(unsigned int bitCount)
00012   {
00013       // return pow(2, bitCount) - 1
00014       return (1U << bitCount) - 1;
00015   }
00016 
00017 // return signBit
00018   static inline unsigned int signBit(int v)
00019   {
00020 //    return v < 0 ? 1 : 0;
00021       return (unsigned int) (v) >> SS;
00022   }
00023 
00024 // return -1, if exactly one of v1 and v2 is negative, 0 otherwise
00025   static inline int oppositeSign(int v1, int v2)
00026   {
00027 //    return (v1 ^ v2) < 0 ? -1 : 0;
00028       return (v1 ^ v2) >> SS;
00029   }
00030 
00031 // return v0 if s is 0, return v1 if s is -1, otherwise the behavior is undefined
00032   static inline int selectVal(int s, int v0, int v1)
00033   {
00034 //    return !s ? v0 : v1;
00035       return (~s & v0) | (s & v1);
00036   }
00037 
00038 // clamp to a maximum value
00039   static inline int clampMax(int v, int max)
00040   {
00041 //    return v > max ? max : v;
00042       return max + ((v - max) & ((v - max) >> SS));
00043   }
00044 
00045 // clamp to a minimum value
00046   static inline int clampMin(int v, int min)
00047   {
00048 //    return v < min ? min : v;
00049       return min - ((min - v) & ((min - v) >> SS));
00050   }
00051 
00052 // clamp to zero
00053   static inline int clamp0(int v)
00054   {
00055 //    return v < 0 ? 0 : v;
00056       return v & (-v >> SS);
00057   }
00058 
00059 // clamp to unsigned byte (0 ... 255)
00060   static inline int clampByte(int v)
00061   {
00062 //    return v < 0 ? 0 : v > 255 ? 255 : v;
00063       return clampMax(clamp0(v), 255);
00064   }
00065 
00066 // cancel the value, if the condition value is not positive
00067   static inline int cancelValue(int v, int v0)
00068   {
00069 //    return v0 > 0 ? v : 0;
00070       return v & (-v0 >> SS);
00071   }
00072 
00073 // return absolute value
00074   static inline unsigned int absValue(int v)
00075   {
00076 //    return v < 0 ? -v : v;
00077       return (v + (v >> SS)) ^ (v >> SS);
00078   }
00079 
00080 // bit scan reverse
00081   static inline unsigned int bsr(unsigned int w)
00082   {
00083 //    asm ("bsr %1,%0" : "=r" (w) : "rm" (w)); return w;
00084 //    int k = -1; while (w > 0) { w >>= 1; ++k; } return k;
00085       return __builtin_clz(w) ^ SS;
00086   }
00087 
00088 // number of bits required for unsigned storage (0->0, 1->1, 2...3->2, 4...7->3, etc.
00089   static inline unsigned int numBits(unsigned int v)
00090   {
00091 //    return v > 0 ? 1 + log2(v) : 0;
00092 //    unsigned int k = 0; while (v > 0) { v >>= 1; ++k; } return k;
00093       return cancelValue(1 + bsr(v), v);
00094   }
00095 
00096 // signed to unsigned storage
00097   static inline unsigned int s2u(int s)
00098   {
00099 //    return s > 0 ? (s << 1) : (-s << 1) - 1;
00100       return (s << 1) ^ (s >> SS);
00101   }
00102 
00103 // unsigned storage to signed
00104   static inline int u2s(unsigned int u)
00105   {
00106 //    return u & 1 ? -(u >> 1) - 1 : (u >> 1);
00107       return (u >> 1) ^ (-(u & 1));
00108   }
00109 }
00110 #endif


imagezero
Author(s):
autogenerated on Thu Jun 6 2019 21:34:51