-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.c
2364 lines (2007 loc) · 56.7 KB
/
util.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: Apache-2.0
#define _XOPEN_SOURCE 700 /* realpath */
#include <assert.h>
#include <errno.h>
#include <limits.h> /* PATH_MAX */
#include <inttypes.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h> /* DIR, *dir-family */
#include <libgen.h> /* dirname */
#include <sys/stat.h> /* struct stat, stat */
#include <sys/types.h> /* pid_t */
#include <sys/wait.h> /* wait* */
#include <unistd.h> /* execvp, fork, isatty */
#include "sunder.h"
STATIC_ASSERT(CHAR_BIT_IS_8, CHAR_BIT == 8);
STATIC_ASSERT(UMAX_WIDTH, UINTMAX_MAX >= UINT64_MAX);
STATIC_ASSERT(SMAX_WITDH, INTMAX_MIN <= INT64_MIN && INTMAX_MAX >= INT64_MAX);
static int
cstr_vpcmp(void const* lhs, void const* rhs)
{
assert(lhs != NULL);
assert(rhs != NULL);
return strcmp(*(char const**)lhs, *(char const**)rhs);
}
int
safe_isalnum(int c)
{
return safe_isalpha(c) || safe_isdigit(c);
}
int
safe_isalpha(int c)
{
return safe_isupper(c) || safe_islower(c);
}
int
safe_isdigit(int c)
{
return (unsigned)c - '0' < 10;
}
int
safe_isgraph(int c)
{
return safe_isprint(c) && c != ' ';
}
int
safe_islower(int c)
{
return (unsigned)c - 'a' < 26;
}
int
safe_isprint(int c)
{
return 0x20 <= (unsigned)c && (unsigned)c <= 0x7e;
}
int
safe_ispunct(int c)
{
return safe_isgraph(c) && !safe_isalnum(c);
}
int
safe_isspace(int c)
{
return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t'
|| c == '\v';
}
int
safe_isupper(int c)
{
return (unsigned)c - 'A' < 26;
}
int
safe_isbdigit(int c)
{
return (unsigned)c - '0' < 2;
}
int
safe_isodigit(int c)
{
return (unsigned)c - '0' < 8;
}
int
safe_isxdigit(int c)
{
return safe_isdigit(c) || (unsigned)c - 'a' < 6 || (unsigned)c - 'A' < 6;
}
int
safe_tolower(int c)
{
if (safe_isupper(c)) {
return c | 0x20;
}
return c;
}
int
safe_toupper(int c)
{
if (safe_islower(c)) {
return c & 0x5f;
}
return c;
}
int
safe_memcmp(void const* s1, void const* s2, size_t n)
{
assert(s1 != NULL || n == 0);
assert(s2 != NULL || n == 0);
if (n == 0) {
return 0;
}
return memcmp(s1, s2, n);
}
void*
safe_memmove(void* dest, void const* src, size_t n)
{
assert(dest != NULL || n == 0);
assert(src != NULL || n == 0);
if (n == 0) {
return dest;
}
return memmove(dest, src, n);
}
void*
safe_memset(void* s, int c, size_t n)
{
assert(s != NULL || n == 0);
if (n == 0) {
return s;
}
return memset(s, c, n);
}
void*
xalloc(void* ptr, size_t size)
{
if (size == 0) {
free(ptr);
return NULL;
}
if ((ptr = realloc(ptr, size)) == NULL) {
error(NO_LOCATION, "[%s] Out of memory", __func__);
abort();
}
return ptr;
}
// Prepend othr_size bytes from othr onto the xalloc-allocated buffer of size
// *psize pointed to by *pdata, updating the address of *pdata if necessary.
static void
xalloc_prepend(void** pdata, size_t* psize, void const* othr, size_t othr_size)
{
assert(pdata != NULL);
assert(psize != NULL);
assert(othr != NULL || othr_size == 0);
if (othr_size == 0) {
// [] + [A][B][C] => [A][B][C]
return;
}
size_t const new_size = *psize + othr_size;
void* const new_data = xalloc(*pdata, new_size);
memmove((char*)new_data + othr_size, new_data, *psize);
memcpy(new_data, othr, othr_size);
*pdata = new_data;
*psize = new_size;
}
// Append othr_size bytes from othr onto the xalloc-allocated buffer of size
// *psize pointed to by *pdata, updating the address of *pdata if necessary.
static void
xalloc_append(void** pdata, size_t* psize, void const* othr, size_t othr_size)
{
assert(pdata != NULL);
assert(psize != NULL);
assert(othr != NULL || othr_size == 0);
if (othr_size == 0) {
// [A][B][C] + [] => [A][B][C]
return;
}
size_t const new_size = *psize + othr_size;
void* const new_data = xalloc(*pdata, new_size);
memcpy((char*)new_data + *psize, othr, othr_size);
*pdata = new_data;
*psize = new_size;
}
char const*
canonical_path(char const* path)
{
assert(path != NULL);
char resolved_path[PATH_MAX] = {0};
if (realpath(path, resolved_path) == NULL) {
fatal(
NO_LOCATION,
"failed to resolve path '%s' with error '%s'",
path,
strerror(errno));
}
return intern_cstr(resolved_path);
}
char const*
directory_path(char const* path)
{
assert(path != NULL);
char const* const canonical = canonical_path(path);
char* const tmp = cstr_new_cstr(canonical);
char const* const interned = intern_cstr(dirname(tmp));
xalloc(tmp, XALLOC_FREE);
return interned;
}
char const**
directory_files(char const* path)
{
assert(path != NULL);
DIR* const dir = opendir(path);
if (dir == NULL) {
fatal(
NO_LOCATION,
"failed to open directory '%s' with error '%s'",
path,
strerror(errno));
}
sbuf(char const*) files = NULL;
struct dirent* dirent = {0};
while ((dirent = readdir(dir)) != NULL) {
char const* file = dirent->d_name;
if (strcmp(file, ".") == 0 || strcmp(file, "..") == 0) {
continue;
}
sbuf_push(files, intern_cstr(file));
}
(void)closedir(dir);
qsort(files, sbuf_count(files), sizeof(char const*), cstr_vpcmp);
return files;
}
bool
file_exists(char const* path)
{
return access(path, F_OK) == 0;
}
bool
file_is_directory(char const* path)
{
assert(path != NULL);
struct stat statbuf = {0};
if (stat(path, &statbuf) != 0) {
return false;
}
return S_ISDIR(statbuf.st_mode);
}
static int
stream_read_all(FILE* stream, void** buf, size_t* buf_size)
{
assert(stream != NULL);
assert(buf != NULL);
assert(buf_size != NULL);
unsigned char* bf = NULL;
size_t sz = 0;
char tmp[512] = {0};
size_t nread = 0;
while ((nread = fread(tmp, 1, sizeof(tmp), stream)) != 0) {
bf = xalloc(bf, sz + nread);
memcpy(bf + sz, tmp, nread);
sz += nread;
}
if (ferror(stream)) {
xalloc(bf, XALLOC_FREE);
return -1;
}
*buf = bf;
*buf_size = sz;
return 0;
}
int
file_read_all(char const* path, void** buf, size_t* buf_size)
{
assert(path != NULL);
assert(buf != NULL);
assert(buf_size != NULL);
FILE* const stream = fopen(path, "rb");
if (stream == NULL) {
return -1;
}
int const err = stream_read_all(stream, buf, buf_size);
(void)fclose(stream);
return err;
}
int
file_write_all(char const* path, void const* buf, size_t buf_size)
{
assert(path != NULL);
assert(buf != NULL || buf_size == 0);
FILE* const stream = fopen(path, "wb");
if (stream == NULL) {
return -1;
}
size_t const written = fwrite(buf, 1, buf_size, stream);
int const ferr = ferror(stream);
int const fcls = fclose(stream);
if (written != buf_size) {
return -1;
}
if (ferr) {
return -1;
}
if (fcls == EOF) {
// According to the C99 standard:
// > Any unwritten buffered data for the stream are delivered to the
// > host environment to be written to the file; any unread buffered
// > data are discarded. Whether or not the call succeeds, the stream
// > is disassociated from the file...
// Cautiously assume that the buffer was not fully written to disk.
return -1;
}
return 0;
}
char*
cstr_new(char const* start, size_t count)
{
assert(start != NULL || count == 0);
char* const s = xalloc(NULL, count + STR_LITERAL_COUNT("\0"));
safe_memmove(s, start, count);
s[count] = '\0';
return s;
}
char*
cstr_new_cstr(char const* cstr)
{
assert(cstr != NULL);
size_t const count = strlen(cstr);
char* const s = xalloc(NULL, count + STR_LITERAL_COUNT("\0"));
return strcpy(s, cstr);
}
char*
cstr_new_fmt(char const* fmt, ...)
{
assert(fmt != NULL);
va_list args;
va_start(args, fmt);
char* const new = cstr_new_vfmt(fmt, args);
va_end(args);
return new;
}
char*
cstr_new_vfmt(char const* fmt, va_list args)
{
assert(fmt != NULL);
va_list copy;
va_copy(copy, args);
int const len = vsnprintf(NULL, 0, fmt, copy);
va_end(copy);
if (len < 0) {
fatal(NO_LOCATION, "[%s] Formatting failure", __func__);
}
size_t size = (size_t)len + STR_LITERAL_COUNT("\0");
char* const buf = xalloc(NULL, size);
vsnprintf(buf, size, fmt, args);
return buf;
}
bool
cstr_starts_with(char const* cstr, char const* target)
{
assert(cstr != NULL);
assert(target != NULL);
return strncmp(cstr, target, strlen(target)) == 0;
}
bool
cstr_ends_with(char const* cstr, char const* target)
{
assert(cstr != NULL);
assert(target != NULL);
size_t const cstr_count = strlen(cstr);
size_t const target_count = strlen(target);
if (cstr_count < target_count) {
return 0;
}
char const* const start = cstr + (cstr_count - target_count);
return safe_memcmp(start, target, target_count) == 0;
}
bool
cstr_eq_ignore_case(char const* lhs, char const* rhs)
{
assert(lhs != NULL);
assert(rhs != NULL);
size_t const lhs_count = strlen(lhs);
size_t const rhs_count = strlen(lhs);
if (lhs_count != rhs_count) {
return false;
}
for (size_t i = 0; i < lhs_count; ++i) {
if (safe_tolower(lhs[i]) != safe_tolower(rhs[i])) {
return false;
}
}
return true;
}
char const*
cstr_replace(char const* cstr, char const* target, char const* replacement)
{
struct string* const s = string_new("", 0);
for (char const* cur = cstr; *cur != '\0';) {
if (cstr_starts_with(cur, target)) {
string_append_cstr(s, replacement);
cur += strlen(target);
continue;
}
string_append(s, cur, 1u);
cur += 1;
}
char const* const interned = intern(string_start(s), string_count(s));
string_del(s);
return interned;
}
// Variation of djb2 that hashes a start-count pair.
static uintmax_t
hash_djb2(char const* start, size_t count)
{
uintmax_t hash = 5381;
for (size_t i = 0; i < count; ++i) {
hash = ((hash << 5) + hash) + (uintmax_t)start[i];
}
return hash;
}
uintmax_t
hash(void const* start, size_t count)
{
return hash_djb2(start, count);
}
struct interned_element {
char* string; // Optional (NULL indicates the element is not in use).
size_t count; // Number of bytes in the string before the final NUL.
uintmax_t hash; // Hash of the string contents.
};
// Hash set of interned cstrings.
static sbuf(struct interned_element) interned;
// Number of in-use elements within the interned hash set.
static size_t interned_count = 0;
void
intern_init(void)
{
assert(sbuf_count(interned) == 0);
sbuf_resize(interned, 64); // Arbitrary initial count.
for (size_t i = 0; i < sbuf_count(interned); ++i) {
interned[i] = (struct interned_element){0};
}
}
void
intern_fini(void)
{
for (size_t i = 0; i < sbuf_count(interned); ++i) {
if (interned[i].string != NULL) {
xalloc(interned[i].string, XALLOC_FREE);
}
}
sbuf_fini(interned);
}
char const*
intern(char const* start, size_t count)
{
assert(start != NULL || count == 0);
size_t const hash = (size_t)hash_djb2(start, count);
// Check to see if the string has already been interned.
for (size_t index = hash % sbuf_count(interned);
interned[index].string != NULL;
index = (index + 1) % sbuf_count(interned)) {
if (interned[index].count != count) {
continue;
}
if (safe_memcmp(interned[index].string, start, count) != 0) {
continue;
}
return interned[index].string;
}
// Check to see if the set needs resizing.
if (2 * (interned_count + 1) > sbuf_count(interned)) {
// Insert at 50% occupancy. Create a new set with double the existing
// element count, populate that set with the existing key-value pairs,
// and then replace the existing set with the new set.
sbuf(struct interned_element) new = NULL;
sbuf_resize(new, sbuf_count(interned) * 2);
for (size_t i = 0; i < sbuf_count(new); ++i) {
new[i] = (struct interned_element){0};
}
for (size_t i = 0; i < sbuf_count(interned); ++i) {
if (interned[i].string == NULL) {
continue;
}
size_t index = (size_t)interned[i].hash % sbuf_count(new);
while (new[index].string != NULL) {
index = (index + 1) % sbuf_count(new);
}
new[index] = interned[i];
}
sbuf_fini(interned);
interned = new;
}
// Insert into the set.
size_t index = hash % sbuf_count(interned);
while (interned[index].string != NULL) {
index = (index + 1) % sbuf_count(interned);
}
char* str = cstr_new(start, count);
interned[index] = (struct interned_element){str, count, hash};
interned_count += 1;
return str;
}
char const*
intern_cstr(char const* cstr)
{
return intern(cstr, strlen(cstr));
}
char const*
intern_fmt(char const* fmt, ...)
{
assert(fmt != NULL);
struct string* const s = string_new(NULL, 0);
va_list args;
va_start(args, fmt);
string_append_vfmt(s, fmt, args);
va_end(args);
char const* const interned = intern(string_start(s), string_count(s));
string_del(s);
return interned;
}
/* reserve */
void*
sbuf__rsv_(size_t elemsize, void* sbuf, size_t cap)
{
assert(elemsize != 0);
if (cap <= sbuf_capacity(sbuf)) {
return sbuf;
}
assert(cap != 0);
size_t const size = SBUF__HEADER_OFFSET_ + elemsize * cap;
struct sbuf__header_* const header =
xalloc(sbuf != NULL ? SBUF__PHEAD_MUTBL_(sbuf) : NULL, size);
header->cnt_ = sbuf != NULL ? header->cnt_ : 0;
header->cap_ = cap;
return (char*)header + SBUF__HEADER_OFFSET_;
}
/* resize */
void*
sbuf__rsz_(size_t elemsize, void* sbuf, size_t cnt)
{
assert(elemsize != 0);
if (cnt == 0) {
sbuf_fini(sbuf);
return NULL;
}
if (cnt > sbuf_capacity(sbuf)) {
sbuf = sbuf__rsv_(elemsize, sbuf, cnt);
}
assert(sbuf != NULL);
SBUF__PHEAD_MUTBL_(sbuf)->cnt_ = cnt;
return sbuf;
}
/* grow capacity by doubling */
void*
sbuf__grw_(size_t elemsize, void* sbuf)
{
assert(elemsize != 0);
size_t const cap = sbuf_capacity(sbuf);
assert(sbuf_count(sbuf) == cap);
static size_t const GROWTH_FACTOR = 2;
static size_t const DEFAULT_CAPACITY = 8;
size_t const new_cap = cap ? cap * GROWTH_FACTOR : DEFAULT_CAPACITY;
return sbuf__rsv_(elemsize, sbuf, new_cap);
}
#define BITARR__WORD_TYPE_ unsigned long
#define BITARR__WORD_SIZE_ sizeof(BITARR__WORD_TYPE_)
#define BITARR__WORD_BITS_ (BITARR__WORD_SIZE_ * CHAR_BIT)
struct bitarr {
size_t count;
BITARR__WORD_TYPE_ words[];
};
static inline size_t
bitarr__word_count_(size_t count)
{
return (count / BITARR__WORD_SIZE_) + (count % BITARR__WORD_SIZE_ != 0);
}
static inline size_t
bitarr__size_(size_t count)
{
return sizeof(struct bitarr)
+ (bitarr__word_count_(count) * BITARR__WORD_SIZE_);
}
struct bitarr*
bitarr_new(size_t count)
{
size_t const size = bitarr__size_(count);
struct bitarr* const self = xalloc(NULL, size);
memset(self, 0x00, size);
self->count = count;
return self;
}
void
bitarr_del(struct bitarr* self)
{
if (self == NULL) {
return;
}
size_t const size = bitarr__size_(self->count);
memset(self, 0x00, size); // scrub
xalloc(self, XALLOC_FREE);
}
void
bitarr_freeze(struct bitarr* self)
{
assert(self != NULL);
freeze(self);
}
size_t
bitarr_count(struct bitarr const* self)
{
assert(self != NULL);
return self->count;
}
void
bitarr_set(struct bitarr* self, size_t n, int value)
{
assert(self != NULL);
if (n >= self->count) {
fatal(NO_LOCATION, "[%s] Index out of bounds (%zu)", __func__, n);
}
BITARR__WORD_TYPE_* const pword = &self->words[n / BITARR__WORD_SIZE_];
BITARR__WORD_TYPE_ const mask = (BITARR__WORD_TYPE_)1u
<< (n % BITARR__WORD_SIZE_);
*pword = (BITARR__WORD_TYPE_)(value ? *pword | mask : *pword & ~mask);
}
int
bitarr_get(struct bitarr const* self, size_t n)
{
assert(self != NULL);
if (n >= self->count) {
fatal(NO_LOCATION, "[%s] Index out of bounds (%zu)", __func__, n);
}
BITARR__WORD_TYPE_ const word = self->words[n / BITARR__WORD_SIZE_];
BITARR__WORD_TYPE_ const mask = (BITARR__WORD_TYPE_)1u
<< (n % BITARR__WORD_SIZE_);
return (word & mask) != 0;
}
void
bitarr_assign(struct bitarr* self, struct bitarr const* othr)
{
assert(self != NULL);
assert(othr != NULL);
if (self->count != othr->count) {
fatal(
NO_LOCATION,
"[%s] Mismatched array counts (%zu, %zu)",
__func__,
self->count,
othr->count);
}
assert(bitarr__size_(self->count) == bitarr__size_(othr->count));
safe_memmove(self, othr, bitarr__size_(othr->count));
}
void
bitarr_compl(struct bitarr* res, struct bitarr const* rhs)
{
assert(res != NULL);
assert(rhs != NULL);
if (res->count != rhs->count) {
fatal(
NO_LOCATION,
"[%s] Mismatched array counts (%zu, %zu)",
__func__,
res->count,
rhs->count);
}
for (size_t i = 0; i < bitarr__word_count_(res->count); ++i) {
res->words[i] = ~rhs->words[i];
}
}
void
bitarr_twos_complement_neg(struct bitarr* res, struct bitarr* rhs)
{
assert(res != NULL);
assert(rhs != NULL);
if (res->count != rhs->count) {
fatal(
NO_LOCATION,
"[%s] Mismatched array counts (%zu, %zu)",
__func__,
res->count,
rhs->count);
}
// Invert the bits...
bitarr_compl(res, rhs);
// ...and add one.
int carry = 1;
size_t const bit_count = bitarr_count(res);
for (size_t i = 0; i < bit_count; ++i) {
int const new_digit = (carry + bitarr_get(res, i)) % 2;
int const new_carry = (carry + bitarr_get(res, i)) >= 2;
bitarr_set(res, i, new_digit);
carry = new_carry;
}
}
void
bitarr_shiftl(struct bitarr* res, struct bitarr const* lhs, size_t nbits)
{
assert(res != NULL);
assert(lhs != NULL);
if (res->count != lhs->count) {
fatal(
NO_LOCATION,
"[%s] Mismatched array counts (%zu, %zu)",
__func__,
res->count,
lhs->count);
}
size_t const count = bitarr_count(res);
bitarr_assign(res, lhs);
for (size_t n = 0; n < nbits; ++n) {
for (size_t i = count - 1; i != 0; --i) {
bitarr_set(res, i, bitarr_get(res, i - 1u));
}
bitarr_set(res, 0u, 0);
}
}
void
bitarr_shiftr(
struct bitarr* res, struct bitarr const* lhs, size_t nbits, int high_bit)
{
assert(res != NULL);
assert(lhs != NULL);
if (res->count != lhs->count) {
fatal(
NO_LOCATION,
"[%s] Mismatched array counts (%zu, %zu)",
__func__,
res->count,
lhs->count);
}
size_t const count = bitarr_count(res);
bitarr_assign(res, lhs);
for (size_t n = 0; n < nbits; ++n) {
for (size_t i = 0; i < count - 1; ++i) {
bitarr_set(res, i, bitarr_get(res, i + 1u));
}
bitarr_set(res, count - 1, high_bit);
}
}
void
bitarr_and(
struct bitarr* res, struct bitarr const* lhs, struct bitarr const* rhs)
{
assert(res != NULL);
assert(lhs != NULL);
assert(rhs != NULL);
if (res->count != lhs->count || res->count != rhs->count) {
fatal(
NO_LOCATION,
"[%s] Mismatched array counts (%zu, %zu, %zu)",
__func__,
res->count,
lhs->count,
rhs->count);
}
for (size_t i = 0; i < bitarr__word_count_(res->count); ++i) {
res->words[i] = lhs->words[i] & rhs->words[i];
}
}
void
bitarr_xor(
struct bitarr* res, struct bitarr const* lhs, struct bitarr const* rhs)
{
assert(res != NULL);
assert(lhs != NULL);
assert(rhs != NULL);
if (res->count != lhs->count || res->count != rhs->count) {
fatal(
NO_LOCATION,
"[%s] Mismatched array counts (%zu, %zu, %zu)",
__func__,
res->count,
lhs->count,
rhs->count);
}
for (size_t i = 0; i < bitarr__word_count_(res->count); ++i) {
res->words[i] = lhs->words[i] ^ rhs->words[i];
}
}
void
bitarr_or(
struct bitarr* res, struct bitarr const* lhs, struct bitarr const* rhs)
{
assert(res != NULL);
assert(lhs != NULL);
assert(rhs != NULL);
if (res->count != lhs->count || res->count != rhs->count) {
fatal(
NO_LOCATION,
"[%s] Mismatched array counts (%zu, %zu, %zu)",
__func__,
res->count,
lhs->count,
rhs->count);
}
for (size_t i = 0; i < bitarr__word_count_(res->count); ++i) {
res->words[i] = lhs->words[i] | rhs->words[i];
}
}
void
bitarr_to_bigint(
struct bigint* res, struct bitarr const* bitarr, bool is_signed)
{
assert(res != NULL);
assert(bitarr != NULL);
bigint_assign(res, BIGINT_ZERO);
size_t const bit_count = bitarr_count(bitarr);
struct bitarr* const mag_bits = bitarr_new(bit_count);
for (size_t i = 0; i < bit_count; ++i) {
int const bit = bitarr_get(bitarr, i);
bitarr_set(mag_bits, i, bit);
}
bool const is_neg = is_signed && bitarr_get(bitarr, bit_count - 1u);
if (is_neg) {
// Two's complement negative<->positive conversion.
bitarr_twos_complement_neg(mag_bits, mag_bits);
}
for (size_t i = 0; i < bit_count; ++i) {
int const bit = bitarr_get(mag_bits, i);
bigint_magnitude_bit_set(res, i, bit);
}
if (is_neg) {
bigint_neg(res, res);
}
bitarr_del(mag_bits);
}
// Arbitrary precision integer.
// A bigint conceptually consists of the following components:
// (1) sign: The arithmetic sign of the integer (+, -, or 0).
// (2) magnitude: The absolute value of the bigint, presented through this API
// as an infinitely long sequence of bits with little endian ordering.
//