half  2.2
IEEE 754-based half-precision floating-point library
half.hpp File Reference

Main header file for half-precision functionality. More...

Go to the source code of this file.

Classes

class  half
 Half-precision floating-point type. More...
 
class  numeric_limits< half_float::half >
 Numeric limits for half-precision floats. More...
 
struct  hash< half_float::half >
 Hash function for half-precision floats. More...
 

Namespaces

 half_float
 Main namespace for half-precision functionality.
 
 half_float::literal
 Library-defined half-precision literals.
 
 std
 Extensions to the C++ standard library.
 

Macros

#define HALF_ENABLE_F16C_INTRINSICS   __F16C__
 Enable F16C intruction set intrinsics. More...
 
#define HALF_ARITHMETIC_TYPE   (undefined)
 Type for internal floating-point computations. More...
 
#define HALF_ERRHANDLING_FLAGS   0
 Enable internal exception flags. More...
 
#define HALF_ERRHANDLING_ERRNO   0
 Enable exception propagation to errno. More...
 
#define HALF_ERRHANDLING_FENV   0
 Enable exception propagation to built-in floating-point platform. More...
 
#define HALF_ERRHANDLING_THROW_INVALID   (undefined)
 Throw C++ exception on domain errors. More...
 
#define HALF_ERRHANDLING_THROW_DIVBYZERO   (undefined)
 Throw C++ exception on pole errors. More...
 
#define HALF_ERRHANDLING_THROW_OVERFLOW   (undefined)
 Throw C++ exception on overflow errors. More...
 
#define HALF_ERRHANDLING_THROW_UNDERFLOW   (undefined)
 Throw C++ exception on underflow errors. More...
 
#define HALF_ERRHANDLING_THROW_INEXACT   (undefined)
 Throw C++ exception on rounding errors. More...
 
#define HALF_ERRHANDLING_OVERFLOW_TO_INEXACT   1
 Raise INEXACT exception on overflow. More...
 
#define HALF_ERRHANDLING_UNDERFLOW_TO_INEXACT   1
 Raise INEXACT exception on underflow. More...
 
#define HALF_ROUND_STYLE   1
 Default rounding mode. More...
 
#define HUGE_VALH   std::numeric_limits<half_float::half>::infinity()
 Value signaling overflow. More...
 
#define FP_FAST_FMAH   1
 Fast half-precision fma function. More...
 
#define HLF_ROUNDS   HALF_ROUND_STYLE
 Half rounding mode. More...
 

Functions

half operator""_h (long double value)
 Half literal. More...
 
Comparison operators

constexpr bool operator== (half x, half y)
 Comparison for equality. More...
 
constexpr bool operator!= (half x, half y)
 Comparison for inequality. More...
 
constexpr bool operator< (half x, half y)
 Comparison for less than. More...
 
constexpr bool operator> (half x, half y)
 Comparison for greater than. More...
 
constexpr bool operator<= (half x, half y)
 Comparison for less equal. More...
 
constexpr bool operator>= (half x, half y)
 Comparison for greater equal. More...
 
Arithmetic operators

constexpr half operator+ (half arg)
 Identity. More...
 
constexpr half operator- (half arg)
 Negation. More...
 
half operator+ (half x, half y)
 Addition. More...
 
half operator- (half x, half y)
 Subtraction. More...
 
half operator* (half x, half y)
 Multiplication. More...
 
half operator/ (half x, half y)
 Division. More...
 
Input and output

template<typename charT , typename traits >
std::basic_ostream< charT, traits > & operator<< (std::basic_ostream< charT, traits > &out, half arg)
 Output operator. More...
 
template<typename charT , typename traits >
std::basic_istream< charT, traits > & operator>> (std::basic_istream< charT, traits > &in, half &arg)
 Input operator. More...
 
Basic mathematical operations

constexpr half fabs (half arg)
 Absolute value. More...
 
constexpr half abs (half arg)
 Absolute value. More...
 
half fmod (half x, half y)
 Remainder of division. More...
 
half remainder (half x, half y)
 Remainder of division. More...
 
