inference-engine/src/mkldnn_plugin/nodes/common/fp16_utils.h
// Function to convert F32 into F16
Function to convert F32 into F16
Function to convert F32 into F16
inline float f16tof32(ie_fp16 x) {
    uint32_t u = static_cast<uint32_t>(x);
    uint32_t s = ((u & 0x8000) << 16);
    if ((u & EXP_MASK_F16) == EXP_MASK_F16) {
        u &= 0x03FF;
        if (u) {
            u |= 0x0200;
        }
        u <<= (23 - 10);
        u |= EXP_MASK_F32;
        u |= s;
    } else if ((u & EXP_MASK_F16) == 0) {  
        uint16_t h_sig = (u & 0x03ffu);
        if (h_sig == 0) {
            u = s;
        } else {
            uint16_t h_exp = (u & EXP_MASK_F16);
            h_sig <<= 1;
            while ((h_sig & 0x0400u) == 0) {
                h_sig <<= 1;
                h_exp++;
            }
            uint32_t f_exp = (static_cast<uint32_t>(127 - 15 - h_exp)) << 23;
            uint32_t f_sig = (static_cast<uint32_t>(h_sig & 0x03ffu)) << 13;
            u = s + f_exp + f_sig;
        }
    } else {
        u = (u & 0x7FFF);
        u <<= (23 - 10);
        u += ((127 - 15) << 23);
        u |= s;
    }
    return asfloat(u);
}
inline float f16tof32 ( ie_fp16 x ) { uint32_t u = static_cast < uint32_t > ( x ) ; uint32_t s = ( ( u & 0x8000 ) << 16 ) ; if ( ( u & EXP_MASK_F16 ) == EXP_MASK_F16 ) { u &= 0x03FF ; if ( u ) { u |= 0x0200 ; } u <<= ( 23 - 10 ) ; u |= EXP_MASK_F32 ; u |= s ; } else if ( ( u & EXP_MASK_F16 ) == 0 ) { uint16_t h_sig = ( u & 0x03ffu ) ; if ( h_sig == 0 ) { u = s ; } else { uint16_t h_exp = ( u & EXP_MASK_F16 ) ; h_sig <<= 1 ; while ( ( h_sig & 0x0400u ) == 0 ) { h_sig <<= 1 ; h_exp ++ ; } uint32_t f_exp = ( static_cast < uint32_t > ( 127 - 15 - h_exp ) ) << 23 ; uint32_t f_sig = ( static_cast < uint32_t > ( h_sig & 0x03ffu ) ) << 13 ; u = s + f_exp + f_sig ; } } else { u = ( u & 0x7FFF ) ; u <<= ( 23 - 10 ) ; u += ( ( 127 - 15 ) << 23 ) ; u |= s ; } return asfloat ( u ) ; }
Function to convert F32 into F16
Function to convert F32 into F16
// this is storage for output result // get sign in 32bit format // check for NAN and INF // keep mantissa only // check if it is NAN and raise 10 bit to be align with intrin // check for zero and denormals. /* Signed zero */ /* Subnormal */ // abs // shift mantissa and exp from f16 to f32 position // new bias for exp (f16 bias is 15 and f32 bias is 127) // add sign // finaly represent result as float and return