File: templated_generator.cpp

package info (click to toggle)
halide 21.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,752 kB
  • sloc: cpp: 289,334; ansic: 22,751; python: 7,486; makefile: 4,299; sh: 2,508; java: 1,549; javascript: 282; pascal: 207; xml: 127; asm: 9
file content (35 lines) | stat: -rw-r--r-- 1,593 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "Halide.h"

using namespace Halide;

// Define a templated generator. Normally this is a bad idea, and your template
// parameters (e.g. the type of the input) should be GeneratorParams
// instead. Sometimes, however, it's more convenient to have the C++ type
// available as a template parameter. Or maybe you want to template a Generator
// on something not expressible as a GeneratorParam. Or maybe you have a
// deficient build system and it's difficult to specify GeneratorParams in the
// build (note that HALIDE_REGISTER_GENERATOR_ALIAS also exists for this
// purpose).
template<typename T1, typename T2>
class Templated : public Generator<Templated<T1, T2>> {
public:
    // A major downside of templated generators is that because we use CRTP, you
    // must manually import names of types from the base class. For Input and
    // Output you can also just use these equivalent globally-scoped names:
    GeneratorInput<Buffer<T1, 2>> input{"input"};
    GeneratorOutput<Buffer<T2, 2>> output{"output"};

    void generate() {
        Var x, y;
        output(x, y) = cast<T2>(input(x, y) + (T1)2);

        // Again, due to CRTP, we must manually tell C++ how to look up a method
        // from the base class using this->template.
        output.vectorize(x, this->template natural_vector_size<T2>());
    }
};

// To pass a comma-separated template parameter list to a macro, we must enclose
// the type argument in parentheses.
HALIDE_REGISTER_GENERATOR((Templated<float, double>), templated)
HALIDE_REGISTER_GENERATOR((Templated<uint8_t, uint16_t>), templated_uint8)