Fixpoint Arithmetics
-
I want to multiply the probabilities 0.9999* 0.9955* 0.6360... as fixpoint(unsigned short 16 bit, 14 bit fractional part) I think, I cut too much off. What part of in between value after multiplication should I take for further multiplication?
#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 >>
+ (unsigned short)p1_s + (unsigned short)p2_s) >> 8;
return ans = (p3 + (p2 >>
+ (p1 >>
+ 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 muchProb_QAM_double = 0.1995; //Prob_QAM must be like this
return 0;
}
-
I can see, that u name your functions in german language. Also i have seen in your other postings, that u are able to express you in german.
Are u too bescheuert, to recognize, that this is a german forum, or why do u quäl us with english written questions?
-
schau Dir mal das Ergebnis von
zweihoch(12);an.
-
Belli schrieb:
Are u too bescheuert, to recognize, that this is a german forum, or why do u quäl us with english written questions?
I guess that's a result of "kopieren&einfügen". Let's check google... oh yes, found it: the same question 17 hours ago on stackoverflow
-
... und umul16h kann der Compiler besser, als Du das da hingeschrieben hast.