00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <stdio.h>
00021 #include <grass/Vect.h>
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 #define TEST_PATTERN 1.3333
00061 #define LONG_TEST 0x01020304
00062 #define INT_TEST 0x01020304
00063 #define SHORT_TEST 0x0102
00064
00065 union type_conv
00066 {
00067 double d;
00068 float f;
00069 long l;
00070 int i;
00071 short s;
00072 unsigned char c[PORT_DOUBLE];
00073 };
00074 static union type_conv u;
00075
00076
00077 static unsigned char dbl_cmpr[] =
00078 {0x3f, 0xf5, 0x55, 0x32, 0x61, 0x7c, 0x1b, 0xda};
00079
00080 static unsigned char flt_cmpr[] =
00081 {0x3f, 0xaa, 0xa9, 0x93};
00082 static unsigned char lng_cmpr[] =
00083 {0x01, 0x02, 0x03, 0x04};
00084 static unsigned char int_cmpr[] =
00085 {0x01, 0x02, 0x03, 0x04};
00086 static unsigned char shrt_cmpr[] =
00087 {0x01, 0x02};
00088
00089 static char dbl_cnvrt[sizeof (double)];
00090 static char flt_cnvrt[sizeof (float)];
00091 static char lng_cnvrt[sizeof (long)];
00092 static char int_cnvrt[sizeof (int)];
00093 static char shrt_cnvrt[sizeof (short)];
00094
00095 static int nat_dbl, nat_flt, nat_lng, nat_int, nat_shrt, nat_char;
00096
00097
00098
00099 static int find_offset (unsigned char *, unsigned char, int);
00100 static int dumpflags (void);
00101
00102
00103 int
00104 main (int argc, char **argv)
00105 {
00106 register int i;
00107 int tmp, tmp2;
00108 int err = 0;
00109 int dbl_order, flt_order, lng_order, int_order, shrt_order;
00110
00111
00112 printf ("\n/* Native machine sizes */\n");
00113 printf ("#define NATIVE_DOUBLE %d\n", (nat_dbl = sizeof (double)));
00114 printf ("#define NATIVE_FLOAT %d\n", (nat_flt = sizeof (float)));
00115 printf ("#define NATIVE_LONG %d\n", (nat_lng = sizeof (long)));
00116 printf ("#define NATIVE_INT %d\n", (nat_int = sizeof (int)));
00117 printf ("#define NATIVE_SHORT %d\n", (nat_shrt = sizeof (short)));
00118 printf ("#define NATIVE_CHAR %d\n", (nat_char = sizeof (char)));
00119
00120
00121
00122 if (nat_dbl != PORT_DOUBLE)
00123 {
00124 fprintf (stderr, "ERROR, sizeof (double) != %d\n", PORT_DOUBLE);
00125 err = 1;
00126 }
00127 if (nat_flt != PORT_FLOAT)
00128 {
00129 fprintf (stderr, "ERROR, sizeof (float) != %d\n", PORT_DOUBLE);
00130 err = 1;
00131 }
00132 if (nat_lng < PORT_LONG)
00133 {
00134 fprintf (stderr, "ERROR, sizeof (long) < %d\n", PORT_LONG);
00135 err = 1;
00136 }
00137 if (nat_int < PORT_INT)
00138 {
00139 fprintf (stderr, "ERROR, sizeof (int) < %d\n", PORT_INT);
00140 err = 1;
00141 }
00142 if (nat_shrt < PORT_SHORT)
00143 {
00144 fprintf (stderr, "ERROR, sizeof (short) < %d\n", PORT_SHORT);
00145 err = 1;
00146 }
00147 if (nat_char != PORT_CHAR)
00148 {
00149 fprintf (stderr, "ERROR, sizeof (char) != %d\n", PORT_CHAR);
00150 err = 1;
00151 }
00152
00153
00154
00155
00156
00157
00158
00159 u.d = TEST_PATTERN;
00160 for (i = 0; i < PORT_DOUBLE; i++)
00161 {
00162 tmp = find_offset (u.c, dbl_cmpr[i], PORT_DOUBLE);
00163 if (-1 == tmp)
00164 {
00165 fprintf (stderr, "ERROR, could not find '%x' in double\n", dbl_cmpr[i]);
00166 err = 1;
00167 }
00168 dbl_cnvrt[i] = tmp;
00169 }
00170 tmp = tmp2 = 1;
00171 for (i = 0; i < PORT_DOUBLE; i++)
00172 {
00173 if ( dbl_cnvrt[i] != i ) tmp = 0;
00174 if ( dbl_cnvrt[i] != (PORT_DOUBLE - i - 1 )) tmp2 = 0;
00175 }
00176 if ( tmp ) dbl_order = ENDIAN_BIG;
00177 else if ( tmp2 ) dbl_order = ENDIAN_LITTLE;
00178 else dbl_order = ENDIAN_OTHER;
00179
00180
00181 u.f = TEST_PATTERN;
00182 for (i = 0; i < PORT_FLOAT; i++)
00183 {
00184 tmp = find_offset (u.c, flt_cmpr[i], PORT_FLOAT);
00185 if (-1 == tmp)
00186 {
00187 fprintf (stderr, "ERROR, could not find '%x' in float\n", flt_cmpr[i]);
00188 err = 1;
00189 }
00190 flt_cnvrt[i] = tmp;
00191 }
00192 tmp = tmp2 = 1;
00193 for (i = 0; i < PORT_FLOAT; i++)
00194 {
00195 if ( flt_cnvrt[i] != i ) tmp = 0;
00196 if ( flt_cnvrt[i] != (PORT_FLOAT - i - 1 )) tmp2 = 0;
00197 }
00198 if ( tmp ) flt_order = ENDIAN_BIG;
00199 else if ( tmp2 ) flt_order = ENDIAN_LITTLE;
00200 else flt_order = ENDIAN_OTHER;
00201
00202
00203 u.l = LONG_TEST;
00204 for (i = 0; i < PORT_LONG; i++)
00205 {
00206 tmp = find_offset (u.c, lng_cmpr[i], nat_lng);
00207 if (-1 == tmp)
00208 {
00209 fprintf (stderr, "ERROR, could not find '%x' in long\n", lng_cmpr[i]);
00210 err = 1;
00211 }
00212 lng_cnvrt[i] = tmp;
00213 }
00214 tmp = tmp2 = 1;
00215 for (i = 0; i < PORT_LONG; i++)
00216 {
00217 if ( lng_cnvrt[i] != ( i + ( nat_lng - PORT_LONG) ) ) tmp = 0;
00218 if ( lng_cnvrt[i] != (PORT_LONG - i - 1 )) tmp2 = 0;
00219 }
00220 if ( tmp ) lng_order = ENDIAN_BIG;
00221 else if ( tmp2 ) lng_order = ENDIAN_LITTLE;
00222 else lng_order = ENDIAN_OTHER;
00223
00224
00225 u.i = INT_TEST;
00226 for (i = 0; i < PORT_INT; i++)
00227 {
00228 tmp = find_offset (u.c, int_cmpr[i], nat_int);
00229 if (-1 == tmp)
00230 {
00231 fprintf (stderr, "ERROR, could not find '%x' in int\n", int_cmpr[i]);
00232 err = 1;
00233 }
00234 int_cnvrt[i] = tmp;
00235 }
00236 tmp = tmp2 = 1;
00237 for (i = 0; i < PORT_INT; i++)
00238 {
00239 if ( int_cnvrt[i] != ( i + ( nat_lng - PORT_LONG) ) ) tmp = 0;
00240 if ( int_cnvrt[i] != (PORT_INT - i - 1 )) tmp2 = 0;
00241 }
00242 if ( tmp ) int_order = ENDIAN_BIG;
00243 else if ( tmp2 ) int_order = ENDIAN_LITTLE;
00244 else int_order = ENDIAN_OTHER;
00245
00246
00247 u.s = SHORT_TEST;
00248 for (i = 0; i < PORT_SHORT; i++)
00249 {
00250 tmp = find_offset (u.c, shrt_cmpr[i], nat_shrt);
00251 if (-1 == tmp)
00252 {
00253 fprintf (stderr, "ERROR, could not find '%x' in shrt\n", shrt_cmpr[i]);
00254 err = 1;
00255 }
00256 shrt_cnvrt[i] = tmp;
00257 }
00258 tmp = tmp2 = 1;
00259 for (i = 0; i < PORT_SHORT; i++)
00260 {
00261 if ( shrt_cnvrt[i] != ( i + ( nat_shrt - PORT_SHORT) ) ) tmp = 0;
00262 if ( shrt_cnvrt[i] != (PORT_SHORT - i - 1 )) tmp2 = 0;
00263 }
00264 if ( tmp ) shrt_order = ENDIAN_BIG;
00265 else if ( tmp2 ) shrt_order = ENDIAN_LITTLE;
00266 else shrt_order = ENDIAN_OTHER;
00267
00268 printf ("\n/* Native machine byte orders */\n");
00269 printf ("#define DOUBLE_ORDER %d\n", dbl_order);
00270 printf ("#define FLOAT_ORDER %d\n", flt_order);
00271 printf ("#define LONG_ORDER %d\n", lng_order);
00272 printf ("#define INT_ORDER %d\n", int_order);
00273 printf ("#define SHORT_ORDER %d\n", shrt_order);
00274
00275 printf ("\n\n/* Translation matrices from big endian to native */\n");
00276 dumpflags ();
00277
00278 return (err);
00279 }
00280
00281
00282
00283
00284
00285
00286 static int
00287 find_offset (unsigned char *basis, unsigned char search_value, int size)
00288 {
00289 register int i;
00290
00291 for (i = 0; i < size; i++)
00292 if (basis[i] == search_value)
00293 return (i);
00294
00295 return (-1);
00296 }
00297
00298
00299 static int dumpflags (void)
00300 {
00301 int i;
00302
00303 fprintf (stdout, "\n/* Double format: */\nstatic int dbl_cnvrt[] = {");
00304 i = 0;
00305 while (i < nat_dbl)
00306 {
00307 fprintf (stdout, "%d", dbl_cnvrt[i]);
00308 if (++i < nat_dbl)
00309 fprintf (stdout, ", ");
00310 }
00311 fprintf (stdout, "};\n\n");
00312
00313 fprintf (stdout, "/* Float format : */\nstatic int flt_cnvrt[] = {");
00314 i = 0;
00315 while (i < nat_flt)
00316 {
00317 fprintf (stdout, "%d", flt_cnvrt[i]);
00318 if (++i < nat_flt)
00319 fprintf (stdout, ", ");
00320 }
00321 fprintf (stdout, "};\n\n");
00322
00323 fprintf (stdout, "/* Long format : */\nstatic int lng_cnvrt[] = {");
00324 i = 0;
00325 while (i < nat_lng)
00326 {
00327 fprintf (stdout, "%d", lng_cnvrt[i]);
00328 if (++i < nat_lng)
00329 fprintf (stdout, ", ");
00330 }
00331 fprintf (stdout, "};\n\n");
00332
00333 fprintf (stdout, "/* Int format : */\nstatic int int_cnvrt[] = {");
00334 i = 0;
00335 while (i < nat_int)
00336 {
00337 fprintf (stdout, "%d", int_cnvrt[i]);
00338 if (++i < nat_int)
00339 fprintf (stdout, ", ");
00340 }
00341 fprintf (stdout, "};\n\n");
00342
00343 fprintf (stdout, "/* Short format : */\nstatic int shrt_cnvrt[] = {");
00344 i = 0;
00345 while (i < nat_shrt)
00346 {
00347 fprintf (stdout, "%d", shrt_cnvrt[i]);
00348 if (++i < nat_shrt)
00349 fprintf (stdout, ", ");
00350 }
00351 fprintf (stdout, "};\n\n");
00352
00353 return 0;
00354 }
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393