Classes | Namespaces | Functions
floating_conversion.hpp File Reference

fixed_point <--> floating-point conversion functions. More...

#include <cudf/utilities/export.hpp>
#include <cudf/utilities/traits.hpp>
#include <cuda/std/cmath>
#include <cuda/std/limits>
#include <cuda/std/type_traits>
#include <cstring>

Go to the source code of this file.

Classes

struct  numeric::detail::floating_converter< FloatingType, >
 Helper struct for getting and setting the components of a floating-point value. More...
 
struct  numeric::detail::shifting_constants< FloatingType >
 Helper struct with common constants needed by the floating <--> decimal conversions. More...
 

Namespaces

 numeric
 fixed_point and supporting types
 

Functions

template<typename T , CUDF_ENABLE_IF(std::is_same_v< T, uint32_t >||std::is_same_v< T, uint64_t >|| std::is_same_v< T, __uint128_t >) >
CUDF_HOST_DEVICE int numeric::detail::count_significant_bits (T value)
 Determine the number of significant bits in an integer. More...
 
template<int Pow10>
constexpr __uint128_t numeric::detail::large_power_of_10 ()
 Recursively calculate a signed large power of 10 (>= 10^19) that can only be stored in an 128bit integer. More...
 
template<typename T , CUDF_ENABLE_IF(cuda::std::is_unsigned_v< T >) >
CUDF_HOST_DEVICEnumeric::detail::divide_power10_32bit (T value, int pow10)
 Divide by a power of 10 that fits within a 32bit integer. More...
 
template<typename T , CUDF_ENABLE_IF(cuda::std::is_unsigned_v< T >) >
CUDF_HOST_DEVICEnumeric::detail::divide_power10_64bit (T value, int pow10)
 Divide by a power of 10 that fits within a 64bit integer. More...
 
template<typename T , CUDF_ENABLE_IF(cuda::std::is_unsigned_v< T >) >
constexpr CUDF_HOST_DEVICEnumeric::detail::divide_power10_128bit (T value, int pow10)
 Divide by a power of 10 that fits within a 128bit integer. More...
 
template<typename T , CUDF_ENABLE_IF(cuda::std::is_unsigned_v< T >) >
constexpr CUDF_HOST_DEVICEnumeric::detail::multiply_power10_32bit (T value, int pow10)
 Multiply by a power of 10 that fits within a 32bit integer. More...
 
template<typename T , CUDF_ENABLE_IF(cuda::std::is_unsigned_v< T >) >
constexpr CUDF_HOST_DEVICEnumeric::detail::multiply_power10_64bit (T value, int pow10)
 Multiply by a power of 10 that fits within a 64bit integer. More...
 
template<typename T , CUDF_ENABLE_IF(cuda::std::is_unsigned_v< T >) >
constexpr CUDF_HOST_DEVICEnumeric::detail::multiply_power10_128bit (T value, int pow10)
 Multiply by a power of 10 that fits within a 128bit integer. More...
 
template<typename Rep , typename T , CUDF_ENABLE_IF(cuda::std::is_unsigned_v< T >) >
constexpr CUDF_HOST_DEVICEnumeric::detail::multiply_power10 (T value, int pow10)
 Multiply an integer by a power of 10. More...
 
template<typename Rep , typename T , CUDF_ENABLE_IF(cuda::std::is_unsigned_v< T >) >
constexpr CUDF_HOST_DEVICEnumeric::detail::divide_power10 (T value, int pow10)
 Divide an integer by a power of 10. More...
 
template<typename IntegerType , CUDF_ENABLE_IF(cuda::std::is_unsigned_v< IntegerType >) >
CUDF_HOST_DEVICE IntegerType numeric::detail::guarded_left_shift (IntegerType value, int bit_shift)
 Perform a bit-shift left, guarding against undefined behavior. More...
 
template<typename IntegerType , CUDF_ENABLE_IF(cuda::std::is_unsigned_v< IntegerType >) >
CUDF_HOST_DEVICE IntegerType numeric::detail::guarded_right_shift (IntegerType value, int bit_shift)
 Perform a bit-shift right, guarding against undefined behavior. More...
 
