MCPcopy Create free account
hub / github.com/PrincetonVision/marvin / cpu_half2float

Function cpu_half2float

tools/tensorIO_matlab/half2float.cpp:3–28  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1#include "mex.h"
2
3float cpu_half2float(unsigned short x)
4{
5 unsigned sign = ((x >> 15) & 1);
6 unsigned exponent = ((x >> 10) & 0x1f);
7 unsigned mantissa = ((x & 0x3ff) << 13);
8 if (exponent == 0x1f) { /* NaN or Inf */
9 mantissa = (mantissa ? (sign = 0, 0x7fffff) : 0);
10 exponent = 0xff;
11 } else if (!exponent) { /* Denorm or Zero */
12 if (mantissa) {
13 unsigned int msb;
14 exponent = 0x71;
15 do {
16 msb = (mantissa & 0x400000);
17 mantissa <<= 1; /* normalize */
18 --exponent;
19 } while (!msb);
20 mantissa &= 0x7fffff; /* 1.mantissa is implicit */
21 }
22 } else {
23 exponent += 0x70;
24 }
25 int temp = ((sign << 31) | (exponent << 23) | mantissa);
26
27 return *((float*)((void*)&temp));
28}
29
30void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
31

Callers 1

mexFunctionFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected