1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program Tester Core
3 * ----------------------------------------
5 * Copyright 2014 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * \brief Utility class to build seeds from different data types.
23 * Values are first XORed with type specifig mask, which makes sure that
24 * two values with different types, but same bit presentation produce
25 * different results. Then values are passed through 32 bit crc.
26 *//*--------------------------------------------------------------------*/
28 #include "tcuSeedBuilder.hpp"
38 deUint32 advanceCrc32 (deUint32 oldCrc, size_t len, const deUint8* data)
40 const deUint32 generator = 0x04C11DB7u;
41 deUint32 crc = oldCrc;
43 for (size_t i = 0; i < len; i++)
45 const deUint32 current = static_cast<deUint32>(data[i]);
48 for (size_t bitNdx = 0; bitNdx < 8; bitNdx++)
51 crc = (crc >> 1u) ^ generator;
62 SeedBuilder::SeedBuilder (void)
63 : m_hash (0xccf139d7u)
67 void SeedBuilder::feed (size_t size, const void* ptr)
69 m_hash = advanceCrc32(m_hash, size, (const deUint8*)ptr);
72 SeedBuilder& operator<< (SeedBuilder& builder, bool value)
74 const deUint8 val = (value ? 54: 7);
76 builder.feed(sizeof(val), &val);
80 SeedBuilder& operator<< (SeedBuilder& builder, deInt8 value)
82 const deInt8 val = value ^ 75;
84 builder.feed(sizeof(val), &val);
88 SeedBuilder& operator<< (SeedBuilder& builder, deUint8 value)
90 const deUint8 val = value ^ 140u;
92 builder.feed(sizeof(val), &val);
96 SeedBuilder& operator<< (SeedBuilder& builder, deInt16 value)
98 const deInt16 val = value ^ 555;
99 const deUint8 data[] =
101 (deUint8)(((deUint16)val) & 0xFFu),
102 (deUint8)(((deUint16)val) >> 8),
105 builder.feed(sizeof(data), data);
109 SeedBuilder& operator<< (SeedBuilder& builder, deUint16 value)
111 const deUint16 val = value ^ 37323u;
112 const deUint8 data[] =
114 (deUint8)(val & 0xFFu),
118 builder.feed(sizeof(data), data);
122 SeedBuilder& operator<< (SeedBuilder& builder, deInt32 value)
124 const deInt32 val = value ^ 53054741;
125 const deUint8 data[] =
127 (deUint8)(((deUint32)val) & 0xFFu),
128 (deUint8)((((deUint32)val) >> 8) & 0xFFu),
129 (deUint8)((((deUint32)val) >> 16) & 0xFFu),
130 (deUint8)((((deUint32)val) >> 24) & 0xFFu),
133 builder.feed(sizeof(data), data);
137 SeedBuilder& operator<< (SeedBuilder& builder, deUint32 value)
139 const deUint32 val = value ^ 1977303630u;
140 const deUint8 data[] =
142 (deUint8)(val & 0xFFu),
143 (deUint8)((val >> 8) & 0xFFu),
144 (deUint8)((val >> 16) & 0xFFu),
145 (deUint8)((val >> 24) & 0xFFu),
148 builder.feed(sizeof(data), data);
152 SeedBuilder& operator<< (SeedBuilder& builder, deInt64 value)
154 const deInt64 val = value ^ 772935234179004386ll;
155 const deUint8 data[] =
157 (deUint8)(((deUint64)val) & 0xFFu),
158 (deUint8)((((deUint64)val) >> 8) & 0xFFu),
159 (deUint8)((((deUint64)val) >> 16) & 0xFFu),
160 (deUint8)((((deUint64)val) >> 24) & 0xFFu),
162 (deUint8)((((deUint64)val) >> 32) & 0xFFu),
163 (deUint8)((((deUint64)val) >> 40) & 0xFFu),
164 (deUint8)((((deUint64)val) >> 48) & 0xFFu),
165 (deUint8)((((deUint64)val) >> 56) & 0xFFu),
168 builder.feed(sizeof(data), data);
172 SeedBuilder& operator<< (SeedBuilder& builder, deUint64 value)
174 const deUint64 val = value ^ 4664937258000467599ull;
175 const deUint8 data[] =
177 (deUint8)(val & 0xFFu),
178 (deUint8)((val >> 8) & 0xFFu),
179 (deUint8)((val >> 16) & 0xFFu),
180 (deUint8)((val >> 24) & 0xFFu),
182 (deUint8)((val >> 32) & 0xFFu),
183 (deUint8)((val >> 40) & 0xFFu),
184 (deUint8)((val >> 48) & 0xFFu),
185 (deUint8)((val >> 56) & 0xFFu),
188 builder.feed(sizeof(data), data);
192 SeedBuilder& operator<< (SeedBuilder& builder, float value)
194 // \note Assume that float has same endianess as uint32.
197 deMemcpy(&val, &value, sizeof(deUint32));
200 const deUint8 data[] =
202 (deUint8)(val & 0xFFu),
203 (deUint8)((val >> 8) & 0xFFu),
204 (deUint8)((val >> 16) & 0xFFu),
205 (deUint8)((val >> 24) & 0xFFu),
208 builder.feed(sizeof(data), data);
213 SeedBuilder& operator<< (SeedBuilder& builder, double value)
215 // \note Assume that double has same endianess as uint64.
218 deMemcpy(&val, &value, sizeof(deUint64));
220 const deUint8 data[] =
222 (deUint8)(val & 0xFFu),
223 (deUint8)((val >> 8) & 0xFFu),
224 (deUint8)((val >> 16) & 0xFFu),
225 (deUint8)((val >> 24) & 0xFFu),
227 (deUint8)((val >> 32) & 0xFFu),
228 (deUint8)((val >> 40) & 0xFFu),
229 (deUint8)((val >> 48) & 0xFFu),
230 (deUint8)((val >> 56) & 0xFFu),
233 builder.feed(sizeof(data), data);
237 SeedBuilder& operator<< (SeedBuilder& builder, const std::string& value)
239 builder.feed(value.length(), value.c_str());