CUDA Math API (PDF) - v11.0.207 (older) - Last updated June 29, 2020 - Send Feedback

## 1.1.1. Half Arithmetic Functions

## [Half Precision Intrinsics]

To use these functions include the header file `cuda_fp16.h` in your program.

### Functions

__device__ ​ __half __habs ( const __half a )
  Calculates the absolute value of input `half` number and returns the result.
__device__ ​ __half __hadd ( const __half a, const __half b )
  Performs `half` addition in round-to-nearest-even mode.
__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].
__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].
__device__ ​ __half __hmul ( const __half a, const __half b )
  Performs `half` multiplication in round-to-nearest-even mode.
__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].
__device__ ​ __half __hneg ( const __half a )
  Negates input `half` number and returns the result.
__device__ ​ __half __hsub ( const __half a, const __half b )
  Performs `half` subtraction in round-to-nearest-even mode.
__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].

### Functions

__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.

__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.

__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.

__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 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.

__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 mode.

__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 mode, and clamps the result to range [0.0, 1.0]. NaN results are flushed to +0.0.

__device__ ​ __half __hneg ( const __half a )
  Negates input `half` number and returns the result.

  ###### Description

  Negates input `half` number and returns the result.

__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 mode.

__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 mode, and clamps the result to range [0.0, 1.0]. NaN results are flushed to +0.0.