-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapint.h
More file actions
117 lines (101 loc) · 5.75 KB
/
apint.h
File metadata and controls
117 lines (101 loc) · 5.75 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
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
#ifndef APINT_H
#define APINT_H
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/types.h>
#define APINT_EMPTY { .num_blocks = 0, .blocks = NULL, .positive = true }
#define APINT_ZERO { .num_blocks = 1, .blocks = (uint64_t[]){0}, .positive = true }
#define APINT_CONSTANT(value) { \
.num_blocks = 1, \
.blocks = (uint64_t[]){(value) >= 0 ? (uint64_t)(value) : -(uint64_t)(value)}, \
.positive = (value) >= 0 \
}
struct apint {
ssize_t num_blocks;
uint64_t *blocks;
bool positive;
};
void apint_cleanup(void);
void apint_destroy(struct apint *x);
struct apint *apint_resize(struct apint *x, ssize_t num_blocks);
struct apint *apint_init(struct apint *x);
#define apint_set(x, value) \
_Generic((value), \
bool: apint_set_uint, \
unsigned char: apint_set_uint, \
unsigned short: apint_set_uint, \
unsigned int: apint_set_uint, \
unsigned long: apint_set_uint, \
unsigned long long: apint_set_uint, \
char: apint_set_int, \
signed char: apint_set_int, \
signed short: apint_set_int, \
signed int: apint_set_int, \
signed long: apint_set_int, \
signed long long: apint_set_int, \
float: apint_set_double, \
double: apint_set_double, \
char *: apint_set_str1, \
struct apint *: apint_set_apint)(x, value)
struct apint *apint_set_uint(struct apint *x, uint64_t value);
struct apint *apint_set_int(struct apint *x, int64_t value);
struct apint *apint_set_double(struct apint *x, double value);
struct apint *apint_set_str1(struct apint *x, const char *value);
struct apint *apint_set_str2(struct apint *x, const char *value, ssize_t length, int radix);
struct apint *apint_set_apint(struct apint *x, const struct apint *value);
#define apint_init_set(x, value) \
_Generic((value), \
bool: apint_init_set_uint, \
unsigned char: apint_init_set_uint, \
unsigned short: apint_init_set_uint, \
unsigned int: apint_init_set_uint, \
unsigned long: apint_init_set_uint, \
unsigned long long: apint_init_set_uint, \
char: apint_init_set_int, \
signed char: apint_init_set_int, \
signed short: apint_init_set_int, \
signed int: apint_init_set_int, \
signed long: apint_init_set_int, \
signed long long: apint_init_set_int, \
float: apint_init_set_double, \
double: apint_init_set_double, \
char *: apint_init_set_str1, \
struct apint *: apint_init_set_apint)(x, value)
struct apint *apint_init_set_uint(struct apint *x, uint64_t value);
struct apint *apint_init_set_int(struct apint *x, int64_t value);
struct apint *apint_init_set_double(struct apint *x, double value);
struct apint *apint_init_set_str1(struct apint *x, const char *value);
struct apint *apint_init_set_str2(struct apint *x, const char *value, ssize_t length, int radix);
struct apint *apint_init_set_apint(struct apint *x, const struct apint *value);
int apint_sign(const struct apint *x);
int apint_cmp(const struct apint *x1, const struct apint *x2);
int apint_cmp_abs(const struct apint *x1, const struct apint *x2);
bool apint_is_even(const struct apint *x);
struct apint *apint_arshift(struct apint *result, const struct apint *x, int64_t n);
struct apint *apint_not(struct apint *result, const struct apint *x);
struct apint *apint_and(struct apint *result, const struct apint *x1, const struct apint *x2);
struct apint *apint_or(struct apint *result, const struct apint *x1, const struct apint *x2);
struct apint *apint_xor(struct apint *result, const struct apint *x1, const struct apint *x2);
struct apint *apint_neg(struct apint *result, const struct apint *x);
struct apint *apint_abs(struct apint *result, const struct apint *x);
struct apint *apint_inc(struct apint *result, const struct apint *x);
struct apint *apint_dec(struct apint *result, const struct apint *x);
struct apint *apint_add(struct apint *result, const struct apint *x1, const struct apint *x2);
struct apint *apint_sub(struct apint *result, const struct apint *x1, const struct apint *x2);
struct apint *apint_mul(struct apint *result, const struct apint *x1, const struct apint *x2);
struct apint *apint_mul_pow2(struct apint *result, const struct apint *x, int64_t n);
struct apint *apint_div(struct apint *result, const struct apint *x1, const struct apint *x2);
struct apint *apint_rem(struct apint *result, const struct apint *x1, const struct apint *x2);
struct apint *apint_mod(struct apint *result, const struct apint *x1, const struct apint *x2);
struct apint *apint_divrem(struct apint *quotient, struct apint *remainder, const struct apint *x1, const struct apint *x2);
struct apint *apint_divmod(struct apint *quotient, struct apint *remainder, const struct apint *x1, const struct apint *x2);
struct apint *apint_pow(struct apint *result, const struct apint *base, const struct apint *exponent);
struct apint *apint_powmod(struct apint *result, const struct apint *base, const struct apint *exponent, const struct apint *modulus);
uint64_t apint_to_uint(const struct apint *x);
int64_t apint_to_int(const struct apint *x);
double apint_to_double(const struct apint *x);
char *apint_to_str(char *s, ssize_t size, const struct apint *x, int radix);
int apint_fprint(FILE *f, const struct apint *x, int radix);
#endif // APINT_H