conv.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2025, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <cudf/fixed_point/detail/floating_conversion.hpp>
21 #include <cudf/types.hpp>
22 #include <cudf/utilities/export.hpp>
24 
25 namespace CUDF_EXPORT cudf {
47 template <typename Fixed,
48  typename Floating,
49  CUDF_ENABLE_IF(cuda::std::is_floating_point_v<Floating>&& is_fixed_point<Fixed>())>
51 {
52  using Rep = typename Fixed::rep;
53  auto const value = [&]() {
54  if constexpr (Fixed::rad == numeric::Radix::BASE_10) {
55  return numeric::detail::convert_floating_to_integral<Rep>(floating, scale);
56  } else {
57  return static_cast<Rep>(numeric::detail::shift<Rep, Fixed::rad>(floating, scale));
58  }
59  }();
60 
61  return Fixed(numeric::scaled_integer<Rep>{value, scale});
62 }
63 
77 template <typename Floating,
78  typename Fixed,
79  CUDF_ENABLE_IF(cuda::std::is_floating_point_v<Floating>&& is_fixed_point<Fixed>())>
81 {
82  using Rep = typename Fixed::rep;
83  if constexpr (Fixed::rad == numeric::Radix::BASE_10) {
84  return numeric::detail::convert_integral_to_floating<Floating>(fixed.value(), fixed.scale());
85  } else {
86  auto const casted = static_cast<Floating>(fixed.value());
87  auto const scale = numeric::scale_type{-fixed.scale()};
88  return numeric::detail::shift<Rep, Fixed::rad>(casted, scale);
89  }
90 }
91 
100 template <typename Floating,
101  typename Input,
102  CUDF_ENABLE_IF(cuda::std::is_floating_point_v<Floating>)>
104 {
105  if constexpr (is_fixed_point<Input>()) {
106  return convert_fixed_to_floating<Floating>(input);
107  } else {
108  return static_cast<Floating>(input);
109  }
110 }
111  // end of group
113 } // namespace CUDF_EXPORT cudf
Class definition for fixed point data type.
CUDF_HOST_DEVICE Floating convert_fixed_to_floating(Fixed fixed)
Convert a fixed-point value to floating point.
Definition: conv.hpp:80
CUDF_HOST_DEVICE Floating convert_to_floating(Input input)
Convert a value to floating point.
Definition: conv.hpp:103
scale_type
The scale type for fixed_point.
Definition: fixed_point.hpp:44
CUDF_HOST_DEVICE Fixed convert_floating_to_fixed(Floating floating, numeric::scale_type scale)
Convert a floating-point value to fixed point.
Definition: conv.hpp:50
#define CUDF_ENABLE_IF(...)
Convenience macro for SFINAE as an unnamed template parameter.
Definition: traits.hpp:50
cuDF interfaces
Definition: host_udf.hpp:37
Helper struct for constructing fixed_point when value is already shifted.
Type declarations for libcudf.
#define CUDF_HOST_DEVICE
Indicates that the function or method is usable on host and device.
Definition: types.hpp:32