half remquo (half x, half y, int *quo)
 Remainder of division. More...
 
half fma (half x, half y, half z)
 Fused multiply add. More...
 
constexpr half fmax (half x, half y)
 Maximum of half expressions. More...
 
constexpr half fmin (half x, half y)
 Minimum of half expressions. More...
 
half fdim (half x, half y)
 Positive difference. More...
 
half nanh (const char *arg)
 Get NaN value. More...
 
Exponential functions

half exp (half arg)
 Exponential function. More...
 
half exp2 (half arg)
 Binary exponential. More...
 
half expm1 (half arg)
 Exponential minus one. More...
 
half log (half arg)
 Natural logarithm. More...
 
half log10 (half arg)
 Common logarithm. More...
 
half log2 (half arg)
 Binary logarithm. More...
 
half log1p (half arg)
 Natural logarithm plus one. More...
 
Power functions

half sqrt (half arg)
 Square root. More...
 
half rsqrt (half arg)
 Inverse square root. More...
 
half cbrt (half arg)
 Cubic root. More...
 
half hypot (half x, half y)
 Hypotenuse function. More...
 
half hypot (half x, half y, half z)
 Hypotenuse function. More...
 
half pow (half x, half y)
 Power function. More...
 
Trigonometric functions

void sincos (half arg, half *sin, half *cos)
 Compute sine and cosine simultaneously. More...
 
half sin (half arg)
 Sine function. More...
 
half cos (half arg)
 Cosine function. More...
 
half tan (half arg)
 Tangent function. More...
 
half asin (half arg)
 Arc sine. More...
 
half acos (half arg)
 Arc cosine function. More...
 
half atan (half arg)
 Arc tangent function. More...
 
half atan2 (half y, half x)
 Arc tangent function. More...
 
Hyperbolic functions

half sinh (half arg)
 Hyperbolic sine. More...
 
half cosh (half arg)
 Hyperbolic cosine. More...
 
half tanh (half arg)
 Hyperbolic tangent. More...
 
half asinh (half arg)
 Hyperbolic area sine. More...
 
half acosh (half arg)
 Hyperbolic area cosine. More...
 
half atanh (half arg)
 Hyperbolic area tangent. More...
 
Error and gamma functions

half erf (half arg)
 Error function. More...
 
half erfc (half arg)
 Complementary error function. More...
 
half lgamma (half arg)
 Natural logarithm of gamma function. More...
 
half tgamma (half arg)
 Gamma function. More...
 
Rounding

half ceil (half arg)
 Nearest integer not less than half value. More...
 
half floor (half arg)
 Nearest integer not greater than half value. More...
 
half trunc (half arg)
 Nearest integer not greater in magnitude than half value. More...
 
half round (half arg)
 Nearest integer. More...
 
long lround (half arg)
 Nearest integer. More...
 
half rint (half arg)
 Nearest integer using half's internal rounding mode. More...
 
long lrint (half arg)
 Nearest integer using half's internal rounding mode. More...
 
half nearbyint (half arg)
 Nearest integer using half's internal rounding mode. More...
 
long long llround (half arg)
 Nearest integer. More...
 
long long llrint (half arg)
 Nearest integer using half's internal rounding mode. More...
 
Floating point manipulation

half frexp (half arg, int *exp)
 Decompress floating-point number. More...
 
half scalbln (half arg, long exp)
 Multiply by power of two. More...
 
half scalbn (half arg, int exp)
 Multiply by power of two. More...
 
half ldexp (half arg, int exp)
 Multiply by power of two. More...
 
half modf (half arg, half *iptr)
 Extract integer and fractional parts. More...
 
int ilogb (half arg)
 Extract exponent. More...
 
half logb (half arg)
 Extract exponent. More...
 
half nextafter (half from, half to)
 Next representable value. More...
 
half nexttoward (half from, long double to)
 Next representable value. More...
 
constexpr half copysign (half x, half y)
 Take sign. More...
 
Floating point classification

constexpr int fpclassify (half arg)
 Classify floating-point value. More...
 
constexpr bool isfinite (half arg)
 Check if finite number. More...
 
