port_test.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. ****************************************************************************
  3. *
  4. * MODULE: Vector library
  5. *
  6. * AUTHOR(S): Original author CERL, probably Dave Gerdes.
  7. * Update to GRASS 5.7 Radim Blazek.
  8. *
  9. * PURPOSE: Lower level functions for reading/writing/manipulating vectors.
  10. *
  11. * COPYRIGHT: (C) 2001 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General Public
  14. * License (>=v2). Read the file COPYING that comes with GRASS
  15. * for details.
  16. *
  17. *****************************************************************************/
  18. #include <stdio.h>
  19. #include <grass/Vect.h>
  20. /*
  21. ** Written by Dave Gerdes 9/1988
  22. ** US Army Construction Engineering Research Lab
  23. */
  24. /*
  25. **
  26. ** This code is a quick hack to allow the writing of portable
  27. ** binary data files.
  28. ** The approach is to take known values and compare them against
  29. ** the current machine's internal representation. A cross reference
  30. ** table is then built, and then all file reads and writes must go through
  31. ** through these routines to correct the numbers if need be.
  32. **
  33. ** As long as the byte switching is symetrical, the conversion routines
  34. ** will work both directions.
  35. ** The integer test patterns are quite simple, and their choice was
  36. ** arbitrary, but the float and double valued were more critical.
  37. ** I did not have a specification for IEEE to go by, so it is possible
  38. ** that I have missed something. My criteria were:
  39. **
  40. ** First, true IEEE numbers had to be chosen to avoid getting an FPE.
  41. ** Second, every byte in the test pattern had to be unique. And
  42. ** finally, the number had to not be sensitive to rounding by the
  43. ** specific hardware implementation.
  44. **
  45. ** By experimentation it was found that the number 1.3333 met
  46. ** all these criteria for both floats and doubles
  47. ** See the discourse at the end of this file for more information
  48. **
  49. **
  50. */
  51. #define TEST_PATTERN 1.3333
  52. #define LONG_TEST 0x01020304
  53. #define INT_TEST 0x01020304
  54. #define SHORT_TEST 0x0102
  55. union type_conv
  56. {
  57. double d;
  58. float f;
  59. long l;
  60. int i;
  61. short s;
  62. unsigned char c[PORT_DOUBLE];
  63. };
  64. static union type_conv u;
  65. /* dbl_cmpr holds the bytes of an IEEE representation of TEST_PATTERN */
  66. static unsigned char dbl_cmpr[] =
  67. {0x3f, 0xf5, 0x55, 0x32, 0x61, 0x7c, 0x1b, 0xda};
  68. /* flt_cmpr holds the bytes of an IEEE representation of TEST_PATTERN */
  69. static unsigned char flt_cmpr[] =
  70. {0x3f, 0xaa, 0xa9, 0x93};
  71. static unsigned char lng_cmpr[] =
  72. {0x01, 0x02, 0x03, 0x04};
  73. static unsigned char int_cmpr[] =
  74. {0x01, 0x02, 0x03, 0x04};
  75. static unsigned char shrt_cmpr[] =
  76. {0x01, 0x02};
  77. static char dbl_cnvrt[sizeof (double)];
  78. static char flt_cnvrt[sizeof (float)];
  79. static char lng_cnvrt[sizeof (long)];
  80. static char int_cnvrt[sizeof (int)];
  81. static char shrt_cnvrt[sizeof (short)];
  82. static int nat_dbl, nat_flt, nat_lng, nat_int, nat_shrt, nat_char;
  83. /* function prototypes */
  84. static int find_offset (unsigned char *, unsigned char, int);
  85. static int dumpflags (void);
  86. int
  87. main (int argc, char **argv)
  88. {
  89. register int i;
  90. int tmp, tmp2;
  91. int err = 0;
  92. int dbl_order, flt_order, lng_order, int_order, shrt_order;
  93. /* Find native sizes */
  94. printf ("\n/* Native machine sizes */\n");
  95. printf ("#define NATIVE_DOUBLE %d\n", (nat_dbl = sizeof (double)));
  96. printf ("#define NATIVE_FLOAT %d\n", (nat_flt = sizeof (float)));
  97. printf ("#define NATIVE_LONG %d\n", (nat_lng = sizeof (long)));
  98. printf ("#define NATIVE_INT %d\n", (nat_int = sizeof (int)));
  99. printf ("#define NATIVE_SHORT %d\n", (nat_shrt = sizeof (short)));
  100. printf ("#define NATIVE_CHAR %d\n", (nat_char = sizeof (char)));
  101. /* Following code checks only if all assumptions are fullfilled */
  102. /* Check sizes */
  103. if (nat_dbl != PORT_DOUBLE)
  104. {
  105. fprintf (stderr, "ERROR, sizeof (double) != %d\n", PORT_DOUBLE);
  106. err = 1;
  107. }
  108. if (nat_flt != PORT_FLOAT)
  109. {
  110. fprintf (stderr, "ERROR, sizeof (float) != %d\n", PORT_DOUBLE);
  111. err = 1;
  112. }
  113. if (nat_lng < PORT_LONG)
  114. {
  115. fprintf (stderr, "ERROR, sizeof (long) < %d\n", PORT_LONG);
  116. err = 1;
  117. }
  118. if (nat_int < PORT_INT)
  119. {
  120. fprintf (stderr, "ERROR, sizeof (int) < %d\n", PORT_INT);
  121. err = 1;
  122. }
  123. if (nat_shrt < PORT_SHORT)
  124. {
  125. fprintf (stderr, "ERROR, sizeof (short) < %d\n", PORT_SHORT);
  126. err = 1;
  127. }
  128. if (nat_char != PORT_CHAR)
  129. {
  130. fprintf (stderr, "ERROR, sizeof (char) != %d\n", PORT_CHAR);
  131. err = 1;
  132. }
  133. /* Find for each byte in big endian test pattern (*_cmpr)
  134. * offset of corresponding byte in machine native order.
  135. * Look if native byte order is little or big or some other (pdp)
  136. * endian.
  137. */
  138. /* Find double order */
  139. u.d = TEST_PATTERN;
  140. for (i = 0; i < PORT_DOUBLE; i++)
  141. {
  142. tmp = find_offset (u.c, dbl_cmpr[i], PORT_DOUBLE);
  143. if (-1 == tmp)
  144. {
  145. fprintf (stderr, "ERROR, could not find '%x' in double\n", dbl_cmpr[i]);
  146. err = 1;
  147. }
  148. dbl_cnvrt[i] = tmp;
  149. }
  150. tmp = tmp2 = 1;
  151. for (i = 0; i < PORT_DOUBLE; i++)
  152. {
  153. if ( dbl_cnvrt[i] != i ) tmp = 0; /* isn't big endian */
  154. if ( dbl_cnvrt[i] != (PORT_DOUBLE - i - 1 )) tmp2 = 0; /* isn't little endian */
  155. }
  156. if ( tmp ) dbl_order = ENDIAN_BIG;
  157. else if ( tmp2 ) dbl_order = ENDIAN_LITTLE;
  158. else dbl_order = ENDIAN_OTHER;
  159. /* Find float order */
  160. u.f = TEST_PATTERN;
  161. for (i = 0; i < PORT_FLOAT; i++)
  162. {
  163. tmp = find_offset (u.c, flt_cmpr[i], PORT_FLOAT);
  164. if (-1 == tmp)
  165. {
  166. fprintf (stderr, "ERROR, could not find '%x' in float\n", flt_cmpr[i]);
  167. err = 1;
  168. }
  169. flt_cnvrt[i] = tmp;
  170. }
  171. tmp = tmp2 = 1;
  172. for (i = 0; i < PORT_FLOAT; i++)
  173. {
  174. if ( flt_cnvrt[i] != i ) tmp = 0;
  175. if ( flt_cnvrt[i] != (PORT_FLOAT - i - 1 )) tmp2 = 0;
  176. }
  177. if ( tmp ) flt_order = ENDIAN_BIG;
  178. else if ( tmp2 ) flt_order = ENDIAN_LITTLE;
  179. else flt_order = ENDIAN_OTHER;
  180. /* Find long order */
  181. u.l = LONG_TEST;
  182. for (i = 0; i < PORT_LONG; i++)
  183. {
  184. tmp = find_offset (u.c, lng_cmpr[i], nat_lng);
  185. if (-1 == tmp)
  186. {
  187. fprintf (stderr, "ERROR, could not find '%x' in long\n", lng_cmpr[i]);
  188. err = 1;
  189. }
  190. lng_cnvrt[i] = tmp;
  191. }
  192. tmp = tmp2 = 1;
  193. for (i = 0; i < PORT_LONG; i++)
  194. {
  195. if ( lng_cnvrt[i] != ( i + ( nat_lng - PORT_LONG) ) ) tmp = 0;
  196. if ( lng_cnvrt[i] != (PORT_LONG - i - 1 )) tmp2 = 0;
  197. }
  198. if ( tmp ) lng_order = ENDIAN_BIG;
  199. else if ( tmp2 ) lng_order = ENDIAN_LITTLE;
  200. else lng_order = ENDIAN_OTHER;
  201. /* Find int order */
  202. u.i = INT_TEST;
  203. for (i = 0; i < PORT_INT; i++)
  204. {
  205. tmp = find_offset (u.c, int_cmpr[i], nat_int);
  206. if (-1 == tmp)
  207. {
  208. fprintf (stderr, "ERROR, could not find '%x' in int\n", int_cmpr[i]);
  209. err = 1;
  210. }
  211. int_cnvrt[i] = tmp;
  212. }
  213. tmp = tmp2 = 1;
  214. for (i = 0; i < PORT_INT; i++)
  215. {
  216. if ( int_cnvrt[i] != ( i + ( nat_lng - PORT_LONG) ) ) tmp = 0;
  217. if ( int_cnvrt[i] != (PORT_INT - i - 1 )) tmp2 = 0;
  218. }
  219. if ( tmp ) int_order = ENDIAN_BIG;
  220. else if ( tmp2 ) int_order = ENDIAN_LITTLE;
  221. else int_order = ENDIAN_OTHER;
  222. /* Find short order */
  223. u.s = SHORT_TEST;
  224. for (i = 0; i < PORT_SHORT; i++)
  225. {
  226. tmp = find_offset (u.c, shrt_cmpr[i], nat_shrt);
  227. if (-1 == tmp)
  228. {
  229. fprintf (stderr, "ERROR, could not find '%x' in shrt\n", shrt_cmpr[i]);
  230. err = 1;
  231. }
  232. shrt_cnvrt[i] = tmp;
  233. }
  234. tmp = tmp2 = 1;
  235. for (i = 0; i < PORT_SHORT; i++)
  236. {
  237. if ( shrt_cnvrt[i] != ( i + ( nat_shrt - PORT_SHORT) ) ) tmp = 0;
  238. if ( shrt_cnvrt[i] != (PORT_SHORT - i - 1 )) tmp2 = 0;
  239. }
  240. if ( tmp ) shrt_order = ENDIAN_BIG;
  241. else if ( tmp2 ) shrt_order = ENDIAN_LITTLE;
  242. else shrt_order = ENDIAN_OTHER;
  243. printf ("\n/* Native machine byte orders */\n");
  244. printf ("#define DOUBLE_ORDER %d\n", dbl_order);
  245. printf ("#define FLOAT_ORDER %d\n", flt_order);
  246. printf ("#define LONG_ORDER %d\n", lng_order);
  247. printf ("#define INT_ORDER %d\n", int_order);
  248. printf ("#define SHORT_ORDER %d\n", shrt_order);
  249. printf ("\n\n/* Translation matrices from big endian to native */\n");
  250. dumpflags ();
  251. return (err);
  252. }
  253. /*
  254. ** match search_value against each char in basis.
  255. ** return offset or -1 if not found
  256. */
  257. static int
  258. find_offset (unsigned char *basis, unsigned char search_value, int size)
  259. {
  260. register int i;
  261. for (i = 0; i < size; i++)
  262. if (basis[i] == search_value)
  263. return (i);
  264. return (-1);
  265. }
  266. static int dumpflags (void)
  267. {
  268. int i;
  269. fprintf (stdout, "\n/* Double format: */\nstatic int dbl_cnvrt[] = {");
  270. i = 0;
  271. while (i < nat_dbl)
  272. {
  273. fprintf (stdout, "%d", dbl_cnvrt[i]);
  274. if (++i < nat_dbl)
  275. fprintf (stdout, ", ");
  276. }
  277. fprintf (stdout, "};\n\n");
  278. fprintf (stdout, "/* Float format : */\nstatic int flt_cnvrt[] = {");
  279. i = 0;
  280. while (i < nat_flt)
  281. {
  282. fprintf (stdout, "%d", flt_cnvrt[i]);
  283. if (++i < nat_flt)
  284. fprintf (stdout, ", ");
  285. }
  286. fprintf (stdout, "};\n\n");
  287. fprintf (stdout, "/* Long format : */\nstatic int lng_cnvrt[] = {");
  288. i = 0;
  289. while (i < nat_lng)
  290. {
  291. fprintf (stdout, "%d", lng_cnvrt[i]);
  292. if (++i < nat_lng)
  293. fprintf (stdout, ", ");
  294. }
  295. fprintf (stdout, "};\n\n");
  296. fprintf (stdout, "/* Int format : */\nstatic int int_cnvrt[] = {");
  297. i = 0;
  298. while (i < nat_int)
  299. {
  300. fprintf (stdout, "%d", int_cnvrt[i]);
  301. if (++i < nat_int)
  302. fprintf (stdout, ", ");
  303. }
  304. fprintf (stdout, "};\n\n");
  305. fprintf (stdout, "/* Short format : */\nstatic int shrt_cnvrt[] = {");
  306. i = 0;
  307. while (i < nat_shrt)
  308. {
  309. fprintf (stdout, "%d", shrt_cnvrt[i]);
  310. if (++i < nat_shrt)
  311. fprintf (stdout, ", ");
  312. }
  313. fprintf (stdout, "};\n\n");
  314. return 0;
  315. }
  316. /*
  317. The 3.0 dig, and dig_plus files are inherently non-portable. This
  318. can be seen in moving files between a SUN 386i and other SUN machines.
  319. The recommended way to transport files was always to convert to ASCII
  320. (b.a.vect) and copy the ASCII files: dig_ascii and dig_att to the
  321. destination machine.
  322. The problem lies in the way that different architectures internally
  323. represent data. If a number is internally store as 0x01020304 on
  324. a 680x0 family machine, the same number will be stored as
  325. 0x04030201 on an 80386 class machine.
  326. The CERL port of GRASS to the Compaq 386 already has code to deal
  327. with this incompatibility. This code converts all files that are written
  328. out to conform to the 680x0 standard. These binary files can then be
  329. shared between machines without conversion.
  330. This code is designed to work with the majority of computers in use
  331. today that fit the following requirements:
  332. byte == 8 bits
  333. int == 4 bytes
  334. long == 4 bytes
  335. double == IEEE standard 64 bit
  336. float == IEEE standard 32 bit
  337. bytes can be swapped around in any reasonable way, but bits within each
  338. byte must be maintained in normal high to low ordering: 76543210
  339. If this ability is desired on a SUN 386i, for example, you simply
  340. define the compiler flag CERL_PORTABLE in the src/CMD/makehead file
  341. and recompile all of the mapdev programs.
  342. Binary DLG files are NOT supported by this code, and will continue to
  343. be non-portable between different architectures.
  344. -dave gerdes
  345. */