Skip to content

Commit 257335f

Browse files
committed
Move cast_to to from memory.h to conversion.h
1 parent c51828f commit 257335f

File tree

2 files changed

+41
-41
lines changed

2 files changed

+41
-41
lines changed

include/kernel_float/conversion.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,47 @@ KERNEL_FLOAT_INLINE vector<R, extent<N>> convert(const V& input, extent<N> new_s
193193
return convert_storage(input);
194194
}
195195

196+
template<typename T, RoundingMode M = RoundingMode::ANY>
197+
struct AssignConversionProxy {
198+
KERNEL_FLOAT_INLINE
199+
explicit AssignConversionProxy(T* ptr) : ptr_(ptr) {}
200+
201+
template<typename U>
202+
KERNEL_FLOAT_INLINE AssignConversionProxy& operator=(U&& values) {
203+
*ptr_ = detail::convert_impl<
204+
vector_value_type<U>,
205+
vector_extent_type<U>,
206+
vector_value_type<T>,
207+
vector_extent_type<T>,
208+
M>::call(into_vector_storage(values));
209+
210+
return *this;
211+
}
212+
213+
private:
214+
T* ptr_;
215+
};
216+
217+
/**
218+
* Takes a vector reference and gives back a helper object. This object helps when you want to assign one vector to another
219+
* vector of a different type. It's a way to enable implicit type conversion.
220+
*
221+
* For example, if `x = expression;` does not compile because `x` and `expression` are different vector types, you can use
222+
* `cast_to(x) = expression;` to make it work.
223+
*
224+
* Example
225+
* =======
226+
* ```
227+
* vec<float, 2> x;
228+
* vec<double, 2> y = {1.0, 2.0};
229+
* cast_to(x) = y; // Normally, `x = y;` would give an error, but `cast_to` fixes that.
230+
* ```
231+
*/
232+
template<typename T, RoundingMode M = RoundingMode::ANY>
233+
KERNEL_FLOAT_INLINE AssignConversionProxy<T, M> cast_to(T& input) {
234+
return AssignConversionProxy<T, M>(&input);
235+
}
236+
196237
/**
197238
* Returns a vector containing `N` copies of `value`.
198239
*

include/kernel_float/memory.h

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -215,47 +215,6 @@ KERNEL_FLOAT_INLINE void storen(const V& values, T* ptr, size_t offset, size_t m
215215
return store(values, ptr, indices, indices < max_length);
216216
}
217217

218-
template<typename T, size_t N>
219-
struct AssignConversionProxy {
220-
KERNEL_FLOAT_INLINE
221-
explicit AssignConversionProxy(T* ptr) : ptr_(ptr) {}
222-
223-
template<typename U>
224-
KERNEL_FLOAT_INLINE AssignConversionProxy& operator=(U&& values) {
225-
auto indices = range<size_t, N>();
226-
detail::store_impl<T, N>::call(
227-
ptr_,
228-
convert_storage<T, N>(std::forward<U>(values)).data(),
229-
indices.data());
230-
231-
return *this;
232-
}
233-
234-
private:
235-
T* ptr_;
236-
};
237-
238-
/**
239-
* Takes a reference to a vector and returns a special proxy object that automatically performs the correct conversion
240-
* when a vector of a different element type is assigned. This is useful to perform implicit type conversions.
241-
*
242-
* For example, let assume that a line like `x = expression;` would not compile since `x` and `expressions` are
243-
* vectors of different element types. Then it is possible to use `cast_to(x) = expression;` to fix this error,
244-
* which possibly introduces a type conversion.
245-
*
246-
* Example
247-
* =======
248-
* ```
249-
* vec<float, 2> x;
250-
* vec<double, 2> y = {1.0, 2.0};
251-
* cast_to(x) = y; // normally, the line `x = y;` would not compile, but `cast_to` make this possible
252-
* ```
253-
*/
254-
template<typename T, typename E>
255-
KERNEL_FLOAT_INLINE AssignConversionProxy<T, E::value> cast_to(vector<T, E>& input) {
256-
return AssignConversionProxy<T, E::value>(input.data());
257-
}
258-
259218
/**
260219
* Returns the original pointer ``ptr`` and hints to the compiler that this pointer is aligned to ``alignment`` bytes.
261220
* If this is not actually the case, compiler optimizations will break things and generate invalid code. Be careful!

0 commit comments

Comments
 (0)