This repository was archived by the owner on Aug 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfloatTop8.cpp
More file actions
57 lines (54 loc) · 1.75 KB
/
floatTop8.cpp
File metadata and controls
57 lines (54 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "mex.h"
//#include "zposit8.hpp"
#include "posit.h"
#include <stdint.h>
using zposit_type = Posit<int8_t,8,0,uint16_t,true>;
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
if(nrhs != 1 && nlhs != 1)
{
mexPrintf("floatTozp8(input) -> output\n");
return;
}
bool complex = mxIsComplex(prhs[0]);
const auto ndims = mxGetNumberOfDimensions(prhs[0]);
const mwSize * dimi = mxGetDimensions(prhs[0]);
plhs[0] = mxCreateUninitNumericArray(ndims,(mwSize*)dimi,mxINT8_CLASS,!complex ? mxREAL: mxCOMPLEX);
zposit_type * dst = (zposit_type*)mxGetData(plhs[0]);
int n = mxGetNumberOfElements(prhs[0])*(complex?2:1);
switch(mxGetClassID(prhs[0]))
{
case mxDOUBLE_CLASS:
// double precision
{
double * src = (double*)mxGetData(prhs[0]);
for(int i = 0;i < n; i++)
{
dst[i] = zposit_type(src[i]);
}
}
break;
case mxSINGLE_CLASS:
{
float * src = (float*)mxGetData(prhs[0]);
for(int i = 0;i < n; i++)
{
dst[i] = zposit_type(src[i]);
}
}
break;
case mxINT16_CLASS:
case mxUINT16_CLASS:
{
halffloat * src = (halffloat*)mxGetData(prhs[0]);
for(int i = 0;i < n; i++)
{
dst[i] = zposit_type(typename zposit_type::UnpackedT(halffloat(src[i]))); // wrap cast to unpacked then to posit
}
}
break;
default:
mexErrMsgTxt("Unsupported type of output: only double, single or 16-bit has half float\n");
break;
}
}