Go to the documentation of this file.
19 #include <cudf/detail/utilities/assert.cuh>
20 #include <cudf/fixed_point/temporary.hpp>
25 #include <cuda/std/climits>
26 #include <cuda/std/limits>
27 #include <cuda/std/type_traits>
49 enum class Radix : int32_t { BASE_2 = 2, BASE_10 = 10 };
60 return cuda::std::is_same_v<T, int32_t> ||
61 cuda::std::is_same_v<T, int64_t> ||
62 cuda::std::is_same_v<T, __int128_t>;
74 return cuda::std::is_integral<T>() || cuda::std::is_floating_point_v<T>;
91 template <
typename Rep,
94 typename cuda::std::enable_if_t<(cuda::std::is_same_v<int32_t, T> &&
95 is_supported_representation_type<Rep>())>* =
nullptr>
96 CUDF_HOST_DEVICE
inline Rep
ipow(T exponent)
98 cudf_assert(exponent >= 0 &&
"integer exponentiation with negative exponent is not possible.");
99 if (exponent == 0) {
return static_cast<Rep
>(1); }
101 auto extra =
static_cast<Rep
>(1);
102 auto square =
static_cast<Rep
>(Base);
103 while (exponent > 1) {
111 return square * extra;
125 template <
typename Rep, Radix Rad,
typename T>
128 return val / ipow<Rep, Rad>(
static_cast<int32_t
>(scale));
142 template <
typename Rep, Radix Rad,
typename T>
145 return val * ipow<Rep, Rad>(
static_cast<int32_t
>(-scale));
161 template <
typename Rep, Radix Rad,
typename T>
162 CUDF_HOST_DEVICE
inline constexpr T shift(T
const& val,
scale_type const& scale)
164 if (scale == 0) {
return val; }
165 if (scale > 0) {
return right_shift<Rep, Rad>(val, scale); }
166 return left_shift<Rep, Rad>(val, scale);
189 template <
typename Rep,
190 typename cuda::std::enable_if_t<is_supported_representation_type<Rep>()>* =
nullptr>
212 template <
typename Rep, Radix Rad>
228 template <
typename T,
229 typename cuda::std::enable_if_t<cuda::std::is_floating_point<T>() &&
230 is_supported_representation_type<Rep>()>* =
nullptr>
232 : _value{
static_cast<Rep
>(detail::shift<Rep, Rad>(
value,
scale))}, _scale{
scale}
244 template <
typename T,
245 typename cuda::std::enable_if_t<cuda::std::is_integral<T>() &&
246 is_supported_representation_type<Rep>()>* =
nullptr>
250 : _value{detail::shift<Rep, Rad>(
static_cast<Rep
>(
value),
scale)}, _scale{
scale}
260 : _value{s.
value}, _scale{s.scale}
271 template <
typename T,
272 typename cuda::std::enable_if_t<is_supported_construction_value_type<T>()>* =
nullptr>
290 template <
typename U,
291 typename cuda::std::enable_if_t<cuda::std::is_floating_point_v<U>>* =
nullptr>
292 explicit constexpr
operator U()
const
294 return detail::shift<Rep, Rad>(
static_cast<U
>(_value),
scale_type{-_scale});
303 template <
typename U,
typename cuda::std::enable_if_t<cuda::std::is_
integral_v<U>>* =
nullptr>
304 explicit constexpr
operator U()
const
309 auto const value = std::common_type_t<U, Rep>(_value);
310 return static_cast<U
>(detail::shift<Rep, Rad>(
value,
scale_type{-_scale}));
328 CUDF_HOST_DEVICE
inline rep value()
const {
return _value; }
342 CUDF_HOST_DEVICE
inline explicit constexpr
operator bool()
const
344 return static_cast<bool>(_value);
355 template <
typename Rep1, Radix Rad1>
370 template <
typename Rep1, Radix Rad1>
385 template <
typename Rep1, Radix Rad1>
400 template <
typename Rep1, Radix Rad1>
431 template <
typename Rep1, Radix Rad1>
448 template <
typename Rep1, Radix Rad1>
463 template <
typename Rep1, Radix Rad1>
478 template <
typename Rep1, Radix Rad1>
495 template <
typename Rep1, Radix Rad1>
512 template <
typename Rep1, Radix Rad1>
529 template <
typename Rep1, Radix Rad1>
546 template <
typename Rep1, Radix Rad1>
563 template <
typename Rep1, Radix Rad1>
580 template <
typename Rep1, Radix Rad1>
597 template <
typename Rep1, Radix Rad1>
612 if (
scale == _scale) {
return *
this; }
620 explicit operator std::string()
const
623 auto const av = detail::abs(_value);
624 Rep
const n = detail::exp10<Rep>(-_scale);
625 Rep
const f = av % n;
626 auto const num_zeros =
627 std::max(0, (-_scale -
static_cast<int32_t
>(detail::to_string(f).size())));
628 auto const zeros = std::string(num_zeros,
'0');
629 auto const sign = _value < 0 ? std::string(
"-") : std::string();
630 return sign + detail::to_string(av / n) + std::string(
".") + zeros +
631 detail::to_string(av % n);
633 auto const zeros = std::string(_scale,
'0');
634 return detail::to_string(_value) + zeros;
647 template <
typename Rep,
typename T>
650 return rhs > 0 ? lhs > cuda::std::numeric_limits<Rep>::max() - rhs
651 : lhs < cuda::std::numeric_limits<Rep>::min() - rhs;
662 template <
typename Rep,
typename T>
665 return rhs > 0 ? lhs < cuda::std::numeric_limits<Rep>::min() + rhs
666 : lhs > cuda::std::numeric_limits<Rep>::max() + rhs;
677 template <
typename Rep,
typename T>
680 return lhs == cuda::std::numeric_limits<Rep>::min() && rhs == -1;
691 template <
typename Rep,
typename T>
694 auto const min = cuda::std::numeric_limits<Rep>::min();
695 auto const max = cuda::std::numeric_limits<Rep>::max();
696 if (rhs > 0) {
return lhs > max / rhs || lhs < min / rhs; }
697 if (rhs < -1) {
return lhs > min / rhs || lhs < max / rhs; }
698 return rhs == -1 && lhs == min;
702 template <
typename Rep1, Radix Rad1>
706 auto const scale = std::min(lhs._scale, rhs._scale);
709 #if defined(__CUDACC_DEBUG__)
711 assert(!addition_overflow<Rep1>(lhs.
rescaled(scale)._value, rhs.
rescaled(scale)._value) &&
712 "fixed_point overflow");
720 template <
typename Rep1, Radix Rad1>
724 auto const scale = std::min(lhs._scale, rhs._scale);
727 #if defined(__CUDACC_DEBUG__)
729 assert(!subtraction_overflow<Rep1>(lhs.
rescaled(scale)._value, rhs.
rescaled(scale)._value) &&
730 "fixed_point overflow");
738 template <
typename Rep1, Radix Rad1>
742 #if defined(__CUDACC_DEBUG__)
744 assert(!multiplication_overflow<Rep1>(lhs._value, rhs._value) &&
"fixed_point overflow");
753 template <
typename Rep1, Radix Rad1>
757 #if defined(__CUDACC_DEBUG__)
759 assert(!division_overflow<Rep1>(lhs._value, rhs._value) &&
"fixed_point overflow");
768 template <
typename Rep1, Radix Rad1>
772 auto const scale = std::min(lhs._scale, rhs._scale);
777 template <
typename Rep1, Radix Rad1>
781 auto const scale = std::min(lhs._scale, rhs._scale);
786 template <
typename Rep1, Radix Rad1>
790 auto const scale = std::min(lhs._scale, rhs._scale);
795 template <
typename Rep1, Radix Rad1>
799 auto const scale = std::min(lhs._scale, rhs._scale);
804 template <
typename Rep1, Radix Rad1>
808 auto const scale = std::min(lhs._scale, rhs._scale);
813 template <
typename Rep1, Radix Rad1>
817 auto const scale = std::min(lhs._scale, rhs._scale);
822 template <
typename Rep1, Radix Rad1>
826 auto const scale = std::min(lhs._scale, rhs._scale);
827 auto const remainder = lhs.
rescaled(scale)._value % rhs.
rescaled(scale)._value;
CUDF_HOST_DEVICE fixed_point< Rep, Rad > rescaled(scale_type scale) const
Method for creating a fixed_point number with a new scale
CUDF_HOST_DEVICE scale_type scale() const
Method that returns the scale of the fixed_point number.
CUDF_HOST_DEVICE auto multiplication_overflow(T lhs, T rhs)
Function for identifying integer overflow when multiplying.
CUDF_HOST_DEVICE bool operator>=(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
CUDF_HOST_DEVICE friend bool operator==(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
operator == (for comparing two fixed_point numbers)
CUDF_HOST_DEVICE fixed_point()
Default constructor that constructs fixed_point number with a value and scale of zero.
Radix
Scoped enumerator to use when constructing fixed_point
CUDF_HOST_DEVICE friend fixed_point< Rep1, Rad1 > operator-(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
operator - (for subtracting two fixed_point numbers)
CUDF_HOST_DEVICE fixed_point< Rep1, Rad1 > & operator+=(fixed_point< Rep1, Rad1 > const &rhs)
operator +=
CUDF_HOST_DEVICE fixed_point< Rep1, Rad1 > operator+(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
A type for representing a number with a fixed amount of precision.
CUDF_HOST_DEVICE fixed_point< Rep1, Rad1 > operator/(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
Type declarations for libcudf.
CUDF_HOST_DEVICE friend bool operator!=(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
operator != (for comparing two fixed_point numbers)
CUDF_HOST_DEVICE fixed_point< Rep1, Rad1 > & operator/=(fixed_point< Rep1, Rad1 > const &rhs)
operator /=
scale_type scale
The scale of the value.
CUDF_HOST_DEVICE fixed_point< Rep, Rad > & operator++()
operator ++ (post-increment)
constexpr CUDF_HOST_DEVICE T left_shift(T const &val, scale_type const &scale)
Function that performs a left shift scale "times" on the val
CUDF_HOST_DEVICE Rep ipow(T exponent)
A function for integer exponentiation by squaring.
Rep rep
The representation type.
CUDF_HOST_DEVICE friend bool operator>(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
operator > (for comparing two fixed_point numbers)
CUDF_HOST_DEVICE fixed_point< Rep1, Rad1 > & operator*=(fixed_point< Rep1, Rad1 > const &rhs)
operator *=
CUDF_HOST_DEVICE friend fixed_point< Rep1, Rad1 > operator/(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
operator / (for dividing two fixed_point numbers)
constexpr auto is_supported_construction_value_type()
Returns true if the value type is supported for constructing a fixed_point
CUDF_HOST_DEVICE auto subtraction_overflow(T lhs, T rhs)
Function for identifying integer overflow when subtracting.
CUDF_HOST_DEVICE fixed_point< Rep1, Rad1 > operator*(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
CUDF_HOST_DEVICE friend fixed_point< Rep1, Rad1 > operator%(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
operator % (for computing the modulo operation of two fixed_point numbers)
Helper struct for constructing fixed_point when value is already shifted.
CUDF_HOST_DEVICE friend fixed_point< Rep1, Rad1 > operator+(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
operator + (for adding two fixed_point numbers)
CUDF_HOST_DEVICE auto addition_overflow(T lhs, T rhs)
Function for identifying integer overflow when adding.
constexpr auto is_supported_representation_type()
Returns true if the representation type is supported by fixed_point
CUDF_HOST_DEVICE fixed_point< Rep1, Rad1 > operator-(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
CUDF_HOST_DEVICE scaled_integer(Rep v, scale_type s)
Constructor for scaled_integer
CUDF_HOST_DEVICE auto division_overflow(T lhs, T rhs)
Function for identifying integer overflow when dividing.
Rep value
The value of the fixed point number.
CUDF_HOST_DEVICE bool operator>(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
constexpr CUDF_HOST_DEVICE T right_shift(T const &val, scale_type const &scale)
Function that performs a right shift scale "times" on the val
CUDF_HOST_DEVICE friend bool operator>=(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
operator >= (for comparing two fixed_point numbers)
CUDF_HOST_DEVICE friend bool operator<(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
operator < (for comparing two fixed_point numbers)
CUDF_HOST_DEVICE rep value() const
Method that returns the underlying value of the fixed_point number.
CUDF_HOST_DEVICE fixed_point< Rep1, Rad1 > & operator-=(fixed_point< Rep1, Rad1 > const &rhs)
operator -=
CUDF_HOST_DEVICE friend bool operator<=(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
operator <= (for comparing two fixed_point numbers)
CUDF_HOST_DEVICE bool operator==(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
CUDF_HOST_DEVICE friend fixed_point< Rep1, Rad1 > operator*(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
operator * (for multiplying two fixed_point numbers)
CUDF_HOST_DEVICE bool operator<(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
CUDF_HOST_DEVICE fixed_point< Rep1, Rad1 > operator%(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
scale_type
The scale type for fixed_point.
CUDF_HOST_DEVICE bool operator<=(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
fixed_point and supporting types
CUDF_HOST_DEVICE bool operator!=(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)