main.cpp 1.2 KB
Newer Older
J
jiakai 已提交
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
#include "midout.h"
#include <cstdio>
#include <string>

MIDOUT_DECL(Opr);

enum class Opr {
    ADD, SUB
};

namespace calc {
    template<Opr opr>
    int kern_impl(int a, int b);

    template<>
    __attribute__((noinline))
    int kern_impl<Opr::ADD>(int a, int b) {
        return a + b;
    }

    template<>
    __attribute__((noinline))
    int kern_impl<Opr::SUB>(int a, int b) {
        return a - b;
    }

    template<Opr opr>
    int kern(int a, int b) {
        MIDOUT_BEGIN(Opr, midout_iv(opr)) {
            return kern_impl<opr>(a, b);
        } MIDOUT_END();
    }
}

int main(int argc, char **argv) {
    if (argc != 4) {
        fprintf(stderr, "usage: %s <num0> <num1> <+/->\n"
                "    to compute sum/difference of two numbers\n",
                argv[0]);
        return -1;
    }
    int a = std::stoi(argv[1]),
        b = std::stoi(argv[2]),
        c;

    switch (argv[3][0]) {
        case '+':
            c = calc::kern<Opr::ADD>(a, b);
            break;
        case '-':
            c = calc::kern<Opr::SUB>(a, b);
            break;
        default:
            fprintf(stderr, "bad opr\n");
            return 2;
    }

    printf("result=%d\n", c);
}