File: example_type_traits_interface.cpp

package info (click to toggle)
rocprim 6.4.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,428 kB
  • sloc: cpp: 153,383; python: 1,397; sh: 404; xml: 217; makefile: 119
file content (125 lines) | stat: -rw-r--r-- 4,842 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (c) 2024 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include <rocprim/type_traits.hpp>

#include <iostream>
#include <sstream>
#include <string>

template<class InputType,
         std::enable_if_t<rocprim::traits::get<InputType>().is_floating_point()>* = nullptr>
void some_algo()
{
    constexpr auto input_traits = rocprim::traits::get<InputType>();

    std::stringstream ss;

    ss << "root_type:\t[" << typeid(InputType).name() << "] this type is floating point\n";
    ss << "is_arithmetic:\t" << input_traits.is_arithmetic() << '\n';
    ss << "is_fundamental:\t" << input_traits.is_fundamental() << '\n';
    ss << "is_scalar:\t" << input_traits.is_scalar() << '\n';
    constexpr auto mask = input_traits.float_bit_mask();
    ss << "sign_bit:\t" << mask.sign_bit << '\n';
    ss << "exponent:\t" << mask.exponent << '\n';
    ss << "mantissa:\t" << mask.mantissa << '\n';
    std::cout << ss.str() << "\n";
}

template<class InputType,
         std::enable_if_t<rocprim::traits::get<InputType>().is_integral()>* = nullptr>
void some_algo()
{
    constexpr auto input_traits = rocprim::traits::get<InputType>();

    std::stringstream ss;
    ss << "root_type:\t[" << typeid(InputType).name() << "] this type is integral\n";
    ss << "is_arithmetic:\t" << input_traits.is_arithmetic() << '\n';
    ss << "is_fundamental:\t" << input_traits.is_fundamental() << '\n';
    ss << "is_scalar:\t" << input_traits.is_scalar() << '\n';
    ss << "is_signed:\t" << input_traits.is_signed() << '\n';
    ss << "is_unsigned:\t" << input_traits.is_unsigned() << '\n';

    std::cout << ss.str() << "\n";
}

template<class InputType,
         std::enable_if_t<!rocprim::traits::get<InputType>().is_integral()
                          && !rocprim::traits::get<InputType>().is_floating_point()>* = nullptr>
void some_algo()
{
    constexpr auto    input_traits = rocprim::traits::get<InputType>();
    std::stringstream ss;
    ss << "root_type:\t[" << typeid(InputType).name()
       << "] this type is neither integral nor floating point\n";
    ss << "is_arithmetic:\t" << input_traits.is_arithmetic() << '\n';
    ss << "is_fundamental:\t" << input_traits.is_fundamental() << '\n';
    ss << "is_signed:\t" << input_traits.is_signed() << '\n';
    ss << "is_unsigned:\t" << input_traits.is_unsigned() << '\n';
    ss << "is_scalar:\t" << input_traits.is_scalar() << '\n';

    std::cout << ss.str() << "\n";
}

// Your type definition
struct custom_float_type
{};

// you should add this struct specialization for the type to implement the traits
template<>
struct rocprim::traits::define<custom_float_type>
{
    using is_arithmetic = rocprim::traits::is_arithmetic::values<true>;
    using number_format
        = rocprim::traits::number_format::values<number_format::kind::floating_point_type>;
    using float_bit_mask = rocprim::traits::float_bit_mask::values<uint32_t, 10, 10, 10>;
};

// Your type definition
struct custom_int_type
{};

// you should add this struct specialization for the type to implement the traits
template<>
struct rocprim::traits::define<custom_int_type>
{
    using is_arithmetic = rocprim::traits::is_arithmetic::values<true>;
    using number_format
        = rocprim::traits::number_format::values<number_format::kind::integral_type>;
    using integral_sign
        = rocprim::traits::integral_sign::values<traits::integral_sign::kind::signed_type>;
};

int main()
{
    // C++ types
    some_algo<int>();
    some_algo<float>();
    some_algo<double>();

    // rocprim type
    some_algo<rocprim::bfloat16>();
    some_algo<rocprim::half>();
    some_algo<rocprim::int128_t>();
    some_algo<rocprim::uint128_t>();

    // other types
    some_algo<custom_float_type>();
    some_algo<custom_int_type>();
}