-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbn_opsummul.c
116 lines (107 loc) · 2.58 KB
/
bn_opsummul.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* bn_opsummul.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: Kashnitskiy <elijahkash.code@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/30 18:46:29 by odrinkwa #+# #+# */
/* Updated: 2020/01/16 13:57:43 by Kashnitskiy ### ########.fr */
/* */
/* ************************************************************************** */
#include <prf_double.h>
void ft_isumabs_bignum(t_bignum *res, t_bignum bn2)
{
int i;
int maxsize;
int div;
maxsize = res->size > bn2.size ? res->size : bn2.size;
div = 0;
i = 0;
while (i < maxsize)
{
res->number[i] += bn2.number[i] + div;
div = res->number[i] / BASE_BN;
res->number[i] %= BASE_BN;
i++;
}
if (div != 0)
{
res->number[i] = div;
res->size = maxsize + 1;
}
else
res->size = maxsize;
}
void ft_imul_bignum(t_bignum *res, t_bignum bn2)
{
int i;
int j;
t_bignum temp_bn;
initialize_bignum(&temp_bn, res->maxsize);
i = 0;
while (i < temp_bn.maxsize)
{
j = 0;
while (j < temp_bn.maxsize - i)
{
temp_bn.number[i + j] += res->number[i] * bn2.number[j];
j++;
}
i++;
}
temp_bn.sign = res->sign * bn2.sign;
temp_bn.exp = res->exp;
fixup_bignum(&temp_bn);
fixsize_bignum(&temp_bn);
ft_deepcopy_bignum(res, temp_bn);
}
void ft_imul_small_bignum(t_bignum *res, unsigned int n)
{
long int div;
int i;
long int tmp;
t_bignum temp_bn;
if (n / 100 != 0)
{
ft_assign_bignum(&temp_bn, res->maxsize, n);
ft_imul_bignum(res, temp_bn);
return ;
}
div = 0;
i = 0;
while (i < res->size)
{
tmp = res->number[i] * (long int)n + div;
res->number[i] = tmp % BASE_BN;
div = tmp / BASE_BN;
i++;
}
if (div != 0)
{
res->number[i] = div;
res->size++;
}
}
t_bignum ft_mul_bignum(t_bignum bn1, t_bignum bn2)
{
int i;
int j;
t_bignum res_bn;
initialize_bignum(&res_bn, bn1.maxsize);
i = 0;
while (i < res_bn.maxsize)
{
j = 0;
while (j < res_bn.maxsize - i)
{
res_bn.number[i + j] += bn1.number[i] * bn2.number[j];
j++;
}
i++;
}
res_bn.sign = bn1.sign * bn2.sign;
fixup_bignum(&res_bn);
fixsize_bignum(&res_bn);
return (res_bn);
}