1.2.2. Half Arithmetic Functions

[Half Precision Intrinsics]

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 half number and returns the result.
__host____device____half __hadd ( const __half a, const __half b )
Performs half addition in round-to-nearest-even mode.
__host____device____half __hadd_rn ( const __half a, const __half b )
Performs half addition in round-to-nearest-even mode.
__host____device____half __hadd_sat ( const __half a, const __half b )
Performs half addition in round-to-nearest-even mode, with saturation to [0.0, 1.0].
__host____device____half __hdiv ( const __half a, const __half b )
Performs half division in round-to-nearest-even mode.
__device____half __hfma ( const __half a, const __half b, const __half c )
Performs half fused multiply-add in round-to-nearest-even mode.
__device____half __hfma_relu ( const __half a, const __half b, const __half c )
Performs half fused 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 half fused 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 half multiplication in round-to-nearest-even mode.
__host____device____half __hmul_rn ( const __half a, const __half b )
Performs half multiplication in round-to-nearest-even mode.
__host____device____half __hmul_sat ( const __half a, const __half b )
Performs half multiplication in round-to-nearest-even mode, with saturation to [0.0, 1.0].
__host____device____half __hneg ( const __half a )
Negates input half number and returns the result.
__host____device____half __hsub ( const __half a, const __half b )
Performs half subtraction in round-to-nearest-even mode.
__host____device____half __hsub_rn ( const __half a, const __half b )
Performs half subtraction in round-to-nearest-even mode.
__host____device____half __hsub_sat ( const __half a, const __half b )
Performs half subtraction in round-to-nearest-even mode, with saturation to [0.0, 1.0].
__device____half atomicAdd ( const __half* address, const __half val )
Adds val to the value stored at address in global or shared memory, and writes this value back to address. This operation is performed in one atomic operation.
__host____device____half operator* ( const __half& lh, const __half& rh )
__host____device____halfoperator*= ( __half& lh, const __half& rh )
__host____device____half operator+ ( const __half& h )
__host____device____half operator+ ( const __half& lh, const __half& rh )
__host____device____half operator++ ( __half& h, const int  ignored )
__host____device____halfoperator++ ( __half& h )
__host____device____halfoperator+= ( __half& lh, const __half& rh )
__host____device____half operator- ( const __half& h )
__host____device____half operator- ( const __half& lh, const __half& rh )
__host____device____half operator-- ( __half& h, const int  ignored )
__host____device____halfoperator-- ( __half& h )
__host____device____halfoperator-= ( __half& lh, const __half& rh )
__host____device____half operator/ ( const __half& lh, const __half& rh )
__host____device____halfoperator/= ( __half& lh, const __half& rh )

Functions

__host____device____half __habs ( const __half a )
Calculates the absolute value of input half number and returns the result.
Parameters
a
- half. Is only being read.
Returns

half

  • The absolute value of a.
Description

Calculates the absolute value of input half number and returns the result.

__host____device____half __hadd ( const __half a, const __half b )
Performs half addition in round-to-nearest-even mode.
Description

Performs half addition of inputs a and b, in round-to-nearest-even mode.

__host____device____half __hadd_rn ( const __half a, const __half b )
Performs half addition in round-to-nearest-even mode.
Description

Performs half addition of inputs a and b, in round-to-nearest-even mode. Prevents floating-point contractions of mul+add into fma.

__host____device____half __hadd_sat ( const __half a, const __half b )
Performs half addition in round-to-nearest-even mode, with saturation to [0.0, 1.0].
Parameters
a
- half. Is only being read.
b
- half. Is only being read.
Returns

half

  • The sum of a and b, with respect to saturation.
Description

Performs half add of inputs a and b, in round-to-nearest-even mode, and clamps the result to range [0.0, 1.0]. NaN results are flushed to +0.0.

__host____device____half __hdiv ( const __half a, const __half b )
Performs half division in round-to-nearest-even mode.
Description

Divides half input a by input b in round-to-nearest-even mode.

__device____half __hfma ( const __half a, const __half b, const __half c )
Performs half fused multiply-add in round-to-nearest-even mode.
Description

Performs half multiply on inputs a and b, then performs a half add of the result with c, rounding the result once in round-to-nearest-even mode.

__device____half __hfma_relu ( const __half a, const __half b, const __half c )
Performs half fused multiply-add in round-to-nearest-even mode with relu saturation.
Parameters
a
- half. Is only being read.
b
- half. Is only being read.
c
- half. Is only being read.
Returns

half

  • The result of fused multiply-add operation on a, b, and c with relu saturation.
Description

