-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfakefloat.c
More file actions
74 lines (63 loc) · 1.93 KB
/
Copy pathfakefloat.c
File metadata and controls
74 lines (63 loc) · 1.93 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//
// fakefloat.c
// fakefloat
//
// Created by Yaroslav on 1/22/19.
// MIT License
//
#include "fakefloat.h"
//#include <assert.h>
void ffAdd(struct sFakeFloat *a, struct sFakeFloat *b, struct sFakeFloat *result);
void ffMult(struct sFakeFloat *a, struct sFakeFloat *b, struct sFakeFloat *result);
void ffDivide(struct sFakeFloat *a, struct sFakeFloat *b, struct sFakeFloat *result);
void ffAdd(struct sFakeFloat *a, struct sFakeFloat *b, struct sFakeFloat *result) {
int64_t tmp;
// compare b.shift and a.shift and << >>
if (b->shift > a->shift) {
tmp = a->num;
tmp = tmp << (b->shift - a->shift);
tmp = tmp + b->num;
result->shift = b->shift;
} else {
tmp = b->num;
tmp = tmp << (a->shift - b->shift);
tmp = tmp + a->num;
result->shift = a->shift;
}
//printf("tmp=%lld\n", tmp);
while (tmp > INT32_MAX || tmp < -INT32_MAX) {
tmp = tmp >> 1;
result->shift--;
}
//printf("result.shift=%d\n", result->shift);
result->num = (int32_t)tmp;
//printf("result->num=%d\n", result->num);
}
void ffMult(struct sFakeFloat *a, struct sFakeFloat *b, struct sFakeFloat *result) {
int64_t tmp;
tmp = a->num;
tmp = tmp * b->num;
result->shift = 0;
while (tmp > INT32_MAX || tmp < -INT32_MAX) {
tmp = tmp >> 1;
result->shift--;
}
result->num = (int32_t) tmp;
tmp = a->shift + b->shift + result->shift;
//assert(tmp < INT8_MAX);
result->shift = (int32_t) tmp;
}
void ffDivide(struct sFakeFloat *a, struct sFakeFloat *b, struct sFakeFloat *result) {
int64_t tmp;
tmp = a->num;
tmp = tmp / b->num;
result->shift = 0;
while (tmp > INT32_MAX || tmp < -INT32_MAX) {
tmp = tmp >> 1;
result->shift--;
}
result->num = (int32_t) tmp;
tmp = a->shift - b->shift + result->shift;
//assert(tmp < INT8_MAX);
result->shift = (int32_t) tmp;
}