log2的快速算法

在某些不需要那么高精度的log2函数,单要求高速的地方,可以使用以下方法来减少log2的运算量,但精度全靠内存堆,若要提高精度需要把flog2map的长度增加,该表由注释部分代码生成。
当表修改后,对应的“temp = (temp>>15)&0xFF;”也需要修改。
比如,当表长为32时候,就是temp = (temp>>18)&0x1F;
因为32需要5bit来表达
18 = 23-5
0x1F = 0b 0001 1111

#include <stdio.h>
#include "math.h"
/*
表生成算法flog2map
for i = 0:256
    temp = i/256;
    templog = log2(1+temp);
    fprintf('%f,\n',templog)
end
*/

#define uint8_t unsigned char
float flog2map[256]={0.000000,0.005625,0.011227,0.016808,0.022368,0.027906,0.033423,0.038919,0.044394,0.049849,0.055282,0.060696,0.066089,0.071462,0.076816,0.082149,0.087463,0.092757,0.098032,0.103288,0.108524,0.113742,0.118941,0.124121,0.129283,0.134426,0.139551,0.144658,0.149747,0.154818,0.159871,0.164907,0.169925,0.174926,0.179909,0.184875,0.189825,0.194757,0.199672,0.204571,0.209453,0.214319,0.219169,0.224002,0.228819,0.233620,0.238405,0.243174,0.247928,0.252665,0.257388,0.262095,0.266787,0.271463,0.276124,0.280771,0.285402,0.290019,0.294621,0.299208,0.303781,0.308339,0.312883,0.317413,0.321928,0.326429,0.330917,0.335390,0.339850,0.344296,0.348728,0.353147,0.357552,0.361944,0.366322,0.370687,0.375039,0.379378,0.383704,0.388017,0.392317,0.396605,0.400879,0.405141,0.409391,0.413628,0.417853,0.422065,0.426265,0.430453,0.434628,0.438792,0.442943,0.447083,0.451211,0.455327,0.459432,0.463524,0.467606,0.471675,0.475733,0.479780,0.483816,0.487840,0.491853,0.495855,0.499846,0.503826,0.507795,0.511753,0.515700,0.519636,0.523562,0.527477,0.531381,0.535275,0.539159,0.543032,0.546894,0.550747,0.554589,0.558421,0.562242,0.566054,0.569856,0.573647,0.577429,0.581201,0.584963,0.588715,0.592457,0.596190,0.599913,0.603626,0.607330,0.611025,0.614710,0.618386,0.622052,0.625709,0.629357,0.632995,0.636625,0.640245,0.643856,0.647458,0.651052,0.654636,0.658211,0.661778,0.665336,0.668885,0.672425,0.675957,0.679480,0.682995,0.686501,0.689998,0.693487,0.696968,0.700440,0.703904,0.707359,0.710806,0.714246,0.717676,0.721099,0.724514,0.727920,0.731319,0.734710,0.738092,0.741467,0.744834,0.748193,0.751544,0.754888,0.758223,0.761551,0.764872,0.768184,0.771489,0.774787,0.778077,0.781360,0.784635,0.787903,0.791163,0.794416,0.797662,0.800900,0.804131,0.807355,0.810572,0.813781,0.816984,0.820179,0.823367,0.826548,0.829723,0.832890,0.836050,0.839204,0.842350,0.845490,0.848623,0.851749,0.854868,0.857981,0.861087,0.864186,0.867279,0.870365,0.873444,0.876517,0.879583,0.882643,0.885696,0.888743,0.891784,0.894818,0.897845,0.900867,0.903882,0.906891,0.909893,0.912889,0.915879,0.918863,0.921841,0.924813,0.927778,0.930737,0.933691,0.936638,0.939579,0.942515,0.945444,0.948367,0.951285,0.954196,0.957102,0.960002,0.962896,0.965784,0.968667,0.971544,0.974415,0.977280,0.980140,0.982994,0.985842,0.988685,0.991522,0.994353,0.997179,1.000000,};

float flog2(float x) {
    unsigned int temp,i=0;
    int outint;
    float tempf,templog;
    //整数部分
    temp = *(unsigned int*)&x;
    outint = (temp>>23&255)-127;
    //小数部分
    temp = (temp>>15)&0xFF;
    return outint + flog2map[temp];
}
int main()
{
    float input;
    float output1;
    float output2;
    float i;
    output2 = flog2(0.04);
    for(i=-10;i<10;i+=0.1){
        input = pow(2,i);
        output1 = log2f(input);
        output2 = flog2(input);
        printf("%f      %f\n",output1,output2);
    }

    return 0;
}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值