alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
vecConcepts.hpp
Go to the documentation of this file.
1/* Copyright 2024 René Widera
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
7#include <concepts>
8#include <string>
9#include <type_traits>
10
11namespace alpaka
12{
13 namespace concepts
14 {
15 namespace detail
16 {
17 // integral to integral
18 template<typename T_From, typename T_To>
19 constexpr bool integralIntegralLossless
20 = std::is_integral_v<T_From> && std::is_integral_v<T_To>
21 && ((std::is_signed_v<T_From> == std::is_signed_v<T_To>
22 && std::numeric_limits<T_From>::digits <= std::numeric_limits<T_To>::digits)
23 || (std::is_unsigned_v<T_From> && std::is_signed_v<T_To>
24 && std::numeric_limits<T_From>::digits < std::numeric_limits<T_To>::digits));
25
26 // floating-point to floating-point
27 template<typename T_From, typename T_To>
28 constexpr bool floatFloatLossless
29 = std::is_floating_point_v<T_From> && std::is_floating_point_v<T_To>
30 && std::numeric_limits<T_From>::radix == std::numeric_limits<T_To>::radix
31 && std::numeric_limits<T_From>::digits <= std::numeric_limits<T_To>::digits
32 && std::numeric_limits<T_From>::max_exponent <= std::numeric_limits<T_To>::max_exponent
33 && std::numeric_limits<T_From>::min_exponent >= std::numeric_limits<T_To>::min_exponent;
34
35 // integral to floating-point
36 // numeric_limits::digits for integers excludes the sign bit
37 template<typename T_From, typename T_To>
38 constexpr bool integralFloatLossless = std::is_integral_v<T_From> && std::is_floating_point_v<T_To>
39 && (std::numeric_limits<T_From>::digits + std::is_signed_v<T_From>)
40 <= std::numeric_limits<T_To>::digits;
41 } // namespace detail
42
43 /** Concept to check if a type can be lossless converted to another type.
44 *
45 * This concept ensures that a type `T_From` can be converted to a type `T_To` without any loss of information.
46 * It checks for implicit convertibility, signedness compatibility, and precision preservation for both integer
47 * and floating-point types.
48 *
49 * @tparam T_From The source type to be converted.
50 * @tparam T_To The target type to which the source type is converted.
51 */
52 template<typename T_From, typename T_To>
54 = std::convertible_to<T_From, T_To>
55 && (detail::integralIntegralLossless<T_From, T_To> || detail::floatFloatLossless<T_From, T_To>
56 || detail::integralFloatLossless<T_From, T_To>);
57
58 template<typename T_From, typename T_To>
59 concept Convertible = requires { std::is_convertible_v<T_From, T_To>; };
60 }; // namespace concepts
61} // namespace alpaka
Concept to check if a type can be lossless converted to another type.
main alpaka namespace.
Definition alpaka.hpp:76