Skip to content
Parameters.hpp 4.16 KiB
Newer Older
Luker's avatar
Luker committed
/*
Luker's avatar
Luker committed
 * Copyright (c) 2015-2016, Luca Fulchir<luca@fulchir.it>, All rights reserved.
Luker's avatar
Luker committed
 *
 * This file is part of "libRaptorQ".
 *
 * libRaptorQ is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3
 * of the License, or (at your option) any later version.
 *
 * libRaptorQ 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
 * and a copy of the GNU Lesser General Public License
 * along with libRaptorQ.  If not, see <http://www.gnu.org/licenses/>.
 */

Luker's avatar
Luker committed
#pragma once
Luker's avatar
Luker committed

#include "RaptorQ/v1/common.hpp"
#include "RaptorQ/v1/multiplication.hpp"
#include "RaptorQ/v1/table2.hpp"
Luker's avatar
Luker committed
#include <cmath>
#include <Eigen/Core>
#include <vector>

Luker's avatar
Luker committed
namespace RaptorQ__v1 {
Luker's avatar
Luker committed
namespace Impl {

class RAPTORQ_LOCAL Tuple
{
Luker's avatar
Luker committed
    // d  1-30  LT-degree of encoded symbol
    // a  0-(W-1)
    // b  0-(W-1)
    // d1 PI-degree of encoded symbol (2 or 3)
    // a1 0-(P1-1)
    // b1 0-(P1-1)
Luker's avatar
Luker committed
public:
Luker's avatar
Luker committed
    uint16_t d, a, b, d1, a1, b1;   // great names. thanks rfc6330!
Luker's avatar
Luker committed
};

class RAPTORQ_API Parameters
{
public:
Luker's avatar
Luker committed
    explicit Parameters (const uint16_t symbols);
    uint16_t Deg (const uint32_t v) const;
    Tuple tuple (const uint32_t ISI) const;
    std::vector<uint16_t> get_idxs (const uint32_t ISI) const;
Luker's avatar
Luker committed

Luker's avatar
Luker committed
    uint16_t K_padded, S, H, W, L, P, P1, U, B; // RFC 6330, pg 22
    uint16_t J;
Luker's avatar
Luker committed
private:
Luker's avatar
Luker committed
    static bool is_prime (const uint16_t n);
Luker's avatar
Luker committed
};

Luker's avatar
Luker committed
class RAPTORQ_LOCAL Octet
Luker's avatar
Luker committed
{
public:
Luker's avatar
Luker committed
    Octet () {}
    Octet (const uint8_t val) : data(val) {}

    explicit operator uint8_t() const { return data; }

    Octet& operator-= (const Octet a)
    {
        data ^= a.data;
        return *this;
    }
    friend Octet operator- (Octet lhs, const Octet rhs)
    {
        lhs.data ^= rhs.data;
        return lhs;
    }
    Octet& operator+= (const Octet a)
    {
        data ^= a.data;
        return *this;
    }
    friend Octet operator+ (Octet lhs, const Octet rhs)
    {
        lhs.data ^= rhs.data;
        return lhs;
    }
    // xor, addition, subtraction... they're the same to me...
    Octet& operator^= (const Octet &a)
    {
        data ^= a.data;
        return *this;
    }
    friend Octet operator^ (Octet lhs, const Octet rhs)
    {
        lhs.data ^= rhs.data;
        return lhs;
    }
    Octet& operator*= (const Octet a)
    {
        if (data != 0 && a.data != 0) {
            data = RaptorQ__v1::Impl::oct_exp[oct_log[data - 1] +
                                            oct_log[a.data - 1]];
        } else {
            data = 0;
        }
        return *this;
    }
    friend Octet operator* (Octet lhs, const Octet rhs)
    {
        if (lhs.data != 0 && rhs.data != 0) {
            lhs.data = RaptorQ__v1::Impl::oct_exp[oct_log[lhs.data - 1] +
                                                        oct_log[rhs.data - 1]];
        } else {
            lhs.data = 0;
        }
        return lhs;
    }
    Octet& operator/= (const Octet a)
    {
        if (a.data != 0 && data != 0) {
            data = RaptorQ__v1::Impl::oct_exp[oct_log[data - 1] -
                                                    oct_log[a.data - 1] + 255];
        }
        return *this;
    }

    friend Octet operator/ (Octet lhs, const Octet rhs)
    {
        lhs /= rhs;
        return lhs;
    }

    Octet inverse() const
    {
        return Octet (RaptorQ__v1::Impl::oct_exp[255 - oct_log[data - 1]]);
    }
Luker's avatar
Luker committed

    bool operator== (const Octet a) const
Luker's avatar
Luker committed
    { return data == a.data; }
    bool operator!= (const Octet a) const
Luker's avatar
Luker committed
    { return data != a.data; }
Luker's avatar
Luker committed
    friend std::ostream &operator<< (std::ostream &os, const Octet m) {
        // used to print
        os << static_cast<uint32_t> (m.data);
        return os;
    }
Luker's avatar
Luker committed
private:
Luker's avatar
Luker committed
    uint8_t data;
Luker's avatar
Luker committed
};

Luker's avatar
Luker committed
inline uint8_t abs (Octet x) { return static_cast<uint8_t> (x); }

Luker's avatar
Luker committed
}   // namespace Impl
}   // namespace RaptorQ
Luker's avatar
Luker committed

namespace Eigen {
template<>
Luker's avatar
Luker committed
struct NumTraits<RaptorQ__v1::Impl::Octet> : NumTraits<uint8_t> {};
Luker's avatar
Luker committed
}