Performs half multiply on inputs a and b, then performs a half add of the result with c, rounding the result once in round-to-nearest-even mode. Then negative result is clamped to 0. NaN result is converted to canonical NaN.

__device____half __hfma_sat ( const __half a, const __half b, const __half c )
Performs half fused multiply-add in round-to-nearest-even mode, with saturation to [0.0, 1.0].
Parameters
a
- half. Is only being read.
b
- half. Is only being read.
c
- half. Is only being read.
Returns

half

  • The result of fused multiply-add operation on a, b, and c, with respect to saturation.
Description

Performs half multiply on inputs a and b, then performs a half add of the result with c, 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.

__host____device____half __hmul ( const __half a, const __half b )
Performs half multiplication in round-to-nearest-even mode.
Description

Performs half multiplication of inputs a and b, in round-to-nearest-even mode.

__host____device____half __hmul_rn ( const __half a, const __half b )
Performs half multiplication in round-to-nearest-even mode.
Description

Performs half multiplication of inputs a and b, in round-to-nearest-even mode. Prevents floating-point contractions of mul+add or sub into fma.

__host____device____half __hmul_sat ( const __half a, const __half b )
Performs half multiplication in round-to-nearest-even mode, with saturation to [0.0, 1.0].
Parameters
a
- half. Is only being read.
b
- half. Is only being read.
Returns

half

  • The result of multiplying a and b, with respect to saturation.
Description

Performs half multiplication of inputs a and b, in round-to-nearest-even mode, and clamps the result to range [0.0, 1.0]. NaN results are flushed to +0.0.

__host____device____half __hneg ( const __half a )
Negates input half number and returns the result.
Description

Negates input half number and returns the result.

__host____device____half __hsub ( const __half a, const __half b )
Performs half subtraction in round-to-nearest-even mode.
Description

Subtracts half input b from input a in round-to-nearest-even mode.

__host____device____half __hsub_rn ( const __half a, const __half b )
Performs half subtraction in round-to-nearest-even mode.
Description

Subtracts half input b from input a in round-to-nearest-even mode. Prevents floating-point contractions of mul+sub into fma.

__host____device____half __hsub_sat ( const __half a, const __half b )
Performs half subtraction in round-to-nearest-even mode, with saturation to [0.0, 1.0].
Parameters
a
- half. Is only being read.
b
- half. Is only being read.
Returns

half

  • The result of subtraction of b from a, with respect to saturation.
Description

Subtracts half input b from input a in round-to-nearest-even mode, and clamps the result to range [0.0, 1.0]. NaN results are flushed to +0.0.

__device____half atomicAdd ( const __half* address, const __half val )
Adds val to the value stored at address in global or shared memory, and writes this value back to address. This operation is performed in one atomic operation.
Parameters
address
- half*. An address in global or shared memory.
val
- half. The value to be added.
Returns

half

  • The old value read from address.
Description

The location of address must 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 for this function see the Atomic Functions section in the CUDA C++ Programming Guide.

__host____device____half operator* ( const __half& lh, const __half& rh )
Description

Performs half multiplication operation. See also __hmul( __half, __half)

__host____device____half& operator*= ( __half& lh, const __half& rh )
Description

Performs half compound assignment with multiplication operation.

__host____device____half operator+ ( const __half& h )
Description

Implements half unary plus operator, returns input value.

__host____device____half operator+ ( const __half& lh, const __half& rh )
Description

Performs half addition operation. See also __hadd( __half, __half)

__host____device____half operator++ ( __half& h, const int  ignored )
Description

Performs half postfix increment operation.

__host____device____half& operator++ ( __half& h )
Description

Performs half prefix increment operation.

__host____device____half& operator+= ( __half& lh, const __half& rh )
Description

Performs half compound assignment with addition operation.

__host____device____half operator- ( const __half& h )
Description

Implements half unary minus operator. See also __hneg( __half)

__host____device____half operator- ( const __half& lh, const __half& rh )
Description

Performs half subtraction operation. See also __hsub( __half, __half)

__host____device____half operator-- ( __half& h, const int  ignored )
Description

Performs half postfix decrement operation.

__host____device____half& operator-- ( __half& h )
Description

Performs half prefix decrement operation.

__host____device____half& operator-= ( __half& lh, const __half& rh )
Description

Performs half compound assignment with subtraction operation.

__host____device____half operator/ ( const __half& lh, const __half& rh )
Description

Performs half division operation. See also __hdiv( __half, __half)

__host____device____half& operator/= ( __half& lh, const __half& rh )
Description

Performs half compound assignment with division operation.