Half Precision Code Generation Support
To assign a halfprecision data type to a number or variable, use the half
constructor. A halfprecision data type occupies 16 bits of memory, but its floatingpoint representation enables it to handle wider dynamic ranges than integer or fixedpoint data types of the same size. For more information, see FloatingPoint Numbers.
A subset of MATLAB^{®} functions are supported for use with halfprecision inputs. Additionally, some functions support code generation with halfprecision 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. 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  ✔  ✔  ✔ 
and , &  ✔  ✔  ✔ 
any  ✔  ✔  ✔ 
area  ✔  
asin  ✔  ✔  ✔ 
asinh  ✔  ✔  ✔ 
atan  ✔  ✔  ✔ 
atan2  ✔  ✔  ✔ 
atanh  ✔  ✔  ✔ 
bar  ✔  
barh  ✔  
cast  ✔ Supported syntax:
 ✔ Supported syntax:
 ✔ Supported syntax:

cat  ✔  ✔
 ✔

ceil  ✔  ✔  ✔ 
cell  ✔  ✔  ✔ 
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  ✔  ✔  ✔ 
eq , ==  ✔  ✔  ✔ 
exp  ✔  ✔  ✔ 
expm1  ✔  ✔  ✔ 
eye  ✔ Supported syntax:
 ✔ Supported syntax:
 ✔ Supported syntax:

fft  ✔  ✔  
fft2  ✔  ✔  
fftn  ✔  ✔  
fftshift  ✔  ✔  ✔ 
fix  ✔  ✔  ✔ 
flip  ✔  ✔ Dimension argument cannot be half precision.  ✔ Dimension argument cannot be half precision. 
fliplr  ✔  ✔  ✔ 
flipud  ✔  ✔  ✔ 
floor  ✔  ✔  ✔ 
fma  ✔ Complex halfprecision inputs are not supported.  ✔ Complex halfprecision inputs are not supported.  ✔ Complex halfprecision 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 halfprecision input.  ✔ Returns false with halfprecision input.  ✔ Returns false with halfprecision 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  ✔  ✔  ✔ 
Logical Operators: ShortCircuit &&   ✔  ✔  ✔ 
lt , <  ✔  ✔  ✔ 
max  ✔  ✔  ✔ 
mean  ✔  ✔  ✔ 
min  ✔  ✔  ✔ 
minus ,   ✔  ✔  ✔ 
mldivide , \  ✔ Lefthand side must be scalar  
mod  ✔  ✔  ✔ 
mrdivide , /  ✔ Righthand side must be scalar  ✔ Righthand side must be scalar  ✔ Righthand side must be scalar 
mtimes , *  ✔  ✔  ✔ For GPU Code generation, you can perform halfprecision matrix multiplication with real inputs. 
NaN  ✔ Supported syntax:
 ✔ Supported syntax:
 ✔ Supported syntax:

ndims  ✔  ✔  ✔ 
ne , ~=  ✔  ✔  ✔ 
not  ✔  ✔  ✔ 
numel  ✔  ✔  ✔ 
ones  ✔ Supported syntax:
 ✔ Supported syntax:
 ✔ Supported syntax:

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  ✔  ✔  ✔ 
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 halfprecision 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  ✔ 