Skip to content

Commit 6a7bd2e

Browse files
committed
Add operator overloads for constants
1 parent c0939d0 commit 6a7bd2e

File tree

1 file changed

+39
-21
lines changed

1 file changed

+39
-21
lines changed

include/kernel_float/constant.h

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ namespace kernel_float {
99
template<typename T = double>
1010
struct constant {
1111
template<typename R>
12-
KERNEL_FLOAT_INLINE explicit constexpr constant(const constant<R>& that) : value_(that.get()) {}
12+
KERNEL_FLOAT_INLINE explicit constexpr constant(const constant<R>& that) {
13+
auto f = ops::cast<R, T>();
14+
value_ = f(that.get());
15+
}
1316

1417
KERNEL_FLOAT_INLINE
1518
constexpr constant(T value = {}) : value_(value) {}
@@ -70,28 +73,43 @@ struct cast<constant<T>, R, m> {
7073
};
7174
} // namespace ops
7275

73-
#define KERNEL_FLOAT_CONSTANT_DEFINE_OP(OP) \
74-
template<typename L, typename R> \
75-
R operator OP(const constant<L>& left, const R& right) { \
76-
using T = vector_value_type<R>; \
77-
return operator OP(T(left.get()), right); \
78-
} \
79-
\
80-
template<typename L, typename R> \
81-
L operator OP(const L& left, const constant<R>& right) { \
82-
using T = vector_value_type<L>; \
83-
return operator OP(left, T(right.get())); \
84-
} \
85-
\
86-
template<typename L, typename R, typename T = promote_t<L, R>> \
87-
constant<T> operator OP(const constant<L>& left, const constant<R>& right) { \
88-
return constant<T>(operator OP(T(left.get()), T(right.get()))); \
76+
#define KERNEL_FLOAT_CONSTANT_DEFINE_OP(OP) \
77+
template<typename L, typename R> \
78+
KERNEL_FLOAT_INLINE auto operator OP(const constant<L>& left, const R& right) { \
79+
auto f = ops::cast<L, vector_value_type<R>>(); \
80+
return f(left.get()) OP right; \
81+
} \
82+
\
83+
template<typename L, typename R> \
84+
KERNEL_FLOAT_INLINE auto operator OP(const L& left, const constant<R>& right) { \
85+
auto f = ops::cast<R, vector_value_type<L>>(); \
86+
return left OP f(right.get()); \
87+
} \
88+
\
89+
template<typename L, typename R, typename E> \
90+
KERNEL_FLOAT_INLINE auto operator OP(const constant<L>& left, const vector<R, E>& right) { \
91+
auto f = ops::cast<L, R>(); \
92+
return f(left.get()) OP right; \
93+
} \
94+
\
95+
template<typename L, typename R, typename E> \
96+
KERNEL_FLOAT_INLINE auto operator OP(const vector<L, E>& left, const constant<R>& right) { \
97+
auto f = ops::cast<R, L>(); \
98+
return left OP f(right.get()); \
99+
} \
100+
\
101+
template<typename L, typename R, typename T = promote_t<L, R>> \
102+
KERNEL_FLOAT_INLINE constant<T> operator OP( \
103+
const constant<L>& left, \
104+
const constant<R>& right) { \
105+
return constant<T>(left.get()) OP constant<T>(right.get()); \
89106
}
90107

91-
//KERNEL_FLOAT_CONSTANT_DEFINE_OP(+)
92-
//KERNEL_FLOAT_CONSTANT_DEFINE_OP(-)
93-
//KERNEL_FLOAT_CONSTANT_DEFINE_OP(*)
94-
//KERNEL_FLOAT_CONSTANT_DEFINE_OP(/)
108+
KERNEL_FLOAT_CONSTANT_DEFINE_OP(+)
109+
KERNEL_FLOAT_CONSTANT_DEFINE_OP(-)
110+
KERNEL_FLOAT_CONSTANT_DEFINE_OP(*)
111+
KERNEL_FLOAT_CONSTANT_DEFINE_OP(/)
112+
KERNEL_FLOAT_CONSTANT_DEFINE_OP(%)
95113

96114
} // namespace kernel_float
97115

0 commit comments

Comments
 (0)