-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
math.c
384 lines (356 loc) · 7.4 KB
/
math.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include <brl.mod/blitz.mod/blitz.h>
enum binaryOps {
OP_MUL,
OP_DIV,
OP_MOD,
OP_SHL,
OP_SHR,
OP_SAR,
OP_ADD,
OP_SUB,
OP_AND,
OP_XOR,
OP_OR
};
unsigned int bmx_string_to_uint( BBString *t ){
int i=0,neg=0;
unsigned n=0;
while( i<t->length && isspace(t->buf[i]) ) ++i;
if( i==t->length ) return 0;
if( t->buf[i]=='+' ) ++i;
else if( (neg = t->buf[i]=='-') ) ++i;
if( i==t->length ) return 0;
if( t->buf[i]=='%' ){
for( ++i;i<t->length;++i ){
int c=t->buf[i];
if( c!='0' && c!='1' ) break;
n=n*2+(c-'0');
}
}else if( t->buf[i]=='$' ){
for( ++i;i<t->length;++i ){
int c=toupper(t->buf[i]);
if( !isxdigit(c) ) break;
if( c>='A' ) c-=('A'-'0'-10);
n=n*16+(c-'0');
}
}else{
for( ;i<t->length;++i ){
int c=t->buf[i];
if( !isdigit(c) ) break;
n=n*10+(c-'0');
}
}
return neg ? -n : n;
}
unsigned long long bmx_string_to_ulong( BBString *t ){
int i=0,neg=0;
unsigned long long n=0;
while( i<t->length && isspace(t->buf[i]) ) ++i;
if( i==t->length ){ return 0; }
if( t->buf[i]=='+' ) ++i;
else if( (neg = t->buf[i]=='-') ) ++i;
if( i==t->length ){ return 0; }
if( t->buf[i]=='%' ){
for( ++i;i<t->length;++i ){
int c=t->buf[i];
if( c!='0' && c!='1' ) break;
n=n*2+(c-'0');
}
}else if( t->buf[i]=='$' ){
for( ++i;i<t->length;++i ){
int c=toupper(t->buf[i]);
if( !isxdigit(c) ) break;
if( c>='A' ) c-=('A'-'0'-10);
n=n*16+(c-'0');
}
}else{
for( ;i<t->length;++i ){
int c=t->buf[i];
if( !isdigit(c) ) break;
n=n*10+(c-'0');
}
}
return neg ? -n : n;
}
size_t bmx_string_to_size_t( BBString *t ){
int i=0,neg=0;
size_t n=0;
while( i<t->length && isspace(t->buf[i]) ) ++i;
if( i==t->length ){ return 0; }
if( t->buf[i]=='+' ) ++i;
else if( (neg=(t->buf[i]=='-')) ) ++i;
if( i==t->length ){ return 0; }
if( t->buf[i]=='%' ){
for( ++i;i<t->length;++i ){
int c=t->buf[i];
if( c!='0' && c!='1' ) break;
n=n*2+(c-'0');
}
}else if( t->buf[i]=='$' ){
for( ++i;i<t->length;++i ){
int c=toupper(t->buf[i]);
if( !isxdigit(c) ) break;
if( c>='A' ) c-=('A'-'0'-10);
n=n*16+(c-'0');
}
}else{
for( ;i<t->length;++i ){
int c=t->buf[i];
if( !isdigit(c) ) break;
n=n*10+(c-'0');
}
}
return neg ? -n : n;
}
BBString *bmx_string_from_uint( unsigned int n ){
char buf[64];
sprintf(buf, "%u", n);
return bbStringFromBytes( (unsigned char*)buf, strlen(buf) );
}
BBString *bmx_string_from_ulong( unsigned long long n ){
char buf[64];
sprintf(buf, "%llu", n);
return bbStringFromBytes( (unsigned char*)buf, strlen(buf) );
}
BBString *bmx_string_from_size_t( size_t n ){
char buf[64];
#if UINTPTR_MAX == 0xffffffff
sprintf(buf, "%u", n);
#else
sprintf(buf, "%llu", n);
#endif
return bbStringFromBytes( (unsigned char*)buf, strlen(buf) );
}
BBString * bmx_bitwise_not_uint(BBString * value) {
unsigned int v = bmx_string_to_uint(value);
return bmx_string_from_uint(~v);
}
BBString * bmx_bitwise_not_sizet(BBString * value) {
size_t v = bmx_string_to_size_t(value);
return bmx_string_from_size_t(~v);
}
BBString * bmx_bitwise_not_ulong(BBString * value) {
unsigned long long v = bmx_string_to_ulong(value);
return bmx_string_from_ulong(~v);
}
BBString * bmx_bitwise_not_longint(BBString * value, int size) {
if (size == 4) {
int v = bbStringToInt(value);
return bbStringFromInt(~v);
} else { // 8
BBInt64 v = bbStringToLong(value);
return bbStringFromLong(~v);
}
}
BBString * bmx_bitwise_not_ulongint(BBString * value, int size) {
if (size == 4) {
unsigned int v = bmx_string_to_uint(value);
return bmx_string_from_uint(~v);
} else { // 8
unsigned long long v = bmx_string_to_ulong(value);
return bmx_string_from_ulong(~v);
}
}
BBString * bmx_binarymathexpr_sizet(enum binaryOps op, BBString * slhs, BBString * srhs) {
size_t lhs = bmx_string_to_size_t(slhs);
size_t rhs = bmx_string_to_size_t(srhs);
size_t res = 0;
switch (op) {
case OP_MUL:
res = lhs * rhs;
break;
case OP_DIV:
res = lhs / rhs;
break;
case OP_MOD:
res = lhs % rhs;
break;
case OP_SHL:
res = lhs << rhs;
break;
case OP_SHR:
case OP_SAR:
res = lhs >> rhs;
break;
case OP_ADD:
res = lhs + rhs;
break;
case OP_SUB:
res = lhs - rhs;
break;
case OP_AND:
res = lhs & rhs;
break;
case OP_XOR:
res = lhs ^ rhs;
break;
case OP_OR:
res = lhs | rhs;
break;
}
return bmx_string_from_size_t(res);
}
BBString * bmx_binarymathexpr_uint(enum binaryOps op, BBString * slhs, BBString * srhs) {
unsigned int lhs = bmx_string_to_uint(slhs);
unsigned int rhs = bmx_string_to_uint(srhs);
unsigned int res = 0;
switch (op) {
case OP_MUL:
res = lhs * rhs;
break;
case OP_DIV:
res = lhs / rhs;
break;
case OP_MOD:
res = lhs % rhs;
break;
case OP_SHL:
res = lhs << rhs;
break;
case OP_SHR:
case OP_SAR:
res = lhs >> rhs;
break;
case OP_ADD:
res = lhs + rhs;
break;
case OP_SUB:
res = lhs - rhs;
break;
case OP_AND:
res = lhs & rhs;
break;
case OP_XOR:
res = lhs ^ rhs;
break;
case OP_OR:
res = lhs | rhs;
break;
}
return bmx_string_from_uint(res);
}
BBString * bmx_binarymathexpr_ulong(enum binaryOps op, BBString * slhs, BBString * srhs) {
unsigned long long lhs = bmx_string_to_ulong(slhs);
unsigned long long rhs = bmx_string_to_ulong(srhs);
unsigned long long res = 0;
switch (op) {
case OP_MUL:
res = lhs * rhs;
break;
case OP_DIV:
res = lhs / rhs;
break;
case OP_MOD:
res = lhs % rhs;
break;
case OP_SHL:
res = lhs << rhs;
break;
case OP_SHR:
case OP_SAR:
res = lhs >> rhs;
break;
case OP_ADD:
res = lhs + rhs;
break;
case OP_SUB:
res = lhs - rhs;
break;
case OP_AND:
res = lhs & rhs;
break;
case OP_XOR:
res = lhs ^ rhs;
break;
case OP_OR:
res = lhs | rhs;
break;
}
return bmx_string_from_ulong(res);
}
BBString * bmx_binarymathexpr_longint(enum binaryOps op, BBString * slhs, BBString * srhs, int size) {
if (size == 4) {
int lhs = bbStringToInt(slhs);
int rhs = bbStringToInt(srhs);
int res = 0;
switch (op) {
case OP_MUL:
res = lhs * rhs;
break;
case OP_DIV:
res = lhs / rhs;
break;
case OP_MOD:
res = lhs % rhs;
break;
case OP_SHL:
res = lhs << rhs;
break;
case OP_SHR:
case OP_SAR:
res = lhs >> rhs;
break;
case OP_ADD:
res = lhs + rhs;
break;
case OP_SUB:
res = lhs - rhs;
break;
case OP_AND:
res = lhs & rhs;
break;
case OP_XOR:
res = lhs ^ rhs;
break;
case OP_OR:
res = lhs | rhs;
break;
}
return bbStringFromInt(res);
} else { // 8
BBInt64 lhs = bbStringToLong(slhs);
BBInt64 rhs = bbStringToLong(srhs);
BBInt64 res = 0;
switch (op) {
case OP_MUL:
res = lhs * rhs;
break;
case OP_DIV:
res = lhs / rhs;
break;
case OP_MOD:
res = lhs % rhs;
break;
case OP_SHL:
res = lhs << rhs;
break;
case OP_SHR:
case OP_SAR:
res = lhs >> rhs;
break;
case OP_ADD:
res = lhs + rhs;
break;
case OP_SUB:
res = lhs - rhs;
break;
case OP_AND:
res = lhs & rhs;
break;
case OP_XOR:
res = lhs ^ rhs;
break;
case OP_OR:
res = lhs | rhs;
break;
}
return bbStringFromLong(res);
}
}
BBString * bmx_binarymathexpr_ulongint(enum binaryOps op, BBString * slhs, BBString * srhs, int size) {
if (size == 4) {
return bmx_binarymathexpr_uint(op, slhs, srhs);
} else { // 8
return bmx_binarymathexpr_ulong(op, slhs, srhs);
}
}