template<typename FloatingType , CUDF_ENABLE_IF(cuda::std::is_floating_point_v< FloatingType >) >
CUDF_HOST_DEVICE cuda::std::pair< typename floating_converter< FloatingType >::IntegralType, int > numeric::detail::add_half_if_truncates (FloatingType floating, typename floating_converter< FloatingType >::IntegralType integer_rep, int pow2, int pow10)
 Add half a bit to integer rep of floating point if conversion causes truncation. More...
 
template<typename Rep , typename FloatingType , CUDF_ENABLE_IF(cuda::std::is_floating_point_v< FloatingType >) >
CUDF_HOST_DEVICE cuda::std::make_unsigned_t< Rep > numeric::detail::shift_to_decimal_pospow (typename shifting_constants< FloatingType >::IntegerRep const base2_value, int pow2, int pow10)
 Perform base-2 -> base-10 fixed-point conversion for pow10 > 0. More...
 
template<typename Rep , typename FloatingType , CUDF_ENABLE_IF(cuda::std::is_floating_point_v< FloatingType >) >
CUDF_HOST_DEVICE cuda::std::make_unsigned_t< Rep > numeric::detail::shift_to_decimal_negpow (typename shifting_constants< FloatingType >::IntegerRep base2_value, int pow2, int pow10)
 Perform base-2 -> base-10 fixed-point conversion for pow10 < 0. More...
 
template<typename Rep , typename FloatingType , CUDF_ENABLE_IF(cuda::std::is_floating_point_v< FloatingType >) >
CUDF_HOST_DEVICE cuda::std::make_unsigned_t< Rep > numeric::detail::convert_floating_to_integral_shifting (typename floating_converter< FloatingType >::IntegralType base2_value, int pow10, int pow2)
 Perform base-2 -> base-10 fixed-point conversion. More...
 
template<typename Rep , typename FloatingType , CUDF_ENABLE_IF(cuda::std::is_floating_point_v< FloatingType >) >
CUDF_HOST_DEVICE Rep numeric::detail::convert_floating_to_integral (FloatingType const &floating, scale_type const &scale)
 Perform floating-point -> integer decimal conversion. More...
 
template<typename FloatingType , typename DecimalRep , CUDF_ENABLE_IF(cuda::std::is_floating_point_v< FloatingType >) >
CUDF_HOST_DEVICE auto numeric::detail::shift_to_binary_pospow (DecimalRep decimal_rep, int pow10)
 Perform base-10 -> base-2 fixed-point conversion for pow10 > 0. More...
 
template<typename FloatingType , typename DecimalRep , CUDF_ENABLE_IF(cuda::std::is_floating_point_v< FloatingType >) >
CUDF_HOST_DEVICE auto numeric::detail::shift_to_binary_negpow (DecimalRep decimal_rep, int const pow10)
 Perform base-10 -> base-2 fixed-point conversion for pow10 < 0. More...
 
template<typename FloatingType , typename Rep , CUDF_ENABLE_IF(cuda::std::is_floating_point_v< FloatingType >) >
CUDF_HOST_DEVICE FloatingType numeric::detail::convert_integral_to_floating (Rep const &value, scale_type const &scale)
 Perform integer decimal -> floating-point conversion. More...
 

Detailed Description

fixed_point <--> floating-point conversion functions.

Definition in file floating_conversion.hpp.

Function Documentation

◆ add_half_if_truncates()

template<typename FloatingType , CUDF_ENABLE_IF(cuda::std::is_floating_point_v< FloatingType >) >
CUDF_HOST_DEVICE cuda::std::pair<typename floating_converter<FloatingType>::IntegralType, int> numeric::detail::add_half_if_truncates ( FloatingType  floating,
typename floating_converter< FloatingType >::IntegralType  integer_rep,
int  pow2,
int  pow10 
)

Add half a bit to integer rep of floating point if conversion causes truncation.

Note
This fixes problems like 1.2 (value = 1.1999...) at scale -1 -> 11
Template Parameters
FloatingTypeType of integer holding the floating-point significand
Parameters
floatingThe floating-point number to convert
integer_repThe integer representation of the floating-point significand
pow2The power of 2 that needs to be applied to the significand
pow10The power of 10 that needs to be applied to the significand
Returns
integer_rep, shifted 1 and ++'d if the conversion to decimal causes truncation

Definition at line 636 of file floating_conversion.hpp.

◆ convert_floating_to_integral()

