From 57305119e05559c1c37e903aef89cd43f44c42c9 Mon Sep 17 00:00:00 2001 From: Julian T Date: Tue, 11 Feb 2020 11:48:53 +0100 Subject: Added embedded assignment and python assignments --- sem4/embedded/emb_m2/myfloat_red.h | 57 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 sem4/embedded/emb_m2/myfloat_red.h (limited to 'sem4/embedded/emb_m2/myfloat_red.h') diff --git a/sem4/embedded/emb_m2/myfloat_red.h b/sem4/embedded/emb_m2/myfloat_red.h new file mode 100644 index 0000000..b553233 --- /dev/null +++ b/sem4/embedded/emb_m2/myfloat_red.h @@ -0,0 +1,57 @@ +/* + Low level precision floating point lib + Author: Henrik Schiøler +*/ + +//low precision floating pt type +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; +} + +//convert from low precision type to double +double myfloat2double(myfloat_type *arg1) +{ + 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) +{ + 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; + +} -- cgit v1.2.3