1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #pragma once19 20 #include "[myassert.h]"21 #include "../MathGeoLibFwd.h"22 #include "[MathTypes.h]"23 24 [MATH_BEGIN_NAMESPACE]25 26 27 u32 [BinaryStringToValue](const char *str);28 29 30 inline int [CountBitsSet](u32 value)31 {32 int bits = 0;33 while(value)34 {35 value &= value - 1;36 ++bits;37 }38 return bits;39 }40 41 42 inline int [ExtractLSB](unsigned long *value)43 {44 for(int i = 0; i < 32; ++i)45 if ((*value & (1 << i)) != 0)46 {47 *value &= ~(1 << i);48 return i;49 }50 return -1;51 }52 53 54 55 56 57 58 59 60 61 template<int Bits>62 class [LSBT]63 {64 public:65 static const u32 [val] = (1 << Bits) - 1;66 };67 68 69 70 template<>71 class [LSBT]<32>72 {73 public:74 static const u32 [val] = 0xFFFFFFFF;75 };76 77 template<>78 class [LSBT]<31>79 {80 public:81 static const u32 [val] = 0x7FFFFFFF;82 };83 84 85 86 87 88 89 inline u32 [LSB](u32 bits)90 {91 [assert](bits <= 32);92 if (bits >= 32)93 return 0xFFFFFFFF;94 return (1U << bits) - 1;95 }96 97 inline [u64] [LSB64]([u64] bits)98 {99 [assert](bits <= 64);100 if (bits >= 64)101 return 0xFFFFFFFFFFFFFFFFULL;102 return (1ULL << bits) - 1;103 }104 105 106 107 108 109 110 111 112 template<int Pos, int Bits>113 class [BitMaskT]114 {115 public:116 117 static const u32 [val] = [LSBT<Pos+Bits>::val] & ~[LSBT<Pos>::val]; 118 };119 120 121 122 123 inline u32 [BitMask](u32 pos, u32 bits)124 {125 return [LSB](pos + bits) & ~[LSB](pos);126 }127 128 inline [u64] [BitMask64]([u64] pos, [u64] bits)129 {130 return [LSB64](pos + bits) & ~[LSB64](pos);131 }132 133 134 135 template<typename ResultType, typename InputType,136 int APos, int ABits, int RPos, int RBits,137 int GPos, int GBits, int BPos, int BBits>138 ResultType [PackBits](InputType a, InputType r, InputType g, InputType b)139 {140 return (ResultType)(141 ((ResultType)a << APos & [BitMaskT<APos, ABits>::val]) |142 ((ResultType)r << RPos & [BitMaskT<RPos, RBits>::val]) |143 ((ResultType)g << GPos & [BitMaskT<GPos, GBits>::val]) |144 ((ResultType)b << BPos & [BitMaskT<BPos, BBits>::val]));145 }146 147 148 template<typename ResultType, typename InputType>149 ResultType [PackBits](int APos, int ABits, int RPos, int RBits,150 int GPos, int GBits, int BPos, int BBits,151 InputType a, InputType r, InputType g, InputType b)152 {153 return (ResultType)(154 ((ResultType)a << APos & [BitMask](APos, ABits)) |155 ((ResultType)r << RPos & [BitMask](RPos, RBits)) |156 ((ResultType)g << GPos & [BitMask](GPos, GBits)) |157 ((ResultType)b << BPos & [BitMask](BPos, BBits)));158 }159 160 161 162 163 template<typename ResultType, typename InputType, int Pos, int Bits>164 void [ExtractBits](ResultType &out, const InputType &in)165 {166 out = (ResultType)(in >> Pos & [BitMaskT<0, Bits>::val]);167 }168 169 170 171 172 template<typename ResultType, typename InputType>173 void [ExtractBits](int pos, int bits, ResultType &out, const InputType &in)174 {175 out = (ResultType)(in >> pos & [BitMask](0, bits));176 }177 178 [MATH_END_NAMESPACE] Go back to previous page