Commit 63de49e7aa665ba41333e0d9f74951aa6835741b
1 parent
5cc75716
added abs kernel
Showing
1 changed file
with
36 additions
and
31 deletions
sdk/plugins/llvm.cpp
| @@ -3,6 +3,7 @@ | @@ -3,6 +3,7 @@ | ||
| 3 | #include <llvm/LLVMContext.h> | 3 | #include <llvm/LLVMContext.h> |
| 4 | #include <llvm/DerivedTypes.h> | 4 | #include <llvm/DerivedTypes.h> |
| 5 | #include <llvm/ExecutionEngine/JIT.h> | 5 | #include <llvm/ExecutionEngine/JIT.h> |
| 6 | +#include <llvm/IRBuilder.h> | ||
| 6 | #include <llvm/Module.h> | 7 | #include <llvm/Module.h> |
| 7 | #include <llvm/Operator.h> | 8 | #include <llvm/Operator.h> |
| 8 | #include <llvm/PassManager.h> | 9 | #include <llvm/PassManager.h> |
| @@ -15,7 +16,6 @@ | @@ -15,7 +16,6 @@ | ||
| 15 | #include <llvm/ExecutionEngine/ExecutionEngine.h> | 16 | #include <llvm/ExecutionEngine/ExecutionEngine.h> |
| 16 | #include <llvm/Support/raw_ostream.h> | 17 | #include <llvm/Support/raw_ostream.h> |
| 17 | #include <llvm/Support/ManagedStatic.h> | 18 | #include <llvm/Support/ManagedStatic.h> |
| 18 | -#include <llvm/Support/IRBuilder.h> | ||
| 19 | #include <llvm/Support/TargetSelect.h> | 19 | #include <llvm/Support/TargetSelect.h> |
| 20 | #include <llvm/Transforms/Scalar.h> | 20 | #include <llvm/Transforms/Scalar.h> |
| 21 | #include <openbr_plugin.h> | 21 | #include <openbr_plugin.h> |
| @@ -410,6 +410,12 @@ class StitchableKernel : public Kernel | @@ -410,6 +410,12 @@ class StitchableKernel : public Kernel | ||
| 410 | public: | 410 | public: |
| 411 | virtual Value *stitch(const MatrixBuilder &src, const MatrixBuilder &dst, Value *val) const = 0; /*!< A simplification of Kernel::build() for stitchable kernels. */ | 411 | virtual Value *stitch(const MatrixBuilder &src, const MatrixBuilder &dst, Value *val) const = 0; /*!< A simplification of Kernel::build() for stitchable kernels. */ |
| 412 | 412 | ||
| 413 | + virtual int preallocate(const Matrix &src, Matrix &dst) const | ||
| 414 | + { | ||
| 415 | + dst.copyHeader(src); | ||
| 416 | + return dst.elements(); | ||
| 417 | + } | ||
| 418 | + | ||
| 413 | private: | 419 | private: |
| 414 | void build(const MatrixBuilder &src, const MatrixBuilder &dst, PHINode *i) const | 420 | void build(const MatrixBuilder &src, const MatrixBuilder &dst, PHINode *i) const |
| 415 | { | 421 | { |
| @@ -473,12 +479,6 @@ class squareTransform : public StitchableKernel | @@ -473,12 +479,6 @@ class squareTransform : public StitchableKernel | ||
| 473 | { | 479 | { |
| 474 | Q_OBJECT | 480 | Q_OBJECT |
| 475 | 481 | ||
| 476 | - int preallocate(const Matrix &src, Matrix &dst) const | ||
| 477 | - { | ||
| 478 | - dst.copyHeader(src); | ||
| 479 | - return dst.elements(); | ||
| 480 | - } | ||
| 481 | - | ||
| 482 | Value *stitch(const MatrixBuilder &src, const MatrixBuilder &dst, Value *val) const | 482 | Value *stitch(const MatrixBuilder &src, const MatrixBuilder &dst, Value *val) const |
| 483 | { | 483 | { |
| 484 | (void) src; | 484 | (void) src; |
| @@ -492,7 +492,8 @@ BR_REGISTER(Transform, squareTransform) | @@ -492,7 +492,8 @@ BR_REGISTER(Transform, squareTransform) | ||
| 492 | * \ingroup transforms | 492 | * \ingroup transforms |
| 493 | * \brief LLVM pow transform | 493 | * \brief LLVM pow transform |
| 494 | * \author Josh Klontz \cite jklontz | 494 | * \author Josh Klontz \cite jklontz |
| 495 | - */class powTransform : public StitchableKernel | 495 | + */ |
| 496 | +class powTransform : public StitchableKernel | ||
| 496 | { | 497 | { |
| 497 | Q_OBJECT | 498 | Q_OBJECT |
| 498 | Q_PROPERTY(double exponent READ get_exponent WRITE set_exponent RESET reset_exponent STORED false) | 499 | Q_PROPERTY(double exponent READ get_exponent WRITE set_exponent RESET reset_exponent STORED false) |
| @@ -664,12 +665,6 @@ class scaleTransform : public StitchableKernel | @@ -664,12 +665,6 @@ class scaleTransform : public StitchableKernel | ||
| 664 | Q_PROPERTY(double a READ get_a WRITE set_a RESET reset_a STORED false) | 665 | Q_PROPERTY(double a READ get_a WRITE set_a RESET reset_a STORED false) |
| 665 | BR_PROPERTY(double, a, 1) | 666 | BR_PROPERTY(double, a, 1) |
| 666 | 667 | ||
| 667 | - int preallocate(const Matrix &src, Matrix &dst) const | ||
| 668 | - { | ||
| 669 | - dst.copyHeader(src); | ||
| 670 | - return dst.elements(); | ||
| 671 | - } | ||
| 672 | - | ||
| 673 | Value *stitch(const MatrixBuilder &src, const MatrixBuilder &dst, Value *val) const | 668 | Value *stitch(const MatrixBuilder &src, const MatrixBuilder &dst, Value *val) const |
| 674 | { | 669 | { |
| 675 | (void) src; | 670 | (void) src; |
| @@ -680,6 +675,28 @@ class scaleTransform : public StitchableKernel | @@ -680,6 +675,28 @@ class scaleTransform : public StitchableKernel | ||
| 680 | BR_REGISTER(Transform, scaleTransform) | 675 | BR_REGISTER(Transform, scaleTransform) |
| 681 | 676 | ||
| 682 | /*! | 677 | /*! |
| 678 | + * \ingroup absTransform | ||
| 679 | + * \brief LLVM abs transform | ||
| 680 | + * \author Josh Klontz \cite jklontz | ||
| 681 | + */ | ||
| 682 | +class absTransform : public StitchableKernel | ||
| 683 | +{ | ||
| 684 | + Q_OBJECT | ||
| 685 | + | ||
| 686 | + Value *stitch(const MatrixBuilder &src, const MatrixBuilder &dst, Value *val) const | ||
| 687 | + { | ||
| 688 | + (void) dst; | ||
| 689 | + if (!src.isSigned()) return val; | ||
| 690 | + if (src.isFloating()) return src.b->CreateCall(Intrinsic::getDeclaration(TheModule, Intrinsic::fabs, src.tys()), val); | ||
| 691 | + else return src.b->CreateSelect(src.b->CreateICmpSLT(val, src.autoConstant(0)), | ||
| 692 | + src.b->CreateSub(src.autoConstant(0), val), | ||
| 693 | + val); | ||
| 694 | + } | ||
| 695 | +}; | ||
| 696 | + | ||
| 697 | +BR_REGISTER(Transform, absTransform) | ||
| 698 | + | ||
| 699 | +/*! | ||
| 683 | * \ingroup transforms | 700 | * \ingroup transforms |
| 684 | * \brief LLVM add transform | 701 | * \brief LLVM add transform |
| 685 | * \author Josh Klontz \cite jklontz | 702 | * \author Josh Klontz \cite jklontz |
| @@ -690,12 +707,6 @@ class addTransform : public StitchableKernel | @@ -690,12 +707,6 @@ class addTransform : public StitchableKernel | ||
| 690 | Q_PROPERTY(double b READ get_b WRITE set_b RESET reset_b STORED false) | 707 | Q_PROPERTY(double b READ get_b WRITE set_b RESET reset_b STORED false) |
| 691 | BR_PROPERTY(double, b, 0) | 708 | BR_PROPERTY(double, b, 0) |
| 692 | 709 | ||
| 693 | - int preallocate(const Matrix &src, Matrix &dst) const | ||
| 694 | - { | ||
| 695 | - dst.copyHeader(src); | ||
| 696 | - return dst.elements(); | ||
| 697 | - } | ||
| 698 | - | ||
| 699 | Value *stitch(const MatrixBuilder &src, const MatrixBuilder &dst, Value *val) const | 710 | Value *stitch(const MatrixBuilder &src, const MatrixBuilder &dst, Value *val) const |
| 700 | { | 711 | { |
| 701 | (void) src; | 712 | (void) src; |
| @@ -718,12 +729,6 @@ class clampTransform : public StitchableKernel | @@ -718,12 +729,6 @@ class clampTransform : public StitchableKernel | ||
| 718 | BR_PROPERTY(double, min, -std::numeric_limits<double>::max()) | 729 | BR_PROPERTY(double, min, -std::numeric_limits<double>::max()) |
| 719 | BR_PROPERTY(double, max, std::numeric_limits<double>::max()) | 730 | BR_PROPERTY(double, max, std::numeric_limits<double>::max()) |
| 720 | 731 | ||
| 721 | - int preallocate(const Matrix &src, Matrix &dst) const | ||
| 722 | - { | ||
| 723 | - dst.copyHeader(src); | ||
| 724 | - return dst.elements(); | ||
| 725 | - } | ||
| 726 | - | ||
| 727 | Value *stitch(const MatrixBuilder &src, const MatrixBuilder &dst, Value *val) const | 732 | Value *stitch(const MatrixBuilder &src, const MatrixBuilder &dst, Value *val) const |
| 728 | { | 733 | { |
| 729 | (void) src; | 734 | (void) src; |
| @@ -817,22 +822,22 @@ class LLVMInitializer : public Initializer | @@ -817,22 +822,22 @@ class LLVMInitializer : public Initializer | ||
| 817 | Type::getInt16Ty(getGlobalContext()), // hash | 822 | Type::getInt16Ty(getGlobalContext()), // hash |
| 818 | NULL); | 823 | NULL); |
| 819 | 824 | ||
| 820 | - QSharedPointer<Transform> kernel(Transform::make("stitch([scale(2),add(3)])", NULL)); | 825 | + QSharedPointer<Transform> kernel(Transform::make("abs", NULL)); |
| 821 | 826 | ||
| 822 | Template src, dst; | 827 | Template src, dst; |
| 823 | - src.m() = (Mat_<qint8>(2,2) << 1, 2, 3, 4); | 828 | + src.m() = (Mat_<qint8>(2,2) << -1, -2, 3, 4); |
| 824 | kernel->project(src, dst); | 829 | kernel->project(src, dst); |
| 825 | qDebug() << dst.m(); | 830 | qDebug() << dst.m(); |
| 826 | 831 | ||
| 827 | - src.m() = (Mat_<qint32>(2,2) << 1, 3, 9, 27); | 832 | + src.m() = (Mat_<qint32>(2,2) << -1, -3, 9, 27); |
| 828 | kernel->project(src, dst); | 833 | kernel->project(src, dst); |
| 829 | qDebug() << dst.m(); | 834 | qDebug() << dst.m(); |
| 830 | 835 | ||
| 831 | - src.m() = (Mat_<float>(2,2) << 1.5, 2.5, 3.5, 4.5); | 836 | + src.m() = (Mat_<float>(2,2) << -1.5, -2.5, 3.5, 4.5); |
| 832 | kernel->project(src, dst); | 837 | kernel->project(src, dst); |
| 833 | qDebug() << dst.m(); | 838 | qDebug() << dst.m(); |
| 834 | 839 | ||
| 835 | - src.m() = (Mat_<double>(2,2) << 1.75, 2.75, 3.75, 4.75); | 840 | + src.m() = (Mat_<double>(2,2) << 1.75, 2.75, -3.75, -4.75); |
| 836 | kernel->project(src, dst); | 841 | kernel->project(src, dst); |
| 837 | qDebug() << dst.m(); | 842 | qDebug() << dst.m(); |
| 838 | } | 843 | } |