template<typename Rep , typename FloatingType , CUDF_ENABLE_IF(cuda::std::is_floating_point_v< FloatingType >) >
CUDF_HOST_DEVICE Rep numeric::detail::convert_floating_to_integral ( FloatingType const &  floating,
scale_type const &  scale 
)
inline

Perform floating-point -> integer decimal conversion.

Template Parameters
RepThe type of integer we are converting to, to store the decimal value
FloatingTypeThe type of floating-point object we are converting from
Parameters
floatingThe floating point value to convert
scaleThe desired base-10 scale factor: decimal value = returned value * 10^scale
Returns
Integer representation of the floating-point value, given the desired scale

Definition at line 932 of file floating_conversion.hpp.

◆ convert_floating_to_integral_shifting()

template<typename Rep , typename FloatingType , CUDF_ENABLE_IF(cuda::std::is_floating_point_v< FloatingType >) >
CUDF_HOST_DEVICE cuda::std::make_unsigned_t<Rep> numeric::detail::convert_floating_to_integral_shifting ( typename floating_converter< FloatingType >::IntegralType  base2_value,
int  pow10,
int  pow2 
)
inline

Perform base-2 -> base-10 fixed-point conversion.

Template Parameters
RepThe type of integer we are converting to, to store the decimal value
FloatingTypeThe type of floating-point object we are converting from
Parameters
base2_valueThe base-2 fixed-point value we are converting from
pow2The number of powers of 2 to apply to convert from base-2
pow10The number of powers of 10 to apply to reach the desired scale factor
Returns
Integer representation of the floating-point value, given the desired scale

Definition at line 880 of file floating_conversion.hpp.

◆ convert_integral_to_floating()

template<typename FloatingType , typename Rep , CUDF_ENABLE_IF(cuda::std::is_floating_point_v< FloatingType >) >
CUDF_HOST_DEVICE FloatingType numeric::detail::convert_integral_to_floating ( Rep const &  value,
scale_type const &  scale 
)
inline

Perform integer decimal -> floating-point conversion.

Template Parameters
FloatingTypeThe type of floating-point object we are converting to
RepThe decimal integer type we are converting from
Parameters
valueThe decimal integer to convert
scaleThe base-10 scale factor for the input integer
Returns
Floating-point representation of the scaled integral value

Definition at line 1101 of file floating_conversion.hpp.

◆ count_significant_bits()

template<typename T , CUDF_ENABLE_IF(std::is_same_v< T, uint32_t >||std::is_same_v< T, uint64_t >|| std::is_same_v< T, __uint128_t >) >
CUDF_HOST_DEVICE int numeric::detail::count_significant_bits ( value)
inline

Determine the number of significant bits in an integer.

Template Parameters
TType of input integer value. Must be either uint32_t, uint64_t, or __uint128_t
Parameters
valueThe integer whose bits are being counted
Returns
The number of significant bits: the # of bits - # of leading zeroes

Definition at line 49 of file floating_conversion.hpp.

◆ divide_power10()

template<typename Rep , typename T , CUDF_ENABLE_IF(cuda::std::is_unsigned_v< T >) >
constexpr CUDF_HOST_DEVICE T numeric::detail::divide_power10 ( value,
int  pow10 
)
inlineconstexpr

Divide an integer by a power of 10.

Note
Use this function if you have no a-priori knowledge of what pow10 might be. If you do, prefer calling the bit-size-specific versions
Template Parameters
RepRepresentation type needed for integer exponentiation
TIntegral type of value to be divided-from.
Parameters
valueThe number to be divided-from.
pow10The power-of-10 of the denominator.
Returns
Returns value / 10^pow10

Definition at line 507 of file floating_conversion.hpp.

◆ divide_power10_128bit()

template<typename T , CUDF_ENABLE_IF(cuda::std::is_unsigned_v< T >) >
constexpr CUDF_HOST_DEVICE T numeric::detail::divide_power10_128bit ( value,
int  pow10 
)
inlineconstexpr

Divide by a power of 10 that fits within a 128bit integer.

Template Parameters
TType of value to be divided-from.
Parameters
valueThe number to be divided-from.
pow10The power-of-10 of the denominator, from 0 to 38 inclusive.
Returns
Returns value / 10^pow10.

Definition at line 408 of file floating_conversion.hpp.

◆ divide_power10_32bit()

template<typename T , CUDF_ENABLE_IF(cuda::std::is_unsigned_v< T >) >
CUDF_HOST_DEVICE T numeric::detail::divide_power10_32bit ( value,
int  pow10 
)
inline

