port_test.c 12 KB

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