Fixpoint Arithmetics

时间:2016-07-05 13:23:12

标签: c

我想将概率乘以0.9999 * 0.9955 * 0.6360 ... 作为固定点(无符号短16位,14位小数部分) 我想,我减少了太多。乘法后的值之间的哪一部分我应该采取进一步的乘法?

#include <stdio.h>
//#include <new>  

#include <string.h>
#include <stdlib.h>


unsigned short zweihoch(short value) { //2^(12)
    unsigned short ans = 1;
    int i;

    for (i = 0; i < value; i++) {
        ans = 2 * ans;
    }

    return ans;
}

double zweihochneg(short value) {  //2^(-12)
    double ans = 1;
    int i;

    for (i = 0; i < -value; i++) {
        ans = 2 * ans;
    }
    ans = 1 / ans;

    return ans;
}

unsigned short umul16hi(unsigned short a, unsigned short b) //multiplication 16*16 stored in 16, takes the right 16 bits, normal mult cuts 16 left bits out
{
    unsigned short ans = 0;
    /* split operands into halves */
    unsigned short al1 = a << 8;
    unsigned short al = al1 >> 8;
    unsigned short ah = a >> 8;

    unsigned short bl1 = b << 8;
    unsigned short bl = bl1 >> 8;
    unsigned short bh = b >> 8;
    /* compute partial products */
    unsigned short p0 = al * bl;
    unsigned short p1 = al * bh;
    unsigned short p11 = p1 << 8;
    unsigned short p1_s = p11 >> 8;

    unsigned short p2 = ah * bl;
    unsigned short p3 = ah * bh;
    unsigned short p21 = p2 << 8;
    unsigned short p2_s = p21 >> 8;

    /* sum partial products */
    unsigned short cy = ((p0 >> 8) + (unsigned short)p1_s + (unsigned short)p2_s) >> 8;
    return ans = (p3 + (p2 >> 8) + (p1 >> 8) + cy);
}



int main()
{
    unsigned short Prob_QAM = 0;
    double Prob_double[6] = { 0.9999, 0.9955, 0.6360, 0.7767, 0.6700, 0.6057 };
    unsigned short Prob[6] = Prob_double * zweihoch(12);  // circa 4095 4077 2687 3181 2744 2480
    Prob_QAM = umul16hi( Prob[0] , Prob[1] );
    Prob_QAM = Prob_QAM * zweihoch(12); //cuts too much
    Prob_QAM = umul16hi( Prob_QAM , Prob[2]) * zweihoch(12); //cuts too much
    Prob_QAM = umul16hi( Prob_QAM , Prob[3]) * zweihoch(12); //cuts too much
    Prob_QAM = umul16hi( Prob_QAM , Prob[4]) * zweihoch(12); //cuts too much
    Prob_QAM = umul16hi( Prob_QAM , Prob[5]) * zweihoch(12); //cuts too much

    Prob_QAM_double = 0.1995; //Prob_QAM must be like this
    return 0;
}

0 个答案:

没有答案
相关问题