tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 1 | #pragma once
|
| 2 |
|
tanjent@gmail.com | 2aa29c3 | 2011-03-19 08:53:53 +0000 | [diff] [blame] | 3 | #include "Platform.h"
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 4 |
|
tanjent@gmail.com | f67ce94 | 2011-03-14 09:11:18 +0000 | [diff] [blame] | 5 | #include <vector>
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 6 |
|
| 7 | //-----------------------------------------------------------------------------
|
| 8 |
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 9 | void printbits ( void * blob, int len );
|
| 10 | void printhex32 ( void * blob, int len );
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 11 | void printbytes ( void * blob, int len );
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 12 |
|
| 13 | uint32_t getbit ( void * blob, int len, uint32_t bit );
|
| 14 | uint32_t getbit_wrap ( void * blob, int len, uint32_t bit );
|
| 15 |
|
| 16 | void setbit ( void * blob, int len, uint32_t bit );
|
| 17 | void setbit ( void * blob, int len, uint32_t bit, uint32_t val );
|
| 18 |
|
| 19 | void clearbit ( void * blob, int len, uint32_t bit );
|
| 20 |
|
| 21 | void flipbit ( void * blob, int len, uint32_t bit );
|
| 22 |
|
tanjent@gmail.com | f67ce94 | 2011-03-14 09:11:18 +0000 | [diff] [blame] | 23 | int countbits ( uint32_t v );
|
| 24 | int countbits ( std::vector<uint32_t> & v );
|
| 25 |
|
| 26 | int countbits ( void * blob, int len );
|
| 27 |
|
| 28 | void invert ( std::vector<uint32_t> & v );
|
| 29 |
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 30 | //----------
|
| 31 |
|
| 32 | template< typename T >
|
| 33 | inline uint32_t getbit ( T & blob, uint32_t bit )
|
| 34 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 35 | return getbit(&blob,sizeof(blob),bit);
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 36 | }
|
| 37 |
|
| 38 | template<> inline uint32_t getbit ( uint32_t & blob, uint32_t bit ) { return (blob >> (bit & 31)) & 1; }
|
| 39 | template<> inline uint32_t getbit ( uint64_t & blob, uint32_t bit ) { return (blob >> (bit & 63)) & 1; }
|
| 40 |
|
| 41 | //----------
|
| 42 |
|
| 43 | template< typename T >
|
| 44 | inline void setbit ( T & blob, uint32_t bit )
|
| 45 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 46 | return setbit(&blob,sizeof(blob),bit);
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 47 | }
|
| 48 |
|
| 49 | template<> inline void setbit ( uint32_t & blob, uint32_t bit ) { blob |= uint32_t(1) << (bit & 31); }
|
| 50 | template<> inline void setbit ( uint64_t & blob, uint32_t bit ) { blob |= uint64_t(1) << (bit & 63); }
|
| 51 |
|
| 52 | //----------
|
| 53 |
|
| 54 | template< typename T >
|
| 55 | inline void flipbit ( T & blob, uint32_t bit )
|
| 56 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 57 | flipbit(&blob,sizeof(blob),bit);
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 58 | }
|
| 59 |
|
| 60 | template<> inline void flipbit ( uint32_t & blob, uint32_t bit ) { bit &= 31; blob ^= (uint32_t(1) << bit); }
|
| 61 | template<> inline void flipbit ( uint64_t & blob, uint32_t bit ) { bit &= 63; blob ^= (uint64_t(1) << bit); }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 62 |
|
| 63 | //-----------------------------------------------------------------------------
|
| 64 | // Left and right shift of blobs. The shift(N) versions work on chunks of N
|
| 65 | // bits at a time (faster)
|
| 66 |
|
| 67 | void lshift1 ( void * blob, int len, int c );
|
| 68 | void lshift8 ( void * blob, int len, int c );
|
| 69 | void lshift32 ( void * blob, int len, int c );
|
| 70 |
|
| 71 | void rshift1 ( void * blob, int len, int c );
|
| 72 | void rshift8 ( void * blob, int len, int c );
|
| 73 | void rshift32 ( void * blob, int len, int c );
|
| 74 |
|
| 75 | inline void lshift ( void * blob, int len, int c )
|
| 76 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 77 | if((len & 3) == 0)
|
| 78 | {
|
| 79 | lshift32(blob,len,c);
|
| 80 | }
|
| 81 | else
|
| 82 | {
|
| 83 | lshift8(blob,len,c);
|
| 84 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 85 | }
|
| 86 |
|
| 87 | inline void rshift ( void * blob, int len, int c )
|
| 88 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 89 | if((len & 3) == 0)
|
| 90 | {
|
| 91 | rshift32(blob,len,c);
|
| 92 | }
|
| 93 | else
|
| 94 | {
|
| 95 | rshift8(blob,len,c);
|
| 96 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 97 | }
|
| 98 |
|
| 99 | template < typename T >
|
| 100 | inline void lshift ( T & blob, int c )
|
| 101 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 102 | if((sizeof(T) & 3) == 0)
|
| 103 | {
|
| 104 | lshift32(&blob,sizeof(T),c);
|
| 105 | }
|
| 106 | else
|
| 107 | {
|
| 108 | lshift8(&blob,sizeof(T),c);
|
| 109 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 110 | }
|
| 111 |
|
| 112 | template < typename T >
|
| 113 | inline void rshift ( T & blob, int c )
|
| 114 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 115 | if((sizeof(T) & 3) == 0)
|
| 116 | {
|
| 117 | lshift32(&blob,sizeof(T),c);
|
| 118 | }
|
| 119 | else
|
| 120 | {
|
| 121 | lshift8(&blob,sizeof(T),c);
|
| 122 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 123 | }
|
| 124 |
|
| 125 | template<> inline void lshift ( uint32_t & blob, int c ) { blob <<= c; }
|
| 126 | template<> inline void lshift ( uint64_t & blob, int c ) { blob <<= c; }
|
| 127 | template<> inline void rshift ( uint32_t & blob, int c ) { blob >>= c; }
|
| 128 | template<> inline void rshift ( uint64_t & blob, int c ) { blob >>= c; }
|
| 129 |
|
| 130 | //-----------------------------------------------------------------------------
|
| 131 | // Left and right rotate of blobs. The rot(N) versions work on chunks of N
|
| 132 | // bits at a time (faster)
|
| 133 |
|
| 134 | void lrot1 ( void * blob, int len, int c );
|
| 135 | void lrot8 ( void * blob, int len, int c );
|
| 136 | void lrot32 ( void * blob, int len, int c );
|
| 137 |
|
| 138 | void rrot1 ( void * blob, int len, int c );
|
| 139 | void rrot8 ( void * blob, int len, int c );
|
| 140 | void rrot32 ( void * blob, int len, int c );
|
| 141 |
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 142 | inline void lrot ( void * blob, int len, int c )
|
| 143 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 144 | if((len & 3) == 0)
|
| 145 | {
|
| 146 | return lrot32(blob,len,c);
|
| 147 | }
|
| 148 | else
|
| 149 | {
|
| 150 | return lrot8(blob,len,c);
|
| 151 | }
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 152 | }
|
| 153 |
|
| 154 | inline void rrot ( void * blob, int len, int c )
|
| 155 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 156 | if((len & 3) == 0)
|
| 157 | {
|
| 158 | return rrot32(blob,len,c);
|
| 159 | }
|
| 160 | else
|
| 161 | {
|
| 162 | return rrot8(blob,len,c);
|
| 163 | }
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 164 | }
|
| 165 |
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 166 | template < typename T >
|
| 167 | inline void lrot ( T & blob, int c )
|
| 168 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 169 | if((sizeof(T) & 3) == 0)
|
| 170 | {
|
| 171 | return lrot32(&blob,sizeof(T),c);
|
| 172 | }
|
| 173 | else
|
| 174 | {
|
| 175 | return lrot8(&blob,sizeof(T),c);
|
| 176 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 177 | }
|
| 178 |
|
| 179 | template < typename T >
|
| 180 | inline void rrot ( T & blob, int c )
|
| 181 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 182 | if((sizeof(T) & 3) == 0)
|
| 183 | {
|
| 184 | return rrot32(&blob,sizeof(T),c);
|
| 185 | }
|
| 186 | else
|
| 187 | {
|
| 188 | return rrot8(&blob,sizeof(T),c);
|
| 189 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 190 | }
|
| 191 |
|
tanjent@gmail.com | 2aa29c3 | 2011-03-19 08:53:53 +0000 | [diff] [blame] | 192 | template<> inline void lrot ( uint32_t & blob, int c ) { blob = ROTL32(blob,c); }
|
| 193 | template<> inline void lrot ( uint64_t & blob, int c ) { blob = ROTL64(blob,c); }
|
| 194 | template<> inline void rrot ( uint32_t & blob, int c ) { blob = ROTR32(blob,c); }
|
| 195 | template<> inline void rrot ( uint64_t & blob, int c ) { blob = ROTR64(blob,c); }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 196 |
|
| 197 | //-----------------------------------------------------------------------------
|
| 198 | // Bit-windowing functions - select some N-bit subset of the input blob
|
| 199 |
|
| 200 | uint32_t window1 ( void * blob, int len, int start, int count );
|
| 201 | uint32_t window8 ( void * blob, int len, int start, int count );
|
| 202 | uint32_t window32 ( void * blob, int len, int start, int count );
|
| 203 |
|
| 204 | inline uint32_t window ( void * blob, int len, int start, int count )
|
| 205 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 206 | if(len & 3)
|
| 207 | {
|
| 208 | return window8(blob,len,start,count);
|
| 209 | }
|
| 210 | else
|
| 211 | {
|
| 212 | return window32(blob,len,start,count);
|
| 213 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 214 | }
|
| 215 |
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 216 | template < typename T >
|
| 217 | inline uint32_t window ( T & blob, int start, int count )
|
| 218 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 219 | if((sizeof(T) & 3) == 0)
|
| 220 | {
|
| 221 | return window32(&blob,sizeof(T),start,count);
|
| 222 | }
|
| 223 | else
|
| 224 | {
|
| 225 | return window8(&blob,sizeof(T),start,count);
|
| 226 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 227 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 228 |
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 229 | template<>
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 230 | inline uint32_t window ( uint32_t & blob, int start, int count )
|
| 231 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 232 | return ROTR32(blob,start) & ((1<<count)-1);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 233 | }
|
| 234 |
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 235 | template<>
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 236 | inline uint32_t window ( uint64_t & blob, int start, int count )
|
| 237 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 238 | return (uint32_t)ROTR64(blob,start) & ((1<<count)-1);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 239 | }
|
| 240 |
|
| 241 | //-----------------------------------------------------------------------------
|