halfp - A floating-point half precision support package

版本 1.0.5 (180.4 KB) 作者: Alon Geva
This package comprises a new MATLAB classdef called 'halfp' that is intended to support storing and operating on half precision variables.
21.0 次下载
更新时间 2022/7/22

查看许可证

This package comprises a new MATLAB classdef called 'halfp' that is intended to support storing and operating on half precision variables. The new classdef allows the user the flexibility to choose the length of the Exponent (E), Mantissa (M) and rounding method, as long as E+M+1=16bit.
The classdef file is halfp.m that defines a new matlab half-precision number class with the following properties:
  • E - length of the exponent (in bits)
  • M - length of the mantissa (in bits)
  • roundMethod - the rounding method to use when performing various arithmetic operations:
0=roundTiesToEven
1=roundTowardPositive
2=roundTowardNegative
3=roundTowardZero
4=roundTiesToOdd
  • val - a uint16 value comprises the 16-bit representation of the half-precision number.
A new halfp object can now be easily created by calling, for example:
>> a=halfp;a.val=10000
a =
halfp with properties:
E: 5
M: 10
roundMethod: 0
val: 10000
The new class also comprises methods that allow to act on the 'halfp' object:
  • a.castfrom(x) - imports a's value from a 'double' or 'single' variable x.
  • a.double - returns the 'double' precision value of the halp object a.
  • a.single - returns the 'double' precision value of the halp object a.
  • a.hex - returns the hex value of the uint16 representation of object a.
  • a.bin - returns the binary value of the uint16 representation of object a.
  • a.plus - supports signed addition of 2 halfp objects.
  • a.minus - supports signed subtraction of 2 halfp objects.
  • a.mtimes - supports signed multiplication of 2 halfp objects.
Importing from 'double' is performed in the following way:
>> a=halfp
a =
halfp with properties:
E: 5
M: 10
roundMethod: 0
val: []
>> x=1.25
x =
1.250000000000000e+00
>> a=a.castfrom(x)
a =
halfp with properties:
E: 5
M: 10
roundMethod: 0
val: 15616
The class of x (single or double) will be automatically recognized.
There is also an option to define a as a vector or matrix of halfp object:
>> a.val(1:5)=[1 2 3 4 5]
a =
halfp with properties:
E: 5
M: 10
roundMethod: 0
val: [1 2 3 4 5]
>> a.val
ans =
1×5 uint16 row vector
1 2 3 4 5
Another way to work with halfp vectors/matrices is using the following code for example:
>> a(1:5)=halfp
a =
1×5 halfp array with properties:
E
M
roundMethod
val
>> a(1).val=10000;a(2).val=10000;a(3).val=10000;a(4).val=10000;a(5).val=10000;
>> a.val
ans =
uint16
10000
ans =
uint16
10000
ans =
uint16
10000
ans =
uint16
10000
ans =
uint16
10000
However, please note that the below operation functions cannot apply to vectors/matrices comprising multiple halfp object. To mitigate that, these functions should be called for one halfp object at a time (using a for loop for example).
Addition, subtraction or multiplication of 2 halfp objects is simply done using the normal +,- and * operators:
  • c = a + b
  • c = a - b
  • c = a * b
The input arguments must be valid 'halfp' objects having the same E and M, and the output argument will also be an 'halfp' object with the same E and M. The rounding method to be carried-out will be determined by the roundMethod property of the first input argument. Operators priority is natively supported (e.g. multiplication over addition/subtraction and the use of parentheses).
These operations will also support vector/matrix 'halfp' input arguments and will perform the calculation on each scalar member. Needless to say, the input arguments MUST have the same length if both arguments are of size > 1. Performing the operations when one of the arguments is of size>1 and the other is of size=1, is also supported. Furthermore, inroducing direct 'double' or 'single' precision input arguments to the above operation functions is now also supported (scalar or vector). The code will simply convert them to an 'halfp' object before perfoming the actual arithmetics. For example:
>> a=halfp;a.val=10000;a.double
ans =
0.0276
>> b = a + 1
b =
halfp with properties:
E: 5
M: 10
roundMethod: 0
val: 15388
>> b.double
ans =
1.0273
(halfp_extract_sem.m, halfp_primitive_signed_addition.m and halfp_primitive_signed_multiply.m are internal functions and need not be used by the user).
Note: the code was not optimized for runtime in any way. This will be the focus of future versions.

引用格式

Alon Geva (2024). halfp - A floating-point half precision support package (https://www.mathworks.com/matlabcentral/fileexchange/112720-halfp-a-floating-point-half-precision-support-package), MATLAB Central File Exchange. 检索来源 .

MATLAB 版本兼容性
创建方式 R2018b
兼容任何版本
平台兼容性
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
版本 已发布 发行说明
1.0.5

fixed description

1.0.4

new bugs fixes

1.0.3

* Added support for [+,-,*] operators overloading into halfp classdef (halfp_add, halfp_sub and halfp_mul were left for backwards compatibility for now)
* Bug fixing

1.0.2

* support for direct 'double' or 'single' precision input arguments to the arithmetic functions.
* bug fix in rounding mechanism inside arithmetic functions.

1.0.1

bug fixing and allowing operation with mixed size input arguments when one argument is of size 1 (e.g. [x, y, z, w]+1=[x+1, y+1, z+1, w+1]).

1.0.0