首页 > 其他分享 >float32 和float16互转

float32 和float16互转

时间:2022-10-12 19:11:08浏览次数:43  
标签:float16 quantized int float t1 互转 diff original float32

//int  main()
//{
//    int i = 0;
//    float fVal = -255.123456789;
//    char *pChar;
//    pChar = (char *)&fVal;
//
//    for (i = 0; i<4; i++)
//    {
//        printf("chs[%d] = %x\n", i, pChar[i]);
//    }
//
//    pChar[0] = 0;
//    /*pChar[1] = 0;
//    pChar[2] = 0;
//    pChar[3] = 0;*/
//    float * pFloat = (float *)pChar;
//    printf("fVal = %0.9f\n", pFloat[0]);
//    getchar();
//}


// based on https://gist.github.com/martin-kallman/5049614
// float32
// Martin Kallman
//
// Fast half-precision to single-precision floating point conversion
//  - Supports signed zero and denormals-as-zero (DAZ)
//  - Does not support infinities or NaN
//  - Few, partially pipelinable, non-branching instructions,
//  - Core opreations ~6 clock cycles on modern x86-64
void float32(float *__restrict out, const short in) {
    unsigned int t1;
    unsigned int t2;
    unsigned int t3;

    t1 = in & 0x7fffu;                       // Non-sign bits
    t2 = in & 0x8000u;                       // Sign bit
    t3 = in & 0x7c00u;                       // Exponent

    t1 <<= 13u;                              // Align mantissa on MSB
    t2 <<= 16u;                              // Shift sign bit into position

    t1 += 0x38000000;                       // Adjust bias

    t1 = (t3 == 0 ? 0 : t1);                // Denormals-as-zero

    t1 |= t2;                               // Re-insert sign bit

    *((unsigned int *)out) = t1;
};

// float16
// Martin Kallman
//
// Fast single-precision to half-precision floating point conversion
//  - Supports signed zero, denormals-as-zero (DAZ), flush-to-zero (FTZ),
//    clamp-to-max
//  - Does not support infinities or NaN
//  - Few, partially pipelinable, non-branching instructions,
//  - Core opreations ~10 clock cycles on modern x86-64
void float16(short *__restrict out, const float in) {
    unsigned int  inu = *((unsigned int  *)& in);
    unsigned int  t1;
    unsigned int  t2;
    unsigned int  t3;

    t1 = inu & 0x7fffffffu;                 // Non-sign bits
    t2 = inu & 0x80000000u;                 // Sign bit
    t3 = inu & 0x7f800000u;                 // Exponent

    t1 >>= 13u;                             // Align mantissa on MSB
    t2 >>= 16u;                             // Shift sign bit into position

    t1 -= 0x1c000;                         // Adjust bias

    t1 = (t3 < 0x38800000u) ? 0 : t1;       // Flush-to-zero
    t1 = (t3 > 0x8e000000u) ? 0x7bff : t1;  // Clamp-to-max
    t1 = (t3 == 0 ? 0 : t1);               // Denormals-as-zero

    t1 |= t2;                              // Re-insert sign bit

    *((short *)out) = t1;
};

#define ABS(A) ((A) >= 0 ? (A) : -(A))

int main() {
    float original = -42.42f;
    short small = 0;
    float16(&small, original);
    float quantized = 0.0f;
    float32(&quantized, small);
    float diff = ABS(original - quantized);
    printf("orig %f quantized %f absdiff %f\n", original, quantized, diff);
    getchar();
    //assert(diff < 0.1f);
}

 

标签:float16,quantized,int,float,t1,互转,diff,original,float32
From: https://www.cnblogs.com/profession/p/16785638.html

相关文章

  • javabean、xml、json、map之间的相互转换
    javabean转map利用cglib的BeanMap类实现转换importorg.springframework.cglib.beans.BeanMap;/****@Title:beanToMap*@Description:javabean转map*@parambean*......
  • Echarts——饼图折线图柱状图相互转换
    前言项目里使用的echarts版本是4.9.0,这里就用该版本做演示;配置项:echartsoptioncodesandbox示例:Echarts数据转换内容鉴于echarts折线图和柱状图可以直接通过to......
  • FlinkSQL的DataStream和Table互转的Demo
    1.构建UserLog对象@Data@Builder//创建对象@NoArgsConstructor//无参构造函数@AllArgsConstructor//有参构造函数publicclassUserLog{privateStr......
  • 时间类型和字符串类型相互转换——SimpleDateFormat中parse和format的用法
    SimpleDateFormatsimpleDateFormat=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");simpleDateFormat.parse("xxxxxxx");//字符串->时间simpleDateFormat.format(new......
  • 网络字节序与主机字节序的相互转换
    1.前言知识  字节顺序是指(在计算机中)多于一个字节的数据类型在内存中的存放顺序。例如一个32位整数由4个字节组成,内存中存储这4个字节可以采取两种方法:小端字节序(littl......
  • 网络字节序与主机字节序的相互转换
    在Linux网络编程中,经常碰到网络字节序与主机字节序的相互转换。说到网络字节序与主机字节序需要清晰了解以下几个概念。字节序,顾名思义,指字节在内存中存储的顺序。比如一个......
  • base64与中文字符串互转
    实现代码如下//字符串转base64getEncode64(str){returnbtoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,functiontoSolidBytes(match,p......
  • 网络字节序与主机字节序的相互转换
    1.网络字节序与主机字节序在Linux网络编程中,经常碰到网络字节序与主机字节序的相互转换。说到网络字节序与主机字节序需要清晰了解以下几个概念。字节序,顾名思义,指字节在......
  • 两行命令搞定M3U8和MP4文件互转
    用MP4中制作M3U8:ffmpeg-imyVideo.mp4-ccopy-bsfh264_mp4toannexboutput.tsffmpeg-ioutput.ts-ccopy-map0-fsegment-segment_listplaylist.m3u8-segment_......
  • 基本数据类型和字符串互转
      常用sprintf函数,用于其他类型转字符串:  例子:  简单理解一下sprintf的用法即可  注意,其中  a和b之间会输出空格,因为%d%d之间有空格,他们之间有什么......