refactor(ricepp): split codec into encoder/decoder

This commit is contained in:
Marcus Holland-Moritz 2025-04-02 10:54:03 +02:00
parent d33385ae5f
commit 039926b01d
16 changed files with 619 additions and 332 deletions

View File

@ -78,7 +78,7 @@ foreach(target ${RICEPP_LIBS_CPUSPECIFIC})
list(APPEND RICEPP_OBJECT_TARGETS ${target})
endforeach()
add_library(ricepp-core OBJECT ricepp.cpp)
add_library(ricepp-core OBJECT ricepp.cpp cpu_variant.cpp)
# target_link_libraries(ricepp-core PUBLIC range-v3::range-v3)
target_include_directories(ricepp-core SYSTEM PUBLIC
$<BUILD_INTERFACE:$<TARGET_PROPERTY:range-v3::range-v3,INTERFACE_INCLUDE_DIRECTORIES>>

76
ricepp/cpu_variant.cpp Normal file
View File

@ -0,0 +1,76 @@
/* vim:set ts=2 sw=2 sts=2 et: */
/**
* \author Marcus Holland-Moritz (github@mhxnet.de)
* \copyright Copyright (c) Marcus Holland-Moritz
*
* This file is part of ricepp.
*
* ricepp is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ricepp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ricepp. If not, see <https://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <iostream>
#include "cpu_variant.h"
namespace ricepp::detail {
namespace {
detail::cpu_variant get_cpu_variant_init() {
#ifndef _WIN32
#if defined(__has_builtin)
#if __has_builtin(__builtin_cpu_supports) && \
(defined(RICEPP_CPU_BMI2) || defined(RICEPP_CPU_BMI2_AVX512))
__builtin_cpu_init();
bool const has_avx512vl = __builtin_cpu_supports("avx512vl");
bool const has_avx512vbmi = __builtin_cpu_supports("avx512vbmi");
bool const has_bmi2 = __builtin_cpu_supports("bmi2");
if (has_avx512vl && has_avx512vbmi && has_bmi2) {
return detail::cpu_variant::has_bmi2_avx512;
}
if (has_bmi2) {
return detail::cpu_variant::has_bmi2;
}
#endif
#endif
#endif
return detail::cpu_variant::fallback;
}
} // namespace
detail::cpu_variant get_cpu_variant() {
static detail::cpu_variant const variant = get_cpu_variant_init();
return variant;
}
void show_cpu_variant(std::string_view variant) {
if (std::getenv("RICEPP_SHOW_CPU_VARIANT")) {
std::cerr << "ricepp: using " << variant << " CPU variant\n";
}
}
void show_cpu_variant_once(std::string_view variant) {
static auto const _ = [&variant]() {
show_cpu_variant(variant);
return true;
}();
}
} // namespace ricepp::detail

38
ricepp/cpu_variant.h Normal file
View File

@ -0,0 +1,38 @@
/* vim:set ts=2 sw=2 sts=2 et: */
/**
* \author Marcus Holland-Moritz (github@mhxnet.de)
* \copyright Copyright (c) Marcus Holland-Moritz
*
* This file is part of ricepp.
*
* ricepp is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ricepp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ricepp. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <string_view>
namespace ricepp::detail {
enum class cpu_variant {
fallback,
has_bmi2,
has_bmi2_avx512,
};
cpu_variant get_cpu_variant();
void show_cpu_variant(std::string_view variant);
void show_cpu_variant_once(std::string_view variant);
} // namespace ricepp::detail

View File

@ -22,12 +22,7 @@
#pragma once
#include <bit>
#include <concepts>
#include <memory>
#include <span>
#include <vector>
#include <ricepp/codec_interface.h>
#include <cstddef>
namespace ricepp {
@ -38,8 +33,4 @@ struct codec_config {
unsigned unused_lsb_count;
};
template <std::unsigned_integral PixelT>
std::unique_ptr<codec_interface<PixelT>>
create_codec(codec_config const& config);
} // namespace ricepp

View File

@ -0,0 +1,36 @@
/* vim:set ts=2 sw=2 sts=2 et: */
/**
* \author Marcus Holland-Moritz (github@mhxnet.de)
* \copyright Copyright (c) Marcus Holland-Moritz
*
* This file is part of ricepp.
*
* ricepp is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ricepp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ricepp. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <concepts>
#include <memory>
#include <ricepp/codec_config.h>
#include <ricepp/decoder_interface.h>
namespace ricepp {
template <std::unsigned_integral PixelT>
std::unique_ptr<decoder_interface<PixelT>>
create_decoder(codec_config const& config);
} // namespace ricepp

View File

@ -0,0 +1,36 @@
/* vim:set ts=2 sw=2 sts=2 et: */
/**
* \author Marcus Holland-Moritz (github@mhxnet.de)
* \copyright Copyright (c) Marcus Holland-Moritz
*
* This file is part of ricepp.
*
* ricepp is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ricepp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ricepp. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <concepts>
#include <memory>
#include <ricepp/codec_config.h>
#include <ricepp/encoder_interface.h>
namespace ricepp {
template <std::unsigned_integral PixelT>
std::unique_ptr<encoder_interface<PixelT>>
create_encoder(codec_config const& config);
} // namespace ricepp

View File

@ -0,0 +1,41 @@
/* vim:set ts=2 sw=2 sts=2 et: */
/**
* \author Marcus Holland-Moritz (github@mhxnet.de)
* \copyright Copyright (c) Marcus Holland-Moritz
*
* This file is part of ricepp.
*
* ricepp is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ricepp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ricepp. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <concepts>
#include <cstdint>
#include <span>
namespace ricepp {
template <std::unsigned_integral PixelT>
class decoder_interface {
public:
using pixel_type = PixelT;
virtual ~decoder_interface() = default;
virtual void decode(std::span<pixel_type> output,
std::span<uint8_t const> input) const = 0;
};
} // namespace ricepp

View File

@ -22,17 +22,18 @@
#pragma once
#include <concepts>
#include <cstdint>
#include <span>
#include <vector>
namespace ricepp {
template <std::unsigned_integral PixelT>
class codec_interface {
class encoder_interface {
public:
using pixel_type = PixelT;
virtual ~codec_interface() = default;
virtual ~encoder_interface() = default;
[[nodiscard]] virtual std::vector<uint8_t>
encode(std::span<pixel_type const> input) const = 0;
@ -45,9 +46,6 @@ class codec_interface {
virtual std::span<uint8_t>
encode(std::span<uint8_t> output,
std::span<pixel_type const> input) const = 0;
virtual void decode(std::span<pixel_type> output,
std::span<uint8_t const> input) const = 0;
};
} // namespace ricepp

View File

@ -19,92 +19,25 @@
* along with ricepp. If not, see <https://www.gnu.org/licenses/>.
*/
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <stdexcept>
#include <string_view>
#include <ricepp/ricepp.h>
#include <ricepp/create_decoder.h>
#include <ricepp/create_encoder.h>
#include "ricepp_cpuspecific.h"
namespace ricepp {
namespace {
detail::cpu_variant get_cpu_variant_init() {
#ifndef _WIN32
#if defined(__has_builtin)
#if __has_builtin(__builtin_cpu_supports) && \
(defined(RICEPP_CPU_BMI2) || defined(RICEPP_CPU_BMI2_AVX512))
__builtin_cpu_init();
bool const has_avx512vl = __builtin_cpu_supports("avx512vl");
bool const has_avx512vbmi = __builtin_cpu_supports("avx512vbmi");
bool const has_bmi2 = __builtin_cpu_supports("bmi2");
if (has_avx512vl && has_avx512vbmi && has_bmi2) {
return detail::cpu_variant::has_bmi2_avx512;
}
if (has_bmi2) {
return detail::cpu_variant::has_bmi2;
}
#endif
#endif
#endif
return detail::cpu_variant::fallback;
template <>
std::unique_ptr<encoder_interface<uint16_t>>
create_encoder<uint16_t>(codec_config const& config) {
return detail::create_codec_cpuspecific<
encoder_interface, detail::encoder_cpuspecific_, uint16_t>(config);
}
detail::cpu_variant get_cpu_variant() {
static detail::cpu_variant const variant = get_cpu_variant_init();
return variant;
}
void show_cpu_variant(std::string_view variant) {
if (std::getenv("RICEPP_SHOW_CPU_VARIANT")) {
std::cerr << "ricepp: using " << variant << " CPU variant\n";
}
}
void show_cpu_variant_once(std::string_view variant) {
static auto const _ = [&variant]() {
show_cpu_variant(variant);
return true;
}();
}
} // namespace
template <>
std::unique_ptr<codec_interface<uint16_t>>
create_codec<uint16_t>(codec_config const& config) {
switch (get_cpu_variant()) {
#ifdef RICEPP_CPU_BMI2_AVX512
case detail::cpu_variant::has_bmi2_avx512:
show_cpu_variant_once("BMI2+AVX512");
return detail::create_codec_cpuspecific_<
uint16_t, detail::cpu_variant::has_bmi2_avx512>(config);
#endif
#ifdef RICEPP_CPU_BMI2
case detail::cpu_variant::has_bmi2:
show_cpu_variant_once("BMI2");
return detail::create_codec_cpuspecific_<uint16_t,
detail::cpu_variant::has_bmi2>(
config);
#endif
default:
show_cpu_variant_once("fallback");
return detail::create_codec_cpuspecific_<uint16_t,
detail::cpu_variant::fallback>(
config);
}
throw std::runtime_error("internal error: unknown CPU variant");
std::unique_ptr<decoder_interface<uint16_t>>
create_decoder<uint16_t>(codec_config const& config) {
return detail::create_codec_cpuspecific<
decoder_interface, detail::decoder_cpuspecific_, uint16_t>(config);
}
} // namespace ricepp

View File

@ -25,7 +25,8 @@
#include <benchmark/benchmark.h>
#include <ricepp/byteswap.h>
#include <ricepp/ricepp.h>
#include <ricepp/create_decoder.h>
#include <ricepp/create_encoder.h>
namespace {
@ -76,19 +77,23 @@ class ricepp_bm : public ::benchmark::Fixture {
.full_freq = 1.0 / state.range(5),
});
codec_ = ricepp::create_codec<uint16_t>({
auto config = ricepp::codec_config{
.block_size = static_cast<size_t>(state.range(6)),
.component_stream_count = static_cast<unsigned>(state.range(7)),
.byteorder = state.range(1) ? std::endian::big : std::endian::little,
.unused_lsb_count = static_cast<unsigned>(state.range(2)),
});
};
encoded_ = codec_->encode(data_);
encoder_ = ricepp::create_encoder<uint16_t>(config);
decoder_ = ricepp::create_decoder<uint16_t>(config);
encoded_ = encoder_->encode(data_);
}
void TearDown(::benchmark::State const&) {}
std::unique_ptr<ricepp::codec_interface<uint16_t>> codec_;
std::unique_ptr<ricepp::encoder_interface<uint16_t>> encoder_;
std::unique_ptr<ricepp::decoder_interface<uint16_t>> decoder_;
std::vector<uint16_t> data_;
std::vector<uint8_t> encoded_;
};
@ -119,9 +124,9 @@ void ricepp_params(benchmark::internal::Benchmark* b) {
BENCHMARK_DEFINE_F(ricepp_bm, encode)(::benchmark::State& state) {
std::vector<uint8_t> encoded;
encoded.resize(codec_->worst_case_encoded_bytes(data_));
encoded.resize(encoder_->worst_case_encoded_bytes(data_));
for (auto _ : state) {
auto r = codec_->encode(encoded, data_);
auto r = encoder_->encode(encoded, data_);
::benchmark::DoNotOptimize(r);
}
state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) *
@ -132,7 +137,7 @@ BENCHMARK_DEFINE_F(ricepp_bm, decode)(::benchmark::State& state) {
std::vector<uint16_t> decoded;
decoded.resize(data_.size());
for (auto _ : state) {
codec_->decode(decoded, encoded_);
decoder_->decode(decoded, encoded_);
}
state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) *
data_.size() * sizeof(data_[0]));

View File

@ -30,7 +30,8 @@
#include <benchmark/benchmark.h>
#include <ricepp/byteswap.h>
#include <ricepp/ricepp.h>
#include <ricepp/create_decoder.h>
#include <ricepp/create_encoder.h>
namespace {
@ -72,12 +73,15 @@ class ricepp_bm : public ::benchmark::Fixture {
auto const& camera_info = g_camera_info.at(camera);
codec_ = ricepp::create_codec<uint16_t>({
auto config = ricepp::codec_config{
.block_size = 128,
.component_stream_count = camera_info.component_stream_count,
.byteorder = std::endian::big,
.unused_lsb_count = camera_info.unused_lsb_count,
});
};
encoder_ = ricepp::create_encoder<uint16_t>(config);
decoder_ = ricepp::create_decoder<uint16_t>(config);
if (data_.empty()) {
std::filesystem::path testdata_dir;
@ -89,7 +93,7 @@ class ricepp_bm : public ::benchmark::Fixture {
data_ = load_fits_data(testdata_dir / camera / (test + ".fit"));
encoded_ = codec_->encode(data_);
encoded_ = encoder_->encode(data_);
}
latch_->count_down();
@ -124,7 +128,8 @@ class ricepp_bm : public ::benchmark::Fixture {
return data;
}
std::unique_ptr<ricepp::codec_interface<uint16_t>> codec_;
std::unique_ptr<ricepp::encoder_interface<uint16_t>> encoder_;
std::unique_ptr<ricepp::decoder_interface<uint16_t>> decoder_;
std::vector<uint16_t> data_;
std::vector<uint8_t> encoded_;
std::optional<std::latch> latch_{1};
@ -136,9 +141,9 @@ class ricepp_bm : public ::benchmark::Fixture {
BENCHMARK_DEFINE_F(ricepp_bm, encode_##camera##_##test) \
(::benchmark::State & state) { \
thread_local std::vector<uint8_t> encoded; \
encoded.resize(codec_->worst_case_encoded_bytes(data_)); \
encoded.resize(encoder_->worst_case_encoded_bytes(data_)); \
for (auto _ : state) { \
auto r = codec_->encode(encoded, data_); \
auto r = encoder_->encode(encoded, data_); \
::benchmark::DoNotOptimize(r); \
} \
state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) * \
@ -151,7 +156,7 @@ class ricepp_bm : public ::benchmark::Fixture {
thread_local std::vector<uint16_t> decoded; \
decoded.resize(data_.size()); \
for (auto _ : state) { \
codec_->decode(decoded, encoded_); \
decoder_->decode(decoded, encoded_); \
} \
state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) * \
data_.size() * sizeof(data_[0])); \

View File

@ -19,110 +19,27 @@
* along with ricepp. If not, see <https://www.gnu.org/licenses/>.
*/
#include <cassert>
#include <cstdint>
#include <iostream>
#include <ricepp/bitstream_reader.h>
#include <ricepp/bitstream_writer.h>
#include <ricepp/byteswap.h>
#include <ricepp/codec.h>
#include <ricepp/detail/compiler.h>
#include <ricepp/ricepp.h>
#include "ricepp_cpuspecific.h"
#include "ricepp_cpuspecific_traits.h"
namespace ricepp {
namespace ricepp::detail {
namespace {
template <std::unsigned_integral ValueType>
class dynamic_pixel_traits {
public:
using value_type = ValueType;
static constexpr size_t const kBitCount =
std::numeric_limits<value_type>::digits;
static constexpr value_type const kAllOnes =
std::numeric_limits<value_type>::max();
dynamic_pixel_traits(std::endian byteorder,
unsigned unused_lsb_count) noexcept
: unused_lsb_count_{unused_lsb_count}
, byteorder_{byteorder}
#ifndef NDEBUG
, lsb_mask_{static_cast<value_type>(~(kAllOnes << unused_lsb_count))}
, msb_mask_{static_cast<value_type>(~(kAllOnes >> unused_lsb_count))}
#endif
{
assert(unused_lsb_count < kBitCount);
}
[[nodiscard]] RICEPP_FORCE_INLINE value_type
read(value_type value) const noexcept {
value_type tmp = byteswap(value, byteorder_);
assert((tmp & lsb_mask_) == 0);
return tmp >> unused_lsb_count_;
}
[[nodiscard]] RICEPP_FORCE_INLINE value_type
write(value_type value) const noexcept {
assert((value & msb_mask_) == 0);
return byteswap(static_cast<value_type>(value << unused_lsb_count_),
byteorder_);
}
private:
unsigned const unused_lsb_count_;
std::endian const byteorder_;
#ifndef NDEBUG
value_type const lsb_mask_;
value_type const msb_mask_;
#endif
};
template <std::unsigned_integral ValueType, std::endian ByteOrder,
unsigned UnusedLsbCount>
class static_pixel_traits {
public:
using value_type = ValueType;
static constexpr size_t const kBitCount =
std::numeric_limits<value_type>::digits;
static constexpr value_type const kAllOnes =
std::numeric_limits<value_type>::max();
static constexpr std::endian const kByteOrder = ByteOrder;
static constexpr unsigned const kUnusedLsbCount = UnusedLsbCount;
static constexpr value_type const kLsbMask =
static_cast<value_type>(~(kAllOnes << kUnusedLsbCount));
static constexpr value_type const kMsbMask =
static_cast<value_type>(~(kAllOnes >> kUnusedLsbCount));
static_assert(kUnusedLsbCount < kBitCount);
[[nodiscard]] static RICEPP_FORCE_INLINE value_type
read(value_type value) noexcept {
value_type tmp = byteswap<kByteOrder>(value);
assert((tmp & kLsbMask) == 0);
return tmp >> kUnusedLsbCount;
}
[[nodiscard]] static RICEPP_FORCE_INLINE value_type
write(value_type value) noexcept {
assert((value & kMsbMask) == 0);
return byteswap<kByteOrder>(
static_cast<value_type>(value << kUnusedLsbCount));
}
};
template <size_t MaxBlockSize, size_t ComponentStreamCount,
typename PixelTraits>
class codec_impl final
: public codec_interface<typename PixelTraits::value_type>,
class encoder_impl final
: public encoder_interface<typename PixelTraits::value_type>,
public PixelTraits {
public:
using pixel_type = typename PixelTraits::value_type;
using codec_type = codec<MaxBlockSize, ComponentStreamCount, PixelTraits>;
using codec_type =
ricepp::codec<MaxBlockSize, ComponentStreamCount, PixelTraits>;
codec_impl(PixelTraits const& traits, size_t block_size)
encoder_impl(PixelTraits const& traits, size_t block_size)
: PixelTraits{traits}
, block_size_{block_size} {}
@ -147,11 +64,6 @@ class codec_impl final
input.size());
}
void decode(std::span<pixel_type> output,
std::span<uint8_t const> input) const override {
decode_impl(output.data(), output.size(), input.data(), input.size());
}
private:
size_t worst_case_encoded_bytes_impl(codec_type& codec, size_t size) const {
return (codec.worst_case_bit_count(size) + 8 - 1) / 8;
@ -169,12 +81,6 @@ class codec_impl final
std::span<pixel_type const>{input, input_size});
}
void decode_impl(pixel_type* __restrict output, size_t output_size,
uint8_t const* __restrict input, size_t input_size) const {
return decode_impl(std::span<pixel_type>{output, output_size},
std::span<uint8_t const>{input, input_size});
}
std::vector<uint8_t> encode_impl(std::span<pixel_type const> input) const {
std::vector<uint8_t> output;
codec_type codec{block_size_, *this};
@ -194,6 +100,35 @@ class codec_impl final
return std::span<uint8_t>{output.begin(), writer.iterator()};
}
private:
size_t const block_size_;
};
template <size_t MaxBlockSize, size_t ComponentStreamCount,
typename PixelTraits>
class decoder_impl final
: public decoder_interface<typename PixelTraits::value_type>,
public PixelTraits {
public:
using pixel_type = typename PixelTraits::value_type;
using codec_type = codec<MaxBlockSize, ComponentStreamCount, PixelTraits>;
decoder_impl(PixelTraits const& traits, size_t block_size)
: PixelTraits{traits}
, block_size_{block_size} {}
void decode(std::span<pixel_type> output,
std::span<uint8_t const> input) const override {
decode_impl(output.data(), output.size(), input.data(), input.size());
}
private:
void decode_impl(pixel_type* __restrict output, size_t output_size,
uint8_t const* __restrict input, size_t input_size) const {
return decode_impl(std::span<pixel_type>{output, output_size},
std::span<uint8_t const>{input, input_size});
}
void decode_impl(std::span<pixel_type> output,
std::span<uint8_t const> input) const {
bitstream_reader reader{input.begin(), input.end()};
@ -205,90 +140,30 @@ class codec_impl final
size_t const block_size_;
};
template <size_t ComponentStreamCount, typename PixelTraits>
std::unique_ptr<codec_interface<typename PixelTraits::value_type>>
create_codec_(size_t block_size, PixelTraits const& traits) {
if (block_size <= 512) {
return std::make_unique<codec_impl<512, ComponentStreamCount, PixelTraits>>(
traits, block_size);
}
return nullptr;
}
template <typename PixelTraits>
std::unique_ptr<codec_interface<typename PixelTraits::value_type>>
create_codec_(size_t block_size, size_t component_stream_count,
PixelTraits const& traits) {
switch (component_stream_count) {
case 1:
return create_codec_<1, PixelTraits>(block_size, traits);
case 2:
return create_codec_<2, PixelTraits>(block_size, traits);
default:
break;
}
return nullptr;
}
template <std::unsigned_integral PixelValueType, std::endian ByteOrder,
unsigned UnusedLsbCount>
std::unique_ptr<codec_interface<PixelValueType>>
create_codec_(size_t block_size, size_t component_stream_count) {
using pixel_traits =
static_pixel_traits<PixelValueType, ByteOrder, UnusedLsbCount>;
if (auto codec = create_codec_<pixel_traits>(
block_size, component_stream_count, pixel_traits{})) {
return codec;
}
return nullptr;
}
template <std::unsigned_integral PixelValueType>
std::unique_ptr<codec_interface<PixelValueType>>
create_codec_(codec_config const& config) {
if (config.byteorder == std::endian::big) {
switch (config.unused_lsb_count) {
case 0:
return create_codec_<PixelValueType, std::endian::big, 0>(
config.block_size, config.component_stream_count);
case 2:
return create_codec_<PixelValueType, std::endian::big, 2>(
config.block_size, config.component_stream_count);
case 4:
return create_codec_<PixelValueType, std::endian::big, 4>(
config.block_size, config.component_stream_count);
}
}
using pixel_traits = dynamic_pixel_traits<PixelValueType>;
return create_codec_<pixel_traits>(
config.block_size, config.component_stream_count,
pixel_traits{config.byteorder, config.unused_lsb_count});
}
} // namespace
namespace detail {
template <>
std::unique_ptr<codec_interface<uint16_t>>
create_codec_cpuspecific_<uint16_t, cpu_variant::RICEPP_CPU_VARIANT>(
std::unique_ptr<encoder_interface<uint16_t>>
encoder_cpuspecific_<uint16_t, cpu_variant::RICEPP_CPU_VARIANT>::create(
codec_config const& config) {
if (auto codec = create_codec_<uint16_t>(config)) {
return codec;
if (auto encoder =
create_codec_<encoder_interface, encoder_impl, uint16_t>(config)) {
return encoder;
}
throw std::runtime_error("Unsupported configuration");
}
} // namespace detail
} // namespace ricepp
template <>
std::unique_ptr<decoder_interface<uint16_t>>
decoder_cpuspecific_<uint16_t, cpu_variant::RICEPP_CPU_VARIANT>::create(
codec_config const& config) {
if (auto decoder =
create_codec_<decoder_interface, decoder_impl, uint16_t>(config)) {
return decoder;
}
throw std::runtime_error("Unsupported configuration");
}
} // namespace ricepp::detail

View File

@ -23,8 +23,13 @@
#include <concepts>
#include <memory>
#include <stdexcept>
#include <string_view>
#include <ricepp/codec_interface.h>
#include <ricepp/decoder_interface.h>
#include <ricepp/encoder_interface.h>
#include "cpu_variant.h"
namespace ricepp {
@ -32,15 +37,46 @@ struct codec_config;
namespace detail {
enum class cpu_variant {
fallback,
has_bmi2,
has_bmi2_avx512,
template <template <std::unsigned_integral> typename CodecInterface,
template <std::unsigned_integral, cpu_variant> typename CreateCodec,
std::unsigned_integral PixelValueType>
std::unique_ptr<CodecInterface<PixelValueType>>
create_codec_cpuspecific(codec_config const& config) {
switch (get_cpu_variant()) {
#ifdef RICEPP_CPU_BMI2_AVX512
case detail::cpu_variant::has_bmi2_avx512:
show_cpu_variant_once("BMI2+AVX512");
return CreateCodec<PixelValueType,
detail::cpu_variant::has_bmi2_avx512>::create(config);
#endif
#ifdef RICEPP_CPU_BMI2
case detail::cpu_variant::has_bmi2:
show_cpu_variant_once("BMI2");
return CreateCodec<PixelValueType, detail::cpu_variant::has_bmi2>::create(
config);
#endif
default:
show_cpu_variant_once("fallback");
return CreateCodec<PixelValueType, detail::cpu_variant::fallback>::create(
config);
}
throw std::runtime_error("internal error: unknown CPU variant");
}
template <std::unsigned_integral PixelT, cpu_variant CPU>
struct encoder_cpuspecific_ {
static std::unique_ptr<encoder_interface<PixelT>>
create(codec_config const& config);
};
template <std::unsigned_integral PixelT, cpu_variant CPU>
std::unique_ptr<codec_interface<PixelT>>
create_codec_cpuspecific_(codec_config const& config);
struct decoder_cpuspecific_ {
static std::unique_ptr<decoder_interface<PixelT>>
create(codec_config const& config);
};
} // namespace detail
} // namespace ricepp

View File

@ -0,0 +1,194 @@
/* vim:set ts=2 sw=2 sts=2 et: */
/**
* \author Marcus Holland-Moritz (github@mhxnet.de)
* \copyright Copyright (c) Marcus Holland-Moritz
*
* This file is part of ricepp.
*
* ricepp is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ricepp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ricepp. If not, see <https://www.gnu.org/licenses/>.
*/
#include <cassert>
#include <cstdint>
#include <iostream>
#include <ricepp/byteswap.h>
#include <ricepp/codec_config.h>
#include <ricepp/detail/compiler.h>
#include "ricepp_cpuspecific.h"
namespace ricepp::detail {
template <std::unsigned_integral ValueType>
class dynamic_pixel_traits {
public:
using value_type = ValueType;
static constexpr size_t const kBitCount =
std::numeric_limits<value_type>::digits;
static constexpr value_type const kAllOnes =
std::numeric_limits<value_type>::max();
dynamic_pixel_traits(std::endian byteorder,
unsigned unused_lsb_count) noexcept
: unused_lsb_count_{unused_lsb_count}
, byteorder_{byteorder}
#ifndef NDEBUG
, lsb_mask_{static_cast<value_type>(~(kAllOnes << unused_lsb_count))}
, msb_mask_{static_cast<value_type>(~(kAllOnes >> unused_lsb_count))}
#endif
{
assert(unused_lsb_count < kBitCount);
}
[[nodiscard]] RICEPP_FORCE_INLINE value_type
read(value_type value) const noexcept {
value_type tmp = ricepp::byteswap(value, byteorder_);
assert((tmp & lsb_mask_) == 0);
return tmp >> unused_lsb_count_;
}
[[nodiscard]] RICEPP_FORCE_INLINE value_type
write(value_type value) const noexcept {
assert((value & msb_mask_) == 0);
return ricepp::byteswap(static_cast<value_type>(value << unused_lsb_count_),
byteorder_);
}
private:
unsigned const unused_lsb_count_;
std::endian const byteorder_;
#ifndef NDEBUG
value_type const lsb_mask_;
value_type const msb_mask_;
#endif
};
template <std::unsigned_integral ValueType, std::endian ByteOrder,
unsigned UnusedLsbCount>
class static_pixel_traits {
public:
using value_type = ValueType;
static constexpr size_t const kBitCount =
std::numeric_limits<value_type>::digits;
static constexpr value_type const kAllOnes =
std::numeric_limits<value_type>::max();
static constexpr std::endian const kByteOrder = ByteOrder;
static constexpr unsigned const kUnusedLsbCount = UnusedLsbCount;
static constexpr value_type const kLsbMask =
static_cast<value_type>(~(kAllOnes << kUnusedLsbCount));
static constexpr value_type const kMsbMask =
static_cast<value_type>(~(kAllOnes >> kUnusedLsbCount));
static_assert(kUnusedLsbCount < kBitCount);
[[nodiscard]] static RICEPP_FORCE_INLINE value_type
read(value_type value) noexcept {
value_type tmp = ricepp::byteswap<kByteOrder>(value);
assert((tmp & kLsbMask) == 0);
return tmp >> kUnusedLsbCount;
}
[[nodiscard]] static RICEPP_FORCE_INLINE value_type
write(value_type value) noexcept {
assert((value & kMsbMask) == 0);
return ricepp::byteswap<kByteOrder>(
static_cast<value_type>(value << kUnusedLsbCount));
}
};
template <template <std::unsigned_integral> typename CodecInterface,
template <size_t, size_t, typename> typename CodecImpl,
size_t ComponentStreamCount, typename PixelTraits>
std::unique_ptr<CodecInterface<typename PixelTraits::value_type>>
create_codec_(size_t block_size, PixelTraits const& traits) {
if (block_size <= 512) {
return std::make_unique<CodecImpl<512, ComponentStreamCount, PixelTraits>>(
traits, block_size);
}
return nullptr;
}
template <template <std::unsigned_integral> typename CodecInterface,
template <size_t, size_t, typename> typename CodecImpl,
typename PixelTraits>
std::unique_ptr<CodecInterface<typename PixelTraits::value_type>>
create_codec_(size_t block_size, size_t component_stream_count,
PixelTraits const& traits) {
switch (component_stream_count) {
case 1:
return create_codec_<CodecInterface, CodecImpl, 1, PixelTraits>(block_size,
traits);
case 2:
return create_codec_<CodecInterface, CodecImpl, 2, PixelTraits>(block_size,
traits);
default:
break;
}
return nullptr;
}
template <template <std::unsigned_integral> typename CodecInterface,
template <size_t, size_t, typename> typename CodecImpl,
std::unsigned_integral PixelValueType, std::endian ByteOrder,
unsigned UnusedLsbCount>
std::unique_ptr<CodecInterface<PixelValueType>>
create_codec_(size_t block_size, size_t component_stream_count) {
using pixel_traits =
static_pixel_traits<PixelValueType, ByteOrder, UnusedLsbCount>;
if (auto codec = create_codec_<CodecInterface, CodecImpl, pixel_traits>(
block_size, component_stream_count, pixel_traits{})) {
return codec;
}
return nullptr;
}
template <template <std::unsigned_integral> typename CodecInterface,
template <size_t, size_t, typename> typename CodecImpl,
std::unsigned_integral PixelValueType>
std::unique_ptr<CodecInterface<PixelValueType>>
create_codec_(codec_config const& config) {
if (config.byteorder == std::endian::big) {
switch (config.unused_lsb_count) {
case 0:
return create_codec_<CodecInterface, CodecImpl, PixelValueType,
std::endian::big, 0>(config.block_size,
config.component_stream_count);
case 2:
return create_codec_<CodecInterface, CodecImpl, PixelValueType,
std::endian::big, 2>(config.block_size,
config.component_stream_count);
case 4:
return create_codec_<CodecInterface, CodecImpl, PixelValueType,
std::endian::big, 4>(config.block_size,
config.component_stream_count);
}
}
using pixel_traits = dynamic_pixel_traits<PixelValueType>;
return create_codec_<CodecInterface, CodecImpl, pixel_traits>(
config.block_size, config.component_stream_count,
pixel_traits{config.byteorder, config.unused_lsb_count});
}
} // namespace ricepp::detail

View File

@ -28,7 +28,8 @@
#include <gtest/gtest.h>
#include <ricepp/byteswap.h>
#include <ricepp/ricepp.h>
#include <ricepp/create_decoder.h>
#include <ricepp/create_encoder.h>
namespace {
@ -55,46 +56,56 @@ generate_random_data(size_t count, unsigned unused_lsb_count = 0,
} // namespace
TEST(ricepp, codec_basic_test) {
auto codec = ricepp::create_codec<uint16_t>({
auto config = ricepp::codec_config{
.block_size = 16,
.component_stream_count = 1,
.byteorder = std::endian::big,
.unused_lsb_count = 0,
});
};
auto encoder = ricepp::create_encoder<uint16_t>(config);
auto data = generate_random_data<uint16_t>(12345);
auto encoded = codec->encode(data);
auto encoded = encoder->encode(data);
auto decoder = ricepp::create_decoder<uint16_t>(config);
std::vector<uint16_t> decoded(data.size());
codec->decode(decoded, encoded);
decoder->decode(decoded, encoded);
EXPECT_THAT(decoded, ::testing::ContainerEq(data));
}
TEST(ricepp, codec_unused_lsb_test) {
auto codec = ricepp::create_codec<uint16_t>({
auto config = ricepp::codec_config{
.block_size = 13, // because why not?
.component_stream_count = 1,
.byteorder = std::endian::big,
.unused_lsb_count = 4,
});
};
auto encoder = ricepp::create_encoder<uint16_t>(config);
auto data = generate_random_data<uint16_t>(4321, 4);
auto encoded = codec->encode(data);
auto encoded = encoder->encode(data);
auto decoder = ricepp::create_decoder<uint16_t>(config);
std::vector<uint16_t> decoded(data.size());
codec->decode(decoded, encoded);
decoder->decode(decoded, encoded);
EXPECT_THAT(decoded, ::testing::ContainerEq(data));
}
TEST(ricepp, codec_mixed_data_test) {
auto codec = ricepp::create_codec<uint16_t>({
auto config = ricepp::codec_config{
.block_size = 32,
.component_stream_count = 1,
.byteorder = std::endian::big,
.unused_lsb_count = 0,
});
};
auto encoder = ricepp::create_encoder<uint16_t>(config);
auto data1 = generate_random_data<uint16_t>(500, 0);
auto data2 = std::vector<uint16_t>(500, 25000);
@ -102,59 +113,69 @@ TEST(ricepp, codec_mixed_data_test) {
auto data = ranges::views::concat(data1, data2, data3) | ranges::to_vector;
auto encoded = codec->encode(data);
auto encoded = encoder->encode(data);
auto decoder = ricepp::create_decoder<uint16_t>(config);
std::vector<uint16_t> decoded(data.size());
codec->decode(decoded, encoded);
decoder->decode(decoded, encoded);
EXPECT_THAT(decoded, ::testing::ContainerEq(data));
}
TEST(ricepp, codec_multi_component_test) {
auto codec = ricepp::create_codec<uint16_t>({
auto config = ricepp::codec_config{
.block_size = 29,
.component_stream_count = 2,
.byteorder = std::endian::big,
.unused_lsb_count = 2,
});
};
auto encoder = ricepp::create_encoder<uint16_t>(config);
auto data = generate_random_data<uint16_t>(23456, 2);
auto encoded = codec->encode(data);
auto encoded = encoder->encode(data);
auto decoder = ricepp::create_decoder<uint16_t>(config);
std::vector<uint16_t> decoded(data.size());
codec->decode(decoded, encoded);
decoder->decode(decoded, encoded);
EXPECT_THAT(decoded, ::testing::ContainerEq(data));
}
TEST(ricepp, codec_preallocated_buffer_test) {
auto codec = ricepp::create_codec<uint16_t>({
auto config = ricepp::codec_config{
.block_size = 29,
.component_stream_count = 1,
.byteorder = std::endian::big,
.unused_lsb_count = 0,
});
};
auto encoder = ricepp::create_encoder<uint16_t>(config);
static constexpr size_t const kDataLen = 14443;
auto data = generate_random_data<uint16_t>(kDataLen, 0, std::endian::big, 0);
auto worst_case_bytes = codec->worst_case_encoded_bytes(data);
auto worst_case_bytes = encoder->worst_case_encoded_bytes(data);
static constexpr size_t const kWorstCaseBytes = 29138;
EXPECT_EQ(kWorstCaseBytes, worst_case_bytes);
std::vector<uint8_t> encoded(worst_case_bytes);
auto span = codec->encode(encoded, data);
auto span = encoder->encode(encoded, data);
EXPECT_EQ(kWorstCaseBytes, span.size());
encoded.resize(span.size());
encoded.shrink_to_fit();
auto decoder = ricepp::create_decoder<uint16_t>(config);
std::vector<uint16_t> decoded(data.size());
codec->decode(decoded, encoded);
decoder->decode(decoded, encoded);
EXPECT_THAT(decoded, ::testing::ContainerEq(data));
}
TEST(ricepp, codec_worst_case_bytes_test) {
auto codec = ricepp::create_codec<uint16_t>({
TEST(ricepp, encoder_worst_case_bytes_test) {
auto encoder = ricepp::create_encoder<uint16_t>({
.block_size = 29,
.component_stream_count = 2,
.byteorder = std::endian::big,
@ -162,7 +183,7 @@ TEST(ricepp, codec_worst_case_bytes_test) {
});
static constexpr size_t const kDataLen = 28886;
auto worst_case_bytes = codec->worst_case_encoded_bytes(kDataLen);
auto worst_case_bytes = encoder->worst_case_encoded_bytes(kDataLen);
static constexpr size_t const kWorstCaseBytes = 58275;
EXPECT_EQ(kWorstCaseBytes, worst_case_bytes);
}
@ -170,7 +191,7 @@ TEST(ricepp, codec_worst_case_bytes_test) {
TEST(ricepp, codec_error_test) {
EXPECT_THAT(
[] {
auto codec = ricepp::create_codec<uint16_t>({
auto encoder = ricepp::create_encoder<uint16_t>({
.block_size = 513,
.component_stream_count = 2,
.byteorder = std::endian::big,
@ -182,7 +203,7 @@ TEST(ricepp, codec_error_test) {
EXPECT_THAT(
[] {
auto codec = ricepp::create_codec<uint16_t>({
auto decoder = ricepp::create_decoder<uint16_t>({
.block_size = 128,
.component_stream_count = 3,
.byteorder = std::endian::big,

View File

@ -25,7 +25,8 @@
#include <nlohmann/json.hpp>
#include <ricepp/ricepp.h>
#include <ricepp/create_decoder.h>
#include <ricepp/create_encoder.h>
#include <dwarfs/compressor_registry.h>
#include <dwarfs/decompressor_registry.h>
@ -84,7 +85,7 @@ class ricepp_block_compressor final : public block_compressor::impl {
auto byteorder =
endianness == "big" ? std::endian::big : std::endian::little;
auto codec = ricepp::create_codec<pixel_type>({
auto encoder = ricepp::create_encoder<pixel_type>({
.block_size = block_size_,
.component_stream_count = static_cast<size_t>(component_count),
.byteorder = byteorder,
@ -124,9 +125,10 @@ class ricepp_block_compressor final : public block_compressor::impl {
data.size() / bytes_per_sample};
size_t header_size = compressed.size();
compressed.resize(header_size + codec->worst_case_encoded_bytes(input));
compressed.resize(header_size + encoder->worst_case_encoded_bytes(input));
auto output = codec->encode(compressed.span().subspan(header_size), input);
auto output =
encoder->encode(compressed.span().subspan(header_size), input);
compressed.resize(header_size + output.size());
compressed.shrink_to_fit();
@ -174,7 +176,7 @@ class ricepp_block_decompressor final : public block_decompressor_base {
: uncompressed_size_{varint::decode(data)}
, header_{decode_header(data)}
, data_{data}
, codec_{ricepp::create_codec<uint16_t>(
, decoder_{ricepp::create_decoder<uint16_t>(
{.block_size = header_.block_size().value(),
.component_stream_count = header_.component_count().value(),
.byteorder = header_.big_endian().value() ? std::endian::big
@ -202,7 +204,7 @@ class ricepp_block_decompressor final : public block_decompressor_base {
bool decompress_frame(size_t) override {
DWARFS_CHECK(decompressed_, "decompression not started");
if (!codec_) {
if (!decoder_) {
return false;
}
@ -211,9 +213,9 @@ class ricepp_block_decompressor final : public block_decompressor_base {
reinterpret_cast<uint16_t*>(decompressed_.data()),
decompressed_.size() / 2};
codec_->decode(output, data_);
decoder_->decode(output, data_);
codec_.reset();
decoder_.reset();
return true;
}
@ -239,7 +241,7 @@ class ricepp_block_decompressor final : public block_decompressor_base {
size_t const uncompressed_size_;
thrift::compression::ricepp_block_header const header_;
std::span<uint8_t const> data_;
std::unique_ptr<ricepp::codec_interface<uint16_t>> codec_;
std::unique_ptr<ricepp::decoder_interface<uint16_t>> decoder_;
};
template <typename Base>