Commit 12a029b58b1decb6ba1e36bc06694e93b178cfce

Authored by Josh Klontz
1 parent 927b7781

added c support and c naming convention

sdk/jitcv/jitcv.h
... ... @@ -4,15 +4,16 @@
4 4 #include <stddef.h>
5 5 #include <stdint.h>
6 6  
7   -namespace jitcv
8   -{
  7 +#ifdef __cplusplus
  8 +extern "C" {
  9 +#endif
9 10  
10 11 /*!
11 12 * \brief jitcv matrix
12 13 * \author Josh Klontz \cite jklontz
13 14 * \note Not part of the core SDK
14 15 */
15   -struct Matrix
  16 +struct jit_matrix
16 17 {
17 18 uint8_t *data; /*!< Data */
18 19 uint32_t channels; /*!< Channels */
... ... @@ -28,6 +29,7 @@ struct Matrix
28 29 Single-row : 1
29 30 Single-frame : 1 */
30 31  
  32 +#ifdef __cplusplus
31 33 enum Hash { Bits = 0x00FF,
32 34 Floating = 0x0100,
33 35 Signed = 0x0200,
... ... @@ -48,9 +50,9 @@ struct Matrix
48 50 f32 = 32 + Floating + Signed,
49 51 f64 = 64 + Floating + Signed };
50 52  
51   - Matrix() : data(NULL), channels(0), columns(0), rows(0), frames(0), hash(0) {}
  53 + jit_matrix() : data(NULL), channels(0), columns(0), rows(0), frames(0), hash(0) {}
52 54  
53   - Matrix(uint32_t _channels, uint32_t _columns, uint32_t _rows, uint32_t _frames, uint16_t _hash)
  55 + jit_matrix(uint32_t _channels, uint32_t _columns, uint32_t _rows, uint32_t _frames, uint16_t _hash)
54 56 : data(NULL), channels(_channels), columns(_columns), rows(_rows), frames(_frames), hash(_hash)
55 57 {
56 58 setSingleChannel(channels == 1);
... ... @@ -59,7 +61,7 @@ struct Matrix
59 61 setSingleFrame(frames == 1);
60 62 }
61 63  
62   - inline void copyHeader(const Matrix &other) { channels = other.channels; columns = other.columns; rows = other.rows; frames = other.frames; hash = other.hash; }
  64 + inline void copyHeader(const jit_matrix &other) { channels = other.channels; columns = other.columns; rows = other.rows; frames = other.frames; hash = other.hash; }
63 65 inline void allocate() { deallocate(); data = new uint8_t[bytes()]; }
64 66 inline void deallocate() { delete[] data; data = NULL; }
65 67  
... ... @@ -81,8 +83,14 @@ struct Matrix
81 83 inline void setSingleFrame(bool singleFrame) { singleFrame ? hash |= SingleFrame : hash &= ~SingleFrame; }
82 84 inline uint32_t elements() const { return channels * columns * rows * frames; }
83 85 inline uint32_t bytes() const { return bits() / 8 * elements(); }
  86 +#endif // __cplusplus
84 87 };
85 88  
86   -} // namespace jitcv
  89 +typedef void (*jit_unary_function)(const jit_matrix &src, jit_matrix &dst);
  90 +typedef void (*jit_binary_function)(const jit_matrix &srcA, const jit_matrix &srcB, jit_matrix &dst);
  91 +
  92 +#ifdef __cplusplus
  93 +}
  94 +#endif
87 95  
88 96 #endif // __JITCV_H
... ...
sdk/plugins/llvm.cpp
... ... @@ -25,7 +25,6 @@
25 25  
26 26 using namespace br;
27 27 using namespace cv;
28   -using namespace jitcv;
29 28 using namespace llvm;
30 29  
31 30 static Module *TheModule = NULL;
... ... @@ -34,15 +33,15 @@ static FunctionPassManager *TheFunctionPassManager = NULL;
34 33 static FunctionPassManager *TheExtraFunctionPassManager = NULL;
35 34 static StructType *TheMatrixStruct = NULL;
36 35  
37   -static QString MatrixToString(const Matrix &m)
  36 +static QString MatrixToString(const jit_matrix &m)
