diff options
Diffstat (limited to 'sem4/embedded/emb_m2/myfloat_red.h')
-rw-r--r-- | sem4/embedded/emb_m2/myfloat_red.h | 69 |
1 files changed, 33 insertions, 36 deletions
diff --git a/sem4/embedded/emb_m2/myfloat_red.h b/sem4/embedded/emb_m2/myfloat_red.h index b553233..142d0bc 100644 --- a/sem4/embedded/emb_m2/myfloat_red.h +++ b/sem4/embedded/emb_m2/myfloat_red.h @@ -4,54 +4,51 @@ */ //low precision floating pt type -typedef struct myfloat -{ - signed char mantissa; - signed char exponent; +typedef struct myfloat { + signed char mantissa; + signed char exponent; } myfloat_type; - //convert from double to low precision type void doub2mydouble(double arg, myfloat_type *res) { - int exponent; - double temp; - exponent = ceil(log(abs(arg))/log(2)); //base 2 logarithm - temp=arg*pow(2,7-exponent); - res->mantissa = (signed char)temp; - res->exponent = exponent-7; + int exponent; + double temp; + exponent = ceil(log(abs(arg)) / log(2)); //base 2 logarithm + temp = arg * pow(2, 7 - exponent); + res->mantissa = (signed char)temp; + res->exponent = exponent - 7; } //convert from low precision type to double double myfloat2double(myfloat_type *arg1) { - double res = (double)(arg1->mantissa) * pow(2,arg1->exponent); - return res; + double res = (double)(arg1->mantissa) * pow(2, arg1->exponent); + return res; } //multiply to low precision types -void mult_float(myfloat_type *arg1,myfloat_type *arg2,myfloat_type *result) +void mult_float(myfloat_type *arg1, myfloat_type *arg2, myfloat_type *result) { - int temp; - unsigned char sign; - - sign=0x80 & ((unsigned char)arg1-> mantissa ^ (unsigned char)arg2-> mantissa); //find sign of result - - char i=0; - temp = (int)(arg1-> mantissa) * (int)(arg2-> mantissa); - - temp = temp & 0x7f00; //take away sign from product - - while(abs(temp)>128) - { - i++; - temp=temp>>1; - } - - result->mantissa = (unsigned char) temp; - - result->mantissa = result->mantissa | sign; //add recorded sign - - result->exponent = arg1->exponent + arg2->exponent + i; - + int temp; + unsigned char sign; + + sign = 0x80 & ((unsigned char)arg1->mantissa ^ + (unsigned char)arg2->mantissa); //find sign of result + + char i = 0; + temp = (int)(arg1->mantissa) * (int)(arg2->mantissa); + + temp = temp & 0x7f00; //take away sign from product + + while (abs(temp) > 128) { + i++; + temp = temp >> 1; + } + + result->mantissa = (unsigned char)temp; + + result->mantissa = result->mantissa | sign; //add recorded sign + + result->exponent = arg1->exponent + arg2->exponent + i; } |