conv.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
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 
19 namespace CUDF_EXPORT cudf {
39 template <typename Fixed,
40  typename Floating,
41  CUDF_ENABLE_IF(cuda::std::is_floating_point_v<Floating>&& is_fixed_point<Fixed>())>
43 {
44  using Rep = typename Fixed::rep;
45  auto const value = [&]() {
46  if constexpr (Fixed::rad == numeric::Radix::BASE_10) {
47  return numeric::detail::convert_floating_to_integral<Rep>(floating, scale);
48  } else {
49  return static_cast<Rep>(numeric::detail::shift<Rep, Fixed::rad>(floating, scale));
50  }
51  }();
52 
53  return Fixed(numeric::scaled_integer<Rep>{value, scale});
54 }
55 
69 template <typename Floating,
70  typename Fixed,
71  CUDF_ENABLE_IF(cuda::std::is_floating_point_v<Floating>&& is_fixed_point<Fixed>())>
73 {
74  using Rep = typename Fixed::rep;
75  if constexpr (Fixed::rad == numeric::Radix::BASE_10) {
76  return numeric::detail::convert_integral_to_floating<Floating>(fixed.value(), fixed.scale());
77  } else {
78  auto const casted = static_cast<Floating>(fixed.value());
79  auto const scale = numeric::scale_type{-fixed.scale()};
80  return numeric::detail::shift<Rep, Fixed::rad>(casted, scale);
81  }
82 }
83 
92 template <typename Floating,
93  typename Input,
94  CUDF_ENABLE_IF(cuda::std::is_floating_point_v<Floating>)>
96 {
97  if constexpr (is_fixed_point<Input>()) {
98  return convert_fixed_to_floating<Floating>(input);
99  } else {
100  return static_cast<Floating>(input);
101  }
102 }
103  // end of group
105 } // 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:72
CUDF_HOST_DEVICE Floating convert_to_floating(Input input)
Convert a value to floating point.
Definition: conv.hpp:95
scale_type
The scale type for fixed_point.
Definition: fixed_point.hpp:38
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:42
#define CUDF_ENABLE_IF(...)
Convenience macro for SFINAE as an unnamed template parameter.
Definition: traits.hpp:43
cuDF interfaces
Definition: host_udf.hpp:26
Helper struct for constructing fixed_point when value is already shifted.
Type traits for classifying and querying properties of cudf column and scalar types.
Type declarations for libcudf.
#define CUDF_HOST_DEVICE
Indicates that the function or method is usable on host and device.
Definition: types.hpp:21