aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/erasure/reed_solomon_isa.h
blob: 73a3cd630f07437c43459252196334c5a4d9cb64 (plain) (blame)
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
#pragma once

#include "isa_erasure.h"
#include "reed_solomon.h"

extern "C" {
    #include <contrib/libs/isa-l/include/erasure_code.h>
}

#include <util/generic/array_ref.h>

#include <array>

namespace NErasure {

template <int DataPartCount, int ParityPartCount, int WordSize, class TCodecTraits>
class TReedSolomonIsa
    : public TReedSolomonBase<DataPartCount, ParityPartCount, WordSize, TCodecTraits>
{
    static_assert(WordSize == 8, "ISA-l erasure codes support computations only in GF(2^8)");
public:
    //! Main blob for storing data.
    using TBlobType = typename TCodecTraits::TBlobType;
    //! Main mutable blob for decoding data.
    using TMutableBlobType = typename TCodecTraits::TMutableBlobType;

    TReedSolomonIsa() {
        EncodeGFTables_.resize(DataPartCount * ParityPartCount * 32, 0);
        GeneratorMatrix_.resize((DataPartCount + ParityPartCount) * DataPartCount, 0);

        gf_gen_rs_matrix(
            GeneratorMatrix_.data(),
            DataPartCount + ParityPartCount,
            DataPartCount);

        ec_init_tables(
            DataPartCount,
            ParityPartCount,
            &GeneratorMatrix_.data()[DataPartCount * DataPartCount],
            EncodeGFTables_.data());
    }

    virtual std::vector<TBlobType> Encode(const std::vector<TBlobType>& blocks) const override {
        return ISAErasureEncode<DataPartCount, ParityPartCount, TCodecTraits, TBlobType, TMutableBlobType>(EncodeGFTables_, blocks);
    }

    virtual std::vector<TBlobType> Decode(
        const std::vector<TBlobType>& blocks,
        const TPartIndexList& erasedIndices) const override
    {
        if (erasedIndices.empty()) {
            return std::vector<TBlobType>();
        }

        return ISAErasureDecode<DataPartCount, ParityPartCount, TCodecTraits, TBlobType, TMutableBlobType>(
            blocks,
            erasedIndices,
            TConstArrayRef<TPartIndexList>(),
            GeneratorMatrix_);
    }

    virtual ~TReedSolomonIsa() = default;

private:
    std::vector<unsigned char> GeneratorMatrix_;
    std::vector<unsigned char> EncodeGFTables_;
};

} // namespace NErasure