8 #include <cudf/detail/utilities/assert.cuh>
9 #include <cudf/fixed_point/temporary.hpp>
12 #include <cuda/numeric>
13 #include <cuda/std/cassert>
14 #include <cuda/std/cmath>
15 #include <cuda/std/functional>
16 #include <cuda/std/limits>
17 #include <cuda/std/type_traits>
18 #include <cuda/std/utility>
20 #ifndef __CUDACC_RTC__
46 enum class Radix : int32_t { BASE_2 = 2, BASE_10 = 10 };
57 return cuda::std::is_same_v<T, int32_t> ||
58 cuda::std::is_same_v<T, int64_t> ||
59 cuda::std::is_same_v<T, __int128_t>;
75 template <
typename Rep,
78 typename cuda::std::enable_if_t<(cuda::std::is_same_v<int32_t, T> &&
79 cuda::std::is_integral_v<Rep>)>* =
nullptr>
82 cudf_assert(exponent >= 0 &&
"integer exponentiation with negative exponent is not possible.");
84 if constexpr (Base == numeric::Radix::BASE_2) {
return static_cast<Rep
>(1) << exponent; }
90 if (exponent == 0) {
return static_cast<Rep
>(1); }
91 auto extra =
static_cast<Rep
>(1);
92 auto square =
static_cast<Rep
>(Base);
93 while (exponent > 1) {
94 if (exponent & 1) { extra *= square; }
98 return square * extra;
112 template <
typename Rep, Radix Rad,
typename T>
115 return val / ipow<Rep, Rad>(
static_cast<int32_t
>(scale));
129 template <
typename Rep, Radix Rad,
typename T>
132 return val * ipow<Rep, Rad>(
static_cast<int32_t
>(-scale));
148 template <
typename Rep, Radix Rad,
typename T>
151 if (scale == 0) {
return val; }
152 if (scale > 0) {
return right_shift<Rep, Rad>(val, scale); }
153 return left_shift<Rep, Rad>(val, scale);
176 template <
typename Rep,
177 typename cuda::std::enable_if_t<is_supported_representation_type<Rep>()>* =
nullptr>
199 template <
typename Rep, Radix Rad>
206 static constexpr
auto rad = Rad;
216 template <
typename T,
217 typename cuda::std::enable_if_t<cuda::std::is_integral_v<T> &&
218 is_supported_representation_type<Rep>()>* =
nullptr>
222 : _value{detail::shift<Rep, Rad>(static_cast<Rep>(value), scale)}, _scale{scale}
232 : _value{s.value}, _scale{s.scale}
243 template <
typename T,
typename cuda::std::enable_if_t<cuda::std::is_
integral_v<T>>* =
nullptr>
245 : _value{static_cast<Rep>(value)}, _scale{
scale_type{0}}
261 template <
typename U,
typename cuda::std::enable_if_t<cuda::std::is_
integral_v<U>>* =
nullptr>
267 auto const value = cuda::std::common_type_t<U, Rep>(_value);
268 return static_cast<U
>(detail::shift<Rep, Rad>(value,
scale_type{-_scale}));
302 return static_cast<bool>(_value);
313 template <
typename Rep1, Radix Rad1>
328 template <
typename Rep1, Radix Rad1>
343 template <
typename Rep1, Radix Rad1>
358 template <
typename Rep1, Radix Rad1>
389 template <
typename Rep1, Radix Rad1>
406 template <
typename Rep1, Radix Rad1>
421 template <
typename Rep1, Radix Rad1>
436 template <
typename Rep1, Radix Rad1>
453 template <
typename Rep1, Radix Rad1>
470 template <
typename Rep1, Radix Rad1>
487 template <
typename Rep1, Radix Rad1>
504 template <
typename Rep1, Radix Rad1>
521 template <
typename Rep1, Radix Rad1>
538 template <
typename Rep1, Radix Rad1>
555 template <
typename Rep1, Radix Rad1>
570 if (scale == _scale) {
return *
this; }
571 Rep
const value = detail::shift<Rep, Rad>(_value,
scale_type{scale - _scale});
575 #ifndef __CUDACC_RTC__
580 explicit operator std::string()
const
583 auto const av = detail::abs(_value);
584 Rep
const n = detail::exp10<Rep>(-_scale);
585 Rep
const f = av % n;
586 auto const num_zeros =
587 std::max(0, (-_scale -
static_cast<int32_t
>(detail::to_string(f).size())));
588 auto const zeros = std::string(num_zeros,
'0');
589 auto const sign = _value < 0 ? std::string(
"-") : std::string();
590 return sign + detail::to_string(av / n) + std::string(
".") + zeros +
591 detail::to_string(av % n);
593 auto const zeros = std::string(_scale,
'0');
594 return detail::to_string(_value) + zeros;
609 template <
typename Rep,
typename T>
612 return cuda::add_overflow<Rep>(lhs, rhs).overflow;
623 template <
typename Rep,
typename T>
626 return cuda::sub_overflow<Rep>(lhs, rhs).overflow;
637 template <
typename Rep,
typename T>
640 return cuda::div_overflow<Rep>(lhs, rhs).overflow;
651 template <
typename Rep,
typename T>
654 return cuda::mul_overflow<Rep>(lhs, rhs).overflow;
658 template <
typename Rep1, Radix Rad1>
662 auto const scale = cuda::std::min(lhs._scale, rhs._scale);
665 #if defined(__CUDACC_DEBUG__)
667 assert(!addition_overflow<Rep1>(lhs.
rescaled(scale)._value, rhs.
rescaled(scale)._value) &&
668 "fixed_point overflow");
676 template <
typename Rep1, Radix Rad1>
680 auto const scale = cuda::std::min(lhs._scale, rhs._scale);
683 #if defined(__CUDACC_DEBUG__)
685 assert(!subtraction_overflow<Rep1>(lhs.
rescaled(scale)._value, rhs.
rescaled(scale)._value) &&
686 "fixed_point overflow");
694 template <
typename Rep1, Radix Rad1>
698 #if defined(__CUDACC_DEBUG__)
700 assert(!multiplication_overflow<Rep1>(lhs._value, rhs._value) &&
"fixed_point overflow");
709 template <
typename Rep1, Radix Rad1>
713 #if defined(__CUDACC_DEBUG__)
715 assert(!division_overflow<Rep1>(lhs._value, rhs._value) &&
"fixed_point overflow");
724 template <
typename Rep1, Radix Rad1>
728 auto const scale = cuda::std::min(lhs._scale, rhs._scale);
733 template <
typename Rep1, Radix Rad1>
737 auto const scale = cuda::std::min(lhs._scale, rhs._scale);
742 template <
typename Rep1, Radix Rad1>
746 auto const scale = cuda::std::min(lhs._scale, rhs._scale);
751 template <
typename Rep1, Radix Rad1>
755 auto const scale = cuda::std::min(lhs._scale, rhs._scale);
760 template <
typename Rep1, Radix Rad1>
764 auto const scale = cuda::std::min(lhs._scale, rhs._scale);
769 template <
typename Rep1, Radix Rad1>
773 auto const scale = cuda::std::min(lhs._scale, rhs._scale);
778 template <
typename Rep1, Radix Rad1>
782 auto const scale = cuda::std::min(lhs._scale, rhs._scale);
783 auto const remainder = lhs.
rescaled(scale)._value % rhs.
rescaled(scale)._value;
787 template <
typename Rep>
A type for representing a number with a fixed amount of precision.
CUDF_HOST_DEVICE fixed_point(scaled_integer< Rep > s)
Constructor that will not perform shifting (assumes value already shifted)
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 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 scale_type scale() const
Method that returns the scale of the fixed_point number.
CUDF_HOST_DEVICE fixed_point(T const &value)
"Scale-less" constructor that constructs fixed_point number with a specified value and scale of zero
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 &rhs)
operator +=
Rep rep
The representation type.
CUDF_HOST_DEVICE fixed_point()
Default constructor that constructs fixed_point number with a value and scale of zero.
CUDF_HOST_DEVICE fixed_point< Rep1, Rad1 > & operator/=(fixed_point< Rep1, Rad1 > const &rhs)
operator /=
CUDF_HOST_DEVICE fixed_point(T const &value, scale_type const &scale)
Constructor that will perform shifting to store value appropriately (from integral types)
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
constexpr CUDF_HOST_DEVICE Rep ipow(T exponent)
A function for integer exponentiation by squaring.
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
Radix
Scoped enumerator to use when constructing 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 bool operator>=(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
CUDF_HOST_DEVICE bool operator<=(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
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)
CUDF_HOST_DEVICE auto division_overflow(T lhs, T rhs)
Function for identifying integer overflow when dividing.
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 fixed_point< Rep1, Rad1 > operator*(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
CUDF_HOST_DEVICE bool operator>(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
CUDF_HOST_DEVICE auto addition_overflow(T lhs, T rhs)
Function for identifying integer overflow when adding.
CUDF_HOST_DEVICE fixed_point< Rep1, Rad1 > operator+(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
CUDF_HOST_DEVICE auto multiplication_overflow(T lhs, T rhs)
Function for identifying integer overflow when multiplying.
constexpr CUDF_HOST_DEVICE auto is_supported_representation_type()
Returns true if the representation type is supported by fixed_point
CUDF_HOST_DEVICE bool operator!=(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
CUDF_HOST_DEVICE auto subtraction_overflow(T lhs, T rhs)
Function for identifying integer overflow when subtracting.
CUDF_HOST_DEVICE bool operator<(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
fixed_point and supporting types
Helper struct for constructing fixed_point when value is already shifted.
Rep value
The value of the fixed point number.
CUDF_HOST_DEVICE scaled_integer(Rep v, scale_type s)
Constructor for scaled_integer
scale_type scale
The scale of the value.
Type declarations for libcudf.
#define CUDF_HOST_DEVICE
Indicates that the function or method is usable on host and device.