reed_solomon_isa.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #pragma once
  2. #include "isa_erasure.h"
  3. #include "reed_solomon.h"
  4. extern "C" {
  5. #include <contrib/libs/isa-l/include/erasure_code.h>
  6. }
  7. #include <util/generic/array_ref.h>
  8. #include <array>
  9. namespace NErasure {
  10. template <int DataPartCount, int ParityPartCount, int WordSize, class TCodecTraits>
  11. class TReedSolomonIsa
  12. : public TReedSolomonBase<DataPartCount, ParityPartCount, WordSize, TCodecTraits>
  13. {
  14. static_assert(WordSize == 8, "ISA-l erasure codes support computations only in GF(2^8)");
  15. public:
  16. //! Main blob for storing data.
  17. using TBlobType = typename TCodecTraits::TBlobType;
  18. //! Main mutable blob for decoding data.
  19. using TMutableBlobType = typename TCodecTraits::TMutableBlobType;
  20. TReedSolomonIsa() {
  21. EncodeGFTables_.resize(DataPartCount * ParityPartCount * 32, 0);
  22. GeneratorMatrix_.resize((DataPartCount + ParityPartCount) * DataPartCount, 0);
  23. gf_gen_rs_matrix(
  24. GeneratorMatrix_.data(),
  25. DataPartCount + ParityPartCount,
  26. DataPartCount);
  27. ec_init_tables(
  28. DataPartCount,
  29. ParityPartCount,
  30. &GeneratorMatrix_.data()[DataPartCount * DataPartCount],
  31. EncodeGFTables_.data());
  32. }
  33. virtual std::vector<TBlobType> Encode(const std::vector<TBlobType>& blocks) const override {
  34. return ISAErasureEncode<DataPartCount, ParityPartCount, TCodecTraits, TBlobType, TMutableBlobType>(EncodeGFTables_, blocks);
  35. }
  36. virtual std::vector<TBlobType> Decode(
  37. const std::vector<TBlobType>& blocks,
  38. const TPartIndexList& erasedIndices) const override
  39. {
  40. if (erasedIndices.empty()) {
  41. return std::vector<TBlobType>();
  42. }
  43. return ISAErasureDecode<DataPartCount, ParityPartCount, TCodecTraits, TBlobType, TMutableBlobType>(
  44. blocks,
  45. erasedIndices,
  46. TConstArrayRef<TPartIndexList>(),
  47. GeneratorMatrix_);
  48. }
  49. virtual ~TReedSolomonIsa() = default;
  50. private:
  51. std::vector<unsigned char> GeneratorMatrix_;
  52. std::vector<unsigned char> EncodeGFTables_;
  53. };
  54. } // namespace NErasure