-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
240 lines (177 loc) · 7.1 KB
/
test.c
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include "float_emu.h"
#define nbr_test 10000
// int main(){
// srand(time(NULL));
// for(int i = 0 ; i < nbr_test; i++){
// float a = (float)rand()/((float)RAND_MAX/127);
// float b = (float)rand()/((float)RAND_MAX/127);
// float reference = a * b;
// float result = mult_float(a,b);
// if(abs(reference - result) > 0.1)
// {
// printf("Attended result : %f\n", reference);
// printf("Result obtained : %f\n", result);
// printf("\n\n");
// }
// }
// }
int main(){
// for(int i = 0 ; i < nbr_test ; i ++)
// {
// float a = (float)rand()/((float)RAND_MAX/127);
// float b = (float)rand()/((float)RAND_MAX/127);
// float c = a + b ;
// if(add_float(a,b) - c > 0.01)
// printf("\n result got is %f\nexpected was %f\n",add_float(a,b),c );
// }
// return 0;
int test = 0x1e45a2;
printf("kfapjapfj: %x\n",~test+1 );
float a = 1.6559;
float b = -1.2365;
float c = a + b ;
unsigned int a_copy = *((unsigned int*) &a);
unsigned int b_copy = *((unsigned int*) &b);
unsigned int c_copy = *((unsigned int*) &c);
unsigned int mantice_a ;
unsigned int mantice_b ;
unsigned int exposant_a;
unsigned int exposant_b;
unsigned int sign_a;
unsigned int sign_b;
// | 0x800000 allow to put it in the form 1.010...
// cause the mantice is just the part after the coma, we
// always have to normalize with 1
mantice_a = (a_copy & 0x7FFFFF) ;
mantice_b = (b_copy & 0x7FFFFF) ;
exposant_a = (a_copy >> 23) & 0xFF;
exposant_b = (b_copy >> 23) & 0xFF;
sign_a = (a_copy >> 31) & 0x1;
sign_b = (b_copy >> 31) & 0x1;
printf("a is in hex:%x\n a is in dec %f", a_copy, a);
printf(" a is :\n sign : %x\n exposant : %x\n mantice : %x\n\n", sign_a, exposant_a ,mantice_a);
printf("b is in hex:%x\na is in dec %f", b_copy, b);
printf(" b is :\n sign : %x\n exposant : %x\n mantice : %x\n\n", sign_b, exposant_b ,mantice_b);
unsigned int mantice_c = (c_copy & 0x7FFFFF) ;
unsigned int exposant_c = (c_copy >> 23) & 0xFF;
unsigned int sign_c =(c_copy >> 31) & 0x1;
printf("result expected is :%x\n\n", c_copy);
printf(" result expected is :\n sign : %x\n exposant : %x\n mantice : %x\n\n", sign_c, exposant_c ,mantice_c);
/*
The mantice design the part after the coma,
for example if the mantice of a is 0101, it means a is :
a = 1.010
So we "normalise" a by doing a or with 0x800000
*/
mantice_a |= 0x800000 ;
mantice_b |= 0x800000 ;
unsigned int result_exponent ;
unsigned int mantice_a_new = mantice_a ;
unsigned int mantice_b_new = mantice_b ;
/*
looking for the lowest exponent
to shift the mantice of the number with the lowest exponent
for instance :
a = 1.0101*2**3
b = 0.0011*2**4
a has the lowest exponent, so we need to rewritte a as :
a = 0.10101*2**4
*/
if(sign_a == sign_b){
printf("\ndiff exp : %d\n", (exposant_a - exposant_b) ) ;
if((exposant_a > exposant_b)) // e_x>e_b
{
printf("A great \n") ;
result_exponent = exposant_a ;
mantice_b_new = mantice_b >> (exposant_a - exposant_b) ;
}
else if(exposant_b > exposant_a)
{
printf("B great \n") ;
result_exponent = exposant_b ;
mantice_a_new = mantice_a >> (exposant_b - exposant_a) ;
}
else // exponent are the same
{
result_exponent = exposant_a ; // doesnt matter we can take any of it
}
printf("mantice a after : %x\n", mantice_a_new);
printf("mantice b after : %x\n", mantice_b_new);
/*
Second step is to perform the addition of the 2 mantice, the problem is that the result is on 25 bits
and we only want 23.
It can be on 25 bits because of the normalisation with 0x800000.
Indeed both mantice are 23 bits so will normalization it becomes 24 and addition
can overflow, leading to this 25 bits.
We want to keep the msb. If the mantice has overflow it means we must reajust the exponent.
Example :
mantice_result = 1.0110 -> 0.10110*2
*/
unsigned int mantice_result = mantice_a_new + mantice_b_new ;
printf("Mantice result is : %x\n",mantice_result);
if((mantice_result >> 25 == 1))
{
result_exponent +=2 ;
mantice_result = (mantice_result >> 2) & 0x7FFFFF;
}
else if ((mantice_result >> 24 == 1))
{
result_exponent ++ ;
mantice_result = (mantice_result >> 1) & 0x7FFFFF;
}
else{
mantice_result &= 0x7FFFFF ;
}
printf(" result is :\n result_exp : %x\n result mantice : %x\n", result_exponent, mantice_result);
int result = mantice_result | (result_exponent << 23) | (sign_a << 31);
printf("resultat calculated is : %x\n", result);
float result_est = *((float*) &result);
printf("result got in float : %f\n", result_est);
}
else{
printf("neg and sub nbres\n");
printf("\ndiff exp : %d\n", exposant_a - exposant_b) ;
unsigned long mantice_result;
unsigned int sign_result;
if((exposant_a > exposant_b)) // e_x>e_b
{
result_exponent = exposant_a ;
mantice_b_new = mantice_b >> (exposant_a - exposant_b) ;
}
else if(exposant_b > exposant_a)
{
result_exponent = exposant_b ;
mantice_a_new = mantice_a >> (exposant_b - exposant_a) ;
}
else{
result_exponent = exposant_b ;
}
printf("mantice a after : %x\n", mantice_a_new);
printf("mantice b after : %x\n", mantice_b_new);
if(sign_a) //case where a is neg, so b pos
mantice_a_new = ~mantice_a_new + 1;
else // case where a is pos, so b neg
mantice_b_new = ~mantice_b_new + 1;
printf("mantice a after : %x\n", mantice_a_new);
printf("mantice b after : %x\n", mantice_b_new);
// add a 32 bit and a 24 bit number
mantice_result = (mantice_a_new + mantice_b_new) ;
printf("Mantice result is : %x\n",mantice_result );
if(mantice_a > mantice_b && exposant_a > exposant_b )
sign_result = sign_a ;
else
sign_result = sign_b ;
printf(" result is :\n result_exp : %x\n result mantice : %x\n", result_exponent, mantice_result);
int result = mantice_result | (result_exponent << 23) | (sign_result << 31);
printf("resultat calculated is : %x\n", result);
float result_est = *((float*) &result);
printf("result expected in float : %f\n", result_est);
}
/*
need to equilibrate exponent
So must equilibrate on the smallest
*/
}