19 #include <cudf/detail/utilities/assert.cuh>
20 #include <cudf/fixed_point/temporary.hpp>
23 #include <cuda/std/limits>
24 #include <cuda/std/type_traits>
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>;
71 return cuda::std::is_integral<T>() || cuda::std::is_floating_point_v<T>;
88 template <
typename Rep,
91 typename cuda::std::enable_if_t<(cuda::std::is_same_v<int32_t, T> &&
92 is_supported_representation_type<Rep>())>* =
nullptr>
93 CUDF_HOST_DEVICE
inline Rep
ipow(T exponent)
95 cudf_assert(exponent >= 0 &&
"integer exponentiation with negative exponent is not possible.");
96 if (exponent == 0) {
return static_cast<Rep
>(1); }
98 auto extra =
static_cast<Rep
>(1);
99 auto square =
static_cast<Rep
>(Base);
100 while (exponent > 1) {
108 return square * extra;
122 template <
typename Rep, Radix Rad,
typename T>
125 return val / ipow<Rep, Rad>(
static_cast<int32_t
>(scale));
139 template <
typename Rep, Radix Rad,
typename T>
142 return val * ipow<Rep, Rad>(
static_cast<int32_t
>(-scale));
158 template <
typename Rep, Radix Rad,
typename T>
159 CUDF_HOST_DEVICE
inline constexpr T shift(T
const& val,
scale_type const& scale)
161 if (scale == 0) {
return val; }
162 if (scale > 0) {
return right_shift<Rep, Rad>(val, scale); }
163 return left_shift<Rep, Rad>(val, scale);
186 template <
typename Rep,
187 typename cuda::std::enable_if_t<is_supported_representation_type<Rep>()>* =
nullptr>
209 template <
typename Rep, Radix Rad>
225 template <
typename T,
226 typename cuda::std::enable_if_t<cuda::std::is_floating_point<T>() &&
227 is_supported_representation_type<Rep>()>* =
nullptr>
229 : _value{static_cast<Rep>(detail::shift<Rep, Rad>(
value,
scale))}, _scale{
scale}
241 template <
typename T,
242 typename cuda::std::enable_if_t<cuda::std::is_integral<T>() &&
243 is_supported_representation_type<Rep>()>* =
nullptr>
247 : _value{detail::shift<Rep, Rad>(static_cast<Rep>(
value),
scale)}, _scale{
scale}
268 template <
typename T,
269 typename cuda::std::enable_if_t<is_supported_construction_value_type<T>()>* =
nullptr>
287 template <
typename U,
288 typename cuda::std::enable_if_t<cuda::std::is_floating_point_v<U>>* =
nullptr>
289 explicit constexpr
operator U()
const
291 return detail::shift<Rep, Rad>(
static_cast<U
>(_value),
scale_type{-_scale});
300 template <
typename U,
typename cuda::std::enable_if_t<cuda::std::is_
integral_v<U>>* =
nullptr>
301 explicit constexpr
operator U()
const
306 auto const value = std::common_type_t<U, Rep>(_value);
307 return static_cast<U
>(detail::shift<Rep, Rad>(
value,
scale_type{-_scale}));
325 CUDF_HOST_DEVICE
inline rep value()
const {
return _value; }
339 CUDF_HOST_DEVICE
inline explicit constexpr
operator bool()
const
341 return static_cast<bool>(_value);
352 template <
typename Rep1, Radix Rad1>
367 template <
typename Rep1, Radix Rad1>
382 template <
typename Rep1, Radix Rad1>
397 template <
typename Rep1, Radix Rad1>
428 template <
typename Rep1, Radix Rad1>
445 template <
typename Rep1, Radix Rad1>
460 template <
typename Rep1, Radix Rad1>
475 template <
typename Rep1, Radix Rad1>
492 template <
typename Rep1, Radix Rad1>
509 template <
typename Rep1, Radix Rad1>
526 template <
typename Rep1, Radix Rad1>
543 template <
typename Rep1, Radix Rad1>
560 template <
typename Rep1, Radix Rad1>
577 template <
typename Rep1, Radix Rad1>
594 template <
typename Rep1, Radix Rad1>
609 if (
scale == _scale) {
return *
this; }
617 explicit operator std::string()
const
620 auto const av = detail::abs(_value);
621 Rep
const n = detail::exp10<Rep>(-_scale);
622 Rep
const f = av % n;
623 auto const num_zeros =
624 std::max(0, (-_scale -
static_cast<int32_t
>(detail::to_string(f).size())));
625 auto const zeros = std::string(num_zeros,
'0');
626 auto const sign = _value < 0 ? std::string(
"-") : std::string();
627 return sign + detail::to_string(av / n) + std::string(
".") + zeros +
628 detail::to_string(av % n);
630 auto const zeros = std::string(_scale,
'0');
631 return detail::to_string(_value) + zeros;
644 template <
typename Rep,
typename T>
647 return rhs > 0 ? lhs > cuda::std::numeric_limits<Rep>::max() - rhs
648 : lhs < cuda::std::numeric_limits<Rep>::min() - rhs;
659 template <
typename Rep,
typename T>
662 return rhs > 0 ? lhs < cuda::std::numeric_limits<Rep>::min() + rhs
663 : lhs > cuda::std::numeric_limits<Rep>::max() + rhs;
674 template <
typename Rep,
typename T>
677 return lhs == cuda::std::numeric_limits<Rep>::min() && rhs == -1;
688 template <
typename Rep,
typename T>
691 auto const min = cuda::std::numeric_limits<Rep>::min();
692 auto const max = cuda::std::numeric_limits<Rep>::max();
693 if (rhs > 0) {
return lhs > max / rhs || lhs < min / rhs; }
694 if (rhs < -1) {
return lhs > min / rhs || lhs < max / rhs; }
695 return rhs == -1 && lhs == min;
699 template <
typename Rep1, Radix Rad1>
703 auto const scale = std::min(lhs._scale, rhs._scale);
706 #if defined(__CUDACC_DEBUG__)
708 assert(!addition_overflow<Rep1>(lhs.
rescaled(scale)._value, rhs.
rescaled(scale)._value) &&
709 "fixed_point overflow");
717 template <
typename Rep1, Radix Rad1>
721 auto const scale = std::min(lhs._scale, rhs._scale);
724 #if defined(__CUDACC_DEBUG__)
726 assert(!subtraction_overflow<Rep1>(lhs.
rescaled(scale)._value, rhs.
rescaled(scale)._value) &&
727 "fixed_point overflow");
735 template <
typename Rep1, Radix Rad1>
739 #if defined(__CUDACC_DEBUG__)
741 assert(!multiplication_overflow<Rep1>(lhs._value, rhs._value) &&
"fixed_point overflow");
750 template <
typename Rep1, Radix Rad1>
754 #if defined(__CUDACC_DEBUG__)
756 assert(!division_overflow<Rep1>(lhs._value, rhs._value) &&
"fixed_point overflow");
765 template <
typename Rep1, Radix Rad1>
769 auto const scale = std::min(lhs._scale, rhs._scale);
774 template <
typename Rep1, Radix Rad1>
778 auto const scale = std::min(lhs._scale, rhs._scale);
783 template <
typename Rep1, Radix Rad1>
787 auto const scale = std::min(lhs._scale, rhs._scale);
792 template <
typename Rep1, Radix Rad1>
796 auto const scale = std::min(lhs._scale, rhs._scale);
801 template <
typename Rep1, Radix Rad1>
805 auto const scale = std::min(lhs._scale, rhs._scale);
810 template <
typename Rep1, Radix Rad1>
814 auto const scale = std::min(lhs._scale, rhs._scale);
819 template <
typename Rep1, Radix Rad1>
823 auto const scale = std::min(lhs._scale, rhs._scale);
824 auto const remainder = lhs.
rescaled(scale)._value % rhs.
rescaled(scale)._value;
A type for representing a number with a fixed amount of precision.
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 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 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 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 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 scale_type scale() const
Method that returns the scale of the fixed_point number.
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)
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 adding 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)
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 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 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 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 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)
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 T right_shift(T const &val, scale_type const &scale)
Function that performs a right shift scale "times" on the val
CUDF_HOST_DEVICE Rep ipow(T exponent)
A function for integer exponentiation by squaring.
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)
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.
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
Radix
Scoped enumerator to use when constructing fixed_point
constexpr auto is_supported_construction_value_type()
Returns true if the value type is supported for constructing a fixed_point
scale_type
The scale type for fixed_point.
constexpr auto is_supported_representation_type()
Returns true if the representation type is supported by fixed_point
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.