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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
|
/*
* Copyright 2019 Patrick Stotko
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <gtest/gtest.h>
#include <limits>
#include <random>
#include <thread>
#include <unordered_set>
#include <stdgpu/bit.h>
#include <test_utils.h>
class stdgpu_bit : public ::testing::Test
{
protected:
// Called before each test
void
SetUp() override
{
}
// Called after each test
void
TearDown() override
{
}
};
// Explicit template instantiations
namespace stdgpu
{
template STDGPU_HOST_DEVICE bool
has_single_bit<unsigned int>(const unsigned int);
template STDGPU_HOST_DEVICE unsigned int
bit_ceil<unsigned int>(const unsigned int);
template STDGPU_HOST_DEVICE unsigned int
bit_floor<unsigned int>(const unsigned int);
template STDGPU_HOST_DEVICE unsigned int
bit_mod<unsigned int>(const unsigned int, const unsigned int);
// Instantiation of specialized templates emit no-effect warnings with Clang
/*
template
STDGPU_HOST_DEVICE unsigned int
bit_width<unsigned int>(const unsigned int number);
template
STDGPU_HOST_DEVICE unsigned long long int
bit_width<unsigned long long int>(const unsigned long long int);
template
STDGPU_HOST_DEVICE int
popcount<unsigned int>(const unsigned int number);
template
STDGPU_HOST_DEVICE int
popcount<unsigned long long int>(const unsigned long long int);
*/
template STDGPU_HOST_DEVICE std::int32_t
bit_cast<std::int32_t>(const float&);
} // namespace stdgpu
void
thread_has_single_bit_random(const stdgpu::index_t iterations, const std::unordered_set<std::size_t>& pow2_list)
{
// Generate true random numbers
std::size_t seed = test_utils::random_thread_seed();
std::default_random_engine rng(static_cast<std::default_random_engine::result_type>(seed));
std::uniform_int_distribution<std::size_t> dist(std::numeric_limits<std::size_t>::lowest(),
std::numeric_limits<std::size_t>::max());
for (stdgpu::index_t i = 0; i < iterations; ++i)
{
std::size_t number = dist(rng);
if (pow2_list.find(number) == pow2_list.end())
{
EXPECT_FALSE(stdgpu::has_single_bit(number));
}
}
}
TEST_F(stdgpu_bit, has_single_bit)
{
std::unordered_set<std::size_t> pow2_list;
for (std::size_t i = 0; i < std::numeric_limits<std::size_t>::digits; ++i)
{
std::size_t pow2_i = static_cast<std::size_t>(1) << i;
ASSERT_TRUE(stdgpu::has_single_bit(pow2_i));
pow2_list.insert(pow2_i);
}
const stdgpu::index_t iterations_per_thread = static_cast<stdgpu::index_t>(pow(2, 19));
test_utils::for_each_concurrent_thread(&thread_has_single_bit_random, iterations_per_thread, pow2_list);
}
void
thread_bit_ceil_random(const stdgpu::index_t iterations)
{
// Generate true random numbers
std::size_t seed = test_utils::random_thread_seed();
std::default_random_engine rng(static_cast<std::default_random_engine::result_type>(seed));
std::uniform_int_distribution<std::size_t> dist(
std::numeric_limits<std::size_t>::lowest(),
static_cast<std::size_t>(1) << static_cast<std::size_t>(std::numeric_limits<std::size_t>::digits - 1));
for (stdgpu::index_t i = 0; i < iterations; ++i)
{
std::size_t number = dist(rng);
std::size_t result = stdgpu::bit_ceil(number);
EXPECT_TRUE(stdgpu::has_single_bit(result));
EXPECT_GE(result, number);
EXPECT_LT(result / 2, number);
}
}
TEST_F(stdgpu_bit, bit_ceil_random)
{
const stdgpu::index_t iterations_per_thread = static_cast<stdgpu::index_t>(pow(2, 19));
test_utils::for_each_concurrent_thread(&thread_bit_ceil_random, iterations_per_thread);
}
TEST_F(stdgpu_bit, bit_ceil_zero)
{
EXPECT_EQ(stdgpu::bit_ceil(static_cast<std::size_t>(0)), static_cast<std::size_t>(1));
}
void
thread_bit_floor_random(const stdgpu::index_t iterations)
{
// Generate true random numbers
std::size_t seed = test_utils::random_thread_seed();
std::default_random_engine rng(static_cast<std::default_random_engine::result_type>(seed));
std::uniform_int_distribution<std::size_t> dist(std::numeric_limits<std::size_t>::lowest(),
std::numeric_limits<std::size_t>::max());
for (stdgpu::index_t i = 0; i < iterations; ++i)
{
std::size_t number = dist(rng);
std::size_t result = stdgpu::bit_floor(number);
EXPECT_TRUE(stdgpu::has_single_bit(result));
EXPECT_LE(result, number);
EXPECT_GT(result, number / 2);
}
}
TEST_F(stdgpu_bit, bit_floor_random)
{
const stdgpu::index_t iterations_per_thread = static_cast<stdgpu::index_t>(pow(2, 19));
test_utils::for_each_concurrent_thread(&thread_bit_floor_random, iterations_per_thread);
}
TEST_F(stdgpu_bit, bit_floor_zero)
{
EXPECT_EQ(stdgpu::bit_floor(static_cast<std::size_t>(0)), static_cast<std::size_t>(0));
}
void
thread_bit_mod_random(const stdgpu::index_t iterations, const std::size_t divider)
{
// Generate true random numbers
std::size_t seed = test_utils::random_thread_seed();
std::default_random_engine rng(static_cast<std::default_random_engine::result_type>(seed));
std::uniform_int_distribution<std::size_t> dist(std::numeric_limits<std::size_t>::lowest(),
std::numeric_limits<std::size_t>::max());
for (stdgpu::index_t i = 0; i < iterations; ++i)
{
std::size_t number = dist(rng);
EXPECT_EQ(stdgpu::bit_mod(number, divider), number % divider);
}
}
TEST_F(stdgpu_bit, bit_mod_random)
{
const std::size_t divider = static_cast<std::size_t>(pow(2, 21));
const stdgpu::index_t iterations_per_thread = static_cast<stdgpu::index_t>(pow(2, 19));
test_utils::for_each_concurrent_thread(&thread_bit_mod_random, iterations_per_thread, divider);
}
TEST_F(stdgpu_bit, bit_mod_one_positive)
{
const std::size_t number = 42;
const std::size_t divider = 1;
EXPECT_EQ(stdgpu::bit_mod(number, divider), static_cast<std::size_t>(0));
}
TEST_F(stdgpu_bit, bit_mod_one_zero)
{
const std::size_t number = 0;
const std::size_t divider = 1;
EXPECT_EQ(stdgpu::bit_mod(number, divider), static_cast<std::size_t>(0));
}
void
thread_bit_width_random(const stdgpu::index_t iterations)
{
// Generate true random numbers
std::size_t seed = test_utils::random_thread_seed();
std::default_random_engine rng(static_cast<std::default_random_engine::result_type>(seed));
std::uniform_int_distribution<std::size_t> dist(static_cast<std::size_t>(1),
std::numeric_limits<std::size_t>::max());
for (stdgpu::index_t i = 0; i < iterations; ++i)
{
std::size_t number = dist(rng);
std::size_t result = stdgpu::bit_width(number);
EXPECT_GT(result, static_cast<std::size_t>(0));
EXPECT_LE(result, static_cast<std::size_t>(std::numeric_limits<std::size_t>::digits));
if (result > 0)
{
std::size_t number_lower_bound = static_cast<std::size_t>(1) << (result - 1);
EXPECT_GE(number, number_lower_bound);
if (number < static_cast<std::size_t>(1)
<< static_cast<std::size_t>(std::numeric_limits<std::size_t>::digits - 1))
{
std::size_t number_upper_bound = static_cast<std::size_t>(1) << result;
EXPECT_LT(number, number_upper_bound);
}
}
}
}
TEST_F(stdgpu_bit, bit_width_random)
{
const stdgpu::index_t iterations_per_thread = static_cast<stdgpu::index_t>(pow(2, 19));
test_utils::for_each_concurrent_thread(&thread_bit_width_random, iterations_per_thread);
}
TEST_F(stdgpu_bit, bit_width_zero)
{
EXPECT_EQ(stdgpu::bit_width(static_cast<std::size_t>(0)), static_cast<std::size_t>(0));
}
TEST_F(stdgpu_bit, popcount_zero)
{
EXPECT_EQ(stdgpu::popcount(static_cast<std::size_t>(0)), 0);
}
TEST_F(stdgpu_bit, popcount_pow2)
{
for (int i = 0; i < std::numeric_limits<std::size_t>::digits; ++i)
{
EXPECT_EQ(stdgpu::popcount(static_cast<std::size_t>(1) << static_cast<std::size_t>(i)), 1);
}
}
TEST_F(stdgpu_bit, popcount_pow2m1)
{
for (int i = 0; i < std::numeric_limits<std::size_t>::digits; ++i)
{
EXPECT_EQ(stdgpu::popcount((static_cast<std::size_t>(1) << static_cast<std::size_t>(i)) -
static_cast<std::size_t>(1)),
i);
}
}
template <typename FloatTo, typename IntegerFrom>
void
thread_bit_cast_random(const stdgpu::index_t iterations)
{
// Generate true random numbers
std::size_t seed = test_utils::random_thread_seed();
std::default_random_engine rng(static_cast<std::default_random_engine::result_type>(seed));
std::uniform_int_distribution<IntegerFrom> dist(std::numeric_limits<IntegerFrom>::lowest(),
std::numeric_limits<IntegerFrom>::max());
for (stdgpu::index_t i = 0; i < iterations; ++i)
{
IntegerFrom number_int = dist(rng);
FloatTo number_float = stdgpu::bit_cast<FloatTo>(number_int);
IntegerFrom number_int_back = stdgpu::bit_cast<IntegerFrom>(number_float);
FloatTo number_float_back = stdgpu::bit_cast<FloatTo>(number_int_back);
EXPECT_EQ(number_int, number_int_back);
if (std::isnan(number_float))
{
EXPECT_TRUE(std::isnan(number_float_back));
}
else
{
EXPECT_DOUBLE_EQ(number_float, number_float_back);
}
}
}
TEST_F(stdgpu_bit, bit_cast_random_float_int32)
{
const stdgpu::index_t iterations_per_thread = static_cast<stdgpu::index_t>(pow(2, 19));
test_utils::for_each_concurrent_thread(&thread_bit_cast_random<float, std::int32_t>, iterations_per_thread);
}
TEST_F(stdgpu_bit, bit_cast_random_double_int64)
{
const stdgpu::index_t iterations_per_thread = static_cast<stdgpu::index_t>(pow(2, 19));
test_utils::for_each_concurrent_thread(&thread_bit_cast_random<double, std::int64_t>, iterations_per_thread);
}
|