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
70namespace taichi::lang {
71
72DEFINE_EXPRESSION_FUNC_UNARY(sqrt)
73DEFINE_EXPRESSION_FUNC_UNARY(round)
74DEFINE_EXPRESSION_FUNC_UNARY(floor)
75DEFINE_EXPRESSION_FUNC_UNARY(ceil)
76DEFINE_EXPRESSION_FUNC_UNARY(abs)
77DEFINE_EXPRESSION_FUNC_UNARY(sin)
78DEFINE_EXPRESSION_FUNC_UNARY(asin)
79DEFINE_EXPRESSION_FUNC_UNARY(cos)
80DEFINE_EXPRESSION_FUNC_UNARY(acos)
81DEFINE_EXPRESSION_FUNC_UNARY(tan)
82DEFINE_EXPRESSION_FUNC_UNARY(tanh)
83DEFINE_EXPRESSION_FUNC_UNARY(inv)
84DEFINE_EXPRESSION_FUNC_UNARY(rcp)
85DEFINE_EXPRESSION_FUNC_UNARY(rsqrt)
86DEFINE_EXPRESSION_FUNC_UNARY(exp)
87DEFINE_EXPRESSION_FUNC_UNARY(log)
88DEFINE_EXPRESSION_FUNC_UNARY(logic_not)
89DEFINE_EXPRESSION_OP_UNARY(~, bit_not)
90DEFINE_EXPRESSION_OP_UNARY(-, neg)
91
92DEFINE_EXPRESSION_OP_BINARY(+, add)
93DEFINE_EXPRESSION_OP_BINARY(-, sub)
94DEFINE_EXPRESSION_OP_BINARY(*, mul)
95DEFINE_EXPRESSION_OP_BINARY(/, div)
96DEFINE_EXPRESSION_OP_BINARY(%, mod)
97DEFINE_EXPRESSION_OP_BINARY(&&, logical_and)
98DEFINE_EXPRESSION_OP_BINARY(||, logical_or)
99DEFINE_EXPRESSION_OP_BINARY(&, bit_and)
100DEFINE_EXPRESSION_OP_BINARY(|, bit_or)
101DEFINE_EXPRESSION_OP_BINARY(^, bit_xor)
102DEFINE_EXPRESSION_OP_BINARY(<<, bit_shl)
103DEFINE_EXPRESSION_OP_BINARY(>>, bit_sar)
104DEFINE_EXPRESSION_OP_BINARY(<, cmp_lt)
105DEFINE_EXPRESSION_OP_BINARY(<=, cmp_le)
106DEFINE_EXPRESSION_OP_BINARY(>, cmp_gt)
107DEFINE_EXPRESSION_OP_BINARY(>=, cmp_ge)
108DEFINE_EXPRESSION_OP_BINARY(==, cmp_eq)
109DEFINE_EXPRESSION_OP_BINARY(!=, cmp_ne)
110
111DEFINE_EXPRESSION_FUNC_BINARY(min)
112DEFINE_EXPRESSION_FUNC_BINARY(max)
113DEFINE_EXPRESSION_FUNC_BINARY(atan2)
114DEFINE_EXPRESSION_FUNC_BINARY(pow)
115DEFINE_EXPRESSION_FUNC_BINARY(truediv)
116DEFINE_EXPRESSION_FUNC_BINARY(floordiv)
117DEFINE_EXPRESSION_FUNC_BINARY(bit_shr)
118
119DEFINE_EXPRESSION_FUNC_TERNARY(select)
120DEFINE_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