|
|
|
# <img src="https://posithub.org/images/NGA_logo.png" width="90" height="55"/> SoftPosit
|
|
|
|
|
|
[Information on posit standard](Information on posit standard)
|
|
[Information on posit standard](Information on posit standard)
|
|
|
|
|
|
|
|
# SoftPosit (C)
|
|
|
|
|
|
SoftPosit is based on Berkeley SoftFloat Release 3d to enable software comparison between posits and floats.
|
|
SoftPosit is based on Berkeley SoftFloat Release 3d to enable software comparison between posits and floats.
|
|
|
|
|
|
Version 0.2 it contains all functions in the expected Posit Standard for 16-bit posit with es=1 (posit16_t) and 8-bit posit with es=0 (posit8_t).
|
|
Release v0.4.1rc supports
|
|
|
|
|
|
|
|
* Fast (Optimised for performance) types (recommended posit sizes and their respective exponential sizes (es)
|
|
|
|
* posit8_t : 8-bit (es=0)
|
|
|
|
* posit16_t : 16-bit (es=1)
|
|
|
|
* posit32_t : 32-bit (es=2)
|
|
|
|
* Flexible (Not optimised for performance) types
|
|
|
|
* posit_2_t : 2 to 32-bit (es=2)
|
|
|
|
|
|
|
|
SoftPosit contains all functions in the expected Posit Standard.
|
|
|
|
|
|
**Installation**
|
|
**Installation**
|
|
|
|
|
|
|
|
To optimise the compilation for your architecture, update the OPTIMISATION flag in build/Linux-x86_64-GCC/Makefile.
|
|
|
|
|
|
|
|
```
|
|
cd SoftPosit/build/Linux-x86_64-GCC
|
|
cd SoftPosit/build/Linux-x86_64-GCC
|
|
|
|
|
|
make -j6 all
|
|
make -j6 all
|
|
|
|
```
|
|
|
|
|
|
Please link to the archive (softposit.a) to use.
|
|
Please link to the archive (softposit.a) to use.
|
|
|
|
|
|
**Tested on**
|
|
**Tested on**
|
|
|
|
|
|
Release v0.1 supports 16-bit posit with one exponent bit (posit16_t). This code is tested on
|
|
This code is tested on
|
|
|
|
|
|
* GNU gcc (SUSE Linux) 4.8.5
|
|
* GNU gcc (SUSE Linux) 4.8.5
|
|
* Apple LLVM version 9.1.0 (clang-902.0.39.2)
|
|
* Apple LLVM version 9.1.0 (clang-902.0.39.2)
|
|
|
|
|
|
An example on how to use the code to add:
|
|
### Examples
|
|
|
|
|
|
|
|
#### A 8-bit example on how to use the code to add:
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
#include "softposit.h"
|
|
|
|
|
|
|
|
int main (int argc, char *argv[]){
|
|
|
|
|
|
|
|
posit8_t pA, pB, pZ;
|
|
|
|
pA = castP8(0xF2);
|
|
|
|
pB = castP8(0x23);
|
|
|
|
|
|
|
|
pZ = p8_add(pA, pB);
|
|
|
|
|
|
|
|
//To check answer by converting it to double
|
|
|
|
double dZ = convertP8ToDouble(pZ);
|
|
|
|
printf("dZ: %.15f\n", dZ);
|
|
|
|
|
|
|
|
//To print result in binary
|
|
|
|
uint8_t uiZ = castUI(pZ);
|
|
|
|
printBinary((uint64_t*)&uiZ, 8);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
#### A 16-bit example on how to use the code to multiply:
|
|
|
|
|
|
```
|
|
```
|
|
#include "softposit.h"
|
|
#include "softposit.h"
|
... | @@ -31,18 +77,75 @@ int main (int argc, char *argv[]){ |
... | @@ -31,18 +77,75 @@ int main (int argc, char *argv[]){ |
|
pA = castP16(0x0FF2);
|
|
pA = castP16(0x0FF2);
|
|
pB = castP16(0x2123);
|
|
pB = castP16(0x2123);
|
|
|
|
|
|
pZ = p8_mul(pA, pB);
|
|
pZ = p16_mul(pA, pB);
|
|
|
|
|
|
//To check answer
|
|
//To check answer by converting it to double
|
|
double dZ = convertP16ToDouble(pZ);
|
|
double dZ = convertP16ToDouble(pZ);
|
|
|
|
printf("dZ: %.15f\n", dZ);
|
|
|
|
|
|
//To print result in binary
|
|
//To print result in binary
|
|
uint16_t uiZ = castUI16(pZ);
|
|
uint16_t uiZ = castUI(pZ);
|
|
printBinary(&uiZ, 16);
|
|
printBinary((uint64_t*)&uiZ, 16);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
#### A 24-bit (es=2) example on how to use the code:
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
#include "softposit.h"
|
|
|
|
|
|
|
|
int main (int argc, char *argv[]){
|
|
|
|
|
|
|
|
posit_2_t pA, pB, pZ;
|
|
|
|
pA = {.v = 0xF2}; //this is to set the bits (method 1)
|
|
|
|
pB = castPX2(0x23); //this is to set the bits (method 2)
|
|
|
|
|
|
|
|
pZ = pX2_add(pA, pB, 24);
|
|
|
|
|
|
|
|
//To check answer by converting it to double
|
|
|
|
double dZ = convertPX2ToDouble(pZ);
|
|
|
|
printf("dZ: %.15f\n", dZ);
|
|
|
|
|
|
|
|
//To print result in binary
|
|
|
|
printBinaryPX((uint32_t*)&pZ.v, 24);
|
|
|
|
|
|
|
|
//To print result as double
|
|
|
|
printf("result: %.13f\n", convertPX2ToDouble(pZ));
|
|
|
|
|
|
return 0;
|
|
return 0;
|
|
|
|
|
|
}
|
|
}
|
|
```
|
|
```
|
|
|
|
|
|
|
|
#### For deep learning, please use quire.
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
//Convert double to posit
|
|
|
|
posit16_t pA = convertDoubleToP16(1.02783203125 );
|
|
|
|
posit16_t pB = convertDoubleToP16(0.987060546875);
|
|
|
|
posit16_t pC = convertDoubleToP16(0.4998779296875);
|
|
|
|
posit16_t pD = convertDoubleToP16(0.8797607421875);
|
|
|
|
|
|
|
|
quire16_t qZ;
|
|
|
|
|
|
|
|
//Set quire to 0
|
|
|
|
qZ = q16_clr(qZ);
|
|
|
|
|
|
|
|
//accumulate products without roundings
|
|
|
|
qZ = q16_fdp_add(qZ, pA, pB);
|
|
|
|
qZ = q16_fdp_add(qZ, pC, pD);
|
|
|
|
|
|
|
|
//Convert back to posit
|
|
|
|
posit16_t pZ = q16_to_p16(qZ);
|
|
|
|
|
|
|
|
//To check answer
|
|
|
|
double dZ = convertP16ToDouble(pZ);
|
|
|
|
```
|
|
|
|
|
|
------------------------------------------
|
|
------------------------------------------
|
|
|
|
|
|
Main Posit Functionalities:
|
|
Main Posit Functionalities:
|
... | | ... | |