conv.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #pragma once
7 
8 #include <cudf/fixed_point/detail/floating_conversion.hpp>
10 #include <cudf/types.hpp>
11 #include <cudf/utilities/export.hpp>
13 
14 namespace CUDF_EXPORT cudf {
36 template <typename Fixed,
37  typename Floating,
38  CUDF_ENABLE_IF(cuda::std::is_floating_point_v<Floating>&& is_fixed_point<Fixed>())>
40 {
41  using Rep = typename Fixed::rep;
42  auto const value = [&]() {
43  if constexpr (Fixed::rad == numeric::Radix::BASE_10) {
44  return numeric::detail::convert_floating_to_integral<Rep>(floating, scale);
45  } else {
46  return static_cast<Rep>(numeric::detail::shift<Rep, Fixed::rad>(floating, scale));
47  }
48  }();
49 
50  return Fixed(numeric::scaled_integer<Rep>{value, scale});
51 }
52 
66 template <typename Floating,
67  typename Fixed,
68  CUDF_ENABLE_IF(cuda::std::is_floating_point_v<Floating>&& is_fixed_point<Fixed>())>
70 {
71  using Rep = typename Fixed::rep;
72  if constexpr (Fixed::rad == numeric::Radix::BASE_10) {
73  return numeric::detail::convert_integral_to_floating<Floating>(fixed.value(), fixed.scale());
74  } else {
75  auto const casted = static_cast<Floating>(fixed.value());
76  auto const scale = numeric::scale_type{-fixed.scale()};
77  return numeric::detail::shift<Rep, Fixed::rad>(casted, scale);
78  }
79 }
80 
89 template <typename Floating,
90  typename Input,
91  CUDF_ENABLE_IF(cuda::std::is_floating_point_v<Floating>)>
93 {
94  if constexpr (is_fixed_point<Input>()) {
95  return convert_fixed_to_floating<Floating>(input);
96  } else {
97  return static_cast<Floating>(input);
98  }
99 }
100  // end of group
102 } // 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:69
CUDF_HOST_DEVICE Floating convert_to_floating(Input input)
Convert a value to floating point.
Definition: conv.hpp:92
scale_type
The scale type for fixed_point.
Definition: fixed_point.hpp:33
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:39
#define CUDF_ENABLE_IF(...)
Convenience macro for SFINAE as an unnamed template parameter.
Definition: traits.hpp:39
cuDF interfaces
Definition: host_udf.hpp:26
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:21