PageRenderTime 55ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/other/netcdf_write_matrix/src/libsrc/ncx.m4

http://github.com/jbeezley/wrf-fire
m4 | 2716 lines | 2157 code | 288 blank | 271 comment | 0 complexity | 5326e5c6ac6b0862dbe3c9b68a3419a8 MD5 | raw file
Possible License(s): AGPL-1.0

Large files files are truncated, but you can click here to view the full file

  1. dnl This is m4 source.
  2. dnl Process using m4 to produce 'C' language file.
  3. dnl
  4. dnl If you see this line, you can ignore the next one.
  5. /* Do not edit this file. It is produced from the corresponding .m4 source */
  6. dnl
  7. /*
  8. * Copyright 1996, University Corporation for Atmospheric Research
  9. * See netcdf/COPYRIGHT file for copying and redistribution conditions.
  10. *
  11. * This file contains some routines derived from code
  12. * which is copyrighted by Sun Microsystems, Inc.
  13. * The "#ifdef vax" versions of
  14. * ncx_put_float_float()
  15. * ncx_get_float_float()
  16. * ncx_put_double_double()
  17. * ncx_get_double_double()
  18. * ncx_putn_float_float()
  19. * ncx_getn_float_float()
  20. * ncx_putn_double_double()
  21. * ncx_getn_double_double()
  22. * are derived from xdr_float() and xdr_double() routines
  23. * in the freely available, copyrighted Sun RPCSRC 3.9
  24. * distribution, xdr_float.c.
  25. * Our "value added" is that these are always memory to memory,
  26. * they handle IEEE subnormals properly, and their "n" versions
  27. * operate speedily on arrays.
  28. */
  29. /* $Id: ncx.m4,v 2.47 2004/09/30 18:47:12 russ Exp $ */
  30. /*
  31. * An external data representation interface.
  32. */
  33. #include "ncx.h"
  34. #include <string.h>
  35. #include <limits.h>
  36. /* alias poorly named limits.h macros */
  37. #define SHORT_MAX SHRT_MAX
  38. #define SHORT_MIN SHRT_MIN
  39. #define USHORT_MAX USHRT_MAX
  40. #include <float.h>
  41. #ifndef FLT_MAX /* This POSIX macro missing on some systems */
  42. # ifndef NO_IEEE_FLOAT
  43. # define FLT_MAX 3.40282347e+38f
  44. # else
  45. # error "You will need to define FLT_MAX"
  46. # endif
  47. #endif
  48. #include <assert.h>
  49. /*
  50. * If the machine's float domain is "smaller" than the external one
  51. * use the machine domain
  52. */
  53. #if defined(FLT_MAX_EXP) && FLT_MAX_EXP < 128 /* 128 is X_FLT_MAX_EXP */
  54. #undef X_FLOAT_MAX
  55. # define X_FLOAT_MAX FLT_MAX
  56. #undef X_FLOAT_MIN
  57. # define X_FLOAT_MIN (-X_FLOAT_MAX)
  58. #endif
  59. #if _SX /* NEC SUPER UX */
  60. #if _INT64
  61. #undef INT_MAX /* workaround cpp bug */
  62. #define INT_MAX X_INT_MAX
  63. #undef INT_MIN /* workaround cpp bug */
  64. #define INT_MIN X_INT_MIN
  65. #undef LONG_MAX /* workaround cpp bug */
  66. #define LONG_MAX X_INT_MAX
  67. #undef LONG_MIN /* workaround cpp bug */
  68. #define LONG_MIN X_INT_MIN
  69. #elif _LONG64
  70. #undef LONG_MAX /* workaround cpp bug */
  71. #define LONG_MAX 4294967295L
  72. #undef LONG_MIN /* workaround cpp bug */
  73. #define LONG_MIN -4294967295L
  74. #endif
  75. #endif /* _SX */
  76. static const char nada[X_ALIGN] = {0, 0, 0, 0};
  77. #ifndef WORDS_BIGENDIAN
  78. /* LITTLE_ENDIAN: DEC and intel */
  79. /*
  80. * Routines to convert to BIGENDIAN.
  81. * Optimize the swapn?b() and swap?b() routines aggressivly.
  82. */
  83. #define SWAP2(a) ( (((a) & 0xff) << 8) | \
  84. (((a) >> 8) & 0xff) )
  85. #define SWAP4(a) ( ((a) << 24) | \
  86. (((a) << 8) & 0x00ff0000) | \
  87. (((a) >> 8) & 0x0000ff00) | \
  88. (((a) >> 24) & 0x000000ff) )
  89. static void
  90. swapn2b(void *dst, const void *src, size_t nn)
  91. {
  92. char *op = dst;
  93. const char *ip = src;
  94. while(nn-- != 0)
  95. {
  96. *op++ = *(++ip);
  97. *op++ = *(ip++ -1);
  98. }
  99. }
  100. # ifndef vax
  101. static void
  102. swap4b(void *dst, const void *src)
  103. {
  104. char *op = dst;
  105. const char *ip = src;
  106. op[0] = ip[3];
  107. op[1] = ip[2];
  108. op[2] = ip[1];
  109. op[3] = ip[0];
  110. }
  111. # endif /* !vax */
  112. static void
  113. swapn4b(void *dst, const void *src, size_t nn)
  114. {
  115. char *op = dst;
  116. const char *ip = src;
  117. while(nn-- != 0)
  118. {
  119. op[0] = ip[3];
  120. op[1] = ip[2];
  121. op[2] = ip[1];
  122. op[3] = ip[0];
  123. op += 4;
  124. ip += 4;
  125. }
  126. }
  127. # ifndef vax
  128. static void
  129. swap8b(void *dst, const void *src)
  130. {
  131. char *op = dst;
  132. const char *ip = src;
  133. op[0] = ip[7];
  134. op[1] = ip[6];
  135. op[2] = ip[5];
  136. op[3] = ip[4];
  137. op[4] = ip[3];
  138. op[5] = ip[2];
  139. op[6] = ip[1];
  140. op[7] = ip[0];
  141. }
  142. # endif /* !vax */
  143. # ifndef vax
  144. static void
  145. swapn8b(void *dst, const void *src, size_t nn)
  146. {
  147. char *op = dst;
  148. const char *ip = src;
  149. while(nn-- != 0)
  150. {
  151. op[0] = ip[7];
  152. op[1] = ip[6];
  153. op[2] = ip[5];
  154. op[3] = ip[4];
  155. op[4] = ip[3];
  156. op[5] = ip[2];
  157. op[6] = ip[1];
  158. op[7] = ip[0];
  159. op += 8;
  160. ip += 8;
  161. }
  162. }
  163. # endif /* !vax */
  164. #endif /* LITTLE_ENDIAN */
  165. /*
  166. * Primitive numeric conversion functions.
  167. */
  168. /* x_schar */
  169. /* We don't implement and x_schar primitives. */
  170. /* x_short */
  171. #if SHORT_MAX == X_SHORT_MAX
  172. typedef short ix_short;
  173. #define SIZEOF_IX_SHORT SIZEOF_SHORT
  174. #define IX_SHORT_MAX SHORT_MAX
  175. #elif INT_MAX >= X_SHORT_MAX
  176. typedef int ix_short;
  177. #define SIZEOF_IX_SHORT SIZEOF_INT
  178. #define IX_SHORT_MAX INT_MAX
  179. #elif LONG_MAX >= X_SHORT_MAX
  180. typedef long ix_short;
  181. #define SIZEOF_IX_SHORT SIZEOF_LONG
  182. #define IX_SHORT_MAX LONG_MAX
  183. #else
  184. #error "ix_short implementation"
  185. #endif
  186. static void
  187. get_ix_short(const void *xp, ix_short *ip)
  188. {
  189. const uchar *cp = (const uchar *) xp;
  190. *ip = *cp++ << 8;
  191. #if SIZEOF_IX_SHORT > X_SIZEOF_SHORT
  192. if(*ip & 0x8000)
  193. {
  194. /* extern is negative */
  195. *ip |= (~(0xffff)); /* N.B. Assumes "twos complement" */
  196. }
  197. #endif
  198. *ip |= *cp;
  199. }
  200. static void
  201. put_ix_short(void *xp, const ix_short *ip)
  202. {
  203. uchar *cp = (uchar *) xp;
  204. *cp++ = (*ip) >> 8;
  205. *cp = (*ip) & 0xff;
  206. }
  207. int
  208. ncx_get_short_schar(const void *xp, schar *ip)
  209. {
  210. ix_short xx;
  211. get_ix_short(xp, &xx);
  212. *ip = xx;
  213. if(xx > SCHAR_MAX || xx < SCHAR_MIN)
  214. return NC_ERANGE;
  215. return ENOERR;
  216. }
  217. int
  218. ncx_get_short_uchar(const void *xp, uchar *ip)
  219. {
  220. ix_short xx;
  221. get_ix_short(xp, &xx);
  222. *ip = xx;
  223. if(xx > UCHAR_MAX || xx < 0)
  224. return NC_ERANGE;
  225. return ENOERR;
  226. }
  227. int
  228. ncx_get_short_short(const void *xp, short *ip)
  229. {
  230. #if SIZEOF_IX_SHORT == SIZEOF_SHORT && IX_SHORT_MAX == SHORT_MAX
  231. get_ix_short(xp, (ix_short *)ip);
  232. return ENOERR;
  233. #else
  234. ix_short xx;
  235. get_ix_short(xp, &xx);
  236. *ip = xx;
  237. # if IX_SHORT_MAX > SHORT_MAX
  238. if(xx > SHORT_MAX || xx < SHORT_MIN)
  239. return NC_ERANGE;
  240. # endif
  241. return ENOERR;
  242. #endif
  243. }
  244. int
  245. ncx_get_short_int(const void *xp, int *ip)
  246. {
  247. #if SIZEOF_IX_SHORT == SIZEOF_INT && IX_SHORT_MAX == INT_MAX
  248. get_ix_short(xp, (ix_short *)ip);
  249. return ENOERR;
  250. #else
  251. ix_short xx;
  252. get_ix_short(xp, &xx);
  253. *ip = xx;
  254. # if IX_SHORT_MAX > INT_MAX
  255. if(xx > INT_MAX || xx < INT_MIN)
  256. return NC_ERANGE;
  257. # endif
  258. return ENOERR;
  259. #endif
  260. }
  261. int
  262. ncx_get_short_long(const void *xp, long *ip)
  263. {
  264. #if SIZEOF_IX_SHORT == SIZEOF_LONG && IX_SHORT_MAX == LONG_MAX
  265. get_ix_short(xp, (ix_short *)ip);
  266. return ENOERR;
  267. #else
  268. /* assert(LONG_MAX >= X_SHORT_MAX); */
  269. ix_short xx;
  270. get_ix_short(xp, &xx);
  271. *ip = xx;
  272. return ENOERR;
  273. #endif
  274. }
  275. int
  276. ncx_get_short_float(const void *xp, float *ip)
  277. {
  278. ix_short xx;
  279. get_ix_short(xp, &xx);
  280. *ip = xx;
  281. #if 0 /* TODO: determine when necessary */
  282. if(xx > FLT_MAX || xx < (-FLT_MAX))
  283. return NC_ERANGE;
  284. #endif
  285. return ENOERR;
  286. }
  287. int
  288. ncx_get_short_double(const void *xp, double *ip)
  289. {
  290. /* assert(DBL_MAX >= X_SHORT_MAX); */
  291. ix_short xx;
  292. get_ix_short(xp, &xx);
  293. *ip = xx;
  294. return ENOERR;
  295. }
  296. int
  297. ncx_put_short_schar(void *xp, const schar *ip)
  298. {
  299. uchar *cp = (uchar *) xp;
  300. if(*ip & 0x80)
  301. *cp++ = 0xff;
  302. else
  303. *cp++ = 0;
  304. *cp = (uchar)*ip;
  305. return ENOERR;
  306. }
  307. int
  308. ncx_put_short_uchar(void *xp, const uchar *ip)
  309. {
  310. uchar *cp = (uchar *) xp;
  311. *cp++ = 0;
  312. *cp = *ip;
  313. return ENOERR;
  314. }
  315. int
  316. ncx_put_short_short(void *xp, const short *ip)
  317. {
  318. #if SIZEOF_IX_SHORT == SIZEOF_SHORT && X_SHORT_MAX == SHORT_MAX
  319. put_ix_short(xp, (const ix_short *)ip);
  320. return ENOERR;
  321. #else
  322. ix_short xx = (ix_short)*ip;
  323. put_ix_short(xp, &xx);
  324. # if X_SHORT_MAX < SHORT_MAX
  325. if(*ip > X_SHORT_MAX || *ip < X_SHORT_MIN)
  326. return NC_ERANGE;
  327. # endif
  328. return ENOERR;
  329. #endif
  330. }
  331. int
  332. ncx_put_short_int(void *xp, const int *ip)
  333. {
  334. #if SIZEOF_IX_SHORT == SIZEOF_INT && X_SHORT_MAX == INT_MAX
  335. put_ix_short(xp, (const ix_short *)ip);
  336. return ENOERR;
  337. #else
  338. ix_short xx = (ix_short)*ip;
  339. put_ix_short(xp, &xx);
  340. # if X_SHORT_MAX < INT_MAX
  341. if(*ip > X_SHORT_MAX || *ip < X_SHORT_MIN)
  342. return NC_ERANGE;
  343. # endif
  344. return ENOERR;
  345. #endif
  346. }
  347. int
  348. ncx_put_short_long(void *xp, const long *ip)
  349. {
  350. #if SIZEOF_IX_SHORT == SIZEOF_LONG && X_SHORT_MAX == LONG_MAX
  351. put_ix_short(xp, (const ix_short *)ip);
  352. return ENOERR;
  353. #else
  354. ix_short xx = (ix_short)*ip;
  355. put_ix_short(xp, &xx);
  356. # if X_SHORT_MAX < LONG_MAX
  357. if(*ip > X_SHORT_MAX || *ip < X_SHORT_MIN)
  358. return NC_ERANGE;
  359. # endif
  360. return ENOERR;
  361. #endif
  362. }
  363. int
  364. ncx_put_short_float(void *xp, const float *ip)
  365. {
  366. ix_short xx = *ip;
  367. put_ix_short(xp, &xx);
  368. if(*ip > X_SHORT_MAX || *ip < X_SHORT_MIN)
  369. return NC_ERANGE;
  370. return ENOERR;
  371. }
  372. int
  373. ncx_put_short_double(void *xp, const double *ip)
  374. {
  375. ix_short xx = *ip;
  376. put_ix_short(xp, &xx);
  377. if(*ip > X_SHORT_MAX || *ip < X_SHORT_MIN)
  378. return NC_ERANGE;
  379. return ENOERR;
  380. }
  381. /* x_int */
  382. #if SHORT_MAX == X_INT_MAX
  383. typedef short ix_int;
  384. #define SIZEOF_IX_INT SIZEOF_SHORT
  385. #define IX_INT_MAX SHORT_MAX
  386. #elif INT_MAX >= X_INT_MAX
  387. typedef int ix_int;
  388. #define SIZEOF_IX_INT SIZEOF_INT
  389. #define IX_INT_MAX INT_MAX
  390. #elif LONG_MAX >= X_INT_MAX
  391. typedef long ix_int;
  392. #define SIZEOF_IX_INT SIZEOF_LONG
  393. #define IX_INT_MAX LONG_MAX
  394. #else
  395. #error "ix_int implementation"
  396. #endif
  397. static void
  398. get_ix_int(const void *xp, ix_int *ip)
  399. {
  400. const uchar *cp = (const uchar *) xp;
  401. *ip = *cp++ << 24;
  402. #if SIZEOF_IX_INT > X_SIZEOF_INT
  403. if(*ip & 0x80000000)
  404. {
  405. /* extern is negative */
  406. *ip |= (~(0xffffffff)); /* N.B. Assumes "twos complement" */
  407. }
  408. #endif
  409. *ip |= (*cp++ << 16);
  410. *ip |= (*cp++ << 8);
  411. *ip |= *cp;
  412. }
  413. static void
  414. put_ix_int(void *xp, const ix_int *ip)
  415. {
  416. uchar *cp = (uchar *) xp;
  417. *cp++ = (*ip) >> 24;
  418. *cp++ = ((*ip) & 0x00ff0000) >> 16;
  419. *cp++ = ((*ip) & 0x0000ff00) >> 8;
  420. *cp = ((*ip) & 0x000000ff);
  421. }
  422. int
  423. ncx_get_int_schar(const void *xp, schar *ip)
  424. {
  425. ix_int xx;
  426. get_ix_int(xp, &xx);
  427. *ip = xx;
  428. if(xx > SCHAR_MAX || xx < SCHAR_MIN)
  429. return NC_ERANGE;
  430. return ENOERR;
  431. }
  432. int
  433. ncx_get_int_uchar(const void *xp, uchar *ip)
  434. {
  435. ix_int xx;
  436. get_ix_int(xp, &xx);
  437. *ip = xx;
  438. if(xx > UCHAR_MAX || xx < 0)
  439. return NC_ERANGE;
  440. return ENOERR;
  441. }
  442. int
  443. ncx_get_int_short(const void *xp, short *ip)
  444. {
  445. #if SIZEOF_IX_INT == SIZEOF_SHORT && IX_INT_MAX == SHORT_MAX
  446. get_ix_int(xp, (ix_int *)ip);
  447. return ENOERR;
  448. #else
  449. ix_int xx;
  450. get_ix_int(xp, &xx);
  451. *ip = xx;
  452. # if IX_INT_MAX > SHORT_MAX
  453. if(xx > SHORT_MAX || xx < SHORT_MIN)
  454. return NC_ERANGE;
  455. # endif
  456. return ENOERR;
  457. #endif
  458. }
  459. int
  460. ncx_get_int_int(const void *xp, int *ip)
  461. {
  462. #if SIZEOF_IX_INT == SIZEOF_INT && IX_INT_MAX == INT_MAX
  463. get_ix_int(xp, (ix_int *)ip);
  464. return ENOERR;
  465. #else
  466. ix_int xx;
  467. get_ix_int(xp, &xx);
  468. *ip = xx;
  469. # if IX_INT_MAX > INT_MAX
  470. if(xx > INT_MAX || xx < INT_MIN)
  471. return NC_ERANGE;
  472. # endif
  473. return ENOERR;
  474. #endif
  475. }
  476. int
  477. ncx_get_int_long(const void *xp, long *ip)
  478. {
  479. #if SIZEOF_IX_INT == SIZEOF_LONG && IX_INT_MAX == LONG_MAX
  480. get_ix_int(xp, (ix_int *)ip);
  481. return ENOERR;
  482. #else
  483. ix_int xx;
  484. get_ix_int(xp, &xx);
  485. *ip = xx;
  486. # if IX_INT_MAX > LONG_MAX /* unlikely */
  487. if(xx > LONG_MAX || xx < LONG_MIN)
  488. return NC_ERANGE;
  489. # endif
  490. return ENOERR;
  491. #endif
  492. }
  493. int
  494. ncx_get_int_float(const void *xp, float *ip)
  495. {
  496. ix_int xx;
  497. get_ix_int(xp, &xx);
  498. *ip = xx;
  499. #if 0 /* TODO: determine when necessary */
  500. if(xx > FLT_MAX || xx < (-FLT_MAX))
  501. return NC_ERANGE;
  502. #endif
  503. return ENOERR;
  504. }
  505. int
  506. ncx_get_int_double(const void *xp, double *ip)
  507. {
  508. /* assert((DBL_MAX >= X_INT_MAX); */
  509. ix_int xx;
  510. get_ix_int(xp, &xx);
  511. *ip = xx;
  512. return ENOERR;
  513. }
  514. int
  515. ncx_put_int_schar(void *xp, const schar *ip)
  516. {
  517. uchar *cp = (uchar *) xp;
  518. if(*ip & 0x80)
  519. {
  520. *cp++ = 0xff;
  521. *cp++ = 0xff;
  522. *cp++ = 0xff;
  523. }
  524. else
  525. {
  526. *cp++ = 0x00;
  527. *cp++ = 0x00;
  528. *cp++ = 0x00;
  529. }
  530. *cp = (uchar)*ip;
  531. return ENOERR;
  532. }
  533. int
  534. ncx_put_int_uchar(void *xp, const uchar *ip)
  535. {
  536. uchar *cp = (uchar *) xp;
  537. *cp++ = 0x00;
  538. *cp++ = 0x00;
  539. *cp++ = 0x00;
  540. *cp = *ip;
  541. return ENOERR;
  542. }
  543. int
  544. ncx_put_int_short(void *xp, const short *ip)
  545. {
  546. #if SIZEOF_IX_INT == SIZEOF_SHORT && IX_INT_MAX == SHORT_MAX
  547. put_ix_int(xp, (ix_int *)ip);
  548. return ENOERR;
  549. #else
  550. ix_int xx = (ix_int)(*ip);
  551. put_ix_int(xp, &xx);
  552. # if IX_INT_MAX < SHORT_MAX
  553. if(*ip > X_INT_MAX || *ip < X_INT_MIN)
  554. return NC_ERANGE;
  555. # endif
  556. return ENOERR;
  557. #endif
  558. }
  559. int
  560. ncx_put_int_int(void *xp, const int *ip)
  561. {
  562. #if SIZEOF_IX_INT == SIZEOF_INT && IX_INT_MAX == INT_MAX
  563. put_ix_int(xp, (ix_int *)ip);
  564. return ENOERR;
  565. #else
  566. ix_int xx = (ix_int)(*ip);
  567. put_ix_int(xp, &xx);
  568. # if IX_INT_MAX < INT_MAX
  569. if(*ip > X_INT_MAX || *ip < X_INT_MIN)
  570. return NC_ERANGE;
  571. # endif
  572. return ENOERR;
  573. #endif
  574. }
  575. int
  576. ncx_put_int_long(void *xp, const long *ip)
  577. {
  578. #if SIZEOF_IX_INT == SIZEOF_LONG && IX_INT_MAX == LONG_MAX
  579. put_ix_int(xp, (ix_int *)ip);
  580. return ENOERR;
  581. #else
  582. ix_int xx = (ix_int)(*ip);
  583. put_ix_int(xp, &xx);
  584. # if IX_INT_MAX < LONG_MAX
  585. if(*ip > X_INT_MAX || *ip < X_INT_MIN)
  586. return NC_ERANGE;
  587. # endif
  588. return ENOERR;
  589. #endif
  590. }
  591. int
  592. ncx_put_int_float(void *xp, const float *ip)
  593. {
  594. ix_int xx = (ix_int)(*ip);
  595. put_ix_int(xp, &xx);
  596. if(*ip > (double)X_INT_MAX || *ip < (double)X_INT_MIN)
  597. return NC_ERANGE;
  598. return ENOERR;
  599. }
  600. int
  601. ncx_put_int_double(void *xp, const double *ip)
  602. {
  603. ix_int xx = (ix_int)(*ip);
  604. put_ix_int(xp, &xx);
  605. if(*ip > X_INT_MAX || *ip < X_INT_MIN)
  606. return NC_ERANGE;
  607. return ENOERR;
  608. }
  609. /* x_float */
  610. #if X_SIZEOF_FLOAT == SIZEOF_FLOAT && !defined(NO_IEEE_FLOAT)
  611. static void
  612. get_ix_float(const void *xp, float *ip)
  613. {
  614. #ifdef WORDS_BIGENDIAN
  615. (void) memcpy(ip, xp, sizeof(float));
  616. #else
  617. swap4b(ip, xp);
  618. #endif
  619. }
  620. static void
  621. put_ix_float(void *xp, const float *ip)
  622. {
  623. #ifdef WORDS_BIGENDIAN
  624. (void) memcpy(xp, ip, X_SIZEOF_FLOAT);
  625. #else
  626. swap4b(xp, ip);
  627. #endif
  628. }
  629. #elif vax
  630. /* What IEEE single precision floating point looks like on a Vax */
  631. struct ieee_single {
  632. unsigned int exp_hi : 7;
  633. unsigned int sign : 1;
  634. unsigned int mant_hi : 7;
  635. unsigned int exp_lo : 1;
  636. unsigned int mant_lo_hi : 8;
  637. unsigned int mant_lo_lo : 8;
  638. };
  639. /* Vax single precision floating point */
  640. struct vax_single {
  641. unsigned int mantissa1 : 7;
  642. unsigned int exp : 8;
  643. unsigned int sign : 1;
  644. unsigned int mantissa2 : 16;
  645. };
  646. #define VAX_SNG_BIAS 0x81
  647. #define IEEE_SNG_BIAS 0x7f
  648. static struct sgl_limits {
  649. struct vax_single s;
  650. struct ieee_single ieee;
  651. } max = {
  652. { 0x7f, 0xff, 0x0, 0xffff }, /* Max Vax */
  653. { 0x7f, 0x0, 0x0, 0x1, 0x0, 0x0 } /* Max IEEE */
  654. };
  655. static struct sgl_limits min = {
  656. { 0x0, 0x0, 0x0, 0x0 }, /* Min Vax */
  657. { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 } /* Min IEEE */
  658. };
  659. dnl dnl dnl
  660. dnl
  661. dnl GET_VAX_DFLOAT_Body(xp) (body for get_ix_float)
  662. dnl
  663. define(`GET_VAX_DFLOAT_Body',dnl
  664. `dnl
  665. struct vax_single *const vsp = (struct vax_single *) ip;
  666. const struct ieee_single *const isp =
  667. (const struct ieee_single *) $1;
  668. unsigned exp = isp->exp_hi << 1 | isp->exp_lo;
  669. switch(exp) {
  670. case 0 :
  671. /* ieee subnormal */
  672. if(isp->mant_hi == min.ieee.mant_hi
  673. && isp->mant_lo_hi == min.ieee.mant_lo_hi
  674. && isp->mant_lo_lo == min.ieee.mant_lo_lo)
  675. {
  676. *vsp = min.s;
  677. }
  678. else
  679. {
  680. unsigned mantissa = (isp->mant_hi << 16)
  681. | isp->mant_lo_hi << 8
  682. | isp->mant_lo_lo;
  683. unsigned tmp = mantissa >> 20;
  684. if(tmp >= 4) {
  685. vsp->exp = 2;
  686. } else if (tmp >= 2) {
  687. vsp->exp = 1;
  688. } else {
  689. *vsp = min.s;
  690. break;
  691. } /* else */
  692. tmp = mantissa - (1 << (20 + vsp->exp ));
  693. tmp <<= 3 - vsp->exp;
  694. vsp->mantissa2 = tmp;
  695. vsp->mantissa1 = (tmp >> 16);
  696. }
  697. break;
  698. case 0xfe :
  699. case 0xff :
  700. *vsp = max.s;
  701. break;
  702. default :
  703. vsp->exp = exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
  704. vsp->mantissa2 = isp->mant_lo_hi << 8 | isp->mant_lo_lo;
  705. vsp->mantissa1 = isp->mant_hi;
  706. }
  707. vsp->sign = isp->sign;
  708. ')dnl
  709. static void
  710. get_ix_float(const void *xp, float *ip)
  711. {
  712. GET_VAX_DFLOAT_Body(xp)
  713. }
  714. dnl dnl dnl
  715. dnl
  716. dnl PUT_VAX_DFLOAT_Body(xp) (body for get_ix_float)
  717. dnl
  718. define(`PUT_VAX_DFLOAT_Body',dnl
  719. `dnl
  720. const struct vax_single *const vsp =
  721. (const struct vax_single *)ip;
  722. struct ieee_single *const isp = (struct ieee_single *) $1;
  723. switch(vsp->exp){
  724. case 0 :
  725. /* all vax float with zero exponent map to zero */
  726. *isp = min.ieee;
  727. break;
  728. case 2 :
  729. case 1 :
  730. {
  731. /* These will map to subnormals */
  732. unsigned mantissa = (vsp->mantissa1 << 16)
  733. | vsp->mantissa2;
  734. mantissa >>= 3 - vsp->exp;
  735. mantissa += (1 << (20 + vsp->exp));
  736. isp->mant_lo_lo = mantissa;
  737. isp->mant_lo_hi = mantissa >> 8;
  738. isp->mant_hi = mantissa >> 16;
  739. isp->exp_lo = 0;
  740. isp->exp_hi = 0;
  741. }
  742. break;
  743. case 0xff : /* max.s.exp */
  744. if( vsp->mantissa2 == max.s.mantissa2
  745. && vsp->mantissa1 == max.s.mantissa1)
  746. {
  747. /* map largest vax float to ieee infinity */
  748. *isp = max.ieee;
  749. break;
  750. } /* else, fall thru */
  751. default :
  752. {
  753. unsigned exp = vsp->exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
  754. isp->exp_hi = exp >> 1;
  755. isp->exp_lo = exp;
  756. isp->mant_lo_lo = vsp->mantissa2;
  757. isp->mant_lo_hi = vsp->mantissa2 >> 8;
  758. isp->mant_hi = vsp->mantissa1;
  759. }
  760. }
  761. isp->sign = vsp->sign;
  762. ')dnl
  763. static void
  764. put_ix_float(void *xp, const float *ip)
  765. {
  766. PUT_VAX_DFLOAT_Body(xp)
  767. }
  768. /* vax */
  769. #elif defined(_CRAY)
  770. /*
  771. * Return the number of bytes until the next "word" boundary
  772. * N.B. This is based on the very wierd YMP address structure,
  773. * which puts the address within a word in the leftmost 3 bits
  774. * of the address.
  775. */
  776. static size_t
  777. word_align(const void *vp)
  778. {
  779. const size_t rem = ((size_t)vp >> (64 - 3)) & 0x7;
  780. return (rem != 0);
  781. }
  782. struct ieee_single_hi {
  783. unsigned int sign : 1;
  784. unsigned int exp : 8;
  785. unsigned int mant :23;
  786. unsigned int pad :32;
  787. };
  788. typedef struct ieee_single_hi ieee_single_hi;
  789. struct ieee_single_lo {
  790. unsigned int pad :32;
  791. unsigned int sign : 1;
  792. unsigned int exp : 8;
  793. unsigned int mant :23;
  794. };
  795. typedef struct ieee_single_lo ieee_single_lo;
  796. static const int ieee_single_bias = 0x7f;
  797. struct ieee_double {
  798. unsigned int sign : 1;
  799. unsigned int exp :11;
  800. unsigned int mant :52;
  801. };
  802. typedef struct ieee_double ieee_double;
  803. static const int ieee_double_bias = 0x3ff;
  804. #if defined(NO_IEEE_FLOAT)
  805. struct cray_single {
  806. unsigned int sign : 1;
  807. unsigned int exp :15;
  808. unsigned int mant :48;
  809. };
  810. typedef struct cray_single cray_single;
  811. static const int cs_ieis_bias = 0x4000 - 0x7f;
  812. static const int cs_id_bias = 0x4000 - 0x3ff;
  813. dnl dnl dnl
  814. dnl
  815. dnl GET_IX_FLOAT_Body (body for get_ix_float)
  816. dnl
  817. define(`GET_IX_FLOAT_Body',dnl
  818. `dnl
  819. cray_single *csp = (cray_single *) ip;
  820. if(isp->exp == 0)
  821. {
  822. /* ieee subnormal */
  823. *ip = (double)isp->mant;
  824. if(isp->mant != 0)
  825. {
  826. csp->exp -= (ieee_single_bias + 22);
  827. }
  828. }
  829. else
  830. {
  831. csp->exp = isp->exp + cs_ieis_bias + 1;
  832. csp->mant = isp->mant << (48 - 1 - 23);
  833. csp->mant |= (1 << (48 - 1));
  834. }
  835. csp->sign = isp->sign;
  836. ')dnl
  837. dnl dnl dnl
  838. dnl
  839. dnl PUT_IX_FLOAT_Body (body for put_ix_float)
  840. dnl
  841. define(`PUT_IX_FLOAT_Body',dnl
  842. `dnl
  843. const cray_single *csp = (const cray_single *) ip;
  844. int ieee_exp = csp->exp - cs_ieis_bias -1;
  845. isp->sign = csp->sign;
  846. if(ieee_exp >= 0xff)
  847. {
  848. /* NC_ERANGE => ieee Inf */
  849. isp->exp = 0xff;
  850. isp->mant = 0x0;
  851. }
  852. else if(ieee_exp > 0)
  853. {
  854. /* normal ieee representation */
  855. isp->exp = ieee_exp;
  856. /* assumes cray rep is in normal form */
  857. assert(csp->mant & 0x800000000000);
  858. isp->mant = (((csp->mant << 1) &
  859. 0xffffffffffff) >> (48 - 23));
  860. }
  861. else if(ieee_exp > -23)
  862. {
  863. /* ieee subnormal, right shift */
  864. const int rshift = (48 - 23 - ieee_exp);
  865. isp->mant = csp->mant >> rshift;
  866. #if 0
  867. if(csp->mant & (1 << (rshift -1)))
  868. {
  869. /* round up */
  870. isp->mant++;
  871. }
  872. #endif
  873. isp->exp = 0;
  874. }
  875. else
  876. {
  877. /* smaller than ieee can represent */
  878. isp->exp = 0;
  879. isp->mant = 0;
  880. }
  881. ')dnl
  882. static void
  883. get_ix_float(const void *xp, float *ip)
  884. {
  885. if(word_align(xp) == 0)
  886. {
  887. const ieee_single_hi *isp = (const ieee_single_hi *) xp;
  888. GET_IX_FLOAT_Body
  889. }
  890. else
  891. {
  892. const ieee_single_lo *isp = (const ieee_single_lo *) xp;
  893. GET_IX_FLOAT_Body
  894. }
  895. }
  896. static void
  897. put_ix_float(void *xp, const float *ip)
  898. {
  899. if(word_align(xp) == 0)
  900. {
  901. ieee_single_hi *isp = (ieee_single_hi*)xp;
  902. PUT_IX_FLOAT_Body
  903. }
  904. else
  905. {
  906. ieee_single_lo *isp = (ieee_single_lo*)xp;
  907. PUT_IX_FLOAT_Body
  908. }
  909. }
  910. #else
  911. /* IEEE Cray with only doubles */
  912. static void
  913. get_ix_float(const void *xp, float *ip)
  914. {
  915. ieee_double *idp = (ieee_double *) ip;
  916. if(word_align(xp) == 0)
  917. {
  918. const ieee_single_hi *isp = (const ieee_single_hi *) xp;
  919. if(isp->exp == 0 && isp->mant == 0)
  920. {
  921. idp->exp = 0;
  922. idp->mant = 0;
  923. }
  924. else
  925. {
  926. idp->exp = isp->exp + (ieee_double_bias - ieee_single_bias);
  927. idp->mant = isp->mant << (52 - 23);
  928. }
  929. idp->sign = isp->sign;
  930. }
  931. else
  932. {
  933. const ieee_single_lo *isp = (const ieee_single_lo *) xp;
  934. if(isp->exp == 0 && isp->mant == 0)
  935. {
  936. idp->exp = 0;
  937. idp->mant = 0;
  938. }
  939. else
  940. {
  941. idp->exp = isp->exp + (ieee_double_bias - ieee_single_bias);
  942. idp->mant = isp->mant << (52 - 23);
  943. }
  944. idp->sign = isp->sign;
  945. }
  946. }
  947. static void
  948. put_ix_float(void *xp, const float *ip)
  949. {
  950. const ieee_double *idp = (const ieee_double *) ip;
  951. if(word_align(xp) == 0)
  952. {
  953. ieee_single_hi *isp = (ieee_single_hi*)xp;
  954. if(idp->exp > (ieee_double_bias - ieee_single_bias))
  955. isp->exp = idp->exp - (ieee_double_bias - ieee_single_bias);
  956. else
  957. isp->exp = 0;
  958. isp->mant = idp->mant >> (52 - 23);
  959. isp->sign = idp->sign;
  960. }
  961. else
  962. {
  963. ieee_single_lo *isp = (ieee_single_lo*)xp;
  964. if(idp->exp > (ieee_double_bias - ieee_single_bias))
  965. isp->exp = idp->exp - (ieee_double_bias - ieee_single_bias);
  966. else
  967. isp->exp = 0;
  968. isp->mant = idp->mant >> (52 - 23);
  969. isp->sign = idp->sign;
  970. }
  971. }
  972. #endif
  973. #elif _SX && _FLOAT2
  974. static void
  975. get_ix_float(const void *xp, float *ip)
  976. {
  977. const int ncnv = ie3_fl2(xp, ip, 4, 8, 1);
  978. }
  979. static void
  980. put_ix_float(void *xp, const float *ip)
  981. {
  982. const int ncnv = fl2_ie3(ip, xp, 8, 4, 1);
  983. }
  984. #else
  985. #error "ix_float implementation"
  986. #endif
  987. int
  988. ncx_get_float_schar(const void *xp, schar *ip)
  989. {
  990. float xx;
  991. get_ix_float(xp, &xx);
  992. *ip = (schar) xx;
  993. if(xx > SCHAR_MAX || xx < SCHAR_MIN)
  994. return NC_ERANGE;
  995. return ENOERR;
  996. }
  997. int
  998. ncx_get_float_uchar(const void *xp, uchar *ip)
  999. {
  1000. float xx;
  1001. get_ix_float(xp, &xx);
  1002. *ip = (uchar) xx;
  1003. if(xx > UCHAR_MAX || xx < 0)
  1004. return NC_ERANGE;
  1005. return ENOERR;
  1006. }
  1007. int
  1008. ncx_get_float_short(const void *xp, short *ip)
  1009. {
  1010. float xx;
  1011. get_ix_float(xp, &xx);
  1012. *ip = (short) xx;
  1013. if(xx > SHORT_MAX || xx < SHORT_MIN)
  1014. return NC_ERANGE;
  1015. return ENOERR;
  1016. }
  1017. int
  1018. ncx_get_float_int(const void *xp, int *ip)
  1019. {
  1020. float xx;
  1021. get_ix_float(xp, &xx);
  1022. *ip = (int) xx;
  1023. if(xx > (double)INT_MAX || xx < (double)INT_MIN)
  1024. return NC_ERANGE;
  1025. return ENOERR;
  1026. }
  1027. int
  1028. ncx_get_float_long(const void *xp, long *ip)
  1029. {
  1030. float xx;
  1031. get_ix_float(xp, &xx);
  1032. *ip = (long) xx;
  1033. if(xx > LONG_MAX || xx < LONG_MIN)
  1034. return NC_ERANGE;
  1035. return ENOERR;
  1036. }
  1037. int
  1038. ncx_get_float_float(const void *xp, float *ip)
  1039. {
  1040. /* TODO */
  1041. get_ix_float(xp, ip);
  1042. return ENOERR;
  1043. }
  1044. int
  1045. ncx_get_float_double(const void *xp, double *ip)
  1046. {
  1047. /* TODO */
  1048. float xx;
  1049. get_ix_float(xp, &xx);
  1050. *ip = xx;
  1051. return ENOERR;
  1052. }
  1053. int
  1054. ncx_put_float_schar(void *xp, const schar *ip)
  1055. {
  1056. float xx = (float) *ip;
  1057. put_ix_float(xp, &xx);
  1058. return ENOERR;
  1059. }
  1060. int
  1061. ncx_put_float_uchar(void *xp, const uchar *ip)
  1062. {
  1063. float xx = (float) *ip;
  1064. put_ix_float(xp, &xx);
  1065. return ENOERR;
  1066. }
  1067. int
  1068. ncx_put_float_short(void *xp, const short *ip)
  1069. {
  1070. float xx = (float) *ip;
  1071. put_ix_float(xp, &xx);
  1072. #if 0 /* TODO: figure this out */
  1073. if((float)(*ip) > X_FLOAT_MAX || (float)(*ip) < X_FLOAT_MIN)
  1074. return NC_ERANGE;
  1075. #endif
  1076. return ENOERR;
  1077. }
  1078. int
  1079. ncx_put_float_int(void *xp, const int *ip)
  1080. {
  1081. float xx = (float) *ip;
  1082. put_ix_float(xp, &xx);
  1083. #if 1 /* TODO: figure this out */
  1084. if((float)(*ip) > X_FLOAT_MAX || (float)(*ip) < X_FLOAT_MIN)
  1085. return NC_ERANGE;
  1086. #endif
  1087. return ENOERR;
  1088. }
  1089. int
  1090. ncx_put_float_long(void *xp, const long *ip)
  1091. {
  1092. float xx = (float) *ip;
  1093. put_ix_float(xp, &xx);
  1094. #if 1 /* TODO: figure this out */
  1095. if((float)(*ip) > X_FLOAT_MAX || (float)(*ip) < X_FLOAT_MIN)
  1096. return NC_ERANGE;
  1097. #endif
  1098. return ENOERR;
  1099. }
  1100. int
  1101. ncx_put_float_float(void *xp, const float *ip)
  1102. {
  1103. put_ix_float(xp, ip);
  1104. #ifdef NO_IEEE_FLOAT
  1105. if(*ip > X_FLOAT_MAX || *ip < X_FLOAT_MIN)
  1106. return NC_ERANGE;
  1107. #endif
  1108. return ENOERR;
  1109. }
  1110. int
  1111. ncx_put_float_double(void *xp, const double *ip)
  1112. {
  1113. float xx = (float) *ip;
  1114. put_ix_float(xp, &xx);
  1115. if(*ip > X_FLOAT_MAX || *ip < X_FLOAT_MIN)
  1116. return NC_ERANGE;
  1117. return ENOERR;
  1118. }
  1119. /* x_double */
  1120. #if X_SIZEOF_DOUBLE == SIZEOF_DOUBLE && !defined(NO_IEEE_FLOAT)
  1121. static void
  1122. get_ix_double(const void *xp, double *ip)
  1123. {
  1124. #ifdef WORDS_BIGENDIAN
  1125. (void) memcpy(ip, xp, sizeof(double));
  1126. #else
  1127. swap8b(ip, xp);
  1128. #endif
  1129. }
  1130. static void
  1131. put_ix_double(void *xp, const double *ip)
  1132. {
  1133. #ifdef WORDS_BIGENDIAN
  1134. (void) memcpy(xp, ip, X_SIZEOF_DOUBLE);
  1135. #else
  1136. swap8b(xp, ip);
  1137. #endif
  1138. }
  1139. #elif vax
  1140. /* What IEEE double precision floating point looks like on a Vax */
  1141. struct ieee_double {
  1142. unsigned int exp_hi : 7;
  1143. unsigned int sign : 1;
  1144. unsigned int mant_6 : 4;
  1145. unsigned int exp_lo : 4;
  1146. unsigned int mant_5 : 8;
  1147. unsigned int mant_4 : 8;
  1148. unsigned int mant_lo : 32;
  1149. };
  1150. /* Vax double precision floating point */
  1151. struct vax_double {
  1152. unsigned int mantissa1 : 7;
  1153. unsigned int exp : 8;
  1154. unsigned int sign : 1;
  1155. unsigned int mantissa2 : 16;
  1156. unsigned int mantissa3 : 16;
  1157. unsigned int mantissa4 : 16;
  1158. };
  1159. #define VAX_DBL_BIAS 0x81
  1160. #define IEEE_DBL_BIAS 0x3ff
  1161. #define MASK(nbits) ((1 << nbits) - 1)
  1162. static const struct dbl_limits {
  1163. struct vax_double d;
  1164. struct ieee_double ieee;
  1165. } dbl_limits[2] = {
  1166. {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */
  1167. { 0x7f, 0x0, 0x0, 0xf, 0x0, 0x0, 0x0}}, /* Max IEEE */
  1168. {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */
  1169. { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, /* Min IEEE */
  1170. };
  1171. dnl dnl dnl
  1172. dnl
  1173. dnl GET_VAX_DDOUBLE_Body(xp) (body for get_ix_double)
  1174. dnl
  1175. define(`GET_VAX_DDOUBLE_Body',dnl
  1176. `dnl
  1177. struct vax_double *const vdp =
  1178. (struct vax_double *)ip;
  1179. const struct ieee_double *const idp =
  1180. (const struct ieee_double *) $1;
  1181. {
  1182. const struct dbl_limits *lim;
  1183. int ii;
  1184. for (ii = 0, lim = dbl_limits;
  1185. ii < sizeof(dbl_limits)/sizeof(struct dbl_limits);
  1186. ii++, lim++)
  1187. {
  1188. if ((idp->mant_lo == lim->ieee.mant_lo)
  1189. && (idp->mant_4 == lim->ieee.mant_4)
  1190. && (idp->mant_5 == lim->ieee.mant_5)
  1191. && (idp->mant_6 == lim->ieee.mant_6)
  1192. && (idp->exp_lo == lim->ieee.exp_lo)
  1193. && (idp->exp_hi == lim->ieee.exp_hi)
  1194. )
  1195. {
  1196. *vdp = lim->d;
  1197. goto doneit;
  1198. }
  1199. }
  1200. }
  1201. {
  1202. unsigned exp = idp->exp_hi << 4 | idp->exp_lo;
  1203. vdp->exp = exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
  1204. }
  1205. {
  1206. unsigned mant_hi = ((idp->mant_6 << 16)
  1207. | (idp->mant_5 << 8)
  1208. | idp->mant_4);
  1209. unsigned mant_lo = SWAP4(idp->mant_lo);
  1210. vdp->mantissa1 = (mant_hi >> 13);
  1211. vdp->mantissa2 = ((mant_hi & MASK(13)) << 3)
  1212. | (mant_lo >> 29);
  1213. vdp->mantissa3 = (mant_lo >> 13);
  1214. vdp->mantissa4 = (mant_lo << 3);
  1215. }
  1216. doneit:
  1217. vdp->sign = idp->sign;
  1218. ')dnl
  1219. static void
  1220. get_ix_double(const void *xp, double *ip)
  1221. {
  1222. GET_VAX_DDOUBLE_Body(xp)
  1223. }
  1224. dnl dnl dnl
  1225. dnl
  1226. dnl PUT_VAX_DDOUBLE_Body(xp) (body for put_ix_double)
  1227. dnl
  1228. define(`PUT_VAX_DDOUBLE_Body',dnl
  1229. `dnl
  1230. const struct vax_double *const vdp =
  1231. (const struct vax_double *)ip;
  1232. struct ieee_double *const idp =
  1233. (struct ieee_double *) $1;
  1234. if ((vdp->mantissa4 > (dbl_limits[0].d.mantissa4 - 3)) &&
  1235. (vdp->mantissa3 == dbl_limits[0].d.mantissa3) &&
  1236. (vdp->mantissa2 == dbl_limits[0].d.mantissa2) &&
  1237. (vdp->mantissa1 == dbl_limits[0].d.mantissa1) &&
  1238. (vdp->exp == dbl_limits[0].d.exp))
  1239. {
  1240. *idp = dbl_limits[0].ieee;
  1241. goto shipit;
  1242. }
  1243. if ((vdp->mantissa4 == dbl_limits[1].d.mantissa4) &&
  1244. (vdp->mantissa3 == dbl_limits[1].d.mantissa3) &&
  1245. (vdp->mantissa2 == dbl_limits[1].d.mantissa2) &&
  1246. (vdp->mantissa1 == dbl_limits[1].d.mantissa1) &&
  1247. (vdp->exp == dbl_limits[1].d.exp))
  1248. {
  1249. *idp = dbl_limits[1].ieee;
  1250. goto shipit;
  1251. }
  1252. {
  1253. unsigned exp = vdp->exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
  1254. unsigned mant_lo = ((vdp->mantissa2 & MASK(3)) << 29) |
  1255. (vdp->mantissa3 << 13) |
  1256. ((vdp->mantissa4 >> 3) & MASK(13));
  1257. unsigned mant_hi = (vdp->mantissa1 << 13)
  1258. | (vdp->mantissa2 >> 3);
  1259. if((vdp->mantissa4 & 7) > 4)
  1260. {
  1261. /* round up */
  1262. mant_lo++;
  1263. if(mant_lo == 0)
  1264. {
  1265. mant_hi++;
  1266. if(mant_hi > 0xffffff)
  1267. {
  1268. mant_hi = 0;
  1269. exp++;
  1270. }
  1271. }
  1272. }
  1273. idp->mant_lo = SWAP4(mant_lo);
  1274. idp->mant_6 = mant_hi >> 16;
  1275. idp->mant_5 = (mant_hi & 0xff00) >> 8;
  1276. idp->mant_4 = mant_hi;
  1277. idp->exp_hi = exp >> 4;
  1278. idp->exp_lo = exp;
  1279. }
  1280. shipit:
  1281. idp->sign = vdp->sign;
  1282. ')dnl
  1283. static void
  1284. put_ix_double(void *xp, const double *ip)
  1285. {
  1286. PUT_VAX_DDOUBLE_Body(xp)
  1287. }
  1288. /* vax */
  1289. #elif defined(_CRAY)
  1290. static void
  1291. get_ix_double(const void *xp, double *ip)
  1292. {
  1293. const ieee_double *idp = (const ieee_double *) xp;
  1294. cray_single *csp = (cray_single *) ip;
  1295. if(idp->exp == 0)
  1296. {
  1297. /* ieee subnormal */
  1298. *ip = (double)idp->mant;
  1299. if(idp->mant != 0)
  1300. {
  1301. csp->exp -= (ieee_double_bias + 51);
  1302. }
  1303. }
  1304. else
  1305. {
  1306. csp->exp = idp->exp + cs_id_bias + 1;
  1307. csp->mant = idp->mant >> (52 - 48 + 1);
  1308. csp->mant |= (1 << (48 - 1));
  1309. }
  1310. csp->sign = idp->sign;
  1311. }
  1312. static void
  1313. put_ix_double(void *xp, const double *ip)
  1314. {
  1315. ieee_double *idp = (ieee_double *) xp;
  1316. const cray_single *csp = (const cray_single *) ip;
  1317. int ieee_exp = csp->exp - cs_id_bias -1;
  1318. idp->sign = csp->sign;
  1319. if(ieee_exp >= 0x7ff)
  1320. {
  1321. /* NC_ERANGE => ieee Inf */
  1322. idp->exp = 0x7ff;
  1323. idp->mant = 0x0;
  1324. }
  1325. else if(ieee_exp > 0)
  1326. {
  1327. /* normal ieee representation */
  1328. idp->exp = ieee_exp;
  1329. /* assumes cray rep is in normal form */
  1330. assert(csp->mant & 0x800000000000);
  1331. idp->mant = (((csp->mant << 1) &
  1332. 0xffffffffffff) << (52 - 48));
  1333. }
  1334. else if(ieee_exp >= (-(52 -48)))
  1335. {
  1336. /* ieee subnormal, left shift */
  1337. const int lshift = (52 - 48) + ieee_exp;
  1338. idp->mant = csp->mant << lshift;
  1339. idp->exp = 0;
  1340. }
  1341. else if(ieee_exp >= -52)
  1342. {
  1343. /* ieee subnormal, right shift */
  1344. const int rshift = (- (52 - 48) - ieee_exp);
  1345. idp->mant = csp->mant >> rshift;
  1346. #if 0
  1347. if(csp->mant & (1 << (rshift -1)))
  1348. {
  1349. /* round up */
  1350. idp->mant++;
  1351. }
  1352. #endif
  1353. idp->exp = 0;
  1354. }
  1355. else
  1356. {
  1357. /* smaller than ieee can represent */
  1358. idp->exp = 0;
  1359. idp->mant = 0;
  1360. }
  1361. }
  1362. #elif _SX && _FLOAT2
  1363. static void
  1364. get_ix_double(const void *xp, double *ip)
  1365. {
  1366. const int ncnv = ie3_fl2(xp, ip, 8, 8, 1);
  1367. }
  1368. static void
  1369. put_ix_double(void *xp, const double *ip)
  1370. {
  1371. const int ncnv = fl2_ie3(ip, xp, 8, 8, 1);
  1372. }
  1373. #else
  1374. #error "ix_double implementation"
  1375. #endif
  1376. int
  1377. ncx_get_double_schar(const void *xp, schar *ip)
  1378. {
  1379. double xx;
  1380. get_ix_double(xp, &xx);
  1381. *ip = (schar) xx;
  1382. if(xx > SCHAR_MAX || xx < SCHAR_MIN)
  1383. return NC_ERANGE;
  1384. return ENOERR;
  1385. }
  1386. int
  1387. ncx_get_double_uchar(const void *xp, uchar *ip)
  1388. {
  1389. double xx;
  1390. get_ix_double(xp, &xx);
  1391. *ip = (uchar) xx;
  1392. if(xx > UCHAR_MAX || xx < 0)
  1393. return NC_ERANGE;
  1394. return ENOERR;
  1395. }
  1396. int
  1397. ncx_get_double_short(const void *xp, short *ip)
  1398. {
  1399. double xx;
  1400. get_ix_double(xp, &xx);
  1401. *ip = (short) xx;
  1402. if(xx > SHORT_MAX || xx < SHORT_MIN)
  1403. return NC_ERANGE;
  1404. return ENOERR;
  1405. }
  1406. int
  1407. ncx_get_double_int(const void *xp, int *ip)
  1408. {
  1409. double xx;
  1410. get_ix_double(xp, &xx);
  1411. *ip = (int) xx;
  1412. if(xx > INT_MAX || xx < INT_MIN)
  1413. return NC_ERANGE;
  1414. return ENOERR;
  1415. }
  1416. int
  1417. ncx_get_double_long(const void *xp, long *ip)
  1418. {
  1419. double xx;
  1420. get_ix_double(xp, &xx);
  1421. *ip = (long) xx;
  1422. if(xx > LONG_MAX || xx < LONG_MIN)
  1423. return NC_ERANGE;
  1424. return ENOERR;
  1425. }
  1426. int
  1427. ncx_get_double_float(const void *xp, float *ip)
  1428. {
  1429. double xx;
  1430. get_ix_double(xp, &xx);
  1431. if(xx > FLT_MAX || xx < (-FLT_MAX))
  1432. {
  1433. *ip = FLT_MAX;
  1434. return NC_ERANGE;
  1435. }
  1436. if(xx < (-FLT_MAX))
  1437. {
  1438. *ip = (-FLT_MAX);
  1439. return NC_ERANGE;
  1440. }
  1441. *ip = (float) xx;
  1442. return ENOERR;
  1443. }
  1444. int
  1445. ncx_get_double_double(const void *xp, double *ip)
  1446. {
  1447. /* TODO */
  1448. get_ix_double(xp, ip);
  1449. return ENOERR;
  1450. }
  1451. int
  1452. ncx_put_double_schar(void *xp, const schar *ip)
  1453. {
  1454. double xx = (double) *ip;
  1455. put_ix_double(xp, &xx);
  1456. return ENOERR;
  1457. }
  1458. int
  1459. ncx_put_double_uchar(void *xp, const uchar *ip)
  1460. {
  1461. double xx = (double) *ip;
  1462. put_ix_double(xp, &xx);
  1463. return ENOERR;
  1464. }
  1465. int
  1466. ncx_put_double_short(void *xp, const short *ip)
  1467. {
  1468. double xx = (double) *ip;
  1469. put_ix_double(xp, &xx);
  1470. #if 0 /* TODO: figure this out */
  1471. if((double)(*ip) > X_DOUBLE_MAX || (double)(*ip) < X_DOUBLE_MIN)
  1472. return NC_ERANGE;
  1473. #endif
  1474. return ENOERR;
  1475. }
  1476. int
  1477. ncx_put_double_int(void *xp, const int *ip)
  1478. {
  1479. double xx = (double) *ip;
  1480. put_ix_double(xp, &xx);
  1481. #if 0 /* TODO: figure this out */
  1482. if((double)(*ip) > X_DOUBLE_MAX || (double)(*ip) < X_DOUBLE_MIN)
  1483. return NC_ERANGE;
  1484. #endif
  1485. return ENOERR;
  1486. }
  1487. int
  1488. ncx_put_double_long(void *xp, const long *ip)
  1489. {
  1490. double xx = (double) *ip;
  1491. put_ix_double(xp, &xx);
  1492. #if 1 /* TODO: figure this out */
  1493. if((double)(*ip) > X_DOUBLE_MAX || (double)(*ip) < X_DOUBLE_MIN)
  1494. return NC_ERANGE;
  1495. #endif
  1496. return ENOERR;
  1497. }
  1498. int
  1499. ncx_put_double_float(void *xp, const float *ip)
  1500. {
  1501. double xx = (double) *ip;
  1502. put_ix_double(xp, &xx);
  1503. #if 1 /* TODO: figure this out */
  1504. if((double)(*ip) > X_DOUBLE_MAX || (double)(*ip) < X_DOUBLE_MIN)
  1505. return NC_ERANGE;
  1506. #endif
  1507. return ENOERR;
  1508. }
  1509. int
  1510. ncx_put_double_double(void *xp, const double *ip)
  1511. {
  1512. put_ix_double(xp, ip);
  1513. #ifdef NO_IEEE_FLOAT
  1514. if(*ip > X_DOUBLE_MAX || *ip < X_DOUBLE_MIN)
  1515. return NC_ERANGE;
  1516. #endif
  1517. return ENOERR;
  1518. }
  1519. /* x_size_t */
  1520. #if SIZEOF_SIZE_T < X_SIZEOF_SIZE_T
  1521. #error "x_size_t implementation"
  1522. /* netcdf requires size_t which can hold a values from 0 to 2^32 -1 */
  1523. #endif
  1524. int
  1525. ncx_put_size_t(void **xpp, const size_t *ulp)
  1526. {
  1527. /* similar to put_ix_int() */
  1528. uchar *cp = (uchar *) *xpp;
  1529. assert(*ulp <= X_SIZE_MAX);
  1530. *cp++ = (uchar)((*ulp) >> 24);
  1531. *cp++ = (uchar)(((*ulp) & 0x00ff0000) >> 16);
  1532. *cp++ = (uchar)(((*ulp) & 0x0000ff00) >> 8);
  1533. *cp = (uchar)((*ulp) & 0x000000ff);
  1534. *xpp = (void *)((char *)(*xpp) + X_SIZEOF_SIZE_T);
  1535. return ENOERR;
  1536. }
  1537. int
  1538. ncx_get_size_t(const void **xpp, size_t *ulp)
  1539. {
  1540. /* similar to get_ix_int */
  1541. const uchar *cp = (const uchar *) *xpp;
  1542. *ulp = (unsigned)(*cp++ << 24);
  1543. *ulp |= (*cp++ << 16);
  1544. *ulp |= (*cp++ << 8);
  1545. *ulp |= *cp;
  1546. *xpp = (const void *)((const char *)(*xpp) + X_SIZEOF_SIZE_T);
  1547. return ENOERR;
  1548. }
  1549. /* x_off_t */
  1550. int
  1551. ncx_put_off_t(void **xpp, const off_t *lp, size_t sizeof_off_t)
  1552. {
  1553. /* similar to put_ix_int() */
  1554. uchar *cp = (uchar *) *xpp;
  1555. /* No negative offsets stored in netcdf */
  1556. if (*lp < 0) {
  1557. /* Assume this is an overflow of a 32-bit int... */
  1558. return ERANGE;
  1559. }
  1560. assert(sizeof_off_t == 4 || sizeof_off_t == 8);
  1561. if (sizeof_off_t == 4) {
  1562. *cp++ = (uchar) ((*lp) >> 24);
  1563. *cp++ = (uchar)(((*lp) & 0x00ff0000) >> 16);
  1564. *cp++ = (uchar)(((*lp) & 0x0000ff00) >> 8);
  1565. *cp = (uchar)( (*lp) & 0x000000ff);
  1566. } else {
  1567. #if SIZEOF_OFF_T == 4
  1568. /* Write a 64-bit offset on a system with only a 32-bit offset */
  1569. *cp++ = (uchar)0;
  1570. *cp++ = (uchar)0;
  1571. *cp++ = (uchar)0;
  1572. *cp++ = (uchar)0;
  1573. *cp++ = (uchar)(((*lp) & 0xff000000) >> 24);
  1574. *cp++ = (uchar)(((*lp) & 0x00ff0000) >> 16);
  1575. *cp++ = (uchar)(((*lp) & 0x0000ff00) >> 8);
  1576. *cp = (uchar)( (*lp) & 0x000000ff);
  1577. #else
  1578. *cp++ = (uchar) ((*lp) >> 56);
  1579. *cp++ = (uchar)(((*lp) & 0x00ff000000000000ULL) >> 48);
  1580. *cp++ = (uchar)(((*lp) & 0x0000ff0000000000ULL) >> 40);
  1581. *cp++ = (uchar)(((*lp) & 0x000000ff00000000ULL) >> 32);
  1582. *cp++ = (uchar)(((*lp) & 0x00000000ff000000ULL) >> 24);
  1583. *cp++ = (uchar)(((*lp) & 0x0000000000ff0000ULL) >> 16);
  1584. *cp++ = (uchar)(((*lp) & 0x000000000000ff00ULL) >> 8);
  1585. *cp = (uchar)( (*lp) & 0x00000000000000ffULL);
  1586. #endif
  1587. }
  1588. *xpp = (void *)((char *)(*xpp) + sizeof_off_t);
  1589. return ENOERR;
  1590. }
  1591. int
  1592. ncx_get_off_t(const void **xpp, off_t *lp, size_t sizeof_off_t)
  1593. {
  1594. /* similar to get_ix_int() */
  1595. const uchar *cp = (const uchar *) *xpp;
  1596. assert(sizeof_off_t == 4 || sizeof_off_t == 8);
  1597. if (sizeof_off_t == 4) {
  1598. *lp = *cp++ << 24;
  1599. *lp |= (*cp++ << 16);
  1600. *lp |= (*cp++ << 8);
  1601. *lp |= *cp;
  1602. } else {
  1603. #if SIZEOF_OFF_T == 4
  1604. /* Read a 64-bit offset on a system with only a 32-bit offset */
  1605. /* If the offset overflows, set an error code and return */
  1606. *lp = ((off_t)(*cp++) << 24);
  1607. *lp |= ((off_t)(*cp++) << 16);
  1608. *lp |= ((off_t)(*cp++) << 8);
  1609. *lp |= ((off_t)(*cp++));
  1610. /*
  1611. * lp now contains the upper 32-bits of the 64-bit offset. if lp is
  1612. * not zero, then the dataset is larger than can be represented
  1613. * on this system. Set an error code and return.
  1614. */
  1615. if (*lp != 0) {
  1616. return ERANGE;
  1617. }
  1618. *lp = ((off_t)(*cp++) << 24);
  1619. *lp |= ((off_t)(*cp++) << 16);
  1620. *lp |= ((off_t)(*cp++) << 8);
  1621. *lp |= (off_t)*cp;
  1622. if (*lp < 0) {
  1623. /*
  1624. * If this fails, then the offset is >2^31, but less
  1625. * than 2^32 which is not allowed, but is not caught
  1626. * by the previous check
  1627. */
  1628. return ERANGE;
  1629. }
  1630. #else
  1631. *lp = ((off_t)(*cp++) << 56);
  1632. *lp |= ((off_t)(*cp++) << 48);
  1633. *lp |= ((off_t)(*cp++) << 40);
  1634. *lp |= ((off_t)(*cp++) << 32);
  1635. *lp |= ((off_t)(*cp++) << 24);
  1636. *lp |= ((off_t)(*cp++) << 16);
  1637. *lp |= ((off_t)(*cp++) << 8);
  1638. *lp |= (off_t)*cp;
  1639. #endif
  1640. }
  1641. *xpp = (const void *)((const char *)(*xpp) + sizeof_off_t);
  1642. return ENOERR;
  1643. }
  1644. /*
  1645. * Aggregate numeric conversion functions.
  1646. */
  1647. dnl dnl dnl
  1648. dnl
  1649. dnl Upcase(str)
  1650. dnl
  1651. define(`Upcase',dnl
  1652. `dnl
  1653. translit($1, abcdefghijklmnopqrstuvwxyz, ABCDEFGHIJKLMNOPQRSTUVWXYZ)')dnl
  1654. dnl dnl dnl
  1655. dnl
  1656. dnl Xsizeof(Xtype)
  1657. dnl
  1658. define(`Xsizeof', ``X_SIZEOF_'Upcase($1)')dnl
  1659. dnl
  1660. dnl dnl dnl
  1661. dnl
  1662. dnl NCX_GETN_Byte_Body (body for one byte types on diagonal)
  1663. dnl
  1664. define(`NCX_GETN_Byte_Body',dnl
  1665. `dnl
  1666. (void) memcpy(tp, *xpp, nelems);
  1667. *xpp = (void *)((char *)(*xpp) + nelems);
  1668. return ENOERR;
  1669. ')dnl
  1670. dnl dnl dnl
  1671. dnl
  1672. dnl NCX_PAD_GETN_Byte_body (body for one byte types on diagonal)
  1673. dnl
  1674. define(`NCX_PAD_GETN_Byte_Body',dnl
  1675. `dnl
  1676. size_t rndup = nelems % X_ALIGN;
  1677. if(rndup)
  1678. rndup = X_ALIGN - rndup;
  1679. (void) memcpy(tp, *xpp, nelems);
  1680. *xpp = (void *)((char *)(*xpp) + nelems + rndup);
  1681. return ENOERR;
  1682. ')dnl
  1683. dnl dnl dnl
  1684. dnl
  1685. dnl NCX_GETN_SCHAR(Type)
  1686. dnl
  1687. define(`NCX_GETN_SCHAR',dnl
  1688. `dnl
  1689. int
  1690. ncx_getn_schar_$1(const void **xpp, size_t nelems, $1 *tp)
  1691. {
  1692. schar *xp = (schar *)(*xpp);
  1693. while(nelems-- != 0)
  1694. {
  1695. *tp++ = *xp++;
  1696. }
  1697. *xpp = (const void *)xp;
  1698. return ENOERR;
  1699. }
  1700. ')dnl
  1701. dnl dnl dnl
  1702. dnl
  1703. dnl NCX_PAD_GETN_SCHAR(Type)
  1704. dnl
  1705. define(`NCX_PAD_GETN_SCHAR',dnl
  1706. `dnl
  1707. int
  1708. ncx_pad_getn_schar_$1(const void **xpp, size_t nelems, $1 *tp)
  1709. {
  1710. size_t rndup = nelems % X_ALIGN;
  1711. schar *xp = (schar *) *xpp;
  1712. if(rndup)
  1713. rndup = X_ALIGN - rndup;
  1714. while(nelems-- != 0)
  1715. {
  1716. *tp++ = *xp++;
  1717. }
  1718. *xpp = (void *)(xp + rndup);
  1719. return ENOERR;
  1720. }
  1721. ')dnl
  1722. dnl dnl dnl
  1723. dnl
  1724. dnl NCX_GETNo(XType, Type) deprecated
  1725. dnl
  1726. define(`NCX_GETNo',dnl
  1727. `dnl
  1728. int
  1729. ncx_getn_$1_$2(const void **xpp, size_t nelems, $2 *tp)
  1730. {
  1731. const char *xp = (const char *) *xpp;
  1732. int status = ENOERR;
  1733. $1 xx;
  1734. for( ; nelems != 0; nelems--, xp += Xsizeof($1), tp++)
  1735. {
  1736. const int lstatus = ncx_get_$1_$1(xp, &xx);
  1737. *tp = ($2)xx;
  1738. if(lstatus != ENOERR)
  1739. status = lstatus;
  1740. }
  1741. *xpp = (const void *)xp;
  1742. return status;
  1743. }
  1744. ')dnl
  1745. dnl dnl dnl
  1746. dnl
  1747. dnl NCX_GETN(XType, Type)
  1748. dnl
  1749. define(`NCX_GETN',dnl
  1750. `dnl
  1751. int
  1752. ncx_getn_$1_$2(const void **xpp, size_t nelems, $2 *tp)
  1753. {
  1754. const char *xp = (const char *) *xpp;
  1755. int status = ENOERR;
  1756. for( ; nelems != 0; nelems--, xp += Xsizeof($1), tp++)
  1757. {
  1758. const int lstatus = ncx_get_$1_$2(xp, tp);
  1759. if(lstatus != ENOERR)
  1760. status = lstatus;
  1761. }
  1762. *xpp = (const void *)xp;
  1763. return status;
  1764. }
  1765. ')dnl
  1766. dnl dnl dnl
  1767. dnl
  1768. dnl NCX_PAD_GETN_SHORT( Type)
  1769. dnl
  1770. define(`NCX_PAD_GETN_SHORT',dnl
  1771. `dnl
  1772. int
  1773. ncx_pad_getn_short_$1(const void **xpp, size_t nelems, $1 *tp)
  1774. {
  1775. const size_t rndup = nelems % 2;
  1776. const char *xp = (const char *) *xpp;
  1777. int status = ENOERR;
  1778. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  1779. {
  1780. const int lstatus = ncx_get_short_$1(xp, tp);
  1781. if(lstatus != ENOERR)
  1782. status = lstatus;
  1783. }
  1784. if(rndup != 0)
  1785. xp += X_SIZEOF_SHORT;
  1786. *xpp = (void *)xp;
  1787. return status;
  1788. }
  1789. ')dnl
  1790. dnl dnl dnl
  1791. dnl
  1792. dnl NCX_PUTN_Byte_Body(Type) (body for one byte types)
  1793. dnl
  1794. define(`NCX_PUTN_Byte_Body',dnl
  1795. `dnl
  1796. (void) memcpy(*xpp, tp, nelems);
  1797. *xpp = (void *)((char *)(*xpp) + nelems);
  1798. return ENOERR;
  1799. ')dnl
  1800. dnl dnl dnl
  1801. dnl
  1802. dnl NCX_PAD_PUTN_Byte_Body(Type) (body for one byte types)
  1803. dnl
  1804. define(`NCX_PAD_PUTN_Byte_Body',dnl
  1805. size_t rndup = nelems % X_ALIGN;
  1806. if(rndup)
  1807. rndup = X_ALIGN - rndup;
  1808. (void) memcpy(*xpp, tp, nelems);
  1809. *xpp = (void *)((char *)(*xpp) + nelems);
  1810. if(rndup)
  1811. {
  1812. (void) memcpy(*xpp, nada, rndup);
  1813. *xpp = (void *)((char *)(*xpp) + rndup);
  1814. }
  1815. return ENOERR;
  1816. `dnl
  1817. ')dnl
  1818. dnl dnl dnl
  1819. dnl
  1820. dnl NCX_PUTN_SCHAR(Type)
  1821. dnl
  1822. define(`NCX_PUTN_SCHAR',dnl
  1823. `dnl
  1824. int
  1825. ncx_putn_schar_$1(void **xpp, size_t nelems, const $1 *tp)
  1826. {
  1827. int status = ENOERR;
  1828. schar *xp = (schar *) *xpp;
  1829. while(nelems-- != 0)
  1830. {
  1831. if(*tp > X_SCHAR_MAX || *tp < X_SCHAR_MIN)
  1832. status = NC_ERANGE;
  1833. *xp++ = (schar) *tp++;
  1834. }
  1835. *xpp = (void *)xp;
  1836. return status;
  1837. }
  1838. ')dnl
  1839. dnl dnl dnl
  1840. dnl
  1841. dnl NCX_PAD_PUTN_SCHAR(Type)
  1842. dnl
  1843. define(`NCX_PAD_PUTN_SCHAR',dnl
  1844. `dnl
  1845. int
  1846. ncx_pad_putn_schar_$1(void **xpp, size_t nelems, const $1 *tp)
  1847. {
  1848. int status = ENOERR;
  1849. size_t rndup = nelems % X_ALIGN;
  1850. schar *xp = (schar *) *xpp;
  1851. if(rndup)
  1852. rndup = X_ALIGN - rndup;
  1853. while(nelems-- != 0)
  1854. {
  1855. /* N.B. schar as signed */
  1856. if(*tp > X_SCHAR_MAX || *tp < X_SCHAR_MIN)
  1857. status = NC_ERANGE;
  1858. *xp++ = (schar) *tp++;
  1859. }
  1860. if(rndup)
  1861. {
  1862. (void) memcpy(xp, nada, rndup);
  1863. xp += rndup;
  1864. }
  1865. *xpp = (void *)xp;
  1866. return status;
  1867. }
  1868. ')dnl
  1869. dnl dnl dnl
  1870. dnl
  1871. dnl NCX_PUTNo(XType, Type) deprecated
  1872. dnl
  1873. define(`NCX_PUTNo',dnl
  1874. `dnl
  1875. int
  1876. ncx_putn_$1_$2(void **xpp, size_t nelems, const $2 *tp)
  1877. {
  1878. char *xp = (char *) *xpp;
  1879. int status = ENOERR;
  1880. $1 xx;
  1881. for( ; nelems != 0; nelems--, xp += Xsizeof($1), tp++)
  1882. {
  1883. xx = ($1) *tp;
  1884. {
  1885. int lstatus = ncx_put_$1_$1(xp, &xx);
  1886. if(lstatus != ENOERR)
  1887. status = lstatus;
  1888. }
  1889. }
  1890. *xpp = (void *)xp;
  1891. return status;
  1892. }
  1893. ')dnl
  1894. dnl dnl dnl
  1895. dnl
  1896. dnl NCX_PUTN(XType, Type)
  1897. dnl
  1898. define(`NCX_PUTN',dnl
  1899. `dnl
  1900. int
  1901. ncx_putn_$1_$2(void **xpp, size_t nelems, const $2 *tp)
  1902. {
  1903. char *xp = (char *) *xpp;
  1904. int status = ENOERR;
  1905. for( ; nelems != 0; nelems--, xp += Xsizeof($1), tp++)
  1906. {
  1907. int lstatus = ncx_put_$1_$2(xp, tp);
  1908. if(lstatus != ENOERR)
  1909. status = lstatus;
  1910. }
  1911. *xpp = (void *)xp;
  1912. return status;
  1913. }
  1914. ')dnl
  1915. dnl dnl dnl
  1916. dnl
  1917. dnl NCX_PAD_PUTN_SHORT(Type)
  1918. dnl
  1919. define(`NCX_PAD_PUTN_SHORT',dnl
  1920. `dnl
  1921. int
  1922. ncx_pad_putn_short_$1(void **xpp, size_t nelems, const $1 *tp)
  1923. {
  1924. const size_t rndup = nelems % 2;
  1925. char *xp = (char *) *xpp;
  1926. int status = ENOERR;
  1927. for( ; nelems != 0; nelems--, xp += Xsizeof(short), tp++)
  1928. {
  1929. int lstatus = ncx_put_short_$1(xp, tp);
  1930. if(lstatus != ENOERR)
  1931. status = lstatus;
  1932. }
  1933. if(rndup != 0)
  1934. {
  1935. (void) memcpy(xp, nada, X_SIZEOF_SHORT);
  1936. xp += X_SIZEOF_SHORT;
  1937. }
  1938. *xpp = (void *)xp;
  1939. return status;
  1940. }
  1941. ')dnl
  1942. dnl dnl dnl
  1943. dnl
  1944. dnl Declare & define routines
  1945. dnl
  1946. dnl dnl dnl
  1947. /* schar */
  1948. dnl NCX_GETN_SCHAR(schar)
  1949. int
  1950. ncx_getn_schar_schar(const void **xpp, size_t nelems, schar *tp)
  1951. {
  1952. NCX_GETN_Byte_Body
  1953. }
  1954. dnl NCX_GETN_SCHAR(uchar)
  1955. int
  1956. ncx_getn_schar_uchar(const void **xpp, size_t nelems, uchar *tp)
  1957. {
  1958. NCX_GETN_Byte_Body
  1959. }
  1960. NCX_GETN_SCHAR(short)
  1961. NCX_GETN_SCHAR(int)
  1962. NCX_GETN_SCHAR(long)
  1963. NCX_GETN_SCHAR(float)
  1964. NCX_GETN_SCHAR(double)
  1965. dnl NCX_PAD_GETN_SCHAR(schar)
  1966. int
  1967. ncx_pad_getn_schar_schar(const void **xpp, size_t nelems, schar *tp)
  1968. {
  1969. NCX_PAD_GETN_Byte_Body
  1970. }
  1971. dnl NCX_PAD_GETN_SCHAR(uchar)
  1972. int
  1973. ncx_pad_getn_schar_uchar(const void **xpp, size_t nelems, uchar *tp)
  1974. {
  1975. NCX_PAD_GETN_Byte_Body
  1976. }
  1977. NCX_PAD_GETN_SCHAR(short)
  1978. NCX_PAD_GETN_SCHAR(int)
  1979. NCX_PAD_GETN_SCHAR(long)
  1980. NCX_PAD_GETN_SCHAR(float)
  1981. NCX_PAD_GETN_SCHAR(double)
  1982. dnl NCX_PUTN_SCHAR(schar)
  1983. int
  1984. ncx_putn_schar_schar(void **xpp, size_t nelems, const schar *tp)
  1985. {
  1986. NCX_PUTN_Byte_Body
  1987. }
  1988. dnl NCX_PUTN_SCHAR(uchar)
  1989. int
  1990. ncx_putn_schar_uchar(void **xpp, size_t nelems, const uchar *tp)
  1991. {
  1992. NCX_PUTN_Byte_Body
  1993. }
  1994. NCX_PUTN_SCHAR(short)
  1995. NCX_PUTN_SCHAR(int)
  1996. NCX_PUTN_SCHAR(long)
  1997. NCX_PUTN_SCHAR(float)
  1998. NCX_PUTN_SCHAR(double)
  1999. dnl NCX_PAD_PUTN_SCHAR(schar)
  2000. int
  2001. ncx_pad_putn_schar_schar(void **xpp, size_t nelems, const schar *tp)
  2002. {
  2003. NCX_PAD_PUTN_Byte_Body
  2004. }
  2005. dnl NCX_PAD_PUTN_SCHAR(uchar)
  2006. int
  2007. ncx_pad_putn_schar_uchar(void **xpp, size_t nelems, const uchar *tp)
  2008. {
  2009. NCX_PAD_PUTN_Byte_Body
  2010. }
  2011. NCX_PAD_PUTN_SCHAR(short)
  2012. NCX_PAD_PUTN_SCHAR(int)
  2013. NCX_PAD_PUTN_SCHAR(long)
  2014. NCX_PAD_PUTN_SCHAR(float)
  2015. NCX_PAD_PUTN_SCHAR(double)
  2016. /* short */
  2017. NCX_GETN(short, schar)
  2018. NCX_GETN(short, uchar)
  2019. #if X_SIZEOF_SHORT == SIZEOF_SHORT
  2020. /* optimized version */
  2021. int
  2022. ncx_getn_short_short(const void **xpp, size_t nelems, short *tp)
  2023. {
  2024. # if WORDS_BIGENDIAN
  2025. (void) memcpy(tp, *xpp, nelems * sizeof(short));
  2026. # else
  2027. swapn2b(tp, *xpp, nelems);
  2028. # endif
  2029. *xpp = (const void *)((const char *)(*xpp) + nelems * X_SIZEOF_SHORT);
  2030. return ENOERR;
  2031. }
  2032. #else
  2033. NCX_GETN(short, short)
  2034. #endif
  2035. NCX_GETN(short, int)
  2036. NCX_GETN(short, long)
  2037. NCX_GETN(short, float)
  2038. NCX_GETN(short, double)
  2039. NCX_PAD_GETN_SHORT(schar)
  2040. NCX_PAD_GETN_SHORT(uchar)
  2041. NCX_PAD_GETN_SHORT(short)
  2042. NCX_PAD_GETN_SHORT(int)
  2043. NCX_PAD_GETN_SHORT(long)
  2044. NCX_PAD_GETN_SHORT(float)
  2045. NCX_PAD_GETN_SHORT(double)
  2046. NCX_PUTN(short, schar)
  2047. NCX_PUTN(short, uchar)
  2048. #if X_SIZEOF_SHORT == SIZEOF_SHORT
  2049. /* optimized version */
  2050. int
  2051. ncx_putn_short_short(void **xpp, size_t nelems, const short *tp)
  2052. {
  2053. # if WORDS_BIGENDIAN
  2054. (void) memcpy(*xpp, tp, nelems * X_SIZEOF_SHORT);
  2055. # else
  2056. swapn2b(*xpp, tp, nelems);
  2057. # endif
  2058. *xpp = (void *)((char *)(*xpp) + nelems * X_SIZEOF_SHORT);
  2059. return ENOERR;
  2060. }
  2061. #else
  2062. NCX_PUTN(short, short)
  2063. #endif
  2064. NCX_PUTN(short, int)
  2065. NCX_PUTN(short, long)
  2066. NCX_PUTN(short, float)
  2067. NCX_PUTN(short, double)
  2068. NCX_PAD_PUTN_SHORT(schar)
  2069. NCX_PAD_PUTN_SHORT(uchar)
  2070. NCX_PAD_PUTN_SHORT(short)
  2071. NCX_PAD_PUTN_SHORT(int)
  2072. NCX_PAD_PUTN_SHORT(long)
  2073. NCX_PAD_PUTN_SHORT(float)
  2074. NCX_PAD_PUTN_SHORT(double)
  2075. /* int */
  2076. NCX_GETN(int, schar)
  2077. NCX_GETN(int, uchar)
  2078. NCX_GETN(int, short)
  2079. #if X_SIZEOF_INT == SIZEOF_INT
  2080. /* optimized version */
  2081. int
  2082. ncx_getn_int_int(const void **xpp, size_t nelems, int *tp)
  2083. {
  2084. # if WORDS_BIGENDIAN
  2085. (void) memcpy(tp, *xpp, nelems * sizeof(int));
  2086. # else
  2087. swapn4b(tp, *xpp, nelems);
  2088. # endif
  2089. *xpp = (const void *)((const char *)(*xpp) + nelems * X_SIZEOF_INT);
  2090. return ENOERR;
  2091. }
  2092. #else
  2093. NCX_GETN(int, int)
  2094. #endif
  2095. #if X_SIZEOF_INT == SIZEOF_LONG
  2096. /* optimized version */
  2097. int
  2098. ncx_getn_int_long(const void **xpp, size_t nelems, long *tp)
  2099. {
  2100. # if WORDS_BIGENDIAN
  2101. (void) memcpy(tp, *xpp, nelems * sizeof(long));
  2102. # else
  2103. swapn4b(tp, *xpp, nelems);
  2104. # endif
  2105. *xpp = (const void *)((const char *)(*xpp) + nelems * X_SIZEOF_INT);
  2106. return ENOERR;
  2107. }
  2108. #else
  2109. NCX_GETN(int, long)
  2110. #endif
  2111. NCX_GETN(int, float)
  2112. NCX_GETN(int, double)
  2113. NCX_PUTN(int, schar)
  2114. NCX_PUTN(int, uchar)
  2115. NCX_PUTN(int, short)
  2116. #if X_SIZEOF_INT == SIZEOF_INT
  2117. /* optimized version */
  2118. int
  2119. ncx_putn_int_int(void **xpp, size_t nelems, const int *tp)
  2120. {
  2121. # if WORDS_BIGENDIAN
  2122. (void) memcpy(*xpp, tp, nelems * X_SIZEOF_INT);
  2123. # else
  2124. swapn4b(*xpp, tp, nelems);
  2125. # endif
  2126. *xpp = (void *)((char *)(*xpp) + nelems * X_SIZEOF_INT);
  2127. return ENOERR;
  2128. }
  2129. #else
  2130. NCX_PUTN(int, int)
  2131. #endif
  2132. #if X_SIZEOF_INT == SIZEOF_LONG
  2133. /* optimized version */
  2134. int
  2135. ncx_putn_int_long(void **xpp, size_t nelems, const long *tp)
  2136. {
  2137. # if WORDS_BIGENDIAN
  2138. (void) memcpy(*xpp, tp, nelems * X_SIZEOF_INT);
  2139. # else
  2140. swapn4b(*xpp, tp, nelems);
  2141. # endif
  2142. *xpp = (void *)((char *)(*xpp) + nelems * X_SIZEOF_INT);
  2143. return ENOERR;
  2144. }
  2145. #else
  2146. NCX_PUTN(int, long)
  2147. #endif
  2148. NCX_PUTN(int, float)
  2149. NCX_PUTN(int, double)
  2150. /* float */
  2151. NCX_GETN(float, schar)
  2152. NCX_GETN(float, uchar)
  2153. NCX_GETN(float, short)
  2154. NCX_GETN(float, int)
  2155. NCX_GETN(float, long)
  2156. #if X_SIZEOF_FLOAT == SIZEOF_FLOAT && !defined(NO_IEEE_FLOAT)
  2157. /* optimized version */
  2158. int
  2159. ncx_getn_float_float(const void **xpp, size_t nelems, float *tp)
  2160. {
  2161. # if WORDS_BIGENDIAN
  2162. (void) memcpy(tp, *xpp, nelems * sizeof(float));
  2163. # else
  2164. swapn4b(tp, *xpp, nelems);
  2165. # endif
  2166. *xpp = (const void *)((const char *)(*xpp) + nelems * X_SIZEOF_FLOAT);
  2167. return ENOERR;
  2168. }
  2169. #elif vax
  2170. int
  2171. ncx_getn_float_float(const void **xpp, size_t nfloats, float *ip)
  2172. {
  2173. float *const end = ip + nfloats;
  2174. while(ip < end)
  2175. {
  2176. GET_VAX_DFLOAT_Body(`(*xpp)')
  2177. ip++;
  2178. *xpp = (char *)(*xpp) + X_SIZEOF_FLOAT;
  2179. }
  2180. return ENOERR;
  2181. }
  2182. #elif _SX && _FLOAT2
  2183. int
  2184. ncx_getn_float_float(const void **xpp, size_t nelems, float *tp)
  2185. {
  2186. const char *const xp = *xpp;
  2187. const int ncnv = ie3_fl2(xp, tp, 4, 8, nelems);
  2188. *xpp = xp + nelems * X_SIZEOF_FLOAT;
  2189. return (nelems == ncnv ? ENOERR : NC_ERANGE);
  2190. }
  2191. #else
  2192. int
  2193. ncx_getn_float_float(const void **xpp, size_t nelems, float *tp)
  2194. {
  2195. const char *xp = *xpp;
  2196. int status = ENOERR;
  2197. for( ; nelems != 0; nelems--, xp += X_SIZEOF_FLOAT, tp++)
  2198. {
  2199. const int lstatus = ncx_get_float_float(xp, tp);
  2200. if(lstatus != ENOERR)
  2201. status = lstatus;
  2202. }
  2203. *xpp = (const void *)xp;
  2204. return status;
  2205. }
  2206. #endif
  2207. NCX_GETN(float, double)
  2208. NCX_PUTN(float, schar)
  2209. NCX_PUTN(float, uchar)
  2210. NCX_PUTN(float, short)
  2211. NCX_PUTN(float, int)
  2212. NCX_PUTN(float, long)
  2213. #if X_SIZEOF_FLOAT == SIZEOF_FLOAT && !defined(NO_IEEE_FLOAT)
  2214. /* optimized version */
  2215. int
  2216. ncx_putn_float_float(void **xpp, size_t nelems, const float *tp)
  2217. {
  2218. # if WORDS_BIGENDIAN
  2219. (void) memcpy(*xpp, tp, nelems * X_SIZEOF_FLOAT);
  2220. # else
  2221. swapn4b(*xpp, tp, nelems);
  2222. # endif
  2223. *xpp = (void *)((char *)(*xpp) + nelems * X_SIZEOF_FLOAT);
  2224. return ENOERR;
  2225. }
  2226. #elif vax
  2227. int
  2228. ncx_putn_float_float(void **xpp, size_t nfloats, const float *ip)
  2229. {
  2230. const float *const end = ip + nfloats;
  2231. while(ip < end)
  2232. {
  2233. PUT_VAX_DFLOAT_Body(`(*xpp)')
  2234. ip++;
  2235. *xpp = (char *)(*xpp) + X_SIZEOF_FLOAT;
  2236. }
  2237. return ENOERR;
  2238. }
  2239. #elif _SX && _FLOAT2
  2240. int
  2241. ncx_putn_float_float(void **xpp, size_t nelems, const float *tp)
  2242. {
  2243. char *const xp = *xpp;
  2244. const int ncnv = fl2_ie3(tp, xp, 8, 4, nelems);
  2245. *xpp = xp + nelems * X_SIZEOF_FLOAT;
  2246. return (nelems == ncnv ? ENOERR : NC_ERANGE);
  2247. }
  2248. #else
  2249. int
  2250. ncx_putn_float_float(void **xpp, size_t nelems, const float *tp)
  2251. {
  2252. char *xp = *xpp;
  2253. int status = ENOERR;
  2254. for( ; nelems != 0; nelems--, xp += X_SIZEOF_FLOAT, tp++)
  2255. {
  2256. int lstatus = ncx_put_float_float(xp, tp);
  2257. if(lstatus != ENOERR)
  2258. status = lstatus;
  2259. }
  2260. *xpp = (void *)xp;
  2261. return status;
  2262. }
  2263. #endif
  2264. NCX_PUTN(float, double)
  2265. /* double */
  2266. NCX_GETN(double, schar)
  2267. NCX_GETN(double, uchar)
  2268. NCX_GETN(double, short)
  2269. NCX_GETN(double, int)
  2270. NCX_GETN(double, long)
  2271. NCX_GETN(double, float)
  2272. dnl NCX_GETN(double, double)
  2273. #if X_SIZEOF_DOUBLE == SIZEOF_DOUBLE && !defined(NO_IEEE_FLOAT)
  2274. /* optimized version */
  2275. int
  2276. ncx_getn_double_double(const void **xpp, size_t nelems, double *tp)
  2277. {
  2278. # if WORDS_BIGENDIAN
  2279. (void) memcpy(tp, *xpp, nelems * sizeof(double));
  2280. # else
  2281. swapn8b(tp, *xpp, nelems);
  2282. # endif
  2283. *xpp = (const void *)((const char *)(*xpp) + nelems * X_SIZEOF_DOUBLE);
  2284. return ENOERR;
  2285. }
  2286. #elif vax
  2287. int
  2288. ncx_getn_double_double(const void **xpp, size_t ndoubles, double *ip)
  2289. {
  2290. double *const end = ip + ndoubles;
  2291. while(ip < end)
  2292. {
  2293. GET_VAX_DDOUBLE_Body(`(*xpp)')
  2294. ip++;
  2295. *xpp = (char *)(*xpp) + X_SIZEOF_DOUBLE;
  2296. }
  2297. return ENOERR;
  2298. }
  2299. /* vax */
  2300. #elif _SX && _FLOAT2
  2301. int
  2302. ncx_getn_double_double(const void **xpp,

Large files files are truncated, but you can click here to view the full file