vir-simd 0.4.189
Parallelism TS 2 extensions and simd fallback implementation
Loading...
Searching...
No Matches
simd_cvt.h
1/* SPDX-License-Identifier: LGPL-3.0-or-later */
2/* Copyright © 2023–2024 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH
3 * Matthias Kretz <m.kretz@gsi.de>
4 */
5
6#ifndef VIR_SIMD_CVT_H_
7#define VIR_SIMD_CVT_H_
8
9#include "simd_concepts.h"
10#define VIR_HAVE_SIMD_CVT 1
11
12namespace vir
13{
14#if not VIR_HAVE_SIMD_CONCEPTS
15 namespace detail
16 {
17 template <typename From, typename To, typename = void>
18 struct can_static_simd_cast
19 : std::false_type
20 {};
21
22 template <typename From, typename To>
23 struct can_static_simd_cast<From, To, std::void_t<decltype(stdx::static_simd_cast<To>(
24 std::declval<const From&>()))>>
25 : std::is_same<decltype(stdx::static_simd_cast<To>(std::declval<const From&>())), To>
26 {};
27 }
28#endif
29
30 template <typename T>
31 class cvt
32 {
33 const T& ref;
34
35 public:
36 constexpr
37 cvt(const T& x)
38 : ref(x)
39 {}
40
41 cvt(const cvt&) = delete;
42
43#if VIR_HAVE_SIMD_CONCEPTS
44 template <typename U>
45 requires std::convertible_to<T, U> or requires(const T&x)
46 {
47 { stdx::static_simd_cast<U>(x) } -> std::same_as<U>;
48 }
49#else
50 template <typename U, typename = std::enable_if_t<
51 std::disjunction_v<std::is_convertible<T, U>,
52 detail::can_static_simd_cast<T, U>>>>
53#endif
54 constexpr
55 operator U() const
56 {
57 if constexpr (std::is_convertible_v<T, U>)
58 return ref;
59 else
60 return stdx::static_simd_cast<U>(ref);
61 }
62 };
63}
64
65#endif // VIR_SIMD_CVT_H_
66// vim: et cc=101 tw=100 sw=2 ts=8
This namespace collects libraries and tools authored by Matthias Kretz.
Definition constexpr_wrapper.h:21
C++20 concepts extending the Parallelism TS 2 (which is limited to C++17).