1 | // Arithmetic operations |
2 | |
3 | #if defined(TI_EXPRESSION_IMPLEMENTATION) |
4 | |
5 | #undef DEFINE_EXPRESSION_OP_UNARY |
6 | #undef DEFINE_EXPRESSION_FUNC_UNARY |
7 | #undef DEFINE_EXPRESSION_OP_BINARY |
8 | #undef DEFINE_EXPRESSION_FUNC_BINARY |
9 | #undef DEFINE_EXPRESSION_FUNC_TERNARY |
10 | |
11 | #define DEFINE_EXPRESSION_OP_UNARY(op, opname) \ |
12 | Expr expr_##opname(const Expr &expr) { \ |
13 | return Expr::make<UnaryOpExpression>(UnaryOpType::opname, expr); \ |
14 | } \ |
15 | Expr operator op(const Expr &expr) { return expr_##opname(expr); } |
16 | |
17 | #define DEFINE_EXPRESSION_FUNC_UNARY(opname) \ |
18 | Expr opname(const Expr &expr) { \ |
19 | return Expr::make<UnaryOpExpression>(UnaryOpType::opname, expr); \ |
20 | } \ |
21 | Expr expr_##opname(const Expr &expr) { return opname(expr); } |
22 | |
23 | #define DEFINE_EXPRESSION_OP_BINARY(op, opname) \ |
24 | Expr operator op(const Expr &lhs, const Expr &rhs) { \ |
25 | return Expr::make<BinaryOpExpression>(BinaryOpType::opname, lhs, rhs); \ |
26 | } \ |
27 | Expr expr_##opname(const Expr &lhs, const Expr &rhs) { return lhs op rhs; } |
28 | |
29 | #define DEFINE_EXPRESSION_FUNC_BINARY(opname) \ |
30 | Expr opname(const Expr &lhs, const Expr &rhs) { \ |
31 | return Expr::make<BinaryOpExpression>(BinaryOpType::opname, lhs, rhs); \ |
32 | } \ |
33 | Expr expr_##opname(const Expr &lhs, const Expr &rhs) { \ |
34 | return opname(lhs, rhs); \ |
35 | } |
36 | |
37 | #define DEFINE_EXPRESSION_FUNC_TERNARY(opname) \ |
38 | Expr expr_##opname(const Expr &cond, const Expr &lhs, const Expr &rhs) { \ |
39 | return Expr::make<TernaryOpExpression>(TernaryOpType::opname, cond, lhs, \ |
40 | rhs); \ |
41 | } \ |
42 | Expr opname(const Expr &cond, const Expr &lhs, const Expr &rhs) { \ |
43 | return expr_##opname(cond, lhs, rhs); \ |
44 | } |
45 | |
46 | #else |
47 | |
48 | #define DEFINE_EXPRESSION_OP_UNARY(op, opname) \ |
49 | Expr operator op(const Expr &expr); \ |
50 | Expr expr_##opname(const Expr &expr); |
51 | |
52 | #define DEFINE_EXPRESSION_FUNC_UNARY(opname) \ |
53 | Expr opname(const Expr &expr); \ |
54 | Expr expr_##opname(const Expr &expr); |
55 | |
56 | #define DEFINE_EXPRESSION_OP_BINARY(op, opname) \ |
57 | Expr operator op(const Expr &lhs, const Expr &rhs); \ |
58 | Expr expr_##opname(const Expr &lhs, const Expr &rhs); |
59 | |
60 | #define DEFINE_EXPRESSION_FUNC_BINARY(opname) \ |
61 | Expr opname(const Expr &lhs, const Expr &rhs); \ |
62 | Expr expr_##opname(const Expr &lhs, const Expr &rhs); |
63 | |
64 | #define DEFINE_EXPRESSION_FUNC_TERNARY(opname) \ |
65 | Expr opname(const Expr &cond, const Expr &lhs, const Expr &rhs); \ |
66 | Expr expr_##opname(const Expr &cond, const Expr &lhs, const Expr &rhs); |
67 | |
68 | #endif |
69 | |
70 | namespace taichi::lang { |
71 | |
72 | DEFINE_EXPRESSION_FUNC_UNARY(sqrt) |
73 | DEFINE_EXPRESSION_FUNC_UNARY(round) |
74 | DEFINE_EXPRESSION_FUNC_UNARY(floor) |
75 | DEFINE_EXPRESSION_FUNC_UNARY(ceil) |
76 | DEFINE_EXPRESSION_FUNC_UNARY(abs) |
77 | DEFINE_EXPRESSION_FUNC_UNARY(sin) |
78 | DEFINE_EXPRESSION_FUNC_UNARY(asin) |
79 | DEFINE_EXPRESSION_FUNC_UNARY(cos) |
80 | DEFINE_EXPRESSION_FUNC_UNARY(acos) |
81 | DEFINE_EXPRESSION_FUNC_UNARY(tan) |
82 | DEFINE_EXPRESSION_FUNC_UNARY(tanh) |
83 | DEFINE_EXPRESSION_FUNC_UNARY(inv) |
84 | DEFINE_EXPRESSION_FUNC_UNARY(rcp) |
85 | DEFINE_EXPRESSION_FUNC_UNARY(rsqrt) |
86 | DEFINE_EXPRESSION_FUNC_UNARY(exp) |
87 | DEFINE_EXPRESSION_FUNC_UNARY(log) |
88 | DEFINE_EXPRESSION_FUNC_UNARY(logic_not) |
89 | DEFINE_EXPRESSION_OP_UNARY(~, bit_not) |
90 | DEFINE_EXPRESSION_OP_UNARY(-, neg) |
91 | |
92 | DEFINE_EXPRESSION_OP_BINARY(+, add) |
93 | DEFINE_EXPRESSION_OP_BINARY(-, sub) |
94 | DEFINE_EXPRESSION_OP_BINARY(*, mul) |
95 | DEFINE_EXPRESSION_OP_BINARY(/, div) |
96 | DEFINE_EXPRESSION_OP_BINARY(%, mod) |
97 | DEFINE_EXPRESSION_OP_BINARY(&&, logical_and) |
98 | DEFINE_EXPRESSION_OP_BINARY(||, logical_or) |
99 | DEFINE_EXPRESSION_OP_BINARY(&, bit_and) |
100 | DEFINE_EXPRESSION_OP_BINARY(|, bit_or) |
101 | DEFINE_EXPRESSION_OP_BINARY(^, bit_xor) |
102 | DEFINE_EXPRESSION_OP_BINARY(<<, bit_shl) |
103 | DEFINE_EXPRESSION_OP_BINARY(>>, bit_sar) |
104 | DEFINE_EXPRESSION_OP_BINARY(<, cmp_lt) |
105 | DEFINE_EXPRESSION_OP_BINARY(<=, cmp_le) |
106 | DEFINE_EXPRESSION_OP_BINARY(>, cmp_gt) |
107 | DEFINE_EXPRESSION_OP_BINARY(>=, cmp_ge) |
108 | DEFINE_EXPRESSION_OP_BINARY(==, cmp_eq) |
109 | DEFINE_EXPRESSION_OP_BINARY(!=, cmp_ne) |
110 | |
111 | DEFINE_EXPRESSION_FUNC_BINARY(min) |
112 | DEFINE_EXPRESSION_FUNC_BINARY(max) |
113 | DEFINE_EXPRESSION_FUNC_BINARY(atan2) |
114 | DEFINE_EXPRESSION_FUNC_BINARY(pow) |
115 | DEFINE_EXPRESSION_FUNC_BINARY(truediv) |
116 | DEFINE_EXPRESSION_FUNC_BINARY(floordiv) |
117 | DEFINE_EXPRESSION_FUNC_BINARY(bit_shr) |
118 | |
119 | DEFINE_EXPRESSION_FUNC_TERNARY(select) |
120 | DEFINE_EXPRESSION_FUNC_TERNARY(ifte) |
121 | |
122 | } // namespace taichi::lang |
123 | |
124 | #undef DEFINE_EXPRESSION_OP_UNARY |
125 | #undef DEFINE_EXPRESSION_OP_BINARY |
126 | #undef DEFINE_EXPRESSION_FUNC_UNARY |
127 | #undef DEFINE_EXPRESSION_FUNC_BINARY |
128 | #undef DEFINE_EXPRESSION_FUNC_TERNARY |
129 | |