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
|
// Copyright (c) Team CharLS.
// SPDX-License-Identifier: BSD-3-Clause
#include <benchmark/benchmark.h>
#include "../src/jpegls_preset_coding_parameters.h"
#include <cstdint>
#include <memory>
#include <vector>
#pragma warning(disable : 26409) // Avoid calling new explicitly (triggered by BENCHMARK macro)
int8_t quantize_gradient_org(const charls::jpegls_pc_parameters& preset, const int32_t di) noexcept
{
constexpr int32_t near_lossless{};
if (di <= -preset.threshold3)
return -4;
if (di <= -preset.threshold2)
return -3;
if (di <= -preset.threshold1)
return -2;
if (di < -near_lossless)
return -1;
if (di <= near_lossless)
return 0;
if (di < preset.threshold1)
return 1;
if (di < preset.threshold2)
return 2;
if (di < preset.threshold3)
return 3;
return 4;
}
std::vector<int8_t> create_quantize_lut_lossless(const int32_t bit_count)
{
const charls::jpegls_pc_parameters preset{charls::compute_default((1 << static_cast<uint32_t>(bit_count)) - 1, 0)};
const int32_t range{preset.maximum_sample_value + 1};
std::vector<int8_t> lut(static_cast<size_t>(range) * 2);
for (size_t i{}; i != lut.size(); ++i)
{
lut[i] = quantize_gradient_org(preset, static_cast<int32_t>(i) - range);
}
return lut;
}
const std::vector<int8_t> quantization_lut_lossless_8{create_quantize_lut_lossless(8)};
template<typename Traits>
struct scan_decoder
{
int32_t t1_{};
int32_t t2_{};
int32_t t3_{};
Traits traits_;
explicit scan_decoder(Traits traits, const int32_t bit_count) noexcept : traits_{std::move(traits)}
{
const charls::jpegls_pc_parameters preset{charls::compute_default((1 << static_cast<uint32_t>(bit_count)) - 1, 0)};
t1_ = preset.threshold1;
t2_ = preset.threshold2;
t3_ = preset.threshold3;
}
int8_t quantize_gradient_org(const int32_t di) const noexcept
{
if (di <= -t3_)
return -4;
if (di <= -t2_)
return -3;
if (di <= -t1_)
return -2;
if (di < -traits_.near_lossless)
return -1;
if (di <= traits_.near_lossless)
return 0;
if (di < t1_)
return 1;
if (di < t2_)
return 2;
if (di < t3_)
return 3;
return 4;
}
};
struct lossless_traits final
{
static constexpr int32_t near_lossless{};
};
__declspec(noinline) int32_t get_predicted_value_default(const int32_t ra, const int32_t rb, const int32_t rc) noexcept
{
if (ra < rb)
{
if (rc < ra)
return rb;
if (rc > rb)
return ra;
}
else
{
if (rc < rb)
return ra;
if (rc > ra)
return rb;
}
return ra + rb - rc;
}
constexpr size_t int32_t_bit_count = sizeof(int32_t) * 8;
constexpr int32_t bit_wise_sign(const int32_t i) noexcept
{
return i >> (int32_t_bit_count - 1);
}
__declspec(noinline) int32_t get_predicted_value_optimized(const int32_t ra, const int32_t rb, const int32_t rc) noexcept
{
// sign trick reduces the number of if statements (branches)
const int32_t sign{bit_wise_sign(rb - ra)};
// is Ra between Rc and Rb?
if ((sign ^ (rc - ra)) < 0)
{
return rb;
}
if ((sign ^ (rb - rc)) < 0)
{
return ra;
}
// default case, valid if Rc element of [Ra,Rb]
return ra + rb - rc;
}
#if defined(_M_X64) || defined(_M_ARM64)
inline int countl_zero(const uint64_t value) noexcept
{
if (value == 0)
return 64;
unsigned long index;
_BitScanReverse64(&index, value);
return 63 - static_cast<int>(index);
}
#endif
static void bm_get_predicted_value_default(benchmark::State& state)
{
for (const auto _ : state)
{
benchmark::DoNotOptimize(get_predicted_value_default(100, 200, 300));
benchmark::DoNotOptimize(get_predicted_value_default(200, 100, 300));
}
}
BENCHMARK(bm_get_predicted_value_default);
static void bm_get_predicted_value_optimized(benchmark::State& state)
{
for (const auto _ : state)
{
benchmark::DoNotOptimize(get_predicted_value_optimized(100, 200, 300));
benchmark::DoNotOptimize(get_predicted_value_default(200, 100, 300));
}
}
BENCHMARK(bm_get_predicted_value_optimized);
static void bm_quantize_gradient_calculated(benchmark::State& state)
{
const scan_decoder<lossless_traits> sd({}, 8);
for (const auto _ : state)
{
benchmark::DoNotOptimize(sd.quantize_gradient_org(0));
benchmark::DoNotOptimize(sd.quantize_gradient_org(127));
benchmark::DoNotOptimize(sd.quantize_gradient_org(255));
}
}
BENCHMARK(bm_quantize_gradient_calculated);
static void bm_quantize_gradient_lut(benchmark::State& state)
{
for (const auto _ : state)
{
benchmark::DoNotOptimize(quantization_lut_lossless_8[0]);
benchmark::DoNotOptimize(quantization_lut_lossless_8[127]);
benchmark::DoNotOptimize(quantization_lut_lossless_8[255]);
}
}
BENCHMARK(bm_quantize_gradient_lut);
int peek_zero_bits(uint64_t val_test) noexcept
{
for (int32_t count{}; count < 16; ++count)
{
if ((val_test & (uint64_t{1} << (64 - 1))) != 0)
return count;
val_test <<= 1;
}
return -1;
}
static void bm_peek_zero_bits(benchmark::State& state)
{
for (const auto _ : state)
{
benchmark::DoNotOptimize(peek_zero_bits(0));
benchmark::DoNotOptimize(peek_zero_bits(UINT64_MAX));
}
}
BENCHMARK(bm_peek_zero_bits);
#if defined(_M_X64) || defined(_M_ARM64)
int peek_zero_bits_intrinsic(const uint64_t value) noexcept
{
const auto count = countl_zero(value);
return count < 16 ? count : -1;
}
static void bm_peek_zero_bits_intrinsic(benchmark::State& state)
{
for (const auto _ : state)
{
benchmark::DoNotOptimize(peek_zero_bits_intrinsic(0));
benchmark::DoNotOptimize(peek_zero_bits_intrinsic(UINT64_MAX));
}
}
BENCHMARK(bm_peek_zero_bits_intrinsic);
#endif
std::vector<uint8_t> allocate_buffer(const size_t size)
{
std::vector<uint8_t> buffer;
buffer.resize(size);
return buffer;
}
static void bm_resize_vector(benchmark::State& state)
{
for (const auto _ : state)
{
benchmark::DoNotOptimize(allocate_buffer(size_t{512} * 512 * 16));
benchmark::DoNotOptimize(allocate_buffer(size_t{1024} * 1024 * 8 * 3));
}
}
BENCHMARK(bm_resize_vector);
class overwrite_buffer
{
public:
void reset(const size_t new_size)
{
if (new_size <= size_)
{
size_ = new_size;
return;
}
data_.reset(); // First release, then re-alloc new memory.
data_.reset(new uint8_t[new_size]);
size_ = new_size;
}
uint8_t* data() const noexcept
{
return data_.get();
}
size_t size() const noexcept
{
return size_;
}
private:
std::unique_ptr<uint8_t[]> data_{};
size_t size_{};
};
overwrite_buffer allocate_overwrite_buffer(const size_t size)
{
overwrite_buffer buffer;
buffer.reset(size);
return buffer;
}
static void bm_resize_overwrite_buffer(benchmark::State& state)
{
for (const auto _ : state)
{
benchmark::DoNotOptimize(allocate_buffer(size_t{512} * 512 * 16));
benchmark::DoNotOptimize(allocate_buffer(size_t{1024} * 1024 * 8 * 3));
}
}
BENCHMARK(bm_resize_overwrite_buffer);
BENCHMARK_MAIN();
|