-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_store_iflt.c
More file actions
85 lines (78 loc) · 2.63 KB
/
Copy pathft_store_iflt.c
File metadata and controls
85 lines (78 loc) · 2.63 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
75
76
77
78
79
80
81
82
83
84
85
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_store_iflt.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dnakano <dnakano@student.42tokyo.jp> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/13 15:48:02 by dnakano #+# #+# */
/* Updated: 2020/10/18 12:38:51 by dnakano ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
static void ft_store_iflt_mts(t_float *iflt)
{
int i;
u_int64_t mts_bin;
int8_t mts[FLT_MTSSIZE];
ft_bzero(iflt->mts_dec, sizeof(iflt->mts_dec));
if (iflt->exp >= FLT_FRACBIT || (!iflt->frac && iflt->exp == -FLT_EXPBIAS))
return ;
if (iflt->exp >= 0)
mts_bin = iflt->frac << iflt->exp;
else if (iflt->exp == -FLT_EXPBIAS)
mts_bin = iflt->frac;
else
mts_bin = (iflt->frac >> 1) | (1LL << (sizeof(mts_bin) * 8 - 1));
ft_bzero(mts, sizeof(mts));
mts[0] = 5;
i = 0;
while (i++ < -iflt->exp - 1)
ft_mts_divbytwo(mts, FLT_MTSSIZE);
i = -1;
while (++i < (FLT_FRACBIT + 1))
{
if (mts_bin & (1LL << ((sizeof(mts_bin) * 8 - 1) - i)))
ft_arr_add(iflt->mts_dec, mts, FLT_MTSSIZE);
ft_mts_divbytwo(mts, FLT_MTSSIZE);
}
}
static void ft_store_iflt_int(t_float *iflt)
{
int i;
int offset;
u_int64_t itg_bin;
int8_t itg[FLT_INTSIZE];
ft_bzero(iflt->int_dec, sizeof(iflt->int_dec));
if (iflt->exp <= 0)
{
if (!iflt->exp)
iflt->int_dec[FLT_INTSIZE - 1] = 1;
return ;
}
offset = (iflt->exp >= FLT_FRACBIT) ? FLT_FRACBIT : iflt->exp;
itg_bin = (iflt->frac >> (sizeof(itg_bin) * 8 - offset)) | (1LL << offset);
ft_bzero(itg, sizeof(itg));
itg[FLT_INTSIZE - 1] = 1;
i = -1;
while (++i < (FLT_FRACBIT + 1))
{
if (itg_bin & (1LL << i))
ft_arr_add(iflt->int_dec, itg, FLT_INTSIZE);
ft_itg_dbl(itg, FLT_INTSIZE);
}
while (++i <= iflt->exp + 1)
ft_itg_dbl(iflt->int_dec, FLT_INTSIZE);
}
t_float ft_store_iflt(double num)
{
u_int64_t mem;
t_float iflt;
ft_memcpy(&mem, &num, sizeof(num));
iflt.sign = mem >> (FLT_FRACBIT + FLT_EXPBIT);
iflt.exp = (mem >> FLT_FRACBIT & ~(1 << FLT_EXPBIT)) - FLT_EXPBIAS;
iflt.frac = mem << (FLT_EXPBIT + 1);
ft_store_iflt_int(&iflt);
ft_store_iflt_mts(&iflt);
return (iflt);
}