-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharith.c
171 lines (159 loc) · 3.84 KB
/
arith.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
#include <stdint.h>
#include <assert.h>
#include "common.h"
static inline char allzero8(uint8_t *f, uint32_t n) {
for(uint32_t i=0; i<n; i++)
if(f[i]) return 0;
return 1;
}
static inline char allzero64(uint8_t *ff, uint32_t n) {
uint64_t *f = (uint64_t*)ff;
n /= 8;
for(uint32_t i=0; i<n; i++)
if(f[i]) return 0;
return 1;
}
static inline char add8(uint8_t *f, const uint8_t *g, uint32_t n) {
uint16_t t = 0;
for(uint32_t i=0; i<n; i++) {
t = t + f[i] + g[i];
f[i] = t;
t >>= 8;
}
if(t) {
t = 0;
for(uint32_t i=0; i<n; i++) {
t = t + f[i] + 0xFF;
f[i] = t;
t >>= 8;
}
if(t == 0) return 0;
}
return 1;
}
static inline char add64(uint8_t *ff, const uint8_t *gg, uint32_t nn) {
uint64_t *f = (uint64_t*)ff;
const uint64_t *g = (uint64_t*)gg;
const uint32_t n = nn/8;
uint128_t t = 0;
for(uint32_t i=0; i<n; i++) {
t = t + f[i] + g[i];
f[i] = t;
t >>= 64;
}
if(t) {
t = 0;
for(uint32_t i=0; i<n; i++) {
t = t + f[i] + 0xFFFFFFFFFFFFFFFFULL;
f[i] = t;
t >>= 64;
}
if(t == 0) return 0;
}
return 1;
}
extern inline char add(uint8_t *f, const uint8_t *g, uint32_t n) {
if(n % 8)
return add8(f, g, n);
else
return add64(f, g, n);
}
static inline uint8_t addc8(uint8_t *f, const uint8_t *g, uint32_t n) {
uint16_t t = 0;
for(uint32_t i=0; i<n; i++) {
t = t + f[i] + g[i];
f[i] = t;
t >>= 8;
}
return t;
}
static inline uint8_t addc64(uint8_t *ff, const uint8_t *gg, uint32_t nn) {
uint64_t *f = (uint64_t*)ff;
const uint64_t *g = (uint64_t*)gg;
const uint32_t n = nn/8;
uint128_t t = 0;
for(uint32_t i=0; i<n; i++) {
t = t + f[i] + g[i];
f[i] = t;
t >>= 64;
}
return t;
}
extern inline uint8_t addc(uint8_t *f, const uint8_t *g, uint32_t n) {
if(n % 8)
return addc8(f, g, n);
else
return addc64(f, g, n);
}
static inline char sub8(uint8_t *f, const uint8_t *g, uint32_t n) {
uint16_t t = 2;
for(uint32_t i=0; i<n; i++) {
t = t + f[i] + (uint8_t)~g[i];
f[i] = t;
t >>= 8;
}
if(t) {
if(allzero8(f, n)) return 0;
t = 0;
for(uint32_t i=0; i<n; i++) {
t = t + f[i] + 0xFF;
f[i] = t;
t >>= 8;
}
}
return 1;
}
static inline char sub64(uint8_t *ff, const uint8_t *gg, uint32_t n) {
uint64_t *f = (uint64_t*)ff;
const uint64_t *g = (const uint64_t*)gg;
n /= 8;
uint128_t t = 2;
for(uint32_t i=0; i<n; i++) {
t = t + f[i] + (uint64_t)~g[i];
f[i] = t;
t >>= 64;
}
if(t) {
if(allzero64(ff, n*8)) return 0;
t = 0;
for(uint32_t i=0; i<n; i++) {
t = t + f[i] + 0xFFFFFFFFFFFFFFFFULL;
f[i] = t;
t >>= 64;
}
}
return 1;
}
extern inline char sub(uint8_t *f, const uint8_t *g, uint32_t n) {
if(n % 8)
return sub8(f, g, n);
else
return sub64(f, g, n);
}
static inline uint8_t subc8(uint8_t *f, const uint8_t *g, uint32_t n) {
uint16_t t = 1;
for(uint32_t i=0; i<n; i++) {
t = t + f[i] + (uint8_t)~g[i];
f[i] = t;
t >>= 8;
}
return t;
}
static inline uint8_t subc64(uint8_t *ff, const uint8_t *gg, uint32_t n) {
uint64_t *f = (uint64_t*)ff;
const uint64_t *g = (const uint64_t*)gg;
n /= 8;
uint128_t t = 1;
for(uint32_t i=0; i<n; i++) {
t = t + f[i] + (uint64_t)~g[i];
f[i] = t;
t >>= 64;
}
return t;
}
extern inline uint8_t subc(uint8_t *f, const uint8_t *g, uint32_t n) {
if(n % 8)
return subc8(f, g, n);
else
return subc64(f, g, n);
}