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