Divide by a power of 10 that fits within a 32bit integer.

Template Parameters
TType of value to be divided-from.
Parameters
valueThe number to be divided-from.
pow10The power-of-10 of the denominator, from 0 to 9 inclusive.
Returns
Returns value / 10^pow10

Definition at line 344 of file floating_conversion.hpp.

◆ divide_power10_64bit()

template<typename T , CUDF_ENABLE_IF(cuda::std::is_unsigned_v< T >) >
CUDF_HOST_DEVICE T numeric::detail::divide_power10_64bit ( value,
int  pow10 
)
inline

Divide by a power of 10 that fits within a 64bit integer.

Template Parameters
TType of value to be divided-from.
Parameters
valueThe number to be divided-from.
pow10The power-of-10 of the denominator, from 0 to 19 inclusive.
Returns
Returns value / 10^pow10

Definition at line 394 of file floating_conversion.hpp.

◆ guarded_left_shift()

template<typename IntegerType , CUDF_ENABLE_IF(cuda::std::is_unsigned_v< IntegerType >) >
CUDF_HOST_DEVICE IntegerType numeric::detail::guarded_left_shift ( IntegerType  value,
int  bit_shift 
)
inline

Perform a bit-shift left, guarding against undefined behavior.

Template Parameters
IntegerTypeType of input unsigned integer value
Parameters
valueThe integer whose bits are being shifted
bit_shiftThe number of bits to shift left
Returns
The bit-shifted integer, except max value if UB would occur

Definition at line 529 of file floating_conversion.hpp.

◆ guarded_right_shift()

template<typename IntegerType , CUDF_ENABLE_IF(cuda::std::is_unsigned_v< IntegerType >) >
CUDF_HOST_DEVICE IntegerType numeric::detail::guarded_right_shift ( IntegerType  value,
int  bit_shift 
)
inline

Perform a bit-shift right, guarding against undefined behavior.

Template Parameters
IntegerTypeType of input unsigned integer value
Parameters
valueThe integer whose bits are being shifted
bit_shiftThe number of bits to shift right
Returns
The bit-shifted integer, which is zero on underflow

Definition at line 546 of file floating_conversion.hpp.

◆ large_power_of_10()

template<int Pow10>
constexpr __uint128_t numeric::detail::large_power_of_10 ( )
constexpr

Recursively calculate a signed large power of 10 (>= 10^19) that can only be stored in an 128bit integer.

Note
Intended to be run at compile time.
Template Parameters
Pow10The power of 10 to calculate
Returns
Returns 10^Pow10

Definition at line 325 of file floating_conversion.hpp.

◆ multiply_power10()

template<typename Rep , typename T , CUDF_ENABLE_IF(cuda::std::is_unsigned_v< T >) >
constexpr CUDF_HOST_DEVICE T numeric::detail::multiply_power10 ( value,
int  pow10 
)
inlineconstexpr

Multiply an integer by a power of 10.

Note
Use this function if you have no a-priori knowledge of what pow10 might be. If you do, prefer calling the bit-size-specific versions
Template Parameters
RepRepresentation type needed for integer exponentiation
TIntegral type of value to be multiplied.
Parameters
valueThe number to be multiplied.
pow10The power-of-10 of the multiplier.
Returns
Returns value * 10^pow10

Definition at line 481 of file floating_conversion.hpp.

◆ multiply_power10_128bit()

template<typename T , CUDF_ENABLE_IF(cuda::std::is_unsigned_v< T >) >
constexpr CUDF_HOST_DEVICE T numeric::detail::multiply_power10_128bit ( value,
int  pow10 
)
inlineconstexpr

Multiply by a power of 10 that fits within a 128bit integer.

Template Parameters
TType of value to be multiplied.
Parameters
valueThe number to be multiplied.
pow10The power-of-10 of the multiplier, from 0 to 38 inclusive.
Returns
Returns value * 10^pow10.

Definition at line 463 of file floating_conversion.hpp.

◆ multiply_power10_32bit()

template<typename T , CUDF_ENABLE_IF(cuda::std::is_unsigned_v< T >) >
constexpr CUDF_HOST_DEVICE T numeric::detail::multiply_power10_32bit ( value,
int  pow10 
)
inlineconstexpr

Multiply by a power of 10 that fits within a 32bit integer.