constexpr bool isinf (half arg)
 Check for infinity. More...
 
constexpr bool isnan (half arg)
 Check for NaN. More...
 
constexpr bool isnormal (half arg)
 Check if normal number. More...
 
constexpr bool signbit (half arg)
 Check sign. More...
 
Comparison

constexpr bool isgreater (half x, half y)
 Quiet comparison for greater than. More...
 
constexpr bool isgreaterequal (half x, half y)
 Quiet comparison for greater equal. More...
 
constexpr bool isless (half x, half y)
 Quiet comparison for less than. More...
 
constexpr bool islessequal (half x, half y)
 Quiet comparison for less equal. More...
 
constexpr bool islessgreater (half x, half y)
 Quiet comarison for less or greater. More...
 
constexpr bool isunordered (half x, half y)
 Quiet check if unordered. More...
 
Casting

template<typename T , typename U >
half_cast (U arg)
 Cast to or from half-precision floating-point number. More...
 
template<typename T , std::float_round_style R, typename U >
half_cast (U arg)
 Cast to or from half-precision floating-point number. More...
 
Error handling

int feclearexcept (int excepts)
 Clear exception flags. More...
 
int fetestexcept (int excepts)
 Test exception flags. More...
 
int feraiseexcept (int excepts)
 Raise exception flags. More...
 
int fegetexceptflag (int *flagp, int excepts)
 Save exception flags. More...
 
int fesetexceptflag (const int *flagp, int excepts)
 Restore exception flags. More...
 
void fethrowexcept (int excepts, const char *msg="")
 Throw C++ exceptions based on set exception flags. More...
 

Detailed Description

Main header file for half-precision functionality.

Macro Definition Documentation

◆ HALF_ENABLE_F16C_INTRINSICS

#define HALF_ENABLE_F16C_INTRINSICS   __F16C__

Enable F16C intruction set intrinsics.

Defining this to 1 enables the use of F16C compiler intrinsics for converting between half-precision and single-precision values which may result in improved performance. This will not perform additional checks for support of the F16C instruction set, so an appropriate target platform is required when enabling this feature.

Unless predefined it will be enabled automatically when the __F16C__ symbol is defined, which some compilers do on supporting platforms.

◆ HALF_ARITHMETIC_TYPE

#define HALF_ARITHMETIC_TYPE   (undefined)

Type for internal floating-point computations.

This can be predefined to a built-in floating-point type (float, double or long double) to override the internal half-precision implementation to use this type for computing arithmetic operations and mathematical function (if available). This can result in improved performance for arithmetic operators and mathematical functions but might cause results to deviate from the specified half-precision rounding mode and inhibits proper detection of half-precision exceptions.

◆ HALF_ERRHANDLING_FLAGS

#define HALF_ERRHANDLING_FLAGS   0

Enable internal exception flags.

Defining this to 1 causes operations on half-precision values to raise internal floating-point exception flags according to the IEEE 754 standard. These can then be cleared and checked with clearexcept(), testexcept().

◆ HALF_ERRHANDLING_ERRNO

#define HALF_ERRHANDLING_ERRNO   0

Enable exception propagation to errno.

Defining this to 1 causes operations on half-precision values to propagate floating-point exceptions to errno from <cerrno>. Specifically this will propagate domain errors as EDOM and pole, overflow and underflow errors as ERANGE. Inexact errors won't be propagated.

◆ HALF_ERRHANDLING_FENV

#define HALF_ERRHANDLING_FENV   0

Enable exception propagation to built-in floating-point platform.

Defining this to 1 causes operations on half-precision values to propagate floating-point exceptions to the built-in single- and double-precision implementation's exception flags using the C++11 floating-point environment control from <cfenv>. However, this does not work in reverse and single- or double-precision exceptions will not raise the corresponding half-precision exception flags, nor will explicitly clearing flags clear the corresponding built-in flags.

◆ HALF_ERRHANDLING_THROW_INVALID

#define HALF_ERRHANDLING_THROW_INVALID   (undefined)

Throw C++ exception on domain errors.

