~lhw/ubuntu/precise/s2tc/multiarch-fix

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * Copyright (C) 2011  Rudolf Polzer   All Rights Reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * RUDOLF POLZER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#ifndef S2TC_COMMON_H
#define S2TC_COMMON_H

template <class T> inline T min(const T &a, const T &b)
{
	if(b < a)
		return b;
	return a;
}

template <class T> inline T max(const T &a, const T &b)
{
	if(b > a)
		return b;
	return a;
}

inline int byteidx(int bit)
{
	return bit >> 3;
}

inline int bitidx(int bit)
{
	return bit & 7;
}

inline void setbit(unsigned char *arr, int bit, int v = 1)
{
	arr[byteidx(bit)] |= (v << bitidx(bit));
}

inline void xorbit(unsigned char *arr, int bit, int v = 1)
{
	arr[byteidx(bit)] ^= (v << bitidx(bit));
}

inline int testbit(const unsigned char *arr, int bit, int v = 1)
{
	return (arr[byteidx(bit)] & (v << bitidx(bit)));
}

template<class T, int count, int width> class bitarray
{
	T bits;
	public:
	inline bitarray(): bits(0)
	{
	}
	inline int get(size_t i) const
	{
		return (bits >> (i * width)) & ((T(1) << width) - 1);
	}
	inline void set(size_t i, int v)
	{
		size_t shift = i * width;
		T mask = ((T(1) << width) - 1) << shift;
		bits = (bits & ~mask) | (T(v) << shift);
	}
	inline void do_or(size_t i, int v)
	{
		bits |= (T(v) << (i * width));
	}
	inline void do_xor(size_t i, int v)
	{
		bits ^= (T(v) << (i * width));
	}
	inline void clear()
	{
		bits = 0;
	}
	inline unsigned char getbyte(size_t p) const
	{
		return (bits >> (p * 8)) & 0xFF;
	}
	inline size_t nbytes() const
	{
		return (count * width + 7) >> 3;
	}
	inline void tobytes(unsigned char *out) const
	{
		size_t s = nbytes();
		for(size_t i = 0; i < s; ++i)
			out[i] = getbyte(i);
	}
};

template<int count, int width> class bitarray<void, count, width>
{
	unsigned char bits[count];
	public:
	inline bitarray(): bits()
	{
		clear();
	}
	inline int get(size_t i) const
	{
		return bits[i];
	}
	inline void set(size_t i, int v)
	{
		bits[i] = v;
	}
	inline void do_or(size_t i, int v)
	{
		bits[i] |= v;
	}
	inline void do_xor(size_t i, int v)
	{
		bits[i] ^= v;
	}
	inline void clear()
	{
		memset(bits, 0, sizeof(bits));
	}
	inline unsigned char getbyte(size_t p) const
	{
		size_t bitpos_min = p * 8;
		size_t bitpos_max = p * 8 + 7;
		size_t word_min = bitpos_min / width;
		size_t word_max = bitpos_max / width;
		int shift = bitpos_min % width;
		unsigned char out = get(word_min) >> shift;
		for(size_t i = word_min+1; i <= word_max; ++i)
			out |= get(i) << ((i - word_min) * width - shift);
		return out;
	}
	inline size_t nbytes() const
	{
		return (count * width + 7) >> 3;
	}
	inline void tobytes(unsigned char *out) const
	{
		size_t s = nbytes();
		for(size_t i = 0; i < s; ++i)
			out[i] = getbyte(i);
	}
};

#endif