4.2. Half Arithmetic Functions
To use these functions, include the header file cuda_fp16.h in your program.
Functions
- __host__ __device__ __half __habs(const __half a)
-
Calculates the absolute value of input
halfnumber and returns the result. - __host__ __device__ __half __hadd(const __half a, const __half b)
-
Performs
halfaddition in round-to-nearest-even mode. - __host__ __device__ __half __hadd_rn(const __half a, const __half b)
-
Performs
halfaddition in round-to-nearest-even mode. - __host__ __device__ __half __hadd_sat(const __half a, const __half b)
-
Performs
halfaddition in round-to-nearest-even mode, with saturation to [0.0, 1.0]. - __host__ __device__ __half __hdiv(const __half a, const __half b)
-
Performs
halfdivision in round-to-nearest-even mode. - __device__ __half __hfma(const __half a, const __half b, const __half c)
-
Performs
halffused multiply-add in round-to-nearest-even mode. - __device__ __half __hfma_relu(const __half a, const __half b, const __half c)
-
Performs
halffused multiply-add in round-to-nearest-even mode with relu saturation. - __device__ __half __hfma_sat(const __half a, const __half b, const __half c)
-
Performs
halffused multiply-add in round-to-nearest-even mode, with saturation to [0.0, 1.0]. - __host__ __device__ __half __hmul(const __half a, const __half b)
-
Performs
halfmultiplication in round-to-nearest-even mode. - __host__ __device__ __half __hmul_rn(const __half a, const __half b)
-
Performs
halfmultiplication in round-to-nearest-even mode. - __host__ __device__ __half __hmul_sat(const __half a, const __half b)
-
Performs
halfmultiplication in round-to-nearest-even mode, with saturation to [0.0, 1.0]. - __host__ __device__ __half __hneg(const __half a)
-
Negates input
halfnumber and returns the result. - __host__ __device__ __half __hsub(const __half a, const __half b)
-
Performs
halfsubtraction in round-to-nearest-even mode. - __host__ __device__ __half __hsub_rn(const __half a, const __half b)
-
Performs
halfsubtraction in round-to-nearest-even mode. - __host__ __device__ __half __hsub_sat(const __half a, const __half b)
-
Performs
halfsubtraction in round-to-nearest-even mode, with saturation to [0.0, 1.0]. - __device__ __half atomicAdd(__half *const address, const __half val)
-
Adds
valto the value stored ataddressin global or shared memory, and writes this value back toaddress. - __host__ __device__ __half operator*(const __half &lh, const __half &rh)
-
Performs
halfmultiplication operation. - __host__ __device__ __half & operator*=(__half &lh, const __half &rh)
-
Performs
halfcompound assignment with multiplication operation. - __host__ __device__ __half operator+(const __half &h)
-
Implements
halfunary plus operator, returns input value. - __host__ __device__ __half operator+(const __half &lh, const __half &rh)
-
Performs
halfaddition operation. - __host__ __device__ __half & operator++(__half &h)
-
Performs
halfprefix increment operation. - __host__ __device__ __half operator++(__half &h, const int ignored)
-
Performs
halfpostfix increment operation. - __host__ __device__ __half & operator+=(__half &lh, const __half &rh)
-
Performs
halfcompound assignment with addition operation. - __host__ __device__ __half operator-(const __half &lh, const __half &rh)
-
Performs
halfsubtraction operation. - __host__ __device__ __half operator-(const __half &h)
-
Implements
halfunary minus operator. - __host__ __device__ __half operator–(__half &h, const int ignored)
-
Performs
halfpostfix decrement operation. - __host__ __device__ __half & operator–(__half &h)
-
Performs
halfprefix decrement operation. - __host__ __device__ __half & operator-=(__half &lh, const __half &rh)
-
Performs
halfcompound assignment with subtraction operation. - __host__ __device__ __half operator/(const __half &lh, const __half &rh)
-
Performs
halfdivision operation. - __host__ __device__ __half & operator/=(__half &lh, const __half &rh)
-
Performs
halfcompound assignment with division operation.
4.2.1. Functions
-
__host__ __device__ __half __habs(const __half a)
-
Calculates the absolute value of input
halfnumber and returns the result.Calculates the absolute value of input
halfnumber and returns the result.- Parameters
-
a – [in] - half. Is only being read.
- Returns
-
half
The absolute value of
a.__habs \( (\pm 0)\) returns +0.
__habs \( (\pm \infty)\) returns \( +\infty \).
__habs(NaN) returns NaN.
-
__host__ __device__ __half __hadd(const __half a, const __half b)
-
Performs
halfaddition in round-to-nearest-even mode.Performs
halfaddition of inputsaandb, in round-to-nearest-even mode.- Parameters
-
a – [in] - half. Is only being read.
b – [in] - half. Is only being read.
- Returns
-
half
The sum of
aandb.
-
__host__ __device__ __half __hadd_rn(const __half a, const __half b)
-
Performs
halfaddition in round-to-nearest-even mode.Performs
halfaddition of inputsaandb, in round-to-nearest-even mode. Prevents floating-point contractions of mul+add into fma.- Parameters
-
a – [in] - half. Is only being read.
b – [in] - half. Is only being read.
- Returns
-
half
The sum of
aandb.
-
__host__ __device__ __half __hadd_sat(const __half a, const __half b)
-
Performs
halfaddition in round-to-nearest-even mode, with saturation to [0.0, 1.0].Performs
halfadd of inputsaandb, in round-to-nearest-even mode, and clamps the result to range [0.0, 1.0]. NaN results are flushed to +0.0.- Parameters
-
a – [in] - half. Is only being read.
b – [in] - half. Is only being read.
- Returns
-
half
The sum of
aandb, with respect to saturation.
-
__host__ __device__ __half __hdiv(const __half a, const __half b)
-
Performs
halfdivision in round-to-nearest-even mode.Divides
halfinputaby inputbin round-to-nearest-even mode.- Parameters
-
a – [in] - half. Is only being read.
b – [in] - half. Is only being read.
- Returns
-
half
The result of dividing
abyb.
-
__device__ __half __hfma(const __half a, const __half b, const __half c)
-
Performs
halffused multiply-add in round-to-nearest-even mode.Performs
halfmultiply on inputsaandb, then performs ahalfadd of the result withc, rounding the result once in round-to-nearest-even mode.- Parameters
-
a – [in] - half. Is only being read.
b – [in] - half. Is only being read.
c – [in] - half. Is only being read.
- Returns
-
half
The result of fused multiply-add operation on
a,b, andc.
-
__device__ __half __hfma_relu(const __half a, const __half b, const __half c)
-
Performs
halffused multiply-add in round-to-nearest-even mode with relu saturation.Performs
halfmultiply on inputsaandb, then performs ahalfadd of the result withc, rounding the result once in round-to-nearest-even mode. Then negative result is clamped to 0. NaN result is converted to canonical NaN.- Parameters
-
a – [in] - half. Is only being read.
b – [in] - half. Is only being read.
c – [in] - half. Is only being read.
- Returns
-
half
The result of fused multiply-add operation on
a,b, andcwith relu saturation.
-
__device__ __half __hfma_sat(const __half a, const __half b, const __half c)
-
Performs
halffused multiply-add in round-to-nearest-even mode, with saturation to [0.0, 1.0].Performs
halfmultiply on inputsaandb, then performs ahalfadd of the result withc, rounding the result once in round-to-nearest-even mode, and clamps the result to range [0.0, 1.0]. NaN results are flushed to +0.0.- Parameters
-
a – [in] - half. Is only being read.
b – [in] - half. Is only being read.
c – [in] - half. Is only being read.
- Returns
-
half
The result of fused multiply-add operation on
a,b, andc, with respect to saturation.
-
__host__ __device__ __half __hmul(const __half a, const __half b)
-
Performs
halfmultiplication in round-to-nearest-even mode.Performs
halfmultiplication of inputsaandb, in round-to-nearest-even mode.- Parameters
-
a – [in] - half. Is only being read.
b – [in] - half. Is only being read.
- Returns
-
half
The result of multiplying
aandb.
-
__host__ __device__ __half __hmul_rn(const __half a, const __half b)
-
Performs
halfmultiplication in round-to-nearest-even mode.Performs
halfmultiplication of inputsaandb, in round-to-nearest-even mode. Prevents floating-point contractions of mul+add or sub into fma.- Parameters
-
a – [in] - half. Is only being read.
b – [in] - half. Is only being read.
- Returns
-
half
The result of multiplying
aandb.
-
__host__ __device__ __half __hmul_sat(const __half a, const __half b)
-
Performs
halfmultiplication in round-to-nearest-even mode, with saturation to [0.0, 1.0].Performs
halfmultiplication of inputsaandb, in round-to-nearest-even mode, and clamps the result to range [0.0, 1.0]. NaN results are flushed to +0.0.- Parameters
-
a – [in] - half. Is only being read.
b – [in] - half. Is only being read.
- Returns
-
half
The result of multiplying
aandb, with respect to saturation.
-
__host__ __device__ __half __hneg(const __half a)
-
Negates input
halfnumber and returns the result.Negates input
halfnumber and returns the result.- Parameters
-
a – [in] - half. Is only being read.
- Returns
-
half
Negated input
a.__hneg \( (\pm 0)\) returns \( \mp 0 \).
__hneg \( (\pm \infty)\) returns \( \mp \infty \).
__hneg(NaN) returns NaN.
-
__host__ __device__ __half __hsub(const __half a, const __half b)
-
Performs
halfsubtraction in round-to-nearest-even mode.Subtracts
halfinputbfrom inputain round-to-nearest-even mode.- Parameters
-
a – [in] - half. Is only being read.
b – [in] - half. Is only being read.
- Returns
-
half
The result of subtracting
bfroma.
-
__host__ __device__ __half __hsub_rn(const __half a, const __half b)
-
Performs
halfsubtraction in round-to-nearest-even mode.Subtracts
halfinputbfrom inputain round-to-nearest-even mode. Prevents floating-point contractions of mul+sub into fma.- Parameters
-
a – [in] - half. Is only being read.
b – [in] - half. Is only being read.
- Returns
-
half
The result of subtracting
bfroma.
-
__host__ __device__ __half __hsub_sat(const __half a, const __half b)
-
Performs
halfsubtraction in round-to-nearest-even mode, with saturation to [0.0, 1.0].Subtracts
halfinputbfrom inputain round-to-nearest-even mode, and clamps the result to range [0.0, 1.0]. NaN results are flushed to +0.0.- Parameters
-
a – [in] - half. Is only being read.
b – [in] - half. Is only being read.
- Returns
-
half
The result of subtraction of
bfroma, with respect to saturation.
-
__device__ __half atomicAdd(__half *const address, const __half val)
-
Adds
valto the value stored ataddressin global or shared memory, and writes this value back toaddress.This operation is performed in one atomic operation.
The location of
addressmust be in global or shared memory. This operation has undefined behavior otherwise. This operation is only supported by devices of compute capability 7.x and higher.Note
For more details about this function, see the Atomic Functions section in the CUDA C++ Programming Guide.
- Parameters
-
address – [in] - half*. An address in global or shared memory.
val – [in] - half. The value to be added.
- Returns
-
half
The old value read from
address.
-
__host__ __device__ __half operator*(const __half &lh, const __half &rh)
-
Performs
halfmultiplication operation.See also
-
__host__ __device__ __half &operator*=(__half &lh, const __half &rh)
-
Performs
halfcompound assignment with multiplication operation.See also
-
__host__ __device__ __half operator+(const __half &h)
-
Implements
halfunary plus operator, returns input value.
-
__host__ __device__ __half operator+(const __half &lh, const __half &rh)
-
Performs
halfaddition operation.See also
-
__host__ __device__ __half &operator++(__half &h)
-
Performs
halfprefix increment operation.See also
-
__host__ __device__ __half operator++(__half &h, const int ignored)
-
Performs
halfpostfix increment operation.See also
-
__host__ __device__ __half &operator+=(__half &lh, const __half &rh)
-
Performs
halfcompound assignment with addition operation.See also
-
__host__ __device__ __half operator-(const __half &lh, const __half &rh)
-
Performs
halfsubtraction operation.See also
-
__host__ __device__ __half operator-(const __half &h)
-
Implements
halfunary minus operator.See also
-
__host__ __device__ __half operator--(__half &h, const int ignored)
-
Performs
halfpostfix decrement operation.See also
-
__host__ __device__ __half &operator--(__half &h)
-
Performs
halfprefix decrement operation.See also
-
__host__ __device__ __half &operator-=(__half &lh, const __half &rh)
-
Performs
halfcompound assignment with subtraction operation.See also
-
__host__ __device__ __half operator/(const __half &lh, const __half &rh)
-
Performs
halfdivision operation.See also