Defining this to a string literal causes operations on half-precision values to throw a std::domain_error with the specified message on domain errors.

◆ HALF_ERRHANDLING_THROW_DIVBYZERO

#define HALF_ERRHANDLING_THROW_DIVBYZERO   (undefined)

Throw C++ exception on pole errors.

Defining this to a string literal causes operations on half-precision values to throw a std::domain_error with the specified message on pole errors.

◆ HALF_ERRHANDLING_THROW_OVERFLOW

#define HALF_ERRHANDLING_THROW_OVERFLOW   (undefined)

Throw C++ exception on overflow errors.

Defining this to a string literal causes operations on half-precision values to throw a std::overflow_error with the specified message on overflows.

◆ HALF_ERRHANDLING_THROW_UNDERFLOW

#define HALF_ERRHANDLING_THROW_UNDERFLOW   (undefined)

Throw C++ exception on underflow errors.

Defining this to a string literal causes operations on half-precision values to throw a std::underflow_error with the specified message on underflows.

◆ HALF_ERRHANDLING_THROW_INEXACT

#define HALF_ERRHANDLING_THROW_INEXACT   (undefined)

Throw C++ exception on rounding errors.

Defining this to 1 causes operations on half-precision values to throw a std::range_error with the specified message on general rounding errors.

◆ HALF_ERRHANDLING_OVERFLOW_TO_INEXACT

#define HALF_ERRHANDLING_OVERFLOW_TO_INEXACT   1

Raise INEXACT exception on overflow.

Defining this to 1 (default) causes overflow errors to automatically raise inexact exceptions in addition. These will be raised after any possible handling of the underflow exception.

◆ HALF_ERRHANDLING_UNDERFLOW_TO_INEXACT

#define HALF_ERRHANDLING_UNDERFLOW_TO_INEXACT   1

Raise INEXACT exception on underflow.

Defining this to 1 (default) causes underflow errors to automatically raise inexact exceptions in addition. These will be raised after any possible handling of the underflow exception.

Note: This will actually cause underflow (and the accompanying inexact) exceptions to be raised only when the result is inexact, while if disabled bare underflow errors will be raised for any (possibly exact) subnormal result.

◆ HALF_ROUND_STYLE

#define HALF_ROUND_STYLE   1

Default rounding mode.

This specifies the rounding mode used for all conversions between halfs and more precise types (unless using half_cast() and specifying the rounding mode directly) as well as in arithmetic operations and mathematical functions. It can be redefined (before including half.hpp) to one of the standard rounding modes using their respective constants or the equivalent values of std::float_round_style:

std::float_round_style value rounding
std::round_indeterminate -1 fastest
std::round_toward_zero 0 toward zero
std::round_to_nearest 1 to nearest (default)
std::round_toward_infinity 2 toward positive infinity
std::round_toward_neg_infinity 3 toward negative infinity

By default this is set to 1 (std::round_to_nearest), which rounds results to the nearest representable value. It can even be set to std::numeric_limits<float>::round_style to synchronize the rounding mode with that of the built-in single-precision implementation (which is likely std::round_to_nearest, though).

◆ HUGE_VALH

#define HUGE_VALH   std::numeric_limits<half_float::half>::infinity()

Value signaling overflow.

In correspondence with HUGE_VAL[F|L] from <cmath> this symbol expands to a positive value signaling the overflow of an operation, in particular it just evaluates to positive infinity.

See also: Documentation for HUGE_VAL

◆ FP_FAST_FMAH

#define FP_FAST_FMAH   1

Fast half-precision fma function.

This symbol is defined if the fma() function generally executes as fast as, or faster than, a separate half-precision multiplication followed by an addition, which is always the case.

See also: Documentation for FP_FAST_FMA

◆ HLF_ROUNDS

#define HLF_ROUNDS   HALF_ROUND_STYLE

Half rounding mode.

In correspondence with FLT_ROUNDS from <cfloat> this symbol expands to the rounding mode used for half-precision operations. It is an alias for HALF_ROUND_STYLE.

See also: Documentation for FLT_ROUNDS