forked from sysprog21/fibdrv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bn.h
68 lines (46 loc) · 1.36 KB
/
bn.h
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
#ifndef _BN_H_
#define _BN_H_
#include <linux/list.h>
#include <linux/module.h>
#include <linux/slab.h>
#define digits 18 // max digits per big_num node
#define bound 1000000000000000000ULL
MODULE_LICENSE("Dual MIT/GPL");
extern size_t list_len;
typedef struct big_num {
// unsigned long long upper, lower;
unsigned long long num;
struct list_head head;
} big_num;
void new_node(struct list_head *list, unsigned long long num);
void add_big_num(struct list_head *small,
struct list_head *large); // large add to small
struct list_head *fib_linkedlist(long long k);
void list_reverse(struct list_head *head);
char *list_to_string(struct list_head *list);
/* ----------- Implementation using char array ----------- */
#define _max(x, y) (x > y ? x : y)
#define _swap(x, y) \
({ \
typeof(x) tmp = x; \
x = y; \
y = tmp; \
})
typedef struct big_num2 {
unsigned char *num;
size_t len; // strlen
int sign;
} bn;
bn *bn_alloc(size_t size);
char *reverse_str(char *s);
bn *bn_add(bn *a, bn *b);
// a must > b
bn *bn_sub(bn *a, bn *b);
// s += num start from s[offset]
void bn_mul_add(bn *s, int offset, int num);
// O(n^2) long multiplication
bn *bn_mul(bn *a, bn *b);
void freebn(bn *n);
bn *fast_doubling_char(long long n);
bn *bcd_encode(bn *ans);
#endif