aboutsummaryrefslogtreecommitdiff
path: root/sem4/embedded/emb_m2/myfloat_red.h
blob: 142d0bcf549c9c3f1897a6dda306afee93de1254 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
  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;
}