-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmini-libc.c
2336 lines (1978 loc) · 48.3 KB
/
mini-libc.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
typedef __PTRDIFF_TYPE__ ssize_t;
typedef __WINT_TYPE__ wint_t;
typedef __UINT32_TYPE__ uint32_t;
typedef __UINT64_TYPE__ uint64_t;
typedef __INT64_TYPE__ int64_t;
typedef __INT8_TYPE__ int8_t;
typedef __INT16_TYPE__ int16_t;
typedef __INT32_TYPE__ int32_t;
typedef __INTMAX_TYPE__ intmax_t;
typedef __UINTMAX_TYPE__ uintmax_t;
typedef __UINTPTR_TYPE__ uintptr_t;
typedef __UINT8_TYPE__ uint8_t;
typedef __UINT16_TYPE__ uint16_t;
typedef __UINT_LEAST16_TYPE__ uint_least16_t;
typedef __UINT_FAST16_TYPE__ uint_fast16_t;
typedef __UINT_LEAST32_TYPE__ uint_least32_t;
typedef __UINT_FAST32_TYPE__ uint_fast32_t;
typedef __UINT_LEAST64_TYPE__ uint_least64_t;
typedef __UINT_FAST64_TYPE__ uint_fast64_t;
typedef int word_t __attribute__ ((mode (__word__)));
#define UINT8_MAX __UINT8_MAX__
#define UINT16_MAX __UINT16_MAX__
#define UINT64_MAX __UINT64_MAX__
#define UINT32_MAX __UINT32_MAX__
#define SIZE_MAX __SIZE_MAX__
#define FLT_MAX __FLT_MAX__
#define DBL_MAX __DBL_MAX__
#define LDBL_MAX __LDBL_MAX__
#define isnan __builtin_isnan
#define signbit __builtin_signbit
#define true 1
#define false 0
typedef struct { int quot, rem; } div_t;
typedef struct { long quot, rem; } ldiv_t;
typedef struct { long long quot, rem; } lldiv_t;
typedef struct { intmax_t quot, rem; } imaxdiv_t;
typedef int QItype __attribute__ ((mode (QI)));
typedef unsigned int UQItype __attribute__ ((mode (QI)));
typedef int HItype __attribute__ ((mode (HI)));
typedef unsigned int UHItype __attribute__ ((mode (HI)));
/* These typedefs are usually forbidden on dsp's with UNITS_PER_WORD 1. */
typedef int SItype __attribute__ ((mode (SI)));
typedef unsigned int USItype __attribute__ ((mode (SI)));
#if __SIZEOF_LONG_LONG__ > 4
/* These typedefs are usually forbidden on archs with UNITS_PER_WORD 2. */
typedef int DItype __attribute__ ((mode (DI)));
typedef unsigned int UDItype __attribute__ ((mode (DI)));
#if defined(__SIZEOF_INT128__)
#define CRT_HAS_128BIT
/* These typedefs are usually forbidden on archs with UNITS_PER_WORD 4. */
typedef int TItype __attribute__ ((mode (TI)));
typedef unsigned int UTItype __attribute__ ((mode (TI)));
#endif
#endif
typedef int64_t di_int;
typedef uint64_t du_int;
typedef int32_t si_int;
typedef uint32_t su_int;
// Clang and GCC provide built-in endianness definitions.
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define _YUGA_LITTLE_ENDIAN 0
#define _YUGA_BIG_ENDIAN 1
#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define _YUGA_LITTLE_ENDIAN 1
#define _YUGA_BIG_ENDIAN 0
#else
#error lol
#endif // __BYTE_ORDER__
typedef union {
di_int all;
struct {
#if _YUGA_LITTLE_ENDIAN
su_int low;
si_int high;
#else
si_int high;
su_int low;
#endif // _YUGA_LITTLE_ENDIAN
} s;
} dwords;
typedef union {
du_int all;
struct {
#if _YUGA_LITTLE_ENDIAN
su_int low;
su_int high;
#else
su_int high;
su_int low;
#endif // _YUGA_LITTLE_ENDIAN
} s;
} udwords;
#define COMPILER_RT_ABI
#include <stddef.h>
#include <limits.h>
#ifdef CRT_HAS_128BIT
typedef int ti_int __attribute__((mode(TI)));
typedef unsigned tu_int __attribute__((mode(TI)));
typedef union {
ti_int all;
struct {
#if _YUGA_LITTLE_ENDIAN
du_int low;
di_int high;
#else
di_int high;
du_int low;
#endif // _YUGA_LITTLE_ENDIAN
} s;
} twords;
typedef union {
tu_int all;
struct {
#if _YUGA_LITTLE_ENDIAN
du_int low;
du_int high;
#else
du_int high;
du_int low;
#endif // _YUGA_LITTLE_ENDIAN
} s;
} utwords;
#if !defined(__BPF__) || !defined(__clang__)
ti_int make_ti(di_int h, di_int l) {
twords r;
r.s.high = (du_int)h;
r.s.low = (du_int)l;
return r.all;
}
tu_int make_tu(du_int h, du_int l) {
utwords r;
r.s.high = h;
r.s.low = l;
return r.all;
}
#endif
#endif // CRT_HAS_128BIT
// (most from musl, some from gnulib, some from libiberty, some from 4.4BSD, some from compiler-rt, some from libgcc)
void *
memmove (void *dest0, void const *source0, size_t length)
{
char *dest = dest0;
char const *source = source0;
if (source < dest)
/* Moving from low mem to hi mem; start at end. */
for (source += length, dest += length; length; --length)
*--dest = *--source;
else if (source != dest)
{
/* Moving from hi mem to low mem; start at beginning. */
for (; length; --length)
*dest++ = *source++;
}
return dest0;
}
void *memccpy(void *restrict dest, const void *restrict src, int c, size_t n)
{
unsigned char *d = dest;
const unsigned char *s = src;
c = (unsigned char)c;
for (; n && (*d=*s)!=c; n--, s++, d++);
if (n) return d+1;
return 0;
}
void *memchr(const void *src, int c, size_t n)
{
const unsigned char *s = src;
c = (unsigned char)c;
for (; n && *s != c; s++, n--);
return n ? (void *)s : 0;
}
int memcmp(const void *vl, const void *vr, size_t n)
{
const unsigned char *l=vl, *r=vr;
for (; n && *l == *r; n--, l++, r++);
return n ? *l-*r : 0;
}
#ifndef __BPF__
void *memcpy(void *restrict dest, const void *restrict src, size_t n)
{
unsigned char *d = dest;
const unsigned char *s = src;
for (; n; n--) *d++ = *s++;
return dest;
}
#endif
void *memrchr(const void *m, int c, size_t n)
{
const unsigned char *s = m;
c = (unsigned char)c;
while (n--) if (s[n]==c) return (void *)(s+n);
return 0;
}
#ifndef __BPF__
void *memset(void *dest, int c, size_t n)
{
unsigned char *s = dest;
size_t k;
/* Pure C fallback with no aliasing violations. */
for (; n; n--, s++) *s = c;
return dest;
}
#endif
char *stpcpy(char *restrict d, const char *restrict s)
{
for (; (*d=*s); s++, d++);
return d;
}
char *strchrnul(const char *s, int c)
{
c = (unsigned char)c;
for (; *s && *(unsigned char *)s != c; s++);
return (char *)s;
}
char *
strchr (register const char *s, int c)
{
do {
if (*s == c)
{
return (char*)s;
}
} while (*s++);
return (0);
}
int strcmp(const char *l, const char *r)
{
for (; *l==*r && *l; l++, r++);
return *(unsigned char *)l - *(unsigned char *)r;
}
size_t strlen(const char *s)
{
const char *a = s;
for (; *s; s++);
return s-a;
}
int strncmp(const char *_l, const char *_r, size_t n)
{
const unsigned char *l=(void *)_l, *r=(void *)_r;
if (!n--) return 0;
for (; *l && *r && n && *l == *r ; l++, r++, n--);
return *l - *r;
}
void swab(const void *restrict _src, void *restrict _dest, ssize_t n)
{
const char *src = _src;
char *dest = _dest;
for (; n>1; n-=2) {
dest[0] = src[1];
dest[1] = src[0];
dest += 2;
src += 2;
}
}
int isalpha(int c)
{
return ((unsigned)c|32)-'a' < 26;
}
int isascii(int c)
{
return !(c&~0x7f);
}
int isblank(int c)
{
return (c == ' ' || c == '\t');
}
int iscntrl(int c)
{
return (unsigned)c < 0x20 || c == 0x7f;
}
int isdigit(int c)
{
return (unsigned)c-'0' < 10;
}
int isgraph(int c)
{
return (unsigned)c-0x21 < 0x5e;
}
int islower(int c)
{
return (unsigned)c-'a' < 26;
}
int isprint(int c)
{
return (unsigned)c-0x20 < 0x5f;
}
int isspace(int c)
{
return c == ' ' || (unsigned)c-'\t' < 5;
}
int isupper(int c)
{
return (unsigned)c-'A' < 26;
}
#ifndef __mips16
int iswcntrl(wint_t wc)
{
return (unsigned)wc < 32
|| (unsigned)(wc-0x7f) < 33
|| (unsigned)(wc-0x2028) < 2
|| (unsigned)(wc-0xfff9) < 3;
}
#endif
int iswdigit(wint_t wc)
{
return (unsigned)wc-'0' < 10;
}
#ifndef __mips16
/* Consider all legal codepoints as printable except for:
* - C0 and C1 control characters
* - U+2028 and U+2029 (line/para break)
* - U+FFF9 through U+FFFB (interlinear annotation controls)
* The following code is optimized heavily to make hot paths for the
* expected printable characters. */
int iswprint(wint_t wc)
{
if (wc < 0xffU)
return (wc+1 & 0x7f) >= 0x21;
if (wc < 0x2028U || wc-0x202aU < 0xd800-0x202a || wc-0xe000U < 0xfff9-0xe000)
return 1;
if (wc-0xfffcU > 0x10ffff-0xfffc || (wc&0xfffe)==0xfffe)
return 0;
return 1;
}
#endif
int iswxdigit(wint_t wc)
{
return (unsigned)(wc-'0') < 10 || (unsigned)((wc|32)-'a') < 6;
}
/* nonsense function that should NEVER be used! */
int toascii(int c)
{
return c & 0x7f;
}
#if !defined(__BPF__) || !defined(__clang__)
double fdim(double x, double y)
{
if (isnan(x))
return x;
if (isnan(y))
return y;
return x > y ? x - y : 0;
}
float fdimf(float x, float y)
{
if (isnan(x))
return x;
if (isnan(y))
return y;
return x > y ? x - y : 0;
}
double fmax(double x, double y)
{
if (isnan(x))
return y;
if (isnan(y))
return x;
/* handle signed zeros, see C99 Annex F.9.9.2 */
if (signbit(x) != signbit(y))
return signbit(x) ? y : x;
return x < y ? y : x;
}
float fmaxf(float x, float y)
{
if (isnan(x))
return y;
if (isnan(y))
return x;
/* handle signed zeroes, see C99 Annex F.9.9.2 */
if (signbit(x) != signbit(y))
return signbit(x) ? y : x;
return x < y ? y : x;
}
long double fmaxl(long double x, long double y)
{
if (isnan(x))
return y;
if (isnan(y))
return x;
/* handle signed zeros, see C99 Annex F.9.9.2 */
if (signbit(x) != signbit(y))
return signbit(x) ? y : x;
return x < y ? y : x;
}
double fmin(double x, double y)
{
if (isnan(x))
return y;
if (isnan(y))
return x;
/* handle signed zeros, see C99 Annex F.9.9.2 */
if (signbit(x) != signbit(y))
return signbit(x) ? x : y;
return x < y ? x : y;
}
float fminf(float x, float y)
{
if (isnan(x))
return y;
if (isnan(y))
return x;
/* handle signed zeros, see C99 Annex F.9.9.2 */
if (signbit(x) != signbit(y))
return signbit(x) ? x : y;
return x < y ? x : y;
}
long double fminl(long double x, long double y)
{
if (isnan(x))
return y;
if (isnan(y))
return x;
/* handle signed zeros, see C99 Annex F.9.9.2 */
if (signbit(x) != signbit(y))
return signbit(x) ? x : y;
return x < y ? x : y;
}
#endif
static const char digits[] =
"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
char *l64a(long x0)
{
static char s[7];
char *p;
uint32_t x = x0;
for (p=s; x; p++, x>>=6)
*p = digits[x&63];
*p = 0;
return s;
}
static uint64_t seed;
void srand(unsigned s)
{
seed = s-1;
}
int rand(void)
{
seed = 6364136223846793005ULL*seed + 1;
return seed>>33;
}
struct node {
struct node *next;
struct node *prev;
};
#if !defined(__AVR__) || !defined(__clang__)
void insque(void *element, void *pred)
{
struct node *e = element;
struct node *p = pred;
if (!p) {
e->next = e->prev = 0;
return;
}
e->next = p->next;
e->prev = p;
p->next = e;
if (e->next)
e->next->prev = e;
}
void remque(void *element)
{
struct node *e = element;
if (e->next)
e->next->prev = e->prev;
if (e->prev)
e->prev->next = e->next;
}
#endif
#if !defined(__BPF__) && !defined(__NVPTX__)
void *lsearch(const void *key, void *base, size_t *nelp, size_t width,
int (*compar)(const void *, const void *))
{
char (*p)[width] = base;
size_t n = *nelp;
size_t i;
for (i = 0; i < n; i++)
if (compar(key, p[i]) == 0)
return p[i];
*nelp = n+1;
return memcpy(p[n], key, width);
}
void *lfind(const void *key, const void *base, size_t *nelp,
size_t width, int (*compar)(const void *, const void *))
{
char (*p)[width] = (void *)base;
size_t n = *nelp;
size_t i;
for (i = 0; i < n; i++)
if (compar(key, p[i]) == 0)
return p[i];
return 0;
}
#endif
int abs(int a)
{
return a>0 ? a : -a;
}
int atoi(const char *s)
{
int n=0, neg=0;
while (isspace(*s)) s++;
switch (*s) {
case '-': neg=1;
case '+': s++;
}
/* Compute n as a negative number to avoid overflow on INT_MIN */
while (isdigit(*s))
n = 10*n - (*s++ - '0');
return neg ? n : -n;
}
long atol(const char *s)
{
long n=0;
int neg=0;
while (isspace(*s)) s++;
switch (*s) {
case '-': neg=1;
case '+': s++;
}
/* Compute n as a negative number to avoid overflow on LONG_MIN */
while (isdigit(*s))
n = 10*n - (*s++ - '0');
return neg ? n : -n;
}
long long atoll(const char *s)
{
long long n=0;
int neg=0;
while (isspace(*s)) s++;
switch (*s) {
case '-': neg=1;
case '+': s++;
}
/* Compute n as a negative number to avoid overflow on LLONG_MIN */
while (isdigit(*s))
n = 10*n - (*s++ - '0');
return neg ? n : -n;
}
#ifndef __BPF__
void *bsearch(const void *key, const void *base, size_t nel, size_t width, int (*cmp)(const void *, const void *))
{
void *try;
int sign;
while (nel > 0) {
try = (char *)base + width*(nel/2);
sign = cmp(key, try);
if (sign < 0) {
nel /= 2;
} else if (sign > 0) {
base = (char *)try + width;
nel -= nel/2+1;
} else {
return try;
}
}
return NULL;
}
/*
* Perform a binary search.
*
* The code below is a bit sneaky. After a comparison fails, we
* divide the work in half by moving either left or right. If lim
* is odd, moving left simply involves halving lim: e.g., when lim
* is 5 we look at item 2, so we change lim to 2 so that we will
* look at items 0 & 1. If lim is even, the same applies. If lim
* is odd, moving right again involes halving lim, this time moving
* the base up one item past p: e.g., when lim is 5 we change base
* to item 3 and make lim 2 so that we will look at items 3 and 4.
* If lim is even, however, we have to shrink it by one before
* halving: e.g., when lim is 4, we still looked at item 2, so we
* have to make lim 3, then halve, obtaining 1, so that we will only
* look at item 3.
*/
void *
bsearch_r (const void *key, const void *base0,
size_t nmemb, size_t size,
int (*compar)(const void *, const void *, void *),
void *arg)
{
const char *base = (const char *) base0;
int lim, cmp;
const void *p;
for (lim = nmemb; lim != 0; lim >>= 1) {
p = base + (lim >> 1) * size;
cmp = (*compar)(key, p, arg);
if (cmp == 0)
return (void *)p;
if (cmp > 0) { /* key > p: move right */
base = (const char *)p + size;
lim--;
} /* else move left */
}
return (NULL);
}
#endif
#if !defined(__BPF__) || !defined(__clang__)
div_t div(int num, int den)
{
return (div_t){ num/den, num%den };
}
#endif
intmax_t imaxabs(intmax_t a)
{
return a>0 ? a : -a;
}
#if !defined(__BPF__) || !defined(__clang__)
imaxdiv_t imaxdiv(intmax_t num, intmax_t den)
{
return (imaxdiv_t){ num/den, num%den };
}
#endif
long labs(long a)
{
return a>0 ? a : -a;
}
#if !defined(__BPF__) || !defined(__clang__)
ldiv_t ldiv(long num, long den)
{
return (ldiv_t){ num/den, num%den };
}
#endif
long long llabs(long long a)
{
return a>0 ? a : -a;
}
#if !defined(__BPF__) || !defined(__clang__)
lldiv_t lldiv(long long num, long long den)
{
return (lldiv_t){ num/den, num%den };
}
#endif
wchar_t *wcschr(const wchar_t *s, wchar_t c)
{
for (; *s && *s != c; s++);
return *s ? (wchar_t *)s : 0;
}
int wcscmp(const wchar_t *l, const wchar_t *r)
{
for (; *l==*r && *l && *r; l++, r++);
return *l < *r ? -1 : *l > *r;
}
wchar_t *wcscpy(wchar_t *restrict d, const wchar_t *restrict s)
{
wchar_t *a = d;
while ((*d++ = *s++));
return a;
}
#if !defined(__BPF__) || !defined(__clang__)
size_t wcslen(const wchar_t *s)
{
const wchar_t *a;
for (a=s; *s; s++);
return s-a;
}
#endif
int wcsncmp(const wchar_t *l, const wchar_t *r, size_t n)
{
for (; n && *l==*r && *l && *r; n--, l++, r++);
return n ? (*l < *r ? -1 : *l > *r) : 0;
}
wchar_t *wmemchr(const wchar_t *s, wchar_t c, size_t n)
{
for (; n && *s != c; n--, s++);
return n ? (wchar_t *)s : 0;
}
int wmemcmp(const wchar_t *l, const wchar_t *r, size_t n)
{
for (; n && *l==*r; n--, l++, r++);
return n ? (*l < *r ? -1 : *l > *r) : 0;
}
#ifndef __BPF__
wchar_t *wmemcpy(wchar_t *restrict d, const wchar_t *restrict s, size_t n)
{
wchar_t *a = d;
while (n--) *d++ = *s++;
return a;
}
#endif
wchar_t *wmemmove(wchar_t *d, const wchar_t *s, size_t n)
{
wchar_t *d0 = d;
if (d == s) return d;
if ((uintptr_t)d-(uintptr_t)s < n * sizeof *d)
while (n--) d[n] = s[n];
else
while (n--) *d++ = *s++;
return d0;
}
wchar_t *wmemset(wchar_t *d, wchar_t c, size_t n)
{
wchar_t *ret = d;
while (n--) *d++ = c;
return ret;
}
void
bcopy (void const *source0, void *dest0, size_t length)
{
char const *source = source0;
char *dest = dest0;
if (source < dest)
/* Moving from low mem to hi mem; start at end. */
for (source += length, dest += length; length; --length)
*--dest = *--source;
else if (source != dest)
/* Moving from hi mem to low mem; start at beginning. */
for (; length; --length)
*dest++ = *source++;
}
uint64_t
rotl64 (uint64_t x, int n)
{
return ((x << n) | (x >> (64 - n))) & UINT64_MAX;
}
/* Given an unsigned 64-bit argument X, return the value corresponding
to rotating the bits N steps to the right. N must be between 1 to
63 inclusive.*/
uint64_t
rotr64 (uint64_t x, int n)
{
return ((x >> n) | (x << (64 - n))) & UINT64_MAX;
}
/* Given an unsigned 32-bit argument X, return the value corresponding
to rotating the bits N steps to the left. N must be between 1 and
31 inclusive. */
uint32_t
rotl32 (uint32_t x, int n)
{
return ((x << n) | (x >> (32 - n))) & UINT32_MAX;
}
/* Given an unsigned 32-bit argument X, return the value corresponding
to rotating the bits N steps to the right. N must be between 1 to
31 inclusive.*/
uint32_t
rotr32 (uint32_t x, int n)
{
return ((x >> n) | (x << (32 - n))) & UINT32_MAX;
}
/* Given a size_t argument X, return the value corresponding
to rotating the bits N steps to the left. N must be between 1 and
(CHAR_BIT * sizeof (size_t) - 1) inclusive. */
size_t
rotl_sz (size_t x, int n)
{
return ((x << n) | (x >> ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX;
}
/* Given a size_t argument X, return the value corresponding
to rotating the bits N steps to the right. N must be between 1 to
(CHAR_BIT * sizeof (size_t) - 1) inclusive. */
size_t
rotr_sz (size_t x, int n)
{
return ((x >> n) | (x << ((CHAR_BIT * sizeof x) - n))) & SIZE_MAX;
}
/* Given an unsigned 16-bit argument X, return the value corresponding
to rotating the bits N steps to the left. N must be between 1 to
15 inclusive, but on most relevant targets N can also be 0 and 16
because 'int' is at least 32 bits and the arguments must widen
before shifting. */
uint16_t
rotl16 (uint16_t x, int n)
{
return (((unsigned int) x << n) | ((unsigned int) x >> (16 - n)))
& UINT16_MAX;
}
/* Given an unsigned 16-bit argument X, return the value corresponding
to rotating the bits N steps to the right. N must be in 1 to 15
inclusive, but on most relevant targets N can also be 0 and 16
because 'int' is at least 32 bits and the arguments must widen
before shifting. */
uint16_t
rotr16 (uint16_t x, int n)
{
return (((unsigned int) x >> n) | ((unsigned int) x << (16 - n)))
& UINT16_MAX;
}
/* Given an unsigned 8-bit argument X, return the value corresponding
to rotating the bits N steps to the left. N must be between 1 to 7
inclusive, but on most relevant targets N can also be 0 and 8
because 'int' is at least 32 bits and the arguments must widen
before shifting. */
uint8_t
rotl8 (uint8_t x, int n)
{
return (((unsigned int) x << n) | ((unsigned int) x >> (8 - n))) & UINT8_MAX;
}
/* Given an unsigned 8-bit argument X, return the value corresponding
to rotating the bits N steps to the right. N must be in 1 to 7
inclusive, but on most relevant targets N can also be 0 and 8
because 'int' is at least 32 bits and the arguments must widen
before shifting. */
uint8_t
rotr8 (uint8_t x, int n)
{
return (((unsigned int) x >> n) | ((unsigned int) x << (8 - n))) & UINT8_MAX;
}
/* Given an unsigned 16-bit argument X, return the value corresponding to
X with reversed byte order. */
uint_least16_t
bswap_16 (uint_least16_t x)
{
uint_fast16_t mask = 0xff;
return ( (x & mask << 8 * 1) >> 8 * 1
| (x & mask << 8 * 0) << 8 * 1);
}
/* Given an unsigned 32-bit argument X, return the value corresponding to
X with reversed byte order. */
uint_least32_t
bswap_32 (uint_least32_t x)
{
uint_fast32_t mask = 0xff;
return ( (x & mask << 8 * 3) >> 8 * 3
| (x & mask << 8 * 2) >> 8 * 1
| (x & mask << 8 * 1) << 8 * 1
| (x & mask << 8 * 0) << 8 * 3);
}
/* Given an unsigned 64-bit argument X, return the value corresponding to
X with reversed byte order. */
uint_least64_t
bswap_64 (uint_least64_t x)
{
uint_fast64_t mask = 0xff;
return ( (x & mask << 8 * 7) >> 8 * 7
| (x & mask << 8 * 6) >> 8 * 5
| (x & mask << 8 * 5) >> 8 * 3
| (x & mask << 8 * 4) >> 8 * 1
| (x & mask << 8 * 3) << 8 * 1
| (x & mask << 8 * 2) << 8 * 3
| (x & mask << 8 * 1) << 8 * 5
| (x & mask << 8 * 0) << 8 * 7);
}
int ffs(int i)
{
unsigned int j;
for (j = 0; j < CHAR_BIT * sizeof i; j++)
if (i & (1U << j))
return j + 1;
return 0;
}
int
libiberty_ffs (register int valu)
{
register int bit;
if (valu == 0)
return 0;
for (bit = 1; !(valu & 1); bit++)
valu >>= 1;
return bit;
}