?? hard-params.c
字號:
#endif printf("\n"); bits_per_byte= basic(); Vprintf("\n"); if (F||V) { fprec= fprop(bits_per_byte); dprec= dprop(bits_per_byte); lprec= ldprop(bits_per_byte); efprop(fprec, dprec, lprec); edprop(fprec, dprec, lprec); eldprop(fprec, dprec, lprec); } if (V) { /* An extra goody: the approximate amount of data-space */ /* Allocate store until no more available */ size=1<<((bits_per_byte*sizeof(int))-2); total=0; while (size!=0) { while (malloc(size)!=(char *)NULL) total+=(size/2); size/=2; } Vprintf("%sMemory mallocatable ~= %ld Kbytes%s\n", co, (total+511)/512, oc); } exit(bugs);}Procedure eek_a_bug(problem) char *problem; { printf("\n%s*** WARNING: %s%s\n", co, problem, oc); bugs++;}Procedure i_define(sort, name, val, req) char *sort, *name; long val, req; { if (val >= 0) { printf("#define %s%s %ld\n", sort, name, val); } else { printf("#define %s%s (%ld)\n", sort, name, val); } if (val != req) { printf("%s*** Verify failed for above #define!\n", co); printf(" Compiler has %ld for value%s\n\n", req, oc); bugs++; } Vprintf("\n");}#ifndef NO_UI#ifdef __STDC__#define U "U"#else#define U ""#endifProcedure u_define(sort, name, val, req) char *sort, *name; unsigned long val, req; { printf("#define %s%s %lu%s\n", sort, name, val, U); if (val != req) { printf("%s*** Verify failed for above #define!\n", co); printf(" Compiler has %lu for value%s\n\n", req, oc); bugs++; } Vprintf("\n");}#endif/* Long_double is the longest floating point type available: */#if defined(__STDC__) && !defined(NO_LONG_DOUBLE)#define Long_double long double#else#define Long_double double#endifchar *f_rep();Procedure f_define(sort, name, precision, val, mark) char *sort, *name; int precision; Long_double val; char *mark; { if (stdc) { printf("#define %s%s %s%s\n", sort, name, f_rep(precision, val), mark); } else if (*mark == 'F') { /* non-ANSI C has no float constants, so cast the constant */ printf("#define %s%s ((float)%s)\n", sort, name, f_rep(precision, val)); } else { printf("#define %s%s %s\n", sort, name, f_rep(precision, val)); } Vprintf("\n");}int floor_log(base, x) int base; Long_double x; { /* return floor(log base(x)) */ int r=0; while (x>=base) { r++; x/=base; } return r;}int ceil_log(base, x) int base; Long_double x; { int r=0; while (x>1.0) { r++; x/=base; } return r;}int exponent(x, fract, exp) Long_double x; double *fract; int *exp; { /* Split x into a fraction and a power of ten; returns 0 if x is unusable, 1 otherwise. Only used for error messages about faulty output. */ int r=0, neg=0; Long_double old; *fract=0.0; *exp=0; if (x<0.0) { x= -x; neg= 1; } if (x==0.0) return 1; if (x>=10.0) { while (x>=10.0) { old=x; r++; x/=10.0; if (old==x) return 0; } } else { while (x<1.0) { old=x; r--; x*=10.0; if (old==x) return 0; } } if (neg) *fract= -x; else *fract=x; *exp=r; return 1;}#define fabs(x) (((x)<0.0)?(-x):(x))char *f_rep(precision, val) int precision; Long_double val; { static char buf[1024]; char *f1; if (sizeof(double) == sizeof(Long_double)) { /* Assume they're the same, and use non-stdc format */ /* This is for stdc compilers using non-stdc libraries */ f1= "%.*e"; } else { /* It had better support Le then */ f1= "%.*Le"; } sprintf(buf, f1, precision, val); return buf;}Procedure bitpattern(p, size) char *p; int size; { char c; int i, j; for (i=1; i<=size; i++) { c= *p; p++; for (j=bits_per_byte-1; j>=0; j--) printf("%c", (c>>j)&1 ? '1' : '0'); if (i!=size) printf(" "); }}#define Order(x, px, mode)\ printf("%s %s ", co, mode); for (i=0; i<sizeof(x); i++) px[i]= c[i]; \ for (i=1; i<=sizeof(x); i++) { putchar((char)((x>>(bits_per_byte*(sizeof(x)-i)))&mask)); }\ printf("%s\n", oc);Procedure endian(bits_per_byte) int bits_per_byte; { /*unsigned*/ short s=0; /*unsigned*/ int j=0; /*unsigned*/ long l=0; char *ps= (char *) &s, *pj= (char *) &j, *pl= (char *) &l, *c= "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; unsigned int mask, i; mask=0; for (i=1; i<=bits_per_byte; i++) mask= (mask<<1)|1; if (V) { printf("%sCharacter order:%s\n", co, oc); Order(s, ps, "short:"); Order(j, pj, "int: "); Order(l, pl, "long: "); }}#ifdef VERIFY#ifndef SCHAR_MAX#define SCHAR_MAX char_max#define SCHAR_MIN char_min#endif#ifndef UCHAR_MAX#define UCHAR_MAX char_max#endif#else#define CHAR_BIT char_bit#define CHAR_MAX char_max#define CHAR_MIN char_min#define SCHAR_MAX char_max#define SCHAR_MIN char_min#define UCHAR_MAX char_max#endif /* VERIFY */int cprop() { /* Properties of character */ volatile char c, char_max, char_min; volatile int bits_per_byte, is_signed; long char_bit; Unexpected(2); /* Calculate number of bits per character *************************/ c=1; bits_per_byte=0; do { c=c<<1; bits_per_byte++; } while(c!=0); c= (char)(-1); if (((int)c)<0) is_signed=1; else is_signed=0; Vprintf("%sChar = %d bits, %ssigned%s\n", co, (int)sizeof(c)*bits_per_byte, (is_signed?"":"un"), oc); char_bit=(long)(sizeof(c)*bits_per_byte); if (L) i_define("CHAR", "_BIT", char_bit, (long) CHAR_BIT); c=0; char_max=0; c++; if (setjmp(lab)==0) { /* Yields char_max */ while (c>char_max) { char_max=c; c++; } } else { Vprintf("%sCharacter overflow generates a trap!%s\n", co, oc); } c=0; char_min=0; c--; if (setjmp(lab)==0) { /* Yields char_min */ while (c<char_min) { char_min=c; c--; } } Unexpected(3); if (L) { i_define("CHAR", "_MAX", (long) char_max, (long) CHAR_MAX); i_define("CHAR", "_MIN", (long) char_min, (long) CHAR_MIN); if (is_signed) { i_define("SCHAR", "_MAX", (long) char_max, (long) SCHAR_MAX); i_define("SCHAR", "_MIN", (long) char_min, (long) SCHAR_MIN); } else { i_define("UCHAR", "_MAX", (long) char_max, (long) UCHAR_MAX); } if (is_signed) {#ifndef NO_UC volatile unsigned char c, char_max; c=0; char_max=0; c++; if (setjmp(lab)==0) { /* Yields char_max */ while (c>char_max) { char_max=c; c++; } } Unexpected(4); i_define("UCHAR", "_MAX", (long) char_max, (long) UCHAR_MAX);#endif } else {#ifndef NO_SC /* Define NO_SC if the next line gives a syntax error */ volatile signed char c, char_max, char_min; c=0; char_max=0; c++; if (setjmp(lab)==0) { /* Yields char_max */ while (c>char_max) { char_max=c; c++; } } c=0; char_min=0; c--; if (setjmp(lab)==0) { /* Yields char_min */ while (c<char_min) { char_min=c; c--; } } Unexpected(5); i_define("SCHAR", "_MIN", (long) char_min, (long) SCHAR_MIN); i_define("SCHAR", "_MAX", (long) char_max, (long) SCHAR_MAX);#endif /* NO_SC */ } } return bits_per_byte;}int basic() { /* The properties of the basic types. Returns number of bits per sizeof unit */ volatile int bits_per_byte; bits_per_byte= cprop(); /* Shorts, ints and longs *****************************************/ Vprintf("%sShort=%d int=%d long=%d float=%d double=%d bits %s\n", co, (int) sizeof(short)*bits_per_byte, (int) sizeof(int)*bits_per_byte, (int) sizeof(long)*bits_per_byte, (int) sizeof(float)*bits_per_byte, (int) sizeof(double)*bits_per_byte, oc); if (stdc) { Vprintf("%sLong double=%d bits%s\n", co, (int) sizeof(Long_double)*bits_per_byte, oc); } Vprintf("%sChar pointers = %d bits%s%s\n", co, (int)sizeof(char *)*bits_per_byte, sizeof(char *)>sizeof(int)?" BEWARE! larger than int!":"", oc); Vprintf("%sInt pointers = %d bits%s%s\n", co, (int)sizeof(int *)*bits_per_byte, sizeof(int *)>sizeof(int)?" BEWARE! larger than int!":"", oc); sprop(); iprop(); lprop(); usprop(); uiprop(); ulprop(); Unexpected(6); /* Alignment constants ********************************************/ Vprintf("%sAlignments used for char=%d short=%d int=%d long=%d%s\n", co, (int)sizeof(struct{char i1; char c1;})-(int)sizeof(char), (int)sizeof(struct{short i2; char c2;})-(int)sizeof(short), (int)sizeof(struct{int i3; char c3;})-(int)sizeof(int), (int)sizeof(struct{long i4; char c4;})-(int)sizeof(long), oc); /* Ten little endians *********************************************/ endian(bits_per_byte); /* Pointers *******************************************************/ if (V) { if ("abcd"=="abcd") printf("%sStrings are shared%s\n", co, oc); else printf("%sStrings are not shared%s\n", co, oc); } return bits_per_byte;}#endif /* ifndef PASS *//* As I said, I apologise for the contortions below. The functions are expanded by the preprocessor twice or three times (for float and double, and maybe for long double, and for short, int and long). That way, I never make a change to one that I forget to make to the other. You can look on it as C's fault for not supporting multi-line macro's. This whole file is read 3 times by the preprocessor, with PASSn set for n=1, 2 or 3, to decide which parts to reprocess.*//* #undef on an already undefined thing is (wrongly) flagged as an error by some compilers, therefore the #ifdef that follows: */#ifdef Number#undef Number#undef THING#undef Thing#undef thing#undef FPROP#undef Fname#undef Store#undef Sum#undef Diff#undef Mul#undef Div#undef Self#undef F_check#undef Validate#undef EPROP#undef MARK#undef F_RADIX#undef F_MANT_DIG#undef F_DIG#undef F_ROUNDS#undef F_EPSILON#undef F_MIN_EXP#undef F_MIN#undef F_MIN_10_EXP#undef F_MAX_EXP#undef F_MAX#undef F_MAX_10_EXP#endif#ifdef Integer#undef Integer#undef INT#undef IPROP#undef Iname#undef UPROP#undef Uname#undef OK_UI#undef I_MAX#undef I_MIN#undef U_MAX#endif#ifdef PASS1#define Number float#define THING "FLOAT"#define Thing "Float"#define thing "float"#define Fname "FLT"#define FPROP fprop#define Store fStore#define Sum fSum#define Diff fDiff#define Mul fMul#define Div fDiv#define Self fSelf
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -