|
| 1 | +//===- Polynomial.h - A storage class for polynomial types --------*- C++-*-==// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#ifndef INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIAL_H_ |
| 10 | +#define INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIAL_H_ |
| 11 | + |
| 12 | +#include <utility> |
| 13 | + |
| 14 | +#include "mlir/Support/LLVM.h" |
| 15 | +#include "llvm/ADT/APInt.h" |
| 16 | +#include "llvm/ADT/DenseMapInfo.h" |
| 17 | +#include "llvm/ADT/Hashing.h" |
| 18 | + |
| 19 | +namespace mlir { |
| 20 | + |
| 21 | +class MLIRContext; |
| 22 | + |
| 23 | +namespace polynomial { |
| 24 | + |
| 25 | +// This restricts statically defined polynomials to have at most 64-bit |
| 26 | +// coefficients. This may be relaxed in the future, but it seems unlikely one |
| 27 | +// would want to specify 128-bit polynomials statically in the source code. |
| 28 | +constexpr unsigned apintBitWidth = 64; |
| 29 | + |
| 30 | +namespace detail { |
| 31 | +struct PolynomialStorage; |
| 32 | +} // namespace detail |
| 33 | + |
| 34 | +class Monomial { |
| 35 | +public: |
| 36 | + Monomial(int64_t coeff, uint64_t expo) |
| 37 | + : coefficient(apintBitWidth, coeff), exponent(apintBitWidth, expo) {} |
| 38 | + |
| 39 | + Monomial(APInt coeff, APInt expo) |
| 40 | + : coefficient(std::move(coeff)), exponent(std::move(expo)) {} |
| 41 | + |
| 42 | + Monomial() : coefficient(apintBitWidth, 0), exponent(apintBitWidth, 0) {} |
| 43 | + |
| 44 | + bool operator==(const Monomial &other) const { |
| 45 | + return other.coefficient == coefficient && other.exponent == exponent; |
| 46 | + } |
| 47 | + bool operator!=(const Monomial &other) const { |
| 48 | + return other.coefficient != coefficient || other.exponent != exponent; |
| 49 | + } |
| 50 | + |
| 51 | + /// Monomials are ordered by exponent. |
| 52 | + bool operator<(const Monomial &other) const { |
| 53 | + return (exponent.ult(other.exponent)); |
| 54 | + } |
| 55 | + |
| 56 | + // Prints polynomial to 'os'. |
| 57 | + void print(raw_ostream &os) const; |
| 58 | + |
| 59 | + friend ::llvm::hash_code hash_value(Monomial arg); |
| 60 | + |
| 61 | +public: |
| 62 | + APInt coefficient; |
| 63 | + |
| 64 | + // Always unsigned |
| 65 | + APInt exponent; |
| 66 | +}; |
| 67 | + |
| 68 | +/// A single-variable polynomial with integer coefficients. Polynomials are |
| 69 | +/// immutable and uniqued. |
| 70 | +/// |
| 71 | +/// Eg: x^1024 + x + 1 |
| 72 | +/// |
| 73 | +/// The symbols used as the polynomial's indeterminate don't matter, so long as |
| 74 | +/// it is used consistently throughout the polynomial. |
| 75 | +class Polynomial { |
| 76 | +public: |
| 77 | + using ImplType = detail::PolynomialStorage; |
| 78 | + |
| 79 | + constexpr Polynomial() = default; |
| 80 | + explicit Polynomial(ImplType *terms) : terms(terms) {} |
| 81 | + |
| 82 | + static Polynomial fromMonomials(ArrayRef<Monomial> monomials, |
| 83 | + MLIRContext *context); |
| 84 | + /// Returns a polynomial with coefficients given by `coeffs` |
| 85 | + static Polynomial fromCoefficients(ArrayRef<int64_t> coeffs, |
| 86 | + MLIRContext *context); |
| 87 | + |
| 88 | + MLIRContext *getContext() const; |
| 89 | + |
| 90 | + explicit operator bool() const { return terms != nullptr; } |
| 91 | + bool operator==(Polynomial other) const { return other.terms == terms; } |
| 92 | + bool operator!=(Polynomial other) const { return !(other.terms == terms); } |
| 93 | + |
| 94 | + // Prints polynomial to 'os'. |
| 95 | + void print(raw_ostream &os) const; |
| 96 | + void print(raw_ostream &os, const std::string &separator, |
| 97 | + const std::string &exponentiation) const; |
| 98 | + void dump() const; |
| 99 | + |
| 100 | + // Prints polynomial so that it can be used as a valid identifier |
| 101 | + std::string toIdentifier() const; |
| 102 | + |
| 103 | + // A polynomial's terms are canonically stored in order of increasing degree. |
| 104 | + ArrayRef<Monomial> getTerms() const; |
| 105 | + |
| 106 | + unsigned getDegree() const; |
| 107 | + |
| 108 | + friend ::llvm::hash_code hash_value(Polynomial arg); |
| 109 | + |
| 110 | +private: |
| 111 | + ImplType *terms{nullptr}; |
| 112 | +}; |
| 113 | + |
| 114 | +// Make Polynomial hashable. |
| 115 | +inline ::llvm::hash_code hash_value(Polynomial arg) { |
| 116 | + return ::llvm::hash_value(arg.terms); |
| 117 | +} |
| 118 | + |
| 119 | +inline ::llvm::hash_code hash_value(Monomial arg) { |
| 120 | + return ::llvm::hash_value(arg.coefficient) ^ ::llvm::hash_value(arg.exponent); |
| 121 | +} |
| 122 | + |
| 123 | +inline raw_ostream &operator<<(raw_ostream &os, Polynomial polynomial) { |
| 124 | + polynomial.print(os); |
| 125 | + return os; |
| 126 | +} |
| 127 | + |
| 128 | +} // namespace polynomial |
| 129 | +} // namespace mlir |
| 130 | + |
| 131 | +namespace llvm { |
| 132 | + |
| 133 | +// Polynomials hash just like pointers |
| 134 | +template <> |
| 135 | +struct DenseMapInfo<mlir::polynomial::Polynomial> { |
| 136 | + static mlir::polynomial::Polynomial getEmptyKey() { |
| 137 | + auto *pointer = llvm::DenseMapInfo<void *>::getEmptyKey(); |
| 138 | + return mlir::polynomial::Polynomial( |
| 139 | + static_cast<mlir::polynomial::Polynomial::ImplType *>(pointer)); |
| 140 | + } |
| 141 | + static mlir::polynomial::Polynomial getTombstoneKey() { |
| 142 | + auto *pointer = llvm::DenseMapInfo<void *>::getTombstoneKey(); |
| 143 | + return mlir::polynomial::Polynomial( |
| 144 | + static_cast<mlir::polynomial::Polynomial::ImplType *>(pointer)); |
| 145 | + } |
| 146 | + static unsigned getHashValue(mlir::polynomial::Polynomial val) { |
| 147 | + return mlir::polynomial::hash_value(val); |
| 148 | + } |
| 149 | + static bool isEqual(mlir::polynomial::Polynomial lhs, |
| 150 | + mlir::polynomial::Polynomial rhs) { |
| 151 | + return lhs == rhs; |
| 152 | + } |
| 153 | +}; |
| 154 | + |
| 155 | +} // namespace llvm |
| 156 | + |
| 157 | +#endif // INCLUDE_MLIR_DIALECT_POLYNOMIAL_IR_POLYNOMIAL_H_ |
0 commit comments