aboutsummaryrefslogtreecommitdiff
path: root/sem4/embedded/emb_m2/myfloat_red.h
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2020-02-11 11:48:53 +0100
committerJulian T <julian@jtle.dk>2020-02-11 11:48:53 +0100
commit57305119e05559c1c37e903aef89cd43f44c42c9 (patch)
tree5df95141e99550c22710a94629dca09d7185851a /sem4/embedded/emb_m2/myfloat_red.h
parentbf40ddf3a1970b857adeb070a1b4d0dbc8f3c1b6 (diff)
Added embedded assignment and python assignments
Diffstat (limited to 'sem4/embedded/emb_m2/myfloat_red.h')
-rw-r--r--sem4/embedded/emb_m2/myfloat_red.h57
1 files changed, 57 insertions, 0 deletions
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;
+
+}