Half Precision Code Generation Support
To assign a half-precision data type to a number or variable, use the half
constructor. A half-precision data type occupies 16 bits of memory, but its floating-point representation enables it to handle wider dynamic ranges than integer or fixed-point data types of the same size. For more information, see Floating-Point Numbers.
A subset of MATLAB® functions are supported for use with half-precision inputs. Additionally, some
functions support code generation with half-precision data types. C and C++ code generation
requires MATLAB
Coder™. CUDA® code generation for NVIDIA® GPUs requires GPU Coder™. Supported functions appear in alphabetical order in the following table.
MATLAB
System object™ supports half-precision data type and MATLAB System block
supports half-precision data type with real values. For general information regarding code
generation with half precision, see half
.
Function | MATLAB Simulation Support | C/C++ Code Generation Support | GPU Code Generation Support |
---|---|---|---|
abs | ✔ | ✔ | ✔ |
acos | ✔ | ✔ | ✔ |
acosh | ✔ | ✔ | ✔ |
activations (Deep Learning Toolbox) | ✔ | ✔ Half inputs are cast to single precision and computations are performed in single precision. | ✔ Half inputs are cast to single precision and computations are performed in single precision. To perform computations in half, set the library target to |
all | ✔ | ✔ | ✔ |
allfinite | ✔ | ✔ | ✔ |
and , & | ✔ | ✔ | ✔ |
Short-Circuit
AND | ✔ | ✔ | ✔ |
any | ✔ | ✔ | ✔ |
anynan | ✔ | ✔ | ✔ |
area | ✔ | ||
asin | ✔ | ✔ | ✔ |
asinh | ✔ | ✔ | ✔ |
atan | ✔ | ✔ | ✔ |
atan2 | ✔ | ✔ | ✔ |
atanh | ✔ | ✔ | ✔ |
bar | ✔ | ||
barh | ✔ | ||
cast | ✔ Supported syntax:
| ✔ Supported syntax:
| ✔ Supported syntax:
|
cat | ✔ | ✔
| ✔
|
ceil | ✔ | ✔ | ✔ |
cell | ✔ | ✔ | ✔ |
chol | ✔ | ||
circshift | ✔ | ✔ | ✔ |
classify (Deep Learning Toolbox) | ✔ | ✔ Half inputs are cast to single precision and computations are performed in single precision. | ✔ Half inputs are cast to single precision and computations are performed in single precision. To perform computations in half, set the library target to |
coder.ceval | ✔ | ✔ | |
colon , : | ✔ | ✔ | ✔ |
complex | ✔ | ✔ | |
conj | ✔ | ✔ | ✔ |
conv | ✔ | ✔ | ✔ |
conv2 | ✔ | ✔ | ✔ |
cos | ✔ | ✔ | ✔ |
cosh | ✔ | ✔ | ✔ |
cospi | ✔ | ✔ | ✔ |
ctranspose | ✔ | ✔ | ✔ |
cumsum | ✔ | ||
dot | ✔ | ||
double | ✔ | ✔ | ✔ |
empty | ✔ | ||
eps | ✔ Supported syntax:
| ✔
| ✔
|
eq , == | ✔ | ✔ | ✔ |
exp | ✔ | ✔ | ✔ |
expm1 | ✔ | ✔ | ✔ |
eye | ✔ Supported syntax:
| ✔ Supported syntax:
| ✔ Supported syntax:
|
fft | ✔ | ✔ | |
fft2 | ✔ | ✔ | |
fftn | ✔ | ✔ | |
fftshift | ✔ | ✔ | ✔ |
fix | ✔ | ✔ | ✔ |
fixed.interp1 | ✔ | ✔ | |
fixed.interp2 | ✔ | ✔ | |
fixed.interp3 | ✔ | ✔ | |
fixed.interpn | ✔ | ✔ | |
flintmax | ✔ Supported syntax:
| ||
flip | ✔ | ✔ Dimension argument cannot be half precision. | ✔ Dimension argument cannot be half precision. |
fliplr | ✔ | ✔ | ✔ |
flipud | ✔ | ✔ | ✔ |
floor | ✔ | ✔ | ✔ |
fma | ✔ Complex half-precision inputs are not supported. | ✔ Complex half-precision inputs are not supported. | ✔ Complex half-precision inputs are not supported. |
fplot | ✔ | ||
ge , >= | ✔ | ✔ | ✔ |
gt , > | ✔ | ✔ | ✔ |
half | ✔ | ✔ | ✔ |
horzcat | ✔ | ✔ | ✔ |
hypot | ✔ | ✔ | ✔ |
ifft | ✔ | ✔ | |
ifft2 | ✔ | ✔ | |
ifftn | ✔ | ✔ | |
ifftshift | ✔ | ✔ | ✔ |
imag | ✔ | ✔ | |
Inf | ✔ Supported syntax:
| ✔ Supported syntax:
| ✔ Supported syntax:
|
int16 | ✔ | ✔ | ✔ |
int32 | ✔ | ✔ | ✔ |
int64 | ✔ | ✔ | ✔ |
int8 | ✔ | ✔ | ✔ |
isa | ✔ | ✔ | ✔ |
iscolumn | ✔ | ✔ | ✔ |
isempty | ✔ | ✔ | ✔ |
isequal | ✔ | ✔ | ✔ |
isequaln | ✔ | ✔ | ✔ |
isfinite | ✔ | ✔ | ✔ |
isfloat | ✔ | ✔ | ✔ |
isinf | ✔ | ✔ | ✔ |
isinteger | ✔ | ✔ | ✔ |
islogical | ✔ | ✔ | ✔ |
ismatrix | ✔ | ✔ | ✔ |
isnan | ✔ | ✔ | ✔ |
isnumeric | ✔ | ✔ | ✔ |
isobject | ✔ Returns true with half-precision input. | ✔ Returns false with half-precision input. | ✔ Returns false with half-precision input. |
isreal | ✔ | ✔ | ✔ |
isrow | ✔ | ✔ | ✔ |
isscalar | ✔ | ✔ | ✔ |
issorted | ✔ | ||
isvector | ✔ | ✔ | ✔ |
ldivide | ✔ | ✔ | ✔ |
le , <= | ✔ | ✔ | ✔ |
length | ✔ | ✔ | ✔ |
line | ✔ | ||
log | ✔ | ✔ | ✔ |
log10 | ✔ | ✔ | ✔ |
log1p | ✔ | ✔ | ✔ |
log2 | ✔ | ✔ Two output syntax is not supported. | ✔ Two output syntax is not supported. |
logical | ✔ | ✔ | ✔ |
lt , < | ✔ | ✔ | ✔ |
lu | ✔ | ||
max | ✔ | ✔ | ✔ |
mean | ✔ | ✔ | ✔ |
min | ✔ | ✔ | ✔ |
minus , - | ✔ | ✔ | ✔ |
mldivide , \ | ✔ Left-hand side must be scalar | ||
mod | ✔ | ✔ | ✔ |
mrdivide , / | ✔ Right-hand side must be scalar | ✔ Right-hand side must be scalar | ✔ Right-hand side must be scalar |
mtimes , * | ✔ | ✔ | ✔ For GPU Code generation, you can perform half-precision matrix multiplication with real inputs. |
NaN | ✔ Supported syntax:
| ✔ Supported syntax:
| ✔ Supported syntax:
|
ndims | ✔ | ✔ | ✔ |
ne , ~= | ✔ | ✔ | ✔ |
not | ✔ | ✔ | ✔ |
numel | ✔ | ✔ | ✔ |
ones | ✔ Supported syntax:
| ✔ Supported syntax:
| ✔ Supported syntax:
|
or , || | ✔ | ✔ | ✔ |
Short-Circuit
OR | ✔ | ✔ | ✔ |
permute | ✔ | ✔ | ✔ |
plot | ✔ | ||
plot3 | ✔ | ||
plotmatrix | ✔ | ||
plus , + | ✔ | ✔ | ✔ |
pow10 | ✔ | ✔ | ✔ |
pow2 | ✔ | ✔ | ✔ |
power , .^ | ✔ | ✔ | ✔ |
predict (Deep Learning Toolbox) | ✔ | ✔ Half inputs are cast to single precision and computations are performed in single precision. | ✔ Half inputs are cast to single precision and computations are performed in single precision. To perform computations in half, set the library target to |
predictAndUpdateState (Deep Learning Toolbox) | ✔ | ✔ Half inputs are cast to single precision and computations are performed in single precision. | ✔ Half inputs are cast to single precision and computations are performed in single precision. To perform computations in half, set the library target to |
prod | ✔ Half inputs are cast to single precision and computations are performed in single precision. As a result, saturation behavior differs between single and half inputs: maxhalf = half.realmax; isequal(prod([maxhalf 2 0.5]), maxhalf) ans = logical 1 maxsingle = realmax('single');
isequal(prod([maxsingle 2 0.5]), maxsingle) ans = logical 0 | ✔ | ✔ |
rdivide | ✔ | ✔ | ✔ |
real | ✔ | ✔ | ✔ |
realmax | ✔ Supported syntax:
| ||
realmin | ✔ Supported syntax:
| ||
rem | ✔ | ✔ | ✔ |
repelem | ✔ | ✔ | ✔ |
repmat | ✔ | ✔ Dimension argument cannot be half precision. | ✔ Dimension argument cannot be half precision. |
reshape | ✔ | ✔ Dimension argument cannot be half precision. | ✔ Dimension argument cannot be half precision. |
rgbplot | ✔ | ||
round | ✔ Only one input supported | ✔ Only one input supported | ✔ Only one input supported |
rsqrt | ✔ Complex half-precision inputs are not supported | ||
scatter | ✔ | ||
scatter3 | ✔ | ||
sign | ✔ | ✔ | ✔ |
sin | ✔ | ✔ | ✔ |
single | ✔ | ✔ | ✔ |
sinh | ✔ | ✔ | ✔ |
sinpi | ✔ | ✔ | ✔ |
size | ✔ | ✔ | ✔ |
sort | ✔ | ||
sqrt | ✔ | ✔ | ✔ |
squeeze | ✔ | ✔ | ✔ |
storedInteger | ✔ | ||
sum | ✔ Half inputs are cast to single precision and computations are performed in single precision. As a result, saturation behavior differs between single and half inputs: maxhalfint = half.flintmax; isequal(sum([maxhalfint, 1, -1]), maxhalfint) ans = logical 1 maxsingleint = flintmax('single');
isequal(sum([maxsingleint, 1, -1]), maxsingleint) ans = logical 0 | ✔ | ✔ |
tan | ✔ | ✔ | ✔ |
tanh | ✔ | ✔ | ✔ |
times , .* | ✔ | ✔ | ✔ |
transpose | ✔ | ✔ | ✔ |
typecast | ✔ | ||
uint16 | ✔ | ✔ | ✔ |
uint32 | ✔ | ✔ | ✔ |
uint64 | ✔ | ✔ | ✔ |
uint8 | ✔ | ✔ | ✔ |
uminus | ✔ | ✔ | ✔ |
uplus | ✔ | ✔ | ✔ |
vertcat | ✔ | ✔ | ✔ |
xlim | ✔ | ||
ylim | ✔ | ||
zeros | ✔ Supported syntax:
| ✔ Supported syntax:
| ✔ Supported syntax:
|
zlim | ✔ |