38 37 {
39 38 return QString("%1%2%3%4%5%6%7").arg(QString::number(m.bits()), (m.isSigned() ? "s" : "u"), (m.isFloating() ? "f" : "i"),
40 39 QString::number(m.singleChannel()), QString::number(m.singleColumn()), QString::number(m.singleRow()), QString::number(m.singleFrame()));
41 40 }
42 41  
43   -static Matrix MatrixFromMat(const cv::Mat &mat)
  42 +static jit_matrix MatrixFromMat(const cv::Mat &mat)
44 43 {
45   - Matrix m;
  44 + jit_matrix m;
46 45  
47 46 if (!mat.isContinuous()) qFatal("Matrix requires continuous data.");
48 47 m.channels = mat.channels();
... ... @@ -51,13 +50,13 @@ static Matrix MatrixFromMat(const cv::Mat &amp;mat)
51 50 m.frames = 1;
52 51  
53 52 switch (mat.depth()) {
54   - case CV_8U: m.hash = Matrix::u8; break;
55   - case CV_8S: m.hash = Matrix::s8; break;
56   - case CV_16U: m.hash = Matrix::u16; break;
57   - case CV_16S: m.hash = Matrix::s16; break;
58   - case CV_32S: m.hash = Matrix::s32; break;
59   - case CV_32F: m.hash = Matrix::f32; break;
60   - case CV_64F: m.hash = Matrix::f64; break;
  53 + case CV_8U: m.hash = jit_matrix::u8; break;
  54 + case CV_8S: m.hash = jit_matrix::s8; break;
  55 + case CV_16U: m.hash = jit_matrix::u16; break;
  56 + case CV_16S: m.hash = jit_matrix::s16; break;
  57 + case CV_32S: m.hash = jit_matrix::s32; break;
  58 + case CV_32F: m.hash = jit_matrix::f32; break;
  59 + case CV_64F: m.hash = jit_matrix::f64; break;
61 60 default: qFatal("Unrecognized matrix depth.");
62 61 }
63 62  
... ... @@ -65,17 +64,17 @@ static Matrix MatrixFromMat(const cv::Mat &amp;mat)
65 64 return m;
66 65 }
67 66  
68   -static void AllocateMatrixFromMat(Matrix &m, cv::Mat &mat)
  67 +static void AllocateMatrixFromMat(jit_matrix &m, cv::Mat &mat)
69 68 {
70 69 int cvType = -1;
71 70 switch (m.type()) {
72   - case Matrix::u8: cvType = CV_8U; break;
73   - case Matrix::s8: cvType = CV_8S; break;
74   - case Matrix::u16: cvType = CV_16U; break;
75   - case Matrix::s16: cvType = CV_16S; break;
76   - case Matrix::s32: cvType = CV_32S; break;
77   - case Matrix::f32: cvType = CV_32F; break;
78   - case Matrix::f64: cvType = CV_64F; break;
  71 + case jit_matrix::u8: cvType = CV_8U; break;
  72 + case jit_matrix::s8: cvType = CV_8S; break;
  73 + case jit_matrix::u16: cvType = CV_16U; break;
  74 + case jit_matrix::s16: cvType = CV_16S; break;
  75 + case jit_matrix::s32: cvType = CV_32S; break;
  76 + case jit_matrix::f32: cvType = CV_32F; break;
  77 + case jit_matrix::f64: cvType = CV_64F; break;
79 78 default: qFatal("OpenCV does not support Matrix format: %s", qPrintable(MatrixToString(m)));
80 79 }
81 80  
... ... @@ -84,21 +83,21 @@ static void AllocateMatrixFromMat(Matrix &amp;m, cv::Mat &amp;mat)
84 83 m.data = mat.data;
85 84 }
86 85  
87   -QDebug operator<<(QDebug dbg, const Matrix &m)
  86 +QDebug operator<<(QDebug dbg, const jit_matrix &m)
