#include #if defined(sparc) || defined(stellar) || defined(applec) || defined(THINK_C) || defined(hpux) || defined(IBMRS6000) #define BIGENDIAN 1 #endif /* sun || sparc || stellar || applec || THINK_C */ /* * Allow for both big-endian (sgi) and little-endian (dec) * mips machines. */ #if defined(MIPSEL) || defined(_MIPSEL) || defined(__MIPSEL) #define BIGENDIAN 0 #endif #if defined(MIPSEB) || defined(_MIPSEB) || defined(__MIPSEB) #define BIGENDIAN 1 #endif /* DEC Alpha machines. */ #if defined(__alpha) #define BIGENDIAN 0 #endif /* Various Intel chips. */ #if defined(m386) || defined(M_I86) || defined(MSDOS) || defined(I860) || defined(PVS) || defined (_M_IX86) || defined(__i386__) #define BIGENDIAN 0 #endif /* m386 || M_I86 */ #ifdef vax #define BIGENDIAN 0 #endif /* vax */ #if (BIGENDIAN == 1) typedef struct ieeedouble { unsigned int sign:1; unsigned int exp:11; unsigned int mant:20; unsigned int mant2:32; } ieeedouble; typedef struct ieeefloat { unsigned int sign:1; unsigned int exp:8; unsigned int mant:23; } ieeefloat; #else /* BIGENDIAN 0 or little endian */ typedef struct ieeedouble { unsigned int mant2:32; unsigned int mant:20; unsigned int exp:11; unsigned int sign:1; } ieeedouble; typedef struct ieeefloat { unsigned int mant:23; unsigned int exp:8; unsigned int sign:1; } ieeefloat; #endif /* BIGENDIAN */ typedef struct ieeedouble nativedouble; typedef struct ieeefloat nativefloat; typedef union { ieeedouble ieee; nativedouble native; char b[8]; double d; } double_type; typedef union { ieeefloat ieee; nativefloat native; char b[4]; float f; } float_type; #define ISFRACTION(e) (1022 - 4 <= (e) && (e) <= 1022 + 15) #define EXTRACTFRACTION(dp) \ (((unsigned)(1L<<30)|((unsigned)(dp)->native.mant<<10)| \ ((dp)->native.mant2>>22)) >> (1022+15-(dp)->native.exp)) #define EXTRACTMANT(dp) \ (((unsigned)(dp)->native.mant<<32)|((dp)->native.mant2)) #define EXTRACTEXPONENT(dp) ((dp)->native.exp) #define word unsigned int #define dword unsigned long #define byte unsigned char void pf( int signo, int exp, dword mant); int main( void ) { int i,j; static float nf[]={ 234.12345678, -5.75, 9.99e9, 1.1e-5 }; static double nd[]={ 234.12345678, -5.75, 9.99e9, 1.1e-5 }; FILE *f; register int exp; register long n, an; int fract; union { unsigned long l; char c[sizeof(long)]; } u; float_type ft[4]; double_type dt[4]; #if (BIGENDIAN == 1) fprintf(stderr,"host is bigendian\n"); #else fprintf(stderr,"host is littleendian\n"); #endif if (sizeof(long) > 4) u.l = 0x0102030405060708; else u.l = 0x01020304; for (i=0; i < sizeof(long); i++) fprintf(stderr,"%c",u.c[i]+'0'); fprintf(stderr,"\n"); #if (BIGENDIAN == 1) fprintf(stderr,"creating sun4f\n"); f=fopen("sun4f","w"); #else fprintf(stderr,"creating i86pcf\n"); f=fopen("i86pcf","w"); #endif for(i=0;i<4;i++) { ft[i].f = nf[i]; fprintf(stderr, "f: %f\n", ft[i].f); pf(ft[i].native.sign, ft[i].native.exp, ft[i].native.mant); fwrite( &ft[i].b, sizeof(float), 1, f); } for(i=0;i<4;i++) { dt[i].d = nd[i]; fprintf(stderr, "d: %g\n", dt[i].d); fwrite( &dt[i].d, sizeof(double), 1, f); } fclose(f); #if (BIGENDIAN == 1) fprintf(stderr,"host sun4, open i86pcf\n"); f=fopen("i86pcf","r"); #else fprintf(stderr,"host i86pc, open sun4f\n"); f=fopen("sun4f","r"); #endif if ( NULL == f ) { perror("fopen"); fprintf(stderr,"Please, create the other endian file\n"); exit(1); } for(i=0;i<4;i++) { for( j=0; j>(20-j)))) { fprintf(stderr, "1"); mantu *= (k/mantd); mantu ++; mantd = k; } else fprintf(stderr, "0"); } fprintf(stderr, "\nmant : %llu/%llu\n", mantu, mantd); mant=dt[i].native.mant2; fprintf(stderr, "mant : 0x%lX\nmant : ", mant); for (j=1; j<= 32; j++) { k <<= 1; if ( 0 != (1 & (mant>>(32-j)))) { fprintf(stderr, "1"); mantu *= (k/mantd); mantu ++; mantd = k; } else fprintf(stderr, "0"); } fprintf(stderr, "\nmant : %llu/%llu\n", mantu, mantd); fprintf(stderr, "%g = %c ( %llu / %llu ) * 2^%d\n", dt[i].d, dt[i].native.sign == 1 ? '-' : ' ', mantu,mantd,exp-1023); } } fclose(f); return(0); } void pf( int signo, int exp, dword mant) { int i; int tempexpo; word entero10 = 0; word decimal10 = 0; exp -= 127; mant |= 0x800000U; tempexpo=exp; entero10 = 0; decimal10 = 0; /* calculamos la parte entera. */ for (i=0; tempexpo>=0; i++ ) { entero10 = entero10*2 + ((mant & (0x800000>>i)) ? 1 : 0); tempexpo--; if( i >= 23 ) break; } /* aqui calculamos la parte decimal decimal10 contiene 9 digitos significativos */ if( i<23) { int valbit= 500000000; /* inicialmente el valor 0.5 */ for(; exp < -1; exp++) valbit/=2; for(; i<23; i++) { if( i>=0) decimal10 = decimal10 + ((mant & (0x800000>>i)) ? valbit : 0); valbit/=2; } } /* presentar numero en string ascii */ printf("printfloat: %c%u.%09.9u\n", (signo?'-' : ' '), entero10, decimal10); fflush(stdout); }