9.7.3.7. Floating Point Instructions: mad
mad
Multiply two values and add a third value.
Syntax
mad{.ftz}{.sat}.f32 d, a, b, c; // .target sm_1x
mad.rnd{.ftz}{.sat}.f32 d, a, b, c; // .target sm_20
mad.rnd.f64 d, a, b, c; // .target sm_13 and higher
.rnd = { .rn, .rz, .rm, .rp };Description
Multiplies two values and adds a third, and then writes the resulting value into a destination register.
Semantics
d = a*b + c;Notes
For .target sm_20 and higher:
mad.f32computes the product ofaandbto infinite precision and then addscto this product, again in infinite precision. The resulting value is then rounded to single precision using the rounding mode specified by.rnd.mad.f64computes the product ofaandbto infinite precision and then addscto this product, again in infinite precision. The resulting value is then rounded to double precision using the rounding mode specified by.rnd.mad.{f32,f64}is the same asfma.{f32,f64}.
For .target sm_1x:
mad.f32computes the product ofaandbat double precision, and then the mantissa is truncated to 23 bits, but the exponent is preserved. Note that this is different from computing the product withmul, where the mantissa can be rounded and the exponent will be clamped. The exception formad.f32is whenc = +/-0.0,mad.f32is identical to the result computed using separatemulandaddinstructions. When JIT-compiled for SM 2.0 devices,mad.f32is implemented as a fused multiply-add (i.e.,fma.rn.ftz.f32). In this case,mad.f32can produce slightly different numeric results and backward compatibility is not guaranteed in this case.mad.f64computes the product ofaandbto infinite precision and then addscto this product, again in infinite precision. The resulting value is then rounded to double precision using the rounding mode specified by.rnd. Unlikemad.f32, the treatment of subnormal inputs and output follows IEEE 754 standard.mad.f64is the same asfma.f64.
Rounding modifiers (no default):
.rn— mantissa LSB rounds to nearest even.rz— mantissa LSB rounds towards zero.rm— mantissa LSB rounds towards negative infinity.rp— mantissa LSB rounds towards positive infinity
Subnormal numbers:
sm_20+- By default, subnormal numbers are supported.
mad.ftz.f32flushes subnormal inputs and results to sign-preserving zero.sm_1xmad.f64supports subnormal numbers.mad.f32flushes subnormal inputs and results to sign-preserving zero.
Saturation modifier:
mad.sat.f32 clamps the result to [0.0, 1.0]. NaN results are flushed to +0.0f.
PTX ISA Notes
Introduced in PTX ISA version 1.0.
In PTX ISA versions 1.4 and later, a rounding modifier is required for mad.f64.
Legacy mad.f64 instructions having no rounding modifier will map to mad.rn.f64.
In PTX ISA versions 2.0 and later, a rounding modifier is required for mad.f32 for sm_20 and higher targets.
Errata
mad.f32 requires a rounding modifier for sm_20 and higher targets. However for PTX ISA version 3.0 and earlier, ptxas does not enforce this requirement and mad.f32 silently defaults to mad.rn.f32. For PTX ISA version 3.1, ptxas generates a warning and defaults to mad.rn.f32, and in subsequent releases ptxas will enforce the requirement for PTX ISA version 3.2 and later.
Target ISA Notes
mad.f32 supported on all target architectures.
mad.f64 requires sm_13 or higher.
Rounding modifiers have the following target requirements:
.rn,.rz,.rm,.rpformad.f64, requiressm_13or higher..rn,.rz,.rm,.rpformad.f32, requiressm_20or higher.
Examples
@p mad.f32 d,a,b,c;