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