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.
|
| 6 |
|
| 7 | //-----------------------------------------------------------------------------
|
| 8 |
|
| 9 | uint32_t parity ( uint32_t v );
|
| 10 | uint64_t parity ( uint64_t v );
|
| 11 |
|
| 12 | uint32_t popcount ( uint32_t v );
|
| 13 | uint32_t popcount128 ( uint32_t * v );
|
| 14 |
|
| 15 | void printbits ( void * blob, int len );
|
| 16 | void printhex32 ( void * blob, int len );
|
| 17 |
|
| 18 | uint32_t getbit ( void * blob, int len, uint32_t bit );
|
| 19 | uint32_t getbit_wrap ( void * blob, int len, uint32_t bit );
|
| 20 |
|
| 21 | void setbit ( void * blob, int len, uint32_t bit );
|
| 22 | void setbit ( void * blob, int len, uint32_t bit, uint32_t val );
|
| 23 |
|
| 24 | void clearbit ( void * blob, int len, uint32_t bit );
|
| 25 |
|
| 26 | void flipbit ( void * blob, int len, uint32_t bit );
|
| 27 |
|
| 28 |
|
| 29 | //-----------------------------------------------------------------------------
|
| 30 | // Left and right shift of blobs. The shift(N) versions work on chunks of N
|
| 31 | // bits at a time (faster)
|
| 32 |
|
| 33 | void lshift1 ( void * blob, int len, int c );
|
| 34 | void lshift8 ( void * blob, int len, int c );
|
| 35 | void lshift32 ( void * blob, int len, int c );
|
| 36 |
|
| 37 | void rshift1 ( void * blob, int len, int c );
|
| 38 | void rshift8 ( void * blob, int len, int c );
|
| 39 | void rshift32 ( void * blob, int len, int c );
|
| 40 |
|
| 41 | inline void lshift ( void * blob, int len, int c )
|
| 42 | {
|
| 43 | if((len & 3) == 0)
|
| 44 | {
|
| 45 | lshift32(&blob,len,c);
|
| 46 | }
|
| 47 | else
|
| 48 | {
|
| 49 | lshift8(&blob,len,c);
|
| 50 | }
|
| 51 | }
|
| 52 |
|
| 53 | inline void rshift ( void * blob, int len, int c )
|
| 54 | {
|
| 55 | if((len & 3) == 0)
|
| 56 | {
|
| 57 | rshift32(&blob,len,c);
|
| 58 | }
|
| 59 | else
|
| 60 | {
|
| 61 | rshift8(&blob,len,c);
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | template < typename T >
|
| 66 | inline void lshift ( T & blob, int c )
|
| 67 | {
|
| 68 | if((sizeof(T) & 3) == 0)
|
| 69 | {
|
| 70 | lshift32(&blob,sizeof(T),c);
|
| 71 | }
|
| 72 | else
|
| 73 | {
|
| 74 | lshift8(&blob,sizeof(T),c);
|
| 75 | }
|
| 76 | }
|
| 77 |
|
| 78 | template < typename T >
|
| 79 | inline void rshift ( T & blob, int c )
|
| 80 | {
|
| 81 | if((sizeof(T) & 3) == 0)
|
| 82 | {
|
| 83 | lshift32(&blob,sizeof(T),c);
|
| 84 | }
|
| 85 | else
|
| 86 | {
|
| 87 | lshift8(&blob,sizeof(T),c);
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | template<> inline void lshift ( uint32_t & blob, int c ) { blob <<= c; }
|
| 92 | template<> inline void lshift ( uint64_t & blob, int c ) { blob <<= c; }
|
| 93 | template<> inline void rshift ( uint32_t & blob, int c ) { blob >>= c; }
|
| 94 | template<> inline void rshift ( uint64_t & blob, int c ) { blob >>= c; }
|
| 95 |
|
| 96 | //-----------------------------------------------------------------------------
|
| 97 | // Left and right rotate of blobs. The rot(N) versions work on chunks of N
|
| 98 | // bits at a time (faster)
|
| 99 |
|
| 100 | void lrot1 ( void * blob, int len, int c );
|
| 101 | void lrot8 ( void * blob, int len, int c );
|
| 102 | void lrot32 ( void * blob, int len, int c );
|
| 103 |
|
| 104 | void rrot1 ( void * blob, int len, int c );
|
| 105 | void rrot8 ( void * blob, int len, int c );
|
| 106 | void rrot32 ( void * blob, int len, int c );
|
| 107 |
|
| 108 | template < typename T >
|
| 109 | inline void lrot ( T & blob, int c )
|
| 110 | {
|
| 111 | if((sizeof(T) & 3) == 0)
|
| 112 | {
|
| 113 | return lrot32(&blob,sizeof(T),c);
|
| 114 | }
|
| 115 | else
|
| 116 | {
|
| 117 | return lrot8(&blob,sizeof(T),c);
|
| 118 | }
|
| 119 | }
|
| 120 |
|
| 121 | template < typename T >
|
| 122 | inline void rrot ( T & blob, int c )
|
| 123 | {
|
| 124 | if((sizeof(T) & 3) == 0)
|
| 125 | {
|
| 126 | return rrot32(&blob,sizeof(T),c);
|
| 127 | }
|
| 128 | else
|
| 129 | {
|
| 130 | return rrot8(&blob,sizeof(T),c);
|
| 131 | }
|
| 132 | }
|
| 133 |
|
| 134 | template<> inline void lrot ( uint32_t & blob, int c ) { blob = _rotl(blob,c); }
|
| 135 | template<> inline void lrot ( uint64_t & blob, int c ) { blob = _rotl64(blob,c); }
|
| 136 | template<> inline void rrot ( uint32_t & blob, int c ) { blob = _rotr(blob,c); }
|
| 137 | template<> inline void rrot ( uint64_t & blob, int c ) { blob = _rotr64(blob,c); }
|
| 138 |
|
| 139 | //-----------------------------------------------------------------------------
|
| 140 | // Bit-windowing functions - select some N-bit subset of the input blob
|
| 141 |
|
| 142 | uint32_t window1 ( void * blob, int len, int start, int count );
|
| 143 | uint32_t window8 ( void * blob, int len, int start, int count );
|
| 144 | uint32_t window32 ( void * blob, int len, int start, int count );
|
| 145 |
|
| 146 | inline uint32_t window ( void * blob, int len, int start, int count )
|
| 147 | {
|
| 148 | if(len & 3)
|
| 149 | {
|
| 150 | return window8(blob,len,start,count);
|
| 151 | }
|
| 152 | else
|
| 153 | {
|
| 154 | return window32(blob,len,start,count);
|
| 155 | }
|
| 156 | }
|
| 157 |
|
| 158 | /*
|
| 159 | template < typename T >
|
| 160 | inline uint32_t window ( T & blob, int start, int count )
|
| 161 | {
|
| 162 | if((sizeof(T) & 3) == 0)
|
| 163 | {
|
| 164 | return window32(&blob,sizeof(T),start,count);
|
| 165 | }
|
| 166 | else
|
| 167 | {
|
| 168 | return window8(&blob,sizeof(T),start,count);
|
| 169 | }
|
| 170 | }
|
| 171 | */
|
| 172 |
|
| 173 | // template<>
|
| 174 | inline uint32_t window ( uint32_t & blob, int start, int count )
|
| 175 | {
|
| 176 | return _rotr(blob,start) & ((1<<count)-1);
|
| 177 | }
|
| 178 |
|
| 179 | // template<>
|
| 180 | inline uint32_t window ( uint64_t & blob, int start, int count )
|
| 181 | {
|
| 182 | return (uint32_t)_rotr64(blob,start) & ((1<<count)-1);
|
| 183 | }
|
| 184 |
|
| 185 | //-----------------------------------------------------------------------------
|