88 87 {
89 88 dbg.nospace() << MatrixToString(m);
90 89 return dbg;
91 90 }
92 91  
93   -struct MatrixBuilder : public Matrix
  92 +struct MatrixBuilder : public jit_matrix
94 93 {
95 94 Value *m;
96 95 IRBuilder<> *b;
97 96 Function *f;
98 97 Twine name;
99 98  
100   - MatrixBuilder(const Matrix &matrix, Value *value, IRBuilder<> *builder, Function *function, const Twine &name_)
101   - : Matrix(matrix), m(value), b(builder), f(function), name(name_) {}
  99 + MatrixBuilder(const jit_matrix &matrix, Value *value, IRBuilder<> *builder, Function *function, const Twine &name_)
  100 + : jit_matrix(matrix), m(value), b(builder), f(function), name(name_) {}
102 101  
103 102 static Value *zero() { return constant(0); }
104 103 static Value *one() { return constant(1); }
... ... @@ -200,7 +199,7 @@ struct MatrixBuilder : public Matrix
200 199 template <typename T>
201 200 inline static std::vector<T> toVector(T value) { std::vector<T> vector; vector.push_back(value); return vector; }
202 201  
203   - static Type *ty(const Matrix &m)
  202 + static Type *ty(const jit_matrix &m)
204 203 {
205 204 const int bits = m.bits();
206 205 if (m.isFloating()) {
... ... @@ -220,7 +219,7 @@ struct MatrixBuilder : public Matrix
220 219 inline Type *ty() const { return ty(*this); }
221 220 inline std::vector<Type*> tys() const { return toVector<Type*>(ty()); }
222 221  
223   - static Type *ptrTy(const Matrix &m)
  222 + static Type *ptrTy(const jit_matrix &m)
224 223 {
225 224 const int bits = m.bits();
226 225 if (m.isFloating()) {
... ... @@ -251,17 +250,17 @@ class UnaryKernel : public UntrainableMetaTransform
251 250 {
252 251 Q_OBJECT
253 252  
254   - typedef void (*kernel_t)(const Matrix*, Matrix*, quint32);
  253 + typedef void (*kernel_t)(const jit_matrix*, jit_matrix*, quint32);
255 254 kernel_t kernel;
256 255 quint16 hash;
257 256  
258 257 public:
259 258 UnaryKernel() : kernel(NULL), hash(0) {}
260   - virtual int preallocate(const Matrix &src, Matrix &dst) const = 0; /*!< Preallocate destintation matrix based on source matrix. */
  259 + virtual int preallocate(const jit_matrix &src, jit_matrix &dst) const = 0; /*!< Preallocate destintation matrix based on source matrix. */
261 260 virtual void build(const MatrixBuilder &src, const MatrixBuilder &dst, PHINode *i) const = 0; /*!< Build the kernel. */
262 261  
263 262 private:
264   - QString mangledName(const Matrix &src) const
  263 + QString mangledName(const jit_matrix &src) const
265 264 {
266 265 static QHash<QString, int> argsLUT;
267 266 const QString args = arguments().join(",");
... ... @@ -270,7 +269,7 @@ private:
270 269 return "jitcv_" + name().remove("Transform") + (args.isEmpty() ? QString() : QString::number(uid)) + "_" + MatrixToString(src);
271 270 }
272 271  
273   - Function *compile(const Matrix &m) const
  272 + Function *compile(const jit_matrix &m) const
274 273 {
275 274 Constant *c = TheModule->getOrInsertFunction(qPrintable(mangledName(m)),
276 275 Type::getVoidTy(getGlobalContext()),
... ... @@ -296,7 +295,7 @@ private:
296 295 BasicBlock *kernel;
297 296 PHINode *i = MatrixBuilder::beginLoop(builder, function, entry, &kernel, "i");
298 297  
299   - Matrix n;
  298 + jit_matrix n;
300 299 preallocate(m, n);
301 300 build(MatrixBuilder(m, src, &builder, function, "src"), MatrixBuilder(n, dst, &builder, function, "dst"), i);
302 301  
... ... @@ -308,14 +307,14 @@ private:
308 307  
309 308 void project(const Template &src, Template &dst) const
310 309 {
311   - const Matrix m(MatrixFromMat(src));
312   - Matrix n;
  310 + const jit_matrix m(MatrixFromMat(src));
  311 + jit_matrix n;
313 312 const int size = preallocate(m, n);
314 313 AllocateMatrixFromMat(n, dst);
315 314 invoke(m, n, size);
316 315 }
317 316  
318   - void invoke(const Matrix &src, Matrix &dst, int size) const
  317 + void invoke(const jit_matrix &src, jit_matrix &dst, int size) const
319 318 {
320 319 if (src.hash != hash) {
321 320 static QMutex compilerLock;
... ... @@ -349,22 +348,22 @@ class BinaryKernel: public UntrainableMetaTransform
349 348 {
350 349 Q_OBJECT
351 350  
352   - typedef void (*kernel_t)(const Matrix*, const Matrix*, Matrix*, quint32);
  351 + typedef void (*kernel_t)(const jit_matrix*, const jit_matrix*, jit_matrix*, quint32);
353 352 kernel_t kernel;
354 353 quint16 hashA, hashB;
355 354  
356 355 public:
357 356 BinaryKernel() : kernel(NULL), hashA(0), hashB(0) {}
358   - virtual int preallocate(const Matrix &srcA, const Matrix &srcB, Matrix &dst) const = 0; /*!< Preallocate destintation matrix based on source matrix. */
  357 + virtual int preallocate(const jit_matrix &srcA, const jit_matrix &srcB, jit_matrix &dst) const = 0; /*!< Preallocate destintation matrix based on source matrix. */
359 358 virtual void build(const MatrixBuilder &srcA, const MatrixBuilder &srcB, const MatrixBuilder &dst, PHINode *i) const = 0; /*!< Build the kernel. */
360 359  
361 360 private:
362   - QString mangledName(const Matrix &srcA, const Matrix &srcB) const
  361 + QString mangledName(const jit_matrix &srcA, const jit_matrix &srcB) const
363 362 {
364 363 return "jitcv_" + name().remove("Transform") + "_" + MatrixToString(srcA) + "_" + MatrixToString(srcB);
365 364 }
366 365  
367   - Function *compile(const Matrix &m, const Matrix &n) const
  366 + Function *compile(const jit_matrix &m, const jit_matrix &n) const
368 367 {
369 368 Constant *c = TheModule->getOrInsertFunction(qPrintable(mangledName(m, n)),
370 369 Type::getVoidTy(getGlobalContext()),
... ... @@ -393,7 +392,7 @@ private:
393 392 BasicBlock *kernel;
394 393 PHINode *i = MatrixBuilder::beginLoop(builder, function, entry, &kernel, "i");
395 394  
396   - Matrix o;
  395 + jit_matrix o;
397 396 preallocate(m, n, o);
398 397 build(MatrixBuilder(m, srcA, &builder, function, "srcA"), MatrixBuilder(n, srcB, &builder, function, "srcB"), MatrixBuilder(o, dst, &builder, function, "dst"), i);
399 398  
... ... @@ -403,7 +402,7 @@ private:
403 402 return function;
404 403 }
405 404  
406   - void invoke(const Matrix &srcA, const Matrix &srcB, Matrix &dst, int size) const
  405 + void invoke(const jit_matrix &srcA, const jit_matrix &srcB, jit_matrix &dst, int size) const
407 406 {
408 407 if ((srcA.hash != hashA) || (srcB.hash != hashB)) {
409 408 static QMutex compilerLock;
... ... @@ -441,7 +440,7 @@ class StitchableKernel : public UnaryKernel
441 440 public:
442 441 virtual Value *stitch(const MatrixBuilder &src, const MatrixBuilder &dst, Value *val) const = 0; /*!< A simplification of Kernel::build() for stitchable kernels. */
443 442  
444   - virtual int preallocate(const Matrix &src, Matrix &dst) const
  443 + virtual int preallocate(const jit_matrix &src, jit_matrix &dst) const
445 444 {
446 445 dst.copyHeader(src);
447 446 return dst.elements();
... ... @@ -474,9 +473,9 @@ class stitchTransform : public UnaryKernel
474 473 qFatal("%s is not a stitchable kernel!", qPrintable(transform->name()));
475 474 }
476 475  
477   - int preallocate(const Matrix &src, Matrix &dst) const
  476 + int preallocate(const jit_matrix &src, jit_matrix &dst) const
478 477 {
479   - Matrix tmp = src;
  478 + jit_matrix tmp = src;
480 479 foreach (const Transform *kernel, kernels) {
481 480 static_cast<const UnaryKernel*>(kernel)->preallocate(tmp, dst);
482 481 tmp = dst;
... ... @@ -530,7 +529,7 @@ class powTransform : public StitchableKernel
530 529 Q_PROPERTY(double exponent READ get_exponent WRITE set_exponent RESET reset_exponent STORED false)
531 530 BR_PROPERTY(double, exponent, 2)
532 531  
533   - int preallocate(const Matrix &src, Matrix &dst) const
  532 + int preallocate(const jit_matrix &src, jit_matrix &dst) const
534 533 {
535 534 dst.copyHeader(src);
536 535 dst.setFloating(true);
... ... @@ -575,9 +574,9 @@ class sumTransform : public UnaryKernel
575 574 BR_PROPERTY(bool, rows, true)
576 575 BR_PROPERTY(bool, frames, true)
577 576  
578   - int preallocate(const Matrix &src, Matrix &dst) const
  577 + int preallocate(const jit_matrix &src, jit_matrix &dst) const
579 578 {
580   - dst = Matrix(channels ? 1 : src.channels, columns ? 1 : src.columns, rows ? 1 : src.rows, frames ? 1 : src.frames, src.hash);
  579 + dst = jit_matrix(channels ? 1 : src.channels, columns ? 1 : src.columns, rows ? 1 : src.rows, frames ? 1 : src.frames, src.hash);
581 580 dst.setBits(std::min(2*dst.bits(), dst.isFloating() ? 64 : 32));
582 581 return dst.elements();
583 582 }
... ... @@ -655,23 +654,23 @@ class castTransform : public StitchableKernel
655 654  
656 655 public:
657 656 /*!< */
658   - enum Type { u1 = Matrix::u1,
659   - u8 = Matrix::u8,
660   - u16 = Matrix::u16,
661   - u32 = Matrix::u32,
662   - u64 = Matrix::u64,
663   - s8 = Matrix::s8,
664   - s16 = Matrix::s16,
665   - s32 = Matrix::s32,
666   - s64 = Matrix::s64,
667   - f16 = Matrix::f16,
668   - f32 = Matrix::f32,
669   - f64 = Matrix::f64 };
  657 + enum Type { u1 = jit_matrix::u1,
  658 + u8 = jit_matrix::u8,
  659 + u16 = jit_matrix::u16,
  660 + u32 = jit_matrix::u32,
  661 + u64 = jit_matrix::u64,
  662 + s8 = jit_matrix::s8,
  663 + s16 = jit_matrix::s16,
  664 + s32 = jit_matrix::s32,
  665 + s64 = jit_matrix::s64,
  666 + f16 = jit_matrix::f16,
  667 + f32 = jit_matrix::f32,
  668 + f64 = jit_matrix::f64 };
670 669  
671 670 private:
672 671 BR_PROPERTY(Type, type, f32)
673 672  
674   - int preallocate(const Matrix &src, Matrix &dst) const
  673 + int preallocate(const jit_matrix &src, jit_matrix &dst) const
675 674 {
676 675 dst.copyHeader(src);
677 676 dst.setType(type);
... ...