forked from yszheda/sim-outorder_RRIP-HP-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.c
1189 lines (1005 loc) · 24.8 KB
/
misc.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
/* misc.c - miscellaneous routines */
/* SimpleScalar(TM) Tool Suite
* Copyright (C) 1994-2003 by Todd M. Austin, Ph.D. and SimpleScalar, LLC.
* All Rights Reserved.
*
* THIS IS A LEGAL DOCUMENT, BY USING SIMPLESCALAR,
* YOU ARE AGREEING TO THESE TERMS AND CONDITIONS.
*
* No portion of this work may be used by any commercial entity, or for any
* commercial purpose, without the prior, written permission of SimpleScalar,
* LLC ([email protected]). Nonprofit and noncommercial use is permitted
* as described below.
*
* 1. SimpleScalar is provided AS IS, with no warranty of any kind, express
* or implied. The user of the program accepts full responsibility for the
* application of the program and the use of any results.
*
* 2. Nonprofit and noncommercial use is encouraged. SimpleScalar may be
* downloaded, compiled, executed, copied, and modified solely for nonprofit,
* educational, noncommercial research, and noncommercial scholarship
* purposes provided that this notice in its entirety accompanies all copies.
* Copies of the modified software can be delivered to persons who use it
* solely for nonprofit, educational, noncommercial research, and
* noncommercial scholarship purposes provided that this notice in its
* entirety accompanies all copies.
*
* 3. ALL COMMERCIAL USE, AND ALL USE BY FOR PROFIT ENTITIES, IS EXPRESSLY
* PROHIBITED WITHOUT A LICENSE FROM SIMPLESCALAR, LLC ([email protected]).
*
* 4. No nonprofit user may place any restrictions on the use of this software,
* including as modified by the user, by any other authorized user.
*
* 5. Noncommercial and nonprofit users may distribute copies of SimpleScalar
* in compiled or executable form as set forth in Section 2, provided that
* either: (A) it is accompanied by the corresponding machine-readable source
* code, or (B) it is accompanied by a written offer, with no time limit, to
* give anyone a machine-readable copy of the corresponding source code in
* return for reimbursement of the cost of distribution. This written offer
* must permit verbatim duplication by anyone, or (C) it is distributed by
* someone who received only the executable form, and is accompanied by a
* copy of the written offer of source code.
*
* 6. SimpleScalar was developed by Todd M. Austin, Ph.D. The tool suite is
* currently maintained by SimpleScalar LLC ([email protected]). US Mail:
* 2395 Timbercrest Court, Ann Arbor, MI 48105.
*
* Copyright (C) 1994-2003 by Todd M. Austin, Ph.D. and SimpleScalar, LLC.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#if defined(__alpha) || defined(linux)
#include <unistd.h>
#endif /* __alpha || linux */
#include "host.h"
#include "misc.h"
#include "machine.h"
/* verbose output flag */
int verbose = FALSE;
#ifdef DEBUG
/* active debug flag */
int debugging = FALSE;
#endif /* DEBUG */
/* fatal function hook, this function is called just before an exit
caused by a fatal error, used to spew stats, etc. */
static void (*hook_fn)(FILE *stream) = NULL;
/* register a function to be called when an error is detected */
void
fatal_hook(void (*fn)(FILE *stream)) /* fatal hook function */
{
hook_fn = fn;
}
/* declare a fatal run-time error, calls fatal hook function */
#ifdef __GNUC__
void
_fatal(char *file, char *func, int line, char *fmt, ...)
#else /* !__GNUC__ */
void
fatal(char *fmt, ...)
#endif /* __GNUC__ */
{
va_list v;
va_start(v, fmt);
fprintf(stderr, "fatal: ");
myvfprintf(stderr, fmt, v);
#ifdef __GNUC__
if (verbose)
fprintf(stderr, " [%s:%s, line %d]", func, file, line);
#endif /* __GNUC__ */
fprintf(stderr, "\n");
if (hook_fn)
(*hook_fn)(stderr);
exit(1);
}
/* declare a panic situation, dumps core */
#ifdef __GNUC__
void
_panic(char *file, char *func, int line, char *fmt, ...)
#else /* !__GNUC__ */
void
panic(char *fmt, ...)
#endif /* __GNUC__ */
{
va_list v;
va_start(v, fmt);
fprintf(stderr, "panic: ");
myvfprintf(stderr, fmt, v);
#ifdef __GNUC__
fprintf(stderr, " [%s:%s, line %d]", func, file, line);
#endif /* __GNUC__ */
fprintf(stderr, "\n");
if (hook_fn)
(*hook_fn)(stderr);
abort();
}
/* declare a warning */
#ifdef __GNUC__
void
_warn(char *file, char *func, int line, char *fmt, ...)
#else /* !__GNUC__ */
void
warn(char *fmt, ...)
#endif /* __GNUC__ */
{
va_list v;
va_start(v, fmt);
fprintf(stderr, "warning: ");
myvfprintf(stderr, fmt, v);
#ifdef __GNUC__
if (verbose)
fprintf(stderr, " [%s:%s, line %d]", func, file, line);
#endif /* __GNUC__ */
fprintf(stderr, "\n");
}
/* print general information */
#ifdef __GNUC__
void
_info(char *file, char *func, int line, char *fmt, ...)
#else /* !__GNUC__ */
void
info(char *fmt, ...)
#endif /* __GNUC__ */
{
va_list v;
va_start(v, fmt);
myvfprintf(stderr, fmt, v);
#ifdef __GNUC__
if (verbose)
fprintf(stderr, " [%s:%s, line %d]", func, file, line);
#endif /* __GNUC__ */
fprintf(stderr, "\n");
}
#ifdef DEBUG
/* print a debugging message */
#ifdef __GNUC__
void
_debug(char *file, char *func, int line, char *fmt, ...)
#else /* !__GNUC__ */
void
debug(char *fmt, ...)
#endif /* __GNUC__ */
{
va_list v;
va_start(v, fmt);
if (debugging)
{
fprintf(stderr, "debug: ");
myvfprintf(stderr, fmt, v);
#ifdef __GNUC__
fprintf(stderr, " [%s:%s, line %d]", func, file, line);
#endif
fprintf(stderr, "\n");
}
}
#endif /* DEBUG */
/* seed the random number generator */
void
mysrand(unsigned int seed) /* random number generator seed */
{
#if defined(hpux) || defined(__hpux) || defined(__svr4__) || defined(_MSC_VER)
srand(seed);
#else
srandom(seed);
#endif
}
/* get a random number */
int
myrand(void) /* returns random number */
{
#if !defined(__alpha) && !defined(unix)
extern long random(void);
#endif
#if defined(hpux) || defined(__hpux) || defined(__svr4__) || defined(_MSC_VER)
return rand();
#else
return random();
#endif
}
/* copy a string to a new storage allocation (NOTE: many machines are missing
this trivial function, so I funcdup() it here...) */
char * /* duplicated string */
mystrdup(char *s) /* string to duplicate to heap storage */
{
char *buf;
if (!(buf = (char *)malloc(strlen(s)+1)))
return NULL;
strcpy(buf, s);
return buf;
}
/* find the last occurrence of a character in a string */
char *
mystrrchr(char *s, char c)
{
char *rtnval = 0;
do {
if (*s == c)
rtnval = s;
} while (*s++);
return rtnval;
}
/* case insensitive string compare (NOTE: many machines are missing this
trivial function, so I funcdup() it here...) */
int /* compare result, see strcmp() */
mystricmp(char *s1, char *s2) /* strings to compare, case insensitive */
{
unsigned char u1, u2;
for (;;)
{
u1 = (unsigned char)*s1++; u1 = tolower(u1);
u2 = (unsigned char)*s2++; u2 = tolower(u2);
if (u1 != u2)
return u1 - u2;
if (u1 == '\0')
return 0;
}
}
/* allocate some core, this memory has overhead no larger than a page
in size and it cannot be released. the storage is returned cleared */
void *
getcore(int nbytes)
{
return calloc(nbytes, 1);
#if 0 /* FIXME: sbrk() calls break malloc() on Linux... */
#if !defined(PURIFY) && !defined(_MSC_VER)
void *p = (void *)sbrk(nbytes);
if (p == (void *)-1)
return NULL;
/* this may be superfluous */
#if defined(__svr4__) || defined(_MSC_VER)
memset(p, '\0', nbytes);
#else /* !defined(__svr4__) */
bzero(p, nbytes);
#endif
return p;
#else
return calloc(nbytes, 1);
#endif /* PURIFY */
#endif
}
/* return log of a number to the base 2 */
int
log_base2(int n)
{
int power = 0;
if (n <= 0 || (n & (n-1)) != 0)
panic("log2() only works for positive power of two values");
while (n >>= 1)
power++;
return power;
}
/* return string describing elapsed time, passed in SEC in seconds */
char *
elapsed_time(long sec)
{
static char tstr[256];
char temp[256];
if (sec <= 0)
return "0s";
tstr[0] = '\0';
/* days */
if (sec >= 86400)
{
sprintf(temp, "%ldD ", sec/86400);
strcat(tstr, temp);
sec = sec % 86400;
}
/* hours */
if (sec >= 3600)
{
sprintf(temp, "%ldh ", sec/3600);
strcat(tstr, temp);
sec = sec % 3600;
}
/* mins */
if (sec >= 60)
{
sprintf(temp, "%ldm ", sec/60);
strcat(tstr, temp);
sec = sec % 60;
}
/* secs */
if (sec >= 1)
{
sprintf(temp, "%lds ", sec);
strcat(tstr, temp);
}
tstr[strlen(tstr)-1] = '\0';
return tstr;
}
/* assume bit positions numbered 31 to 0 (31 high order bit), extract num bits
from word starting at position pos (with pos as the high order bit of those
to be extracted), result is right justified and zero filled to high order
bit, for example, extractl(word, 6, 3) w/ 8 bit word = 01101011 returns
00000110 */
unsigned int
extractl(int word, /* the word from which to extract */
int pos, /* bit positions 31 to 0 */
int num) /* number of bits to extract */
{
return(((unsigned int) word >> (pos + 1 - num)) & ~(~0 << num));
}
#define PUT(p, n) \
{ \
int nn, cc; \
\
for (nn = 0; nn < n; nn++) \
{ \
cc = *(p+nn); \
*obuf++ = cc; \
} \
}
#define PAD(s, n) \
{ \
int nn, cc; \
\
cc = *s; \
for (nn = n; nn > 0; nn--) \
*obuf++ = cc; \
}
#ifdef HOST_HAS_QWORD
#define HIBITL LL(0x8000000000000000)
typedef sqword_t slargeint_t;
typedef qword_t largeint_t;
#else /* !HOST_HAS_QWORD */
#define HIBITL 0x80000000L
typedef sword_t slargeint_t;
typedef word_t largeint_t;
#endif /* HOST_HAS_QWORD */
static int
_lowdigit(slargeint_t *valptr)
{
/* this function computes the decimal low-order digit of the number pointed
to by valptr, and returns this digit after dividing *valptr by ten; this
function is called ONLY to compute the low-order digit of a long whose
high-order bit is set */
int lowbit = (int)(*valptr & 1);
slargeint_t value = (*valptr >> 1) & ~HIBITL;
*valptr = value / 5;
return (int)(value % 5 * 2 + lowbit + '0');
}
/* portable vsprintf with qword support, returns end pointer */
char *
myvsprintf(char *obuf, char *format, va_list v)
{
static char _blanks[] = " ";
static char _zeroes[] = "00000000000000000000";
/* counts output characters */
int count = 0;
/* format code */
int fcode;
/* field width and precision */
int width, prec;
/* number of padding zeroes required on the left and right */
int lzero;
/* length of prefix */
int prefixlength;
/* combined length of leading zeroes, trailing zeroes, and suffix */
int otherlength;
/* format flags */
#define PADZERO 0x0001 /* padding zeroes requested via '0' */
#define RZERO 0x0002 /* there will be trailing zeros in output */
#define LZERO 0x0004 /* there will be leading zeroes in output */
#define DOTSEEN 0x0008 /* dot appeared in format specification */
#define LENGTH 0x0010 /* l */
int flagword;
/* maximum number of digits in printable number */
#define MAXDIGS 22
/* starting and ending points for value to be printed */
char *bp, *p;
/* work variables */
int k, lradix, mradix;
/* pointer to sign, "0x", "0X", or empty */
char *prefix;
/* values are developed in this buffer */
static char buf[MAXDIGS*4], buf1[MAXDIGS*4];
/* pointer to a translate table for digits of whatever radix */
char *tab;
/* value being converted, if integer */
slargeint_t val;
/* value being converted, if floating point */
dfloat_t fval;
for (;;)
{
int n;
while ((fcode = *format) != '\0' && fcode != '%')
{
*obuf++ = fcode;
format++;
count++;
}
if (fcode == '\0')
{
/* end of format; terminate and return */
*obuf = '\0';
return obuf;
}
/* % has been found, the following switch is used to parse the format
specification and to perform the operation specified by the format
letter; the program repeatedly goes back to this switch until the
format letter is encountered */
width = prefixlength = otherlength = flagword = 0;
format++;
charswitch:
switch (fcode = *format++)
{
case '0': /* means pad with leading zeros */
flagword |= PADZERO;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
{
int num = fcode - '0';
while (isdigit(fcode = *format))
{
num = num * 10 + fcode - '0';
format++;
}
if (flagword & DOTSEEN)
prec = num;
else
width = num;
goto charswitch;
}
case '.':
flagword |= DOTSEEN;
goto charswitch;
case 'l':
flagword |= LENGTH;
goto charswitch;
case 'n': /* host counter */
#ifdef HOST_HAS_QWORD
flagword |= LENGTH;
/* fallthru */
#else /* !HOST_HAS_QWORD */
flagword |= DOTSEEN;
if (!width)
width = 12;
prec = 0;
goto process_float;
#endif /* HOST_HAS_QWORD */
case 'd':
/* fetch the argument to be printed */
if (flagword & LENGTH)
val = va_arg(v, slargeint_t);
else
val = (slargeint_t)va_arg(v, sword_t);
/* set buffer pointer to last digit */
p = bp = buf + MAXDIGS;
/* If signed conversion, make sign */
if (val < 0)
{
prefix = "-";
prefixlength = 1;
/* negate, checking in advance for possible overflow */
if (val != (slargeint_t)HIBITL)
val = -val;
else
{
/* number is -HIBITL; convert last digit and get pos num */
*--bp = _lowdigit(&val);
}
}
decimal:
{
slargeint_t qval = val;
if (qval <= 9)
*--bp = (int)qval + '0';
else
{
do {
n = (int)qval;
qval /= 10;
*--bp = n - (int)qval * 10 + '0';
}
while (qval > 9);
*--bp = (int)qval + '0';
}
}
break;
case 'u':
/* fetch the argument to be printed */
if (flagword & LENGTH)
val = va_arg(v, largeint_t);
else
val = (largeint_t)va_arg(v, word_t);
/* set buffer pointer to last digit */
p = bp = buf + MAXDIGS;
if (val & HIBITL)
*--bp = _lowdigit(&val);
goto decimal;
case 'o':
mradix = 7;
lradix = 2;
goto fixed;
case 'p': /* target address */
if (sizeof(md_addr_t) > 4)
flagword |= LENGTH;
/* fallthru */
case 'X':
case 'x':
mradix = 15;
lradix = 3;
fixed:
/* fetch the argument to be printed */
if (flagword & LENGTH)
val = va_arg(v, largeint_t);
else
val = (largeint_t)va_arg(v, word_t);
/* set translate table for digits */
tab = (fcode == 'X') ? "0123456789ABCDEF" : "0123456789abcdef";
/* develop the digits of the value */
p = bp = buf + MAXDIGS;
{
slargeint_t qval = val;
if (qval == 0)
{
otherlength = lzero = 1;
flagword |= LZERO;
}
else
do {
*--bp = tab[qval & mradix];
qval = ((qval >> 1) & ~HIBITL) >> lradix;
} while (qval != 0);
}
break;
#ifndef HOST_HAS_QWORD
process_float:
#endif /* !HOST_HAS_QWORD */
case 'f':
if (flagword & DOTSEEN)
sprintf(buf1, "%%%d.%df", width, prec);
else if (width)
sprintf(buf1, "%%%df", width);
else
sprintf(buf1, "%%f");
/* fetch the argument to be printed */
fval = va_arg(v, dfloat_t);
/* print floating point value */
sprintf(buf, buf1, fval);
bp = buf;
p = bp + strlen(bp);
break;
case 's':
bp = va_arg(v, char *);
if (bp == NULL)
bp = "(null)";
p = bp + strlen(bp);
break;
case '%':
buf[0] = fcode;
goto c_merge;
case 'c':
buf[0] = va_arg(v, int);
c_merge:
p = (bp = &buf[0]) + 1;
break;
default:
/* this is technically an error; what we do is to back up the format
pointer to the offending char and continue with the format scan */
format--;
continue;
}
/* calculate number of padding blanks */
k = (n = p - bp) + prefixlength + otherlength;
if (width <= k)
count += k;
else
{
count += width;
/* set up for padding zeroes if requested; otherwise emit padding
blanks unless output is to be left-justified */
if (flagword & PADZERO)
{
if (!(flagword & LZERO))
{
flagword |= LZERO;
lzero = width - k;
}
else
lzero += width - k;
/* cancel padding blanks */
k = width;
}
else
{
/* blanks on left if required */
PAD(_blanks, width - k);
}
}
/* prefix, if any */
if (prefixlength != 0)
{
PUT(prefix, prefixlength);
}
/* zeroes on the left */
if (flagword & LZERO)
{
PAD(_zeroes, lzero);
}
/* the value itself */
if (n > 0)
{
PUT(bp, n);
}
}
}
/* portable sprintf with qword support, returns end pointer */
char *
mysprintf(char *obuf, char *format, ...)
{
/* vararg parameters */
va_list v;
va_start(v, format);
return myvsprintf(obuf, format, v);
}
/* portable vfprintf with qword support, returns end pointer */
void
myvfprintf(FILE *stream, char *format, va_list v)
{
/* temp buffer */
char buf[2048];
myvsprintf(buf, format, v);
fputs(buf, stream);
}
/* portable fprintf with qword support, returns end pointer */
void
myfprintf(FILE *stream, char *format, ...)
{
/* temp buffer */
char buf[2048];
/* vararg parameters */
va_list v;
va_start(v, format);
myvsprintf(buf, format, v);
fputs(buf, stream);
}
#ifdef HOST_HAS_QWORD
#define LL_MAX LL(9223372036854775807)
#define LL_MIN (-LL_MAX - 1)
#define ULL_MAX (ULL(9223372036854775807) * ULL(2) + 1)
/* convert a string to a signed result */
sqword_t
myatosq(char *nptr, char **endp, int base)
{
char *s, *save;
int negative, overflow;
sqword_t cutoff, cutlim, i;
unsigned char c;
extern int errno;
if (!nptr || !*nptr)
panic("strtoll() passed a NULL string");
s = nptr;
/* skip white space */
while (isspace((int)(*s)))
++s;
if (*s == '\0')
goto noconv;
if (base == 0)
{
if (s[0] == '0' && toupper(s[1]) == 'X')
base = 16;
else
base = 10;
}
if (base <= 1 || base > 36)
panic("bogus base: %d", base);
/* check for a sign */
if (*s == '-')
{
negative = 1;
++s;
}
else if (*s == '+')
{
negative = 0;
++s;
}
else
negative = 0;
if (base == 16 && s[0] == '0' && toupper(s[1]) == 'X')
s += 2;
/* save the pointer so we can check later if anything happened */
save = s;
cutoff = LL_MAX / (unsigned long int) base;
cutlim = LL_MAX % (unsigned long int) base;
overflow = 0;
i = 0;
for (c = *s; c != '\0'; c = *++s)
{
if (isdigit (c))
c -= '0';
else if (isalpha (c))
c = toupper(c) - 'A' + 10;
else
break;
if (c >= base)
break;
/* check for overflow */
if (i > cutoff || (i == cutoff && c > cutlim))
overflow = 1;
else
{
i *= (unsigned long int) base;
i += c;
}
}
/* check if anything actually happened */
if (s == save)
goto noconv;
/* store in ENDP the address of one character past the last character
we converted */
if (endp != NULL)
*endp = (char *) s;
if (overflow)
{
errno = ERANGE;
return negative ? LL_MIN : LL_MAX;
}
else
{
errno = 0;
/* return the result of the appropriate sign */
return (negative ? -i : i);
}
noconv:
/* there was no number to convert */
if (endp != NULL)
*endp = (char *) nptr;
return 0;
}
/* convert a string to a unsigned result */
qword_t
myatoq(char *nptr, char **endp, int base)
{
char *s, *save;
int overflow;
qword_t cutoff, cutlim, i;
unsigned char c;
extern int errno;
if (!nptr || !*nptr)
panic("strtoll() passed a NULL string");
s = nptr;
/* skip white space */
while (isspace((int)(*s)))
++s;
if (*s == '\0')
goto noconv;
if (base == 0)
{
if (s[0] == '0' && toupper(s[1]) == 'X')
base = 16;
else
base = 10;
}
if (base <= 1 || base > 36)
panic("bogus base: %d", base);
if (base == 16 && s[0] == '0' && toupper(s[1]) == 'X')
s += 2;
/* save the pointer so we can check later if anything happened */
save = s;
cutoff = ULL_MAX / (unsigned long int) base;
cutlim = ULL_MAX % (unsigned long int) base;
overflow = 0;
i = 0;
for (c = *s; c != '\0'; c = *++s)
{
if (isdigit (c))
c -= '0';
else if (isalpha (c))
c = toupper(c) - 'A' + 10;
else
break;
if (c >= base)
break;
/* check for overflow */
if (i > cutoff || (i == cutoff && c > cutlim))
overflow = 1;
else
{
i *= (unsigned long int) base;
i += c;
}
}
/* check if anything actually happened */
if (s == save)
goto noconv;
/* store in ENDP the address of one character past the last character
we converted */
if (endp != NULL)
*endp = (char *) s;
if (overflow)
{
errno = ERANGE;
return ULL_MAX;
}
else
{
errno = 0;
/* return the result of the appropriate sign */
return i;
}
noconv:
/* there was no number to convert */
if (endp != NULL)
*endp = (char *) nptr;
return 0;
}
#ifdef TESTIT
void
testit(char *s)
{
char buf[128];
qword_t qval;
qval = myatoq(s, NULL, 10);
myqtoa(qval, "%x", buf, NULL);
fprintf(stderr, "x: %s\n", buf);
myqtoa(qval, "%16x", buf, NULL);
fprintf(stderr, "16x: %s\n", buf);
myqtoa(qval, "%016x", buf, NULL);