blob: b3988de245ba2631e08815d117101b7a4b61c8a2 [file] [log] [blame]
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +00001#pragma once
2
3#include "pstdint.h"
4
5#include <stdlib.h> // for _rotl, _rotr, etc.
6
7//-----------------------------------------------------------------------------
8
9uint32_t parity ( uint32_t v );
10uint64_t parity ( uint64_t v );
11
12uint32_t popcount ( uint32_t v );
13uint32_t popcount128 ( uint32_t * v );
14
15void printbits ( void * blob, int len );
16void printhex32 ( void * blob, int len );
17
18uint32_t getbit ( void * blob, int len, uint32_t bit );
19uint32_t getbit_wrap ( void * blob, int len, uint32_t bit );
20
21void setbit ( void * blob, int len, uint32_t bit );
22void setbit ( void * blob, int len, uint32_t bit, uint32_t val );
23
24void clearbit ( void * blob, int len, uint32_t bit );
25
26void 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
33void lshift1 ( void * blob, int len, int c );
34void lshift8 ( void * blob, int len, int c );
35void lshift32 ( void * blob, int len, int c );
36
37void rshift1 ( void * blob, int len, int c );
38void rshift8 ( void * blob, int len, int c );
39void rshift32 ( void * blob, int len, int c );
40
41inline 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
53inline 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
65template < typename T >
66inline 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
78template < typename T >
79inline 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
91template<> inline void lshift ( uint32_t & blob, int c ) { blob <<= c; }
92template<> inline void lshift ( uint64_t & blob, int c ) { blob <<= c; }
93template<> inline void rshift ( uint32_t & blob, int c ) { blob >>= c; }
94template<> 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
100void lrot1 ( void * blob, int len, int c );
101void lrot8 ( void * blob, int len, int c );
102void lrot32 ( void * blob, int len, int c );
103
104void rrot1 ( void * blob, int len, int c );
105void rrot8 ( void * blob, int len, int c );
106void rrot32 ( void * blob, int len, int c );
107
108template < typename T >
109inline 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
121template < typename T >
122inline 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
134template<> inline void lrot ( uint32_t & blob, int c ) { blob = _rotl(blob,c); }
135template<> inline void lrot ( uint64_t & blob, int c ) { blob = _rotl64(blob,c); }
136template<> inline void rrot ( uint32_t & blob, int c ) { blob = _rotr(blob,c); }
137template<> 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
142uint32_t window1 ( void * blob, int len, int start, int count );
143uint32_t window8 ( void * blob, int len, int start, int count );
144uint32_t window32 ( void * blob, int len, int start, int count );
145
146inline 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/*
159template < typename T >
160inline 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<>
174inline uint32_t window ( uint32_t & blob, int start, int count )
175{
176 return _rotr(blob,start) & ((1<<count)-1);
177}
178
179// template<>
180inline uint32_t window ( uint64_t & blob, int start, int count )
181{
182 return (uint32_t)_rotr64(blob,start) & ((1<<count)-1);
183}
184
185//-----------------------------------------------------------------------------