Template Parameters
TType of value to be multiplied.
Parameters
valueThe number to be multiplied.
pow10The power-of-10 of the multiplier, from 0 to 9 inclusive.
Returns
Returns value * 10^pow10

Definition at line 422 of file floating_conversion.hpp.

◆ multiply_power10_64bit()

template<typename T , CUDF_ENABLE_IF(cuda::std::is_unsigned_v< T >) >
constexpr CUDF_HOST_DEVICE T numeric::detail::multiply_power10_64bit ( value,
int  pow10 
)
inlineconstexpr

Multiply by a power of 10 that fits within a 64bit integer.

Template Parameters
TType of value to be multiplied.
Parameters
valueThe number to be multiplied.
pow10The power-of-10 of the multiplier, from 0 to 19 inclusive.
Returns
Returns value * 10^pow10

Definition at line 449 of file floating_conversion.hpp.

◆ shift_to_binary_negpow()

template<typename FloatingType , typename DecimalRep , CUDF_ENABLE_IF(cuda::std::is_floating_point_v< FloatingType >) >
CUDF_HOST_DEVICE auto numeric::detail::shift_to_binary_negpow ( DecimalRep  decimal_rep,
int const  pow10 
)
inline

Perform base-10 -> base-2 fixed-point conversion for pow10 < 0.

Template Parameters
DecimalRepThe decimal integer type we are converting from
FloatingTypeThe type of floating point object we are converting to
Parameters
decimal_repThe decimal integer to convert
pow10The number of powers of 10 to apply to undo the scale factor
Returns
A pair of the base-2 value and the remaining powers of 2 to be applied

Definition at line 1034 of file floating_conversion.hpp.

◆ shift_to_binary_pospow()

template<typename FloatingType , typename DecimalRep , CUDF_ENABLE_IF(cuda::std::is_floating_point_v< FloatingType >) >
CUDF_HOST_DEVICE auto numeric::detail::shift_to_binary_pospow ( DecimalRep  decimal_rep,
int  pow10 
)
inline

Perform base-10 -> base-2 fixed-point conversion for pow10 > 0.

Template Parameters
DecimalRepThe decimal integer type we are converting from
FloatingTypeThe type of floating point object we are converting to
Parameters
decimal_repThe decimal integer to convert
pow10The number of powers of 10 to apply to undo the scale factor
Returns
A pair of the base-2 value and the remaining powers of 2 to be applied

Definition at line 971 of file floating_conversion.hpp.

◆ shift_to_decimal_negpow()

template<typename Rep , typename FloatingType , CUDF_ENABLE_IF(cuda::std::is_floating_point_v< FloatingType >) >
CUDF_HOST_DEVICE cuda::std::make_unsigned_t<Rep> numeric::detail::shift_to_decimal_negpow ( typename shifting_constants< FloatingType >::IntegerRep  base2_value,
int  pow2,
int  pow10 
)
inline

Perform base-2 -> base-10 fixed-point conversion for pow10 < 0.

Template Parameters
RepThe type of the storage for the decimal value
FloatingTypeThe type of the original floating-point value we are converting from
Parameters
base2_valueThe base-2 fixed-point value we are converting from
pow2The number of powers of 2 to apply to convert from base-2
pow10The number of powers of 10 to apply to reach the desired scale factor
Returns
Magnitude of the converted-to decimal integer

Definition at line 794 of file floating_conversion.hpp.

◆ shift_to_decimal_pospow()

template<typename Rep , typename FloatingType , CUDF_ENABLE_IF(cuda::std::is_floating_point_v< FloatingType >) >
CUDF_HOST_DEVICE cuda::std::make_unsigned_t<Rep> numeric::detail::shift_to_decimal_pospow ( typename shifting_constants< FloatingType >::IntegerRep const  base2_value,
int  pow2,
int  pow10 
)
inline

Perform base-2 -> base-10 fixed-point conversion for pow10 > 0.

Template Parameters
RepThe type of the storage for the decimal value
FloatingTypeThe type of the original floating-point value we are converting from
Parameters
base2_valueThe base-2 fixed-point value we are converting from
pow2The number of powers of 2 to apply to convert from base-2
pow10The number of powers of 10 to apply to reach the desired scale factor
Returns
Magnitude of the converted-to decimal integer

Definition at line 707 of file floating_conversion.hpp.