PageRenderTime 86ms CodeModel.GetById 46ms RepoModel.GetById 0ms app.codeStats 0ms

/other/netcdf_write_matrix/src/libsrc/ncx.c

https://github.com/jbeezley/wrf-fire
C | 4215 lines | 3428 code | 636 blank | 151 comment | 708 complexity | 9063d22a435417ff36c4ebc0349dd126 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. /* Do not edit this file. It is produced from the corresponding .m4 source */
  2. /*
  3. * Copyright 1996, University Corporation for Atmospheric Research
  4. * See netcdf/COPYRIGHT file for copying and redistribution conditions.
  5. *
  6. * This file contains some routines derived from code
  7. * which is copyrighted by Sun Microsystems, Inc.
  8. * The "#ifdef vax" versions of
  9. * ncx_put_float_float()
  10. * ncx_get_float_float()
  11. * ncx_put_double_double()
  12. * ncx_get_double_double()
  13. * ncx_putn_float_float()
  14. * ncx_getn_float_float()
  15. * ncx_putn_double_double()
  16. * ncx_getn_double_double()
  17. * are derived from xdr_float() and xdr_double() routines
  18. * in the freely available, copyrighted Sun RPCSRC 3.9
  19. * distribution, xdr_float.c.
  20. * Our "value added" is that these are always memory to memory,
  21. * they handle IEEE subnormals properly, and their "n" versions
  22. * operate speedily on arrays.
  23. */
  24. /* $Id: ncx.m4,v 2.47 2004/09/30 18:47:12 russ Exp $ */
  25. /*
  26. * An external data representation interface.
  27. */
  28. #include "ncx.h"
  29. #include <string.h>
  30. #include <limits.h>
  31. /* alias poorly named limits.h macros */
  32. #define SHORT_MAX SHRT_MAX
  33. #define SHORT_MIN SHRT_MIN
  34. #define USHORT_MAX USHRT_MAX
  35. #include <float.h>
  36. #ifndef FLT_MAX /* This POSIX macro missing on some systems */
  37. # ifndef NO_IEEE_FLOAT
  38. # define FLT_MAX 3.40282347e+38f
  39. # else
  40. # error "You will need to define FLT_MAX"
  41. # endif
  42. #endif
  43. #include <assert.h>
  44. /*
  45. * If the machine's float domain is "smaller" than the external one
  46. * use the machine domain
  47. */
  48. #if defined(FLT_MAX_EXP) && FLT_MAX_EXP < 128 /* 128 is X_FLT_MAX_EXP */
  49. #undef X_FLOAT_MAX
  50. # define X_FLOAT_MAX FLT_MAX
  51. #undef X_FLOAT_MIN
  52. # define X_FLOAT_MIN (-X_FLOAT_MAX)
  53. #endif
  54. #if _SX /* NEC SUPER UX */
  55. #if _INT64
  56. #undef INT_MAX /* workaround cpp bug */
  57. #define INT_MAX X_INT_MAX
  58. #undef INT_MIN /* workaround cpp bug */
  59. #define INT_MIN X_INT_MIN
  60. #undef LONG_MAX /* workaround cpp bug */
  61. #define LONG_MAX X_INT_MAX
  62. #undef LONG_MIN /* workaround cpp bug */
  63. #define LONG_MIN X_INT_MIN
  64. #elif _LONG64
  65. #undef LONG_MAX /* workaround cpp bug */
  66. #define LONG_MAX 4294967295L
  67. #undef LONG_MIN /* workaround cpp bug */
  68. #define LONG_MIN -4294967295L
  69. #endif
  70. #endif /* _SX */
  71. static const char nada[X_ALIGN] = {0, 0, 0, 0};
  72. #ifndef WORDS_BIGENDIAN
  73. /* LITTLE_ENDIAN: DEC and intel */
  74. /*
  75. * Routines to convert to BIGENDIAN.
  76. * Optimize the swapn?b() and swap?b() routines aggressivly.
  77. */
  78. #define SWAP2(a) ( (((a) & 0xff) << 8) | \
  79. (((a) >> 8) & 0xff) )
  80. #define SWAP4(a) ( ((a) << 24) | \
  81. (((a) << 8) & 0x00ff0000) | \
  82. (((a) >> 8) & 0x0000ff00) | \
  83. (((a) >> 24) & 0x000000ff) )
  84. static void
  85. swapn2b(void *dst, const void *src, size_t nn)
  86. {
  87. char *op = dst;
  88. const char *ip = src;
  89. while(nn-- != 0)
  90. {
  91. *op++ = *(++ip);
  92. *op++ = *(ip++ -1);
  93. }
  94. }
  95. # ifndef vax
  96. static void
  97. swap4b(void *dst, const void *src)
  98. {
  99. char *op = dst;
  100. const char *ip = src;
  101. op[0] = ip[3];
  102. op[1] = ip[2];
  103. op[2] = ip[1];
  104. op[3] = ip[0];
  105. }
  106. # endif /* !vax */
  107. static void
  108. swapn4b(void *dst, const void *src, size_t nn)
  109. {
  110. char *op = dst;
  111. const char *ip = src;
  112. while(nn-- != 0)
  113. {
  114. op[0] = ip[3];
  115. op[1] = ip[2];
  116. op[2] = ip[1];
  117. op[3] = ip[0];
  118. op += 4;
  119. ip += 4;
  120. }
  121. }
  122. # ifndef vax
  123. static void
  124. swap8b(void *dst, const void *src)
  125. {
  126. char *op = dst;
  127. const char *ip = src;
  128. op[0] = ip[7];
  129. op[1] = ip[6];
  130. op[2] = ip[5];
  131. op[3] = ip[4];
  132. op[4] = ip[3];
  133. op[5] = ip[2];
  134. op[6] = ip[1];
  135. op[7] = ip[0];
  136. }
  137. # endif /* !vax */
  138. # ifndef vax
  139. static void
  140. swapn8b(void *dst, const void *src, size_t nn)
  141. {
  142. char *op = dst;
  143. const char *ip = src;
  144. while(nn-- != 0)
  145. {
  146. op[0] = ip[7];
  147. op[1] = ip[6];
  148. op[2] = ip[5];
  149. op[3] = ip[4];
  150. op[4] = ip[3];
  151. op[5] = ip[2];
  152. op[6] = ip[1];
  153. op[7] = ip[0];
  154. op += 8;
  155. ip += 8;
  156. }
  157. }
  158. # endif /* !vax */
  159. #endif /* LITTLE_ENDIAN */
  160. /*
  161. * Primitive numeric conversion functions.
  162. */
  163. /* x_schar */
  164. /* We don't implement and x_schar primitives. */
  165. /* x_short */
  166. #if SHORT_MAX == X_SHORT_MAX
  167. typedef short ix_short;
  168. #define SIZEOF_IX_SHORT SIZEOF_SHORT
  169. #define IX_SHORT_MAX SHORT_MAX
  170. #elif INT_MAX >= X_SHORT_MAX
  171. typedef int ix_short;
  172. #define SIZEOF_IX_SHORT SIZEOF_INT
  173. #define IX_SHORT_MAX INT_MAX
  174. #elif LONG_MAX >= X_SHORT_MAX
  175. typedef long ix_short;
  176. #define SIZEOF_IX_SHORT SIZEOF_LONG
  177. #define IX_SHORT_MAX LONG_MAX
  178. #else
  179. #error "ix_short implementation"
  180. #endif
  181. static void
  182. get_ix_short(const void *xp, ix_short *ip)
  183. {
  184. const uchar *cp = (const uchar *) xp;
  185. *ip = *cp++ << 8;
  186. #if SIZEOF_IX_SHORT > X_SIZEOF_SHORT
  187. if(*ip & 0x8000)
  188. {
  189. /* extern is negative */
  190. *ip |= (~(0xffff)); /* N.B. Assumes "twos complement" */
  191. }
  192. #endif
  193. *ip |= *cp;
  194. }
  195. static void
  196. put_ix_short(void *xp, const ix_short *ip)
  197. {
  198. uchar *cp = (uchar *) xp;
  199. *cp++ = (*ip) >> 8;
  200. *cp = (*ip) & 0xff;
  201. }
  202. int
  203. ncx_get_short_schar(const void *xp, schar *ip)
  204. {
  205. ix_short xx;
  206. get_ix_short(xp, &xx);
  207. *ip = xx;
  208. if(xx > SCHAR_MAX || xx < SCHAR_MIN)
  209. return NC_ERANGE;
  210. return ENOERR;
  211. }
  212. int
  213. ncx_get_short_uchar(const void *xp, uchar *ip)
  214. {
  215. ix_short xx;
  216. get_ix_short(xp, &xx);
  217. *ip = xx;
  218. if(xx > UCHAR_MAX || xx < 0)
  219. return NC_ERANGE;
  220. return ENOERR;
  221. }
  222. int
  223. ncx_get_short_short(const void *xp, short *ip)
  224. {
  225. #if SIZEOF_IX_SHORT == SIZEOF_SHORT && IX_SHORT_MAX == SHORT_MAX
  226. get_ix_short(xp, (ix_short *)ip);
  227. return ENOERR;
  228. #else
  229. ix_short xx;
  230. get_ix_short(xp, &xx);
  231. *ip = xx;
  232. # if IX_SHORT_MAX > SHORT_MAX
  233. if(xx > SHORT_MAX || xx < SHORT_MIN)
  234. return NC_ERANGE;
  235. # endif
  236. return ENOERR;
  237. #endif
  238. }
  239. int
  240. ncx_get_short_int(const void *xp, int *ip)
  241. {
  242. #if SIZEOF_IX_SHORT == SIZEOF_INT && IX_SHORT_MAX == INT_MAX
  243. get_ix_short(xp, (ix_short *)ip);
  244. return ENOERR;
  245. #else
  246. ix_short xx;
  247. get_ix_short(xp, &xx);
  248. *ip = xx;
  249. # if IX_SHORT_MAX > INT_MAX
  250. if(xx > INT_MAX || xx < INT_MIN)
  251. return NC_ERANGE;
  252. # endif
  253. return ENOERR;
  254. #endif
  255. }
  256. int
  257. ncx_get_short_long(const void *xp, long *ip)
  258. {
  259. #if SIZEOF_IX_SHORT == SIZEOF_LONG && IX_SHORT_MAX == LONG_MAX
  260. get_ix_short(xp, (ix_short *)ip);
  261. return ENOERR;
  262. #else
  263. /* assert(LONG_MAX >= X_SHORT_MAX); */
  264. ix_short xx;
  265. get_ix_short(xp, &xx);
  266. *ip = xx;
  267. return ENOERR;
  268. #endif
  269. }
  270. int
  271. ncx_get_short_float(const void *xp, float *ip)
  272. {
  273. ix_short xx;
  274. get_ix_short(xp, &xx);
  275. *ip = xx;
  276. #if 0 /* TODO: determine when necessary */
  277. if(xx > FLT_MAX || xx < (-FLT_MAX))
  278. return NC_ERANGE;
  279. #endif
  280. return ENOERR;
  281. }
  282. int
  283. ncx_get_short_double(const void *xp, double *ip)
  284. {
  285. /* assert(DBL_MAX >= X_SHORT_MAX); */
  286. ix_short xx;
  287. get_ix_short(xp, &xx);
  288. *ip = xx;
  289. return ENOERR;
  290. }
  291. int
  292. ncx_put_short_schar(void *xp, const schar *ip)
  293. {
  294. uchar *cp = (uchar *) xp;
  295. if(*ip & 0x80)
  296. *cp++ = 0xff;
  297. else
  298. *cp++ = 0;
  299. *cp = (uchar)*ip;
  300. return ENOERR;
  301. }
  302. int
  303. ncx_put_short_uchar(void *xp, const uchar *ip)
  304. {
  305. uchar *cp = (uchar *) xp;
  306. *cp++ = 0;
  307. *cp = *ip;
  308. return ENOERR;
  309. }
  310. int
  311. ncx_put_short_short(void *xp, const short *ip)
  312. {
  313. #if SIZEOF_IX_SHORT == SIZEOF_SHORT && X_SHORT_MAX == SHORT_MAX
  314. put_ix_short(xp, (const ix_short *)ip);
  315. return ENOERR;
  316. #else
  317. ix_short xx = (ix_short)*ip;
  318. put_ix_short(xp, &xx);
  319. # if X_SHORT_MAX < SHORT_MAX
  320. if(*ip > X_SHORT_MAX || *ip < X_SHORT_MIN)
  321. return NC_ERANGE;
  322. # endif
  323. return ENOERR;
  324. #endif
  325. }
  326. int
  327. ncx_put_short_int(void *xp, const int *ip)
  328. {
  329. #if SIZEOF_IX_SHORT == SIZEOF_INT && X_SHORT_MAX == INT_MAX
  330. put_ix_short(xp, (const ix_short *)ip);
  331. return ENOERR;
  332. #else
  333. ix_short xx = (ix_short)*ip;
  334. put_ix_short(xp, &xx);
  335. # if X_SHORT_MAX < INT_MAX
  336. if(*ip > X_SHORT_MAX || *ip < X_SHORT_MIN)
  337. return NC_ERANGE;
  338. # endif
  339. return ENOERR;
  340. #endif
  341. }
  342. int
  343. ncx_put_short_long(void *xp, const long *ip)
  344. {
  345. #if SIZEOF_IX_SHORT == SIZEOF_LONG && X_SHORT_MAX == LONG_MAX
  346. put_ix_short(xp, (const ix_short *)ip);
  347. return ENOERR;
  348. #else
  349. ix_short xx = (ix_short)*ip;
  350. put_ix_short(xp, &xx);
  351. # if X_SHORT_MAX < LONG_MAX
  352. if(*ip > X_SHORT_MAX || *ip < X_SHORT_MIN)
  353. return NC_ERANGE;
  354. # endif
  355. return ENOERR;
  356. #endif
  357. }
  358. int
  359. ncx_put_short_float(void *xp, const float *ip)
  360. {
  361. ix_short xx = *ip;
  362. put_ix_short(xp, &xx);
  363. if(*ip > X_SHORT_MAX || *ip < X_SHORT_MIN)
  364. return NC_ERANGE;
  365. return ENOERR;
  366. }
  367. int
  368. ncx_put_short_double(void *xp, const double *ip)
  369. {
  370. ix_short xx = *ip;
  371. put_ix_short(xp, &xx);
  372. if(*ip > X_SHORT_MAX || *ip < X_SHORT_MIN)
  373. return NC_ERANGE;
  374. return ENOERR;
  375. }
  376. /* x_int */
  377. #if SHORT_MAX == X_INT_MAX
  378. typedef short ix_int;
  379. #define SIZEOF_IX_INT SIZEOF_SHORT
  380. #define IX_INT_MAX SHORT_MAX
  381. #elif INT_MAX >= X_INT_MAX
  382. typedef int ix_int;
  383. #define SIZEOF_IX_INT SIZEOF_INT
  384. #define IX_INT_MAX INT_MAX
  385. #elif LONG_MAX >= X_INT_MAX
  386. typedef long ix_int;
  387. #define SIZEOF_IX_INT SIZEOF_LONG
  388. #define IX_INT_MAX LONG_MAX
  389. #else
  390. #error "ix_int implementation"
  391. #endif
  392. static void
  393. get_ix_int(const void *xp, ix_int *ip)
  394. {
  395. const uchar *cp = (const uchar *) xp;
  396. *ip = *cp++ << 24;
  397. #if SIZEOF_IX_INT > X_SIZEOF_INT
  398. if(*ip & 0x80000000)
  399. {
  400. /* extern is negative */
  401. *ip |= (~(0xffffffff)); /* N.B. Assumes "twos complement" */
  402. }
  403. #endif
  404. *ip |= (*cp++ << 16);
  405. *ip |= (*cp++ << 8);
  406. *ip |= *cp;
  407. }
  408. static void
  409. put_ix_int(void *xp, const ix_int *ip)
  410. {
  411. uchar *cp = (uchar *) xp;
  412. *cp++ = (*ip) >> 24;
  413. *cp++ = ((*ip) & 0x00ff0000) >> 16;
  414. *cp++ = ((*ip) & 0x0000ff00) >> 8;
  415. *cp = ((*ip) & 0x000000ff);
  416. }
  417. int
  418. ncx_get_int_schar(const void *xp, schar *ip)
  419. {
  420. ix_int xx;
  421. get_ix_int(xp, &xx);
  422. *ip = xx;
  423. if(xx > SCHAR_MAX || xx < SCHAR_MIN)
  424. return NC_ERANGE;
  425. return ENOERR;
  426. }
  427. int
  428. ncx_get_int_uchar(const void *xp, uchar *ip)
  429. {
  430. ix_int xx;
  431. get_ix_int(xp, &xx);
  432. *ip = xx;
  433. if(xx > UCHAR_MAX || xx < 0)
  434. return NC_ERANGE;
  435. return ENOERR;
  436. }
  437. int
  438. ncx_get_int_short(const void *xp, short *ip)
  439. {
  440. #if SIZEOF_IX_INT == SIZEOF_SHORT && IX_INT_MAX == SHORT_MAX
  441. get_ix_int(xp, (ix_int *)ip);
  442. return ENOERR;
  443. #else
  444. ix_int xx;
  445. get_ix_int(xp, &xx);
  446. *ip = xx;
  447. # if IX_INT_MAX > SHORT_MAX
  448. if(xx > SHORT_MAX || xx < SHORT_MIN)
  449. return NC_ERANGE;
  450. # endif
  451. return ENOERR;
  452. #endif
  453. }
  454. int
  455. ncx_get_int_int(const void *xp, int *ip)
  456. {
  457. #if SIZEOF_IX_INT == SIZEOF_INT && IX_INT_MAX == INT_MAX
  458. get_ix_int(xp, (ix_int *)ip);
  459. return ENOERR;
  460. #else
  461. ix_int xx;
  462. get_ix_int(xp, &xx);
  463. *ip = xx;
  464. # if IX_INT_MAX > INT_MAX
  465. if(xx > INT_MAX || xx < INT_MIN)
  466. return NC_ERANGE;
  467. # endif
  468. return ENOERR;
  469. #endif
  470. }
  471. int
  472. ncx_get_int_long(const void *xp, long *ip)
  473. {
  474. #if SIZEOF_IX_INT == SIZEOF_LONG && IX_INT_MAX == LONG_MAX
  475. get_ix_int(xp, (ix_int *)ip);
  476. return ENOERR;
  477. #else
  478. ix_int xx;
  479. get_ix_int(xp, &xx);
  480. *ip = xx;
  481. # if IX_INT_MAX > LONG_MAX /* unlikely */
  482. if(xx > LONG_MAX || xx < LONG_MIN)
  483. return NC_ERANGE;
  484. # endif
  485. return ENOERR;
  486. #endif
  487. }
  488. int
  489. ncx_get_int_float(const void *xp, float *ip)
  490. {
  491. ix_int xx;
  492. get_ix_int(xp, &xx);
  493. *ip = xx;
  494. #if 0 /* TODO: determine when necessary */
  495. if(xx > FLT_MAX || xx < (-FLT_MAX))
  496. return NC_ERANGE;
  497. #endif
  498. return ENOERR;
  499. }
  500. int
  501. ncx_get_int_double(const void *xp, double *ip)
  502. {
  503. /* assert((DBL_MAX >= X_INT_MAX); */
  504. ix_int xx;
  505. get_ix_int(xp, &xx);
  506. *ip = xx;
  507. return ENOERR;
  508. }
  509. int
  510. ncx_put_int_schar(void *xp, const schar *ip)
  511. {
  512. uchar *cp = (uchar *) xp;
  513. if(*ip & 0x80)
  514. {
  515. *cp++ = 0xff;
  516. *cp++ = 0xff;
  517. *cp++ = 0xff;
  518. }
  519. else
  520. {
  521. *cp++ = 0x00;
  522. *cp++ = 0x00;
  523. *cp++ = 0x00;
  524. }
  525. *cp = (uchar)*ip;
  526. return ENOERR;
  527. }
  528. int
  529. ncx_put_int_uchar(void *xp, const uchar *ip)
  530. {
  531. uchar *cp = (uchar *) xp;
  532. *cp++ = 0x00;
  533. *cp++ = 0x00;
  534. *cp++ = 0x00;
  535. *cp = *ip;
  536. return ENOERR;
  537. }
  538. int
  539. ncx_put_int_short(void *xp, const short *ip)
  540. {
  541. #if SIZEOF_IX_INT == SIZEOF_SHORT && IX_INT_MAX == SHORT_MAX
  542. put_ix_int(xp, (ix_int *)ip);
  543. return ENOERR;
  544. #else
  545. ix_int xx = (ix_int)(*ip);
  546. put_ix_int(xp, &xx);
  547. # if IX_INT_MAX < SHORT_MAX
  548. if(*ip > X_INT_MAX || *ip < X_INT_MIN)
  549. return NC_ERANGE;
  550. # endif
  551. return ENOERR;
  552. #endif
  553. }
  554. int
  555. ncx_put_int_int(void *xp, const int *ip)
  556. {
  557. #if SIZEOF_IX_INT == SIZEOF_INT && IX_INT_MAX == INT_MAX
  558. put_ix_int(xp, (ix_int *)ip);
  559. return ENOERR;
  560. #else
  561. ix_int xx = (ix_int)(*ip);
  562. put_ix_int(xp, &xx);
  563. # if IX_INT_MAX < INT_MAX
  564. if(*ip > X_INT_MAX || *ip < X_INT_MIN)
  565. return NC_ERANGE;
  566. # endif
  567. return ENOERR;
  568. #endif
  569. }
  570. int
  571. ncx_put_int_long(void *xp, const long *ip)
  572. {
  573. #if SIZEOF_IX_INT == SIZEOF_LONG && IX_INT_MAX == LONG_MAX
  574. put_ix_int(xp, (ix_int *)ip);
  575. return ENOERR;
  576. #else
  577. ix_int xx = (ix_int)(*ip);
  578. put_ix_int(xp, &xx);
  579. # if IX_INT_MAX < LONG_MAX
  580. if(*ip > X_INT_MAX || *ip < X_INT_MIN)
  581. return NC_ERANGE;
  582. # endif
  583. return ENOERR;
  584. #endif
  585. }
  586. int
  587. ncx_put_int_float(void *xp, const float *ip)
  588. {
  589. ix_int xx = (ix_int)(*ip);
  590. put_ix_int(xp, &xx);
  591. if(*ip > (double)X_INT_MAX || *ip < (double)X_INT_MIN)
  592. return NC_ERANGE;
  593. return ENOERR;
  594. }
  595. int
  596. ncx_put_int_double(void *xp, const double *ip)
  597. {
  598. ix_int xx = (ix_int)(*ip);
  599. put_ix_int(xp, &xx);
  600. if(*ip > X_INT_MAX || *ip < X_INT_MIN)
  601. return NC_ERANGE;
  602. return ENOERR;
  603. }
  604. /* x_float */
  605. #if X_SIZEOF_FLOAT == SIZEOF_FLOAT && !defined(NO_IEEE_FLOAT)
  606. static void
  607. get_ix_float(const void *xp, float *ip)
  608. {
  609. #ifdef WORDS_BIGENDIAN
  610. (void) memcpy(ip, xp, sizeof(float));
  611. #else
  612. swap4b(ip, xp);
  613. #endif
  614. }
  615. static void
  616. put_ix_float(void *xp, const float *ip)
  617. {
  618. #ifdef WORDS_BIGENDIAN
  619. (void) memcpy(xp, ip, X_SIZEOF_FLOAT);
  620. #else
  621. swap4b(xp, ip);
  622. #endif
  623. }
  624. #elif vax
  625. /* What IEEE single precision floating point looks like on a Vax */
  626. struct ieee_single {
  627. unsigned int exp_hi : 7;
  628. unsigned int sign : 1;
  629. unsigned int mant_hi : 7;
  630. unsigned int exp_lo : 1;
  631. unsigned int mant_lo_hi : 8;
  632. unsigned int mant_lo_lo : 8;
  633. };
  634. /* Vax single precision floating point */
  635. struct vax_single {
  636. unsigned int mantissa1 : 7;
  637. unsigned int exp : 8;
  638. unsigned int sign : 1;
  639. unsigned int mantissa2 : 16;
  640. };
  641. #define VAX_SNG_BIAS 0x81
  642. #define IEEE_SNG_BIAS 0x7f
  643. static struct sgl_limits {
  644. struct vax_single s;
  645. struct ieee_single ieee;
  646. } max = {
  647. { 0x7f, 0xff, 0x0, 0xffff }, /* Max Vax */
  648. { 0x7f, 0x0, 0x0, 0x1, 0x0, 0x0 } /* Max IEEE */
  649. };
  650. static struct sgl_limits min = {
  651. { 0x0, 0x0, 0x0, 0x0 }, /* Min Vax */
  652. { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 } /* Min IEEE */
  653. };
  654. static void
  655. get_ix_float(const void *xp, float *ip)
  656. {
  657. struct vax_single *const vsp = (struct vax_single *) ip;
  658. const struct ieee_single *const isp =
  659. (const struct ieee_single *) xp;
  660. unsigned exp = isp->exp_hi << 1 | isp->exp_lo;
  661. switch(exp) {
  662. case 0 :
  663. /* ieee subnormal */
  664. if(isp->mant_hi == min.ieee.mant_hi
  665. && isp->mant_lo_hi == min.ieee.mant_lo_hi
  666. && isp->mant_lo_lo == min.ieee.mant_lo_lo)
  667. {
  668. *vsp = min.s;
  669. }
  670. else
  671. {
  672. unsigned mantissa = (isp->mant_hi << 16)
  673. | isp->mant_lo_hi << 8
  674. | isp->mant_lo_lo;
  675. unsigned tmp = mantissa >> 20;
  676. if(tmp >= 4) {
  677. vsp->exp = 2;
  678. } else if (tmp >= 2) {
  679. vsp->exp = 1;
  680. } else {
  681. *vsp = min.s;
  682. break;
  683. } /* else */
  684. tmp = mantissa - (1 << (20 + vsp->exp ));
  685. tmp <<= 3 - vsp->exp;
  686. vsp->mantissa2 = tmp;
  687. vsp->mantissa1 = (tmp >> 16);
  688. }
  689. break;
  690. case 0xfe :
  691. case 0xff :
  692. *vsp = max.s;
  693. break;
  694. default :
  695. vsp->exp = exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
  696. vsp->mantissa2 = isp->mant_lo_hi << 8 | isp->mant_lo_lo;
  697. vsp->mantissa1 = isp->mant_hi;
  698. }
  699. vsp->sign = isp->sign;
  700. }
  701. static void
  702. put_ix_float(void *xp, const float *ip)
  703. {
  704. const struct vax_single *const vsp =
  705. (const struct vax_single *)ip;
  706. struct ieee_single *const isp = (struct ieee_single *) xp;
  707. switch(vsp->exp){
  708. case 0 :
  709. /* all vax float with zero exponent map to zero */
  710. *isp = min.ieee;
  711. break;
  712. case 2 :
  713. case 1 :
  714. {
  715. /* These will map to subnormals */
  716. unsigned mantissa = (vsp->mantissa1 << 16)
  717. | vsp->mantissa2;
  718. mantissa >>= 3 - vsp->exp;
  719. mantissa += (1 << (20 + vsp->exp));
  720. isp->mant_lo_lo = mantissa;
  721. isp->mant_lo_hi = mantissa >> 8;
  722. isp->mant_hi = mantissa >> 16;
  723. isp->exp_lo = 0;
  724. isp->exp_hi = 0;
  725. }
  726. break;
  727. case 0xff : /* max.s.exp */
  728. if( vsp->mantissa2 == max.s.mantissa2
  729. && vsp->mantissa1 == max.s.mantissa1)
  730. {
  731. /* map largest vax float to ieee infinity */
  732. *isp = max.ieee;
  733. break;
  734. } /* else, fall thru */
  735. default :
  736. {
  737. unsigned exp = vsp->exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
  738. isp->exp_hi = exp >> 1;
  739. isp->exp_lo = exp;
  740. isp->mant_lo_lo = vsp->mantissa2;
  741. isp->mant_lo_hi = vsp->mantissa2 >> 8;
  742. isp->mant_hi = vsp->mantissa1;
  743. }
  744. }
  745. isp->sign = vsp->sign;
  746. }
  747. /* vax */
  748. #elif defined(_CRAY)
  749. /*
  750. * Return the number of bytes until the next "word" boundary
  751. * N.B. This is based on the very wierd YMP address structure,
  752. * which puts the address within a word in the leftmost 3 bits
  753. * of the address.
  754. */
  755. static size_t
  756. word_align(const void *vp)
  757. {
  758. const size_t rem = ((size_t)vp >> (64 - 3)) & 0x7;
  759. return (rem != 0);
  760. }
  761. struct ieee_single_hi {
  762. unsigned int sign : 1;
  763. unsigned int exp : 8;
  764. unsigned int mant :23;
  765. unsigned int pad :32;
  766. };
  767. typedef struct ieee_single_hi ieee_single_hi;
  768. struct ieee_single_lo {
  769. unsigned int pad :32;
  770. unsigned int sign : 1;
  771. unsigned int exp : 8;
  772. unsigned int mant :23;
  773. };
  774. typedef struct ieee_single_lo ieee_single_lo;
  775. static const int ieee_single_bias = 0x7f;
  776. struct ieee_double {
  777. unsigned int sign : 1;
  778. unsigned int exp :11;
  779. unsigned int mant :52;
  780. };
  781. typedef struct ieee_double ieee_double;
  782. static const int ieee_double_bias = 0x3ff;
  783. #if defined(NO_IEEE_FLOAT)
  784. struct cray_single {
  785. unsigned int sign : 1;
  786. unsigned int exp :15;
  787. unsigned int mant :48;
  788. };
  789. typedef struct cray_single cray_single;
  790. static const int cs_ieis_bias = 0x4000 - 0x7f;
  791. static const int cs_id_bias = 0x4000 - 0x3ff;
  792. static void
  793. get_ix_float(const void *xp, float *ip)
  794. {
  795. if(word_align(xp) == 0)
  796. {
  797. const ieee_single_hi *isp = (const ieee_single_hi *) xp;
  798. cray_single *csp = (cray_single *) ip;
  799. if(isp->exp == 0)
  800. {
  801. /* ieee subnormal */
  802. *ip = (double)isp->mant;
  803. if(isp->mant != 0)
  804. {
  805. csp->exp -= (ieee_single_bias + 22);
  806. }
  807. }
  808. else
  809. {
  810. csp->exp = isp->exp + cs_ieis_bias + 1;
  811. csp->mant = isp->mant << (48 - 1 - 23);
  812. csp->mant |= (1 << (48 - 1));
  813. }
  814. csp->sign = isp->sign;
  815. }
  816. else
  817. {
  818. const ieee_single_lo *isp = (const ieee_single_lo *) xp;
  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. }
  837. }
  838. static void
  839. put_ix_float(void *xp, const float *ip)
  840. {
  841. if(word_align(xp) == 0)
  842. {
  843. ieee_single_hi *isp = (ieee_single_hi*)xp;
  844. const cray_single *csp = (const cray_single *) ip;
  845. int ieee_exp = csp->exp - cs_ieis_bias -1;
  846. isp->sign = csp->sign;
  847. if(ieee_exp >= 0xff)
  848. {
  849. /* NC_ERANGE => ieee Inf */
  850. isp->exp = 0xff;
  851. isp->mant = 0x0;
  852. }
  853. else if(ieee_exp > 0)
  854. {
  855. /* normal ieee representation */
  856. isp->exp = ieee_exp;
  857. /* assumes cray rep is in normal form */
  858. assert(csp->mant & 0x800000000000);
  859. isp->mant = (((csp->mant << 1) &
  860. 0xffffffffffff) >> (48 - 23));
  861. }
  862. else if(ieee_exp > -23)
  863. {
  864. /* ieee subnormal, right */
  865. const int rshift = (48 - 23 - ieee_exp);
  866. isp->mant = csp->mant >> rshift;
  867. #if 0
  868. if(csp->mant & (1 << (rshift -1)))
  869. {
  870. /* round up */
  871. isp->mant++;
  872. }
  873. #endif
  874. isp->exp = 0;
  875. }
  876. else
  877. {
  878. /* smaller than ieee can represent */
  879. isp->exp = 0;
  880. isp->mant = 0;
  881. }
  882. }
  883. else
  884. {
  885. ieee_single_lo *isp = (ieee_single_lo*)xp;
  886. const cray_single *csp = (const cray_single *) ip;
  887. int ieee_exp = csp->exp - cs_ieis_bias -1;
  888. isp->sign = csp->sign;
  889. if(ieee_exp >= 0xff)
  890. {
  891. /* NC_ERANGE => ieee Inf */
  892. isp->exp = 0xff;
  893. isp->mant = 0x0;
  894. }
  895. else if(ieee_exp > 0)
  896. {
  897. /* normal ieee representation */
  898. isp->exp = ieee_exp;
  899. /* assumes cray rep is in normal form */
  900. assert(csp->mant & 0x800000000000);
  901. isp->mant = (((csp->mant << 1) &
  902. 0xffffffffffff) >> (48 - 23));
  903. }
  904. else if(ieee_exp > -23)
  905. {
  906. /* ieee subnormal, right */
  907. const int rshift = (48 - 23 - ieee_exp);
  908. isp->mant = csp->mant >> rshift;
  909. #if 0
  910. if(csp->mant & (1 << (rshift -1)))
  911. {
  912. /* round up */
  913. isp->mant++;
  914. }
  915. #endif
  916. isp->exp = 0;
  917. }
  918. else
  919. {
  920. /* smaller than ieee can represent */
  921. isp->exp = 0;
  922. isp->mant = 0;
  923. }
  924. }
  925. }
  926. #else
  927. /* IEEE Cray with only doubles */
  928. static void
  929. get_ix_float(const void *xp, float *ip)
  930. {
  931. ieee_double *idp = (ieee_double *) ip;
  932. if(word_align(xp) == 0)
  933. {
  934. const ieee_single_hi *isp = (const ieee_single_hi *) xp;
  935. if(isp->exp == 0 && isp->mant == 0)
  936. {
  937. idp->exp = 0;
  938. idp->mant = 0;
  939. }
  940. else
  941. {
  942. idp->exp = isp->exp + (ieee_double_bias - ieee_single_bias);
  943. idp->mant = isp->mant << (52 - 23);
  944. }
  945. idp->sign = isp->sign;
  946. }
  947. else
  948. {
  949. const ieee_single_lo *isp = (const ieee_single_lo *) xp;
  950. if(isp->exp == 0 && isp->mant == 0)
  951. {
  952. idp->exp = 0;
  953. idp->mant = 0;
  954. }
  955. else
  956. {
  957. idp->exp = isp->exp + (ieee_double_bias - ieee_single_bias);
  958. idp->mant = isp->mant << (52 - 23);
  959. }
  960. idp->sign = isp->sign;
  961. }
  962. }
  963. static void
  964. put_ix_float(void *xp, const float *ip)
  965. {
  966. const ieee_double *idp = (const ieee_double *) ip;
  967. if(word_align(xp) == 0)
  968. {
  969. ieee_single_hi *isp = (ieee_single_hi*)xp;
  970. if(idp->exp > (ieee_double_bias - ieee_single_bias))
  971. isp->exp = idp->exp - (ieee_double_bias - ieee_single_bias);
  972. else
  973. isp->exp = 0;
  974. isp->mant = idp->mant >> (52 - 23);
  975. isp->sign = idp->sign;
  976. }
  977. else
  978. {
  979. ieee_single_lo *isp = (ieee_single_lo*)xp;
  980. if(idp->exp > (ieee_double_bias - ieee_single_bias))
  981. isp->exp = idp->exp - (ieee_double_bias - ieee_single_bias);
  982. else
  983. isp->exp = 0;
  984. isp->mant = idp->mant >> (52 - 23);
  985. isp->sign = idp->sign;
  986. }
  987. }
  988. #endif
  989. #elif _SX && _FLOAT2
  990. static void
  991. get_ix_float(const void *xp, float *ip)
  992. {
  993. const int ncnv = ie3_fl2(xp, ip, 4, 8, 1);
  994. }
  995. static void
  996. put_ix_float(void *xp, const float *ip)
  997. {
  998. const int ncnv = fl2_ie3(ip, xp, 8, 4, 1);
  999. }
  1000. #else
  1001. #error "ix_float implementation"
  1002. #endif
  1003. int
  1004. ncx_get_float_schar(const void *xp, schar *ip)
  1005. {
  1006. float xx;
  1007. get_ix_float(xp, &xx);
  1008. *ip = (schar) xx;
  1009. if(xx > SCHAR_MAX || xx < SCHAR_MIN)
  1010. return NC_ERANGE;
  1011. return ENOERR;
  1012. }
  1013. int
  1014. ncx_get_float_uchar(const void *xp, uchar *ip)
  1015. {
  1016. float xx;
  1017. get_ix_float(xp, &xx);
  1018. *ip = (uchar) xx;
  1019. if(xx > UCHAR_MAX || xx < 0)
  1020. return NC_ERANGE;
  1021. return ENOERR;
  1022. }
  1023. int
  1024. ncx_get_float_short(const void *xp, short *ip)
  1025. {
  1026. float xx;
  1027. get_ix_float(xp, &xx);
  1028. *ip = (short) xx;
  1029. if(xx > SHORT_MAX || xx < SHORT_MIN)
  1030. return NC_ERANGE;
  1031. return ENOERR;
  1032. }
  1033. int
  1034. ncx_get_float_int(const void *xp, int *ip)
  1035. {
  1036. float xx;
  1037. get_ix_float(xp, &xx);
  1038. *ip = (int) xx;
  1039. if(xx > (double)INT_MAX || xx < (double)INT_MIN)
  1040. return NC_ERANGE;
  1041. return ENOERR;
  1042. }
  1043. int
  1044. ncx_get_float_long(const void *xp, long *ip)
  1045. {
  1046. float xx;
  1047. get_ix_float(xp, &xx);
  1048. *ip = (long) xx;
  1049. if(xx > LONG_MAX || xx < LONG_MIN)
  1050. return NC_ERANGE;
  1051. return ENOERR;
  1052. }
  1053. int
  1054. ncx_get_float_float(const void *xp, float *ip)
  1055. {
  1056. /* TODO */
  1057. get_ix_float(xp, ip);
  1058. return ENOERR;
  1059. }
  1060. int
  1061. ncx_get_float_double(const void *xp, double *ip)
  1062. {
  1063. /* TODO */
  1064. float xx;
  1065. get_ix_float(xp, &xx);
  1066. *ip = xx;
  1067. return ENOERR;
  1068. }
  1069. int
  1070. ncx_put_float_schar(void *xp, const schar *ip)
  1071. {
  1072. float xx = (float) *ip;
  1073. put_ix_float(xp, &xx);
  1074. return ENOERR;
  1075. }
  1076. int
  1077. ncx_put_float_uchar(void *xp, const uchar *ip)
  1078. {
  1079. float xx = (float) *ip;
  1080. put_ix_float(xp, &xx);
  1081. return ENOERR;
  1082. }
  1083. int
  1084. ncx_put_float_short(void *xp, const short *ip)
  1085. {
  1086. float xx = (float) *ip;
  1087. put_ix_float(xp, &xx);
  1088. #if 0 /* TODO: figure this out */
  1089. if((float)(*ip) > X_FLOAT_MAX || (float)(*ip) < X_FLOAT_MIN)
  1090. return NC_ERANGE;
  1091. #endif
  1092. return ENOERR;
  1093. }
  1094. int
  1095. ncx_put_float_int(void *xp, const int *ip)
  1096. {
  1097. float xx = (float) *ip;
  1098. put_ix_float(xp, &xx);
  1099. #if 1 /* TODO: figure this out */
  1100. if((float)(*ip) > X_FLOAT_MAX || (float)(*ip) < X_FLOAT_MIN)
  1101. return NC_ERANGE;
  1102. #endif
  1103. return ENOERR;
  1104. }
  1105. int
  1106. ncx_put_float_long(void *xp, const long *ip)
  1107. {
  1108. float xx = (float) *ip;
  1109. put_ix_float(xp, &xx);
  1110. #if 1 /* TODO: figure this out */
  1111. if((float)(*ip) > X_FLOAT_MAX || (float)(*ip) < X_FLOAT_MIN)
  1112. return NC_ERANGE;
  1113. #endif
  1114. return ENOERR;
  1115. }
  1116. int
  1117. ncx_put_float_float(void *xp, const float *ip)
  1118. {
  1119. put_ix_float(xp, ip);
  1120. #ifdef NO_IEEE_FLOAT
  1121. if(*ip > X_FLOAT_MAX || *ip < X_FLOAT_MIN)
  1122. return NC_ERANGE;
  1123. #endif
  1124. return ENOERR;
  1125. }
  1126. int
  1127. ncx_put_float_double(void *xp, const double *ip)
  1128. {
  1129. float xx = (float) *ip;
  1130. put_ix_float(xp, &xx);
  1131. if(*ip > X_FLOAT_MAX || *ip < X_FLOAT_MIN)
  1132. return NC_ERANGE;
  1133. return ENOERR;
  1134. }
  1135. /* x_double */
  1136. #if X_SIZEOF_DOUBLE == SIZEOF_DOUBLE && !defined(NO_IEEE_FLOAT)
  1137. static void
  1138. get_ix_double(const void *xp, double *ip)
  1139. {
  1140. #ifdef WORDS_BIGENDIAN
  1141. (void) memcpy(ip, xp, sizeof(double));
  1142. #else
  1143. swap8b(ip, xp);
  1144. #endif
  1145. }
  1146. static void
  1147. put_ix_double(void *xp, const double *ip)
  1148. {
  1149. #ifdef WORDS_BIGENDIAN
  1150. (void) memcpy(xp, ip, X_SIZEOF_DOUBLE);
  1151. #else
  1152. swap8b(xp, ip);
  1153. #endif
  1154. }
  1155. #elif vax
  1156. /* What IEEE double precision floating point looks like on a Vax */
  1157. struct ieee_double {
  1158. unsigned int exp_hi : 7;
  1159. unsigned int sign : 1;
  1160. unsigned int mant_6 : 4;
  1161. unsigned int exp_lo : 4;
  1162. unsigned int mant_5 : 8;
  1163. unsigned int mant_4 : 8;
  1164. unsigned int mant_lo : 32;
  1165. };
  1166. /* Vax double precision floating point */
  1167. struct vax_double {
  1168. unsigned int mantissa1 : 7;
  1169. unsigned int exp : 8;
  1170. unsigned int sign : 1;
  1171. unsigned int mantissa2 : 16;
  1172. unsigned int mantissa3 : 16;
  1173. unsigned int mantissa4 : 16;
  1174. };
  1175. #define VAX_DBL_BIAS 0x81
  1176. #define IEEE_DBL_BIAS 0x3ff
  1177. #define MASK(nbits) ((1 << nbits) - 1)
  1178. static const struct dbl_limits {
  1179. struct vax_double d;
  1180. struct ieee_double ieee;
  1181. } dbl_limits[2] = {
  1182. {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */
  1183. { 0x7f, 0x0, 0x0, 0xf, 0x0, 0x0, 0x0}}, /* Max IEEE */
  1184. {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */
  1185. { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, /* Min IEEE */
  1186. };
  1187. static void
  1188. get_ix_double(const void *xp, double *ip)
  1189. {
  1190. struct vax_double *const vdp =
  1191. (struct vax_double *)ip;
  1192. const struct ieee_double *const idp =
  1193. (const struct ieee_double *) xp;
  1194. {
  1195. const struct dbl_limits *lim;
  1196. int ii;
  1197. for (ii = 0, lim = dbl_limits;
  1198. ii < sizeof(dbl_limits)/sizeof(struct dbl_limits);
  1199. ii++, lim++)
  1200. {
  1201. if ((idp->mant_lo == lim->ieee.mant_lo)
  1202. && (idp->mant_4 == lim->ieee.mant_4)
  1203. && (idp->mant_5 == lim->ieee.mant_5)
  1204. && (idp->mant_6 == lim->ieee.mant_6)
  1205. && (idp->exp_lo == lim->ieee.exp_lo)
  1206. && (idp->exp_hi == lim->ieee.exp_hi)
  1207. )
  1208. {
  1209. *vdp = lim->d;
  1210. goto doneit;
  1211. }
  1212. }
  1213. }
  1214. {
  1215. unsigned exp = idp->exp_hi << 4 | idp->exp_lo;
  1216. vdp->exp = exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
  1217. }
  1218. {
  1219. unsigned mant_hi = ((idp->mant_6 << 16)
  1220. | (idp->mant_5 << 8)
  1221. | idp->mant_4);
  1222. unsigned mant_lo = SWAP4(idp->mant_lo);
  1223. vdp->mantissa1 = (mant_hi >> 13);
  1224. vdp->mantissa2 = ((mant_hi & MASK(13)) << 3)
  1225. | (mant_lo >> 29);
  1226. vdp->mantissa3 = (mant_lo >> 13);
  1227. vdp->mantissa4 = (mant_lo << 3);
  1228. }
  1229. doneit:
  1230. vdp->sign = idp->sign;
  1231. }
  1232. static void
  1233. put_ix_double(void *xp, const double *ip)
  1234. {
  1235. const struct vax_double *const vdp =
  1236. (const struct vax_double *)ip;
  1237. struct ieee_double *const idp =
  1238. (struct ieee_double *) xp;
  1239. if ((vdp->mantissa4 > (dbl_limits[0].d.mantissa4 - 3)) &&
  1240. (vdp->mantissa3 == dbl_limits[0].d.mantissa3) &&
  1241. (vdp->mantissa2 == dbl_limits[0].d.mantissa2) &&
  1242. (vdp->mantissa1 == dbl_limits[0].d.mantissa1) &&
  1243. (vdp->exp == dbl_limits[0].d.exp))
  1244. {
  1245. *idp = dbl_limits[0].ieee;
  1246. goto shipit;
  1247. }
  1248. if ((vdp->mantissa4 == dbl_limits[1].d.mantissa4) &&
  1249. (vdp->mantissa3 == dbl_limits[1].d.mantissa3) &&
  1250. (vdp->mantissa2 == dbl_limits[1].d.mantissa2) &&
  1251. (vdp->mantissa1 == dbl_limits[1].d.mantissa1) &&
  1252. (vdp->exp == dbl_limits[1].d.exp))
  1253. {
  1254. *idp = dbl_limits[1].ieee;
  1255. goto shipit;
  1256. }
  1257. {
  1258. unsigned exp = vdp->exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
  1259. unsigned mant_lo = ((vdp->mantissa2 & MASK(3)) << 29) |
  1260. (vdp->mantissa3 << 13) |
  1261. ((vdp->mantissa4 >> 3) & MASK(13));
  1262. unsigned mant_hi = (vdp->mantissa1 << 13)
  1263. | (vdp->mantissa2 >> 3);
  1264. if((vdp->mantissa4 & 7) > 4)
  1265. {
  1266. /* round up */
  1267. mant_lo++;
  1268. if(mant_lo == 0)
  1269. {
  1270. mant_hi++;
  1271. if(mant_hi > 0xffffff)
  1272. {
  1273. mant_hi = 0;
  1274. exp++;
  1275. }
  1276. }
  1277. }
  1278. idp->mant_lo = SWAP4(mant_lo);
  1279. idp->mant_6 = mant_hi >> 16;
  1280. idp->mant_5 = (mant_hi & 0xff00) >> 8;
  1281. idp->mant_4 = mant_hi;
  1282. idp->exp_hi = exp >> 4;
  1283. idp->exp_lo = exp;
  1284. }
  1285. shipit:
  1286. idp->sign = vdp->sign;
  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 */
  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 */
  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. /* schar */
  1648. int
  1649. ncx_getn_schar_schar(const void **xpp, size_t nelems, schar *tp)
  1650. {
  1651. (void) memcpy(tp, *xpp, nelems);
  1652. *xpp = (void *)((char *)(*xpp) + nelems);
  1653. return ENOERR;
  1654. }
  1655. int
  1656. ncx_getn_schar_uchar(const void **xpp, size_t nelems, uchar *tp)
  1657. {
  1658. (void) memcpy(tp, *xpp, nelems);
  1659. *xpp = (void *)((char *)(*xpp) + nelems);
  1660. return ENOERR;
  1661. }
  1662. int
  1663. ncx_getn_schar_short(const void **xpp, size_t nelems, short *tp)
  1664. {
  1665. schar *xp = (schar *)(*xpp);
  1666. while(nelems-- != 0)
  1667. {
  1668. *tp++ = *xp++;
  1669. }
  1670. *xpp = (const void *)xp;
  1671. return ENOERR;
  1672. }
  1673. int
  1674. ncx_getn_schar_int(const void **xpp, size_t nelems, int *tp)
  1675. {
  1676. schar *xp = (schar *)(*xpp);
  1677. while(nelems-- != 0)
  1678. {
  1679. *tp++ = *xp++;
  1680. }
  1681. *xpp = (const void *)xp;
  1682. return ENOERR;
  1683. }
  1684. int
  1685. ncx_getn_schar_long(const void **xpp, size_t nelems, long *tp)
  1686. {
  1687. schar *xp = (schar *)(*xpp);
  1688. while(nelems-- != 0)
  1689. {
  1690. *tp++ = *xp++;
  1691. }
  1692. *xpp = (const void *)xp;
  1693. return ENOERR;
  1694. }
  1695. int
  1696. ncx_getn_schar_float(const void **xpp, size_t nelems, float *tp)
  1697. {
  1698. schar *xp = (schar *)(*xpp);
  1699. while(nelems-- != 0)
  1700. {
  1701. *tp++ = *xp++;
  1702. }
  1703. *xpp = (const void *)xp;
  1704. return ENOERR;
  1705. }
  1706. int
  1707. ncx_getn_schar_double(const void **xpp, size_t nelems, double *tp)
  1708. {
  1709. schar *xp = (schar *)(*xpp);
  1710. while(nelems-- != 0)
  1711. {
  1712. *tp++ = *xp++;
  1713. }
  1714. *xpp = (const void *)xp;
  1715. return ENOERR;
  1716. }
  1717. int
  1718. ncx_pad_getn_schar_schar(const void **xpp, size_t nelems, schar *tp)
  1719. {
  1720. size_t rndup = nelems % X_ALIGN;
  1721. if(rndup)
  1722. rndup = X_ALIGN - rndup;
  1723. (void) memcpy(tp, *xpp, nelems);
  1724. *xpp = (void *)((char *)(*xpp) + nelems + rndup);
  1725. return ENOERR;
  1726. }
  1727. int
  1728. ncx_pad_getn_schar_uchar(const void **xpp, size_t nelems, uchar *tp)
  1729. {
  1730. size_t rndup = nelems % X_ALIGN;
  1731. if(rndup)
  1732. rndup = X_ALIGN - rndup;
  1733. (void) memcpy(tp, *xpp, nelems);
  1734. *xpp = (void *)((char *)(*xpp) + nelems + rndup);
  1735. return ENOERR;
  1736. }
  1737. int
  1738. ncx_pad_getn_schar_short(const void **xpp, size_t nelems, short *tp)
  1739. {
  1740. size_t rndup = nelems % X_ALIGN;
  1741. schar *xp = (schar *) *xpp;
  1742. if(rndup)
  1743. rndup = X_ALIGN - rndup;
  1744. while(nelems-- != 0)
  1745. {
  1746. *tp++ = *xp++;
  1747. }
  1748. *xpp = (void *)(xp + rndup);
  1749. return ENOERR;
  1750. }
  1751. int
  1752. ncx_pad_getn_schar_int(const void **xpp, size_t nelems, int *tp)
  1753. {
  1754. size_t rndup = nelems % X_ALIGN;
  1755. schar *xp = (schar *) *xpp;
  1756. if(rndup)
  1757. rndup = X_ALIGN - rndup;
  1758. while(nelems-- != 0)
  1759. {
  1760. *tp++ = *xp++;
  1761. }
  1762. *xpp = (void *)(xp + rndup);
  1763. return ENOERR;
  1764. }
  1765. int
  1766. ncx_pad_getn_schar_long(const void **xpp, size_t nelems, long *tp)
  1767. {
  1768. size_t rndup = nelems % X_ALIGN;
  1769. schar *xp = (schar *) *xpp;
  1770. if(rndup)
  1771. rndup = X_ALIGN - rndup;
  1772. while(nelems-- != 0)
  1773. {
  1774. *tp++ = *xp++;
  1775. }
  1776. *xpp = (void *)(xp + rndup);
  1777. return ENOERR;
  1778. }
  1779. int
  1780. ncx_pad_getn_schar_float(const void **xpp, size_t nelems, float *tp)
  1781. {
  1782. size_t rndup = nelems % X_ALIGN;
  1783. schar *xp = (schar *) *xpp;
  1784. if(rndup)
  1785. rndup = X_ALIGN - rndup;
  1786. while(nelems-- != 0)
  1787. {
  1788. *tp++ = *xp++;
  1789. }
  1790. *xpp = (void *)(xp + rndup);
  1791. return ENOERR;
  1792. }
  1793. int
  1794. ncx_pad_getn_schar_double(const void **xpp, size_t nelems, double *tp)
  1795. {
  1796. size_t rndup = nelems % X_ALIGN;
  1797. schar *xp = (schar *) *xpp;
  1798. if(rndup)
  1799. rndup = X_ALIGN - rndup;
  1800. while(nelems-- != 0)
  1801. {
  1802. *tp++ = *xp++;
  1803. }
  1804. *xpp = (void *)(xp + rndup);
  1805. return ENOERR;
  1806. }
  1807. int
  1808. ncx_putn_schar_schar(void **xpp, size_t nelems, const schar *tp)
  1809. {
  1810. (void) memcpy(*xpp, tp, nelems);
  1811. *xpp = (void *)((char *)(*xpp) + nelems);
  1812. return ENOERR;
  1813. }
  1814. int
  1815. ncx_putn_schar_uchar(void **xpp, size_t nelems, const uchar *tp)
  1816. {
  1817. (void) memcpy(*xpp, tp, nelems);
  1818. *xpp = (void *)((char *)(*xpp) + nelems);
  1819. return ENOERR;
  1820. }
  1821. int
  1822. ncx_putn_schar_short(void **xpp, size_t nelems, const short *tp)
  1823. {
  1824. int status = ENOERR;
  1825. schar *xp = (schar *) *xpp;
  1826. while(nelems-- != 0)
  1827. {
  1828. if(*tp > X_SCHAR_MAX || *tp < X_SCHAR_MIN)
  1829. status = NC_ERANGE;
  1830. *xp++ = (schar) *tp++;
  1831. }
  1832. *xpp = (void *)xp;
  1833. return status;
  1834. }
  1835. int
  1836. ncx_putn_schar_int(void **xpp, size_t nelems, const int *tp)
  1837. {
  1838. int status = ENOERR;
  1839. schar *xp = (schar *) *xpp;
  1840. while(nelems-- != 0)
  1841. {
  1842. if(*tp > X_SCHAR_MAX || *tp < X_SCHAR_MIN)
  1843. status = NC_ERANGE;
  1844. *xp++ = (schar) *tp++;
  1845. }
  1846. *xpp = (void *)xp;
  1847. return status;
  1848. }
  1849. int
  1850. ncx_putn_schar_long(void **xpp, size_t nelems, const long *tp)
  1851. {
  1852. int status = ENOERR;
  1853. schar *xp = (schar *) *xpp;
  1854. while(nelems-- != 0)
  1855. {
  1856. if(*tp > X_SCHAR_MAX || *tp < X_SCHAR_MIN)
  1857. status = NC_ERANGE;
  1858. *xp++ = (schar) *tp++;
  1859. }
  1860. *xpp = (void *)xp;
  1861. return status;
  1862. }
  1863. int
  1864. ncx_putn_schar_float(void **xpp, size_t nelems, const float *tp)
  1865. {
  1866. int status = ENOERR;
  1867. schar *xp = (schar *) *xpp;
  1868. while(nelems-- != 0)
  1869. {
  1870. if(*tp > X_SCHAR_MAX || *tp < X_SCHAR_MIN)
  1871. status = NC_ERANGE;
  1872. *xp++ = (schar) *tp++;
  1873. }
  1874. *xpp = (void *)xp;
  1875. return status;
  1876. }
  1877. int
  1878. ncx_putn_schar_double(void **xpp, size_t nelems, const double *tp)
  1879. {
  1880. int status = ENOERR;
  1881. schar *xp = (schar *) *xpp;
  1882. while(nelems-- != 0)
  1883. {
  1884. if(*tp > X_SCHAR_MAX || *tp < X_SCHAR_MIN)
  1885. status = NC_ERANGE;
  1886. *xp++ = (schar) *tp++;
  1887. }
  1888. *xpp = (void *)xp;
  1889. return status;
  1890. }
  1891. int
  1892. ncx_pad_putn_schar_schar(void **xpp, size_t nelems, const schar *tp)
  1893. {
  1894. size_t rndup = nelems % X_ALIGN;
  1895. if(rndup)
  1896. rndup = X_ALIGN - rndup;
  1897. (void) memcpy(*xpp, tp, nelems);
  1898. *xpp = (void *)((char *)(*xpp) + nelems);
  1899. if(rndup)
  1900. {
  1901. (void) memcpy(*xpp, nada, rndup);
  1902. *xpp = (void *)((char *)(*xpp) + rndup);
  1903. }
  1904. return ENOERR;
  1905. }
  1906. int
  1907. ncx_pad_putn_schar_uchar(void **xpp, size_t nelems, const uchar *tp)
  1908. {
  1909. size_t rndup = nelems % X_ALIGN;
  1910. if(rndup)
  1911. rndup = X_ALIGN - rndup;
  1912. (void) memcpy(*xpp, tp, nelems);
  1913. *xpp = (void *)((char *)(*xpp) + nelems);
  1914. if(rndup)
  1915. {
  1916. (void) memcpy(*xpp, nada, rndup);
  1917. *xpp = (void *)((char *)(*xpp) + rndup);
  1918. }
  1919. return ENOERR;
  1920. }
  1921. int
  1922. ncx_pad_putn_schar_short(void **xpp, size_t nelems, const short *tp)
  1923. {
  1924. int status = ENOERR;
  1925. size_t rndup = nelems % X_ALIGN;
  1926. schar *xp = (schar *) *xpp;
  1927. if(rndup)
  1928. rndup = X_ALIGN - rndup;
  1929. while(nelems-- != 0)
  1930. {
  1931. /* N.B. schar as signed */
  1932. if(*tp > X_SCHAR_MAX || *tp < X_SCHAR_MIN)
  1933. status = NC_ERANGE;
  1934. *xp++ = (schar) *tp++;
  1935. }
  1936. if(rndup)
  1937. {
  1938. (void) memcpy(xp, nada, rndup);
  1939. xp += rndup;
  1940. }
  1941. *xpp = (void *)xp;
  1942. return status;
  1943. }
  1944. int
  1945. ncx_pad_putn_schar_int(void **xpp, size_t nelems, const int *tp)
  1946. {
  1947. int status = ENOERR;
  1948. size_t rndup = nelems % X_ALIGN;
  1949. schar *xp = (schar *) *xpp;
  1950. if(rndup)
  1951. rndup = X_ALIGN - rndup;
  1952. while(nelems-- != 0)
  1953. {
  1954. /* N.B. schar as signed */
  1955. if(*tp > X_SCHAR_MAX || *tp < X_SCHAR_MIN)
  1956. status = NC_ERANGE;
  1957. *xp++ = (schar) *tp++;
  1958. }
  1959. if(rndup)
  1960. {
  1961. (void) memcpy(xp, nada, rndup);
  1962. xp += rndup;
  1963. }
  1964. *xpp = (void *)xp;
  1965. return status;
  1966. }
  1967. int
  1968. ncx_pad_putn_schar_long(void **xpp, size_t nelems, const long *tp)
  1969. {
  1970. int status = ENOERR;
  1971. size_t rndup = nelems % X_ALIGN;
  1972. schar *xp = (schar *) *xpp;
  1973. if(rndup)
  1974. rndup = X_ALIGN - rndup;
  1975. while(nelems-- != 0)
  1976. {
  1977. /* N.B. schar as signed */
  1978. if(*tp > X_SCHAR_MAX || *tp < X_SCHAR_MIN)
  1979. status = NC_ERANGE;
  1980. *xp++ = (schar) *tp++;
  1981. }
  1982. if(rndup)
  1983. {
  1984. (void) memcpy(xp, nada, rndup);
  1985. xp += rndup;
  1986. }
  1987. *xpp = (void *)xp;
  1988. return status;
  1989. }
  1990. int
  1991. ncx_pad_putn_schar_float(void **xpp, size_t nelems, const float *tp)
  1992. {
  1993. int status = ENOERR;
  1994. size_t rndup = nelems % X_ALIGN;
  1995. schar *xp = (schar *) *xpp;
  1996. if(rndup)
  1997. rndup = X_ALIGN - rndup;
  1998. while(nelems-- != 0)
  1999. {
  2000. /* N.B. schar as signed */
  2001. if(*tp > X_SCHAR_MAX || *tp < X_SCHAR_MIN)
  2002. status = NC_ERANGE;
  2003. *xp++ = (schar) *tp++;
  2004. }
  2005. if(rndup)
  2006. {
  2007. (void) memcpy(xp, nada, rndup);
  2008. xp += rndup;
  2009. }
  2010. *xpp = (void *)xp;
  2011. return status;
  2012. }
  2013. int
  2014. ncx_pad_putn_schar_double(void **xpp, size_t nelems, const double *tp)
  2015. {
  2016. int status = ENOERR;
  2017. size_t rndup = nelems % X_ALIGN;
  2018. schar *xp = (schar *) *xpp;
  2019. if(rndup)
  2020. rndup = X_ALIGN - rndup;
  2021. while(nelems-- != 0)
  2022. {
  2023. /* N.B. schar as signed */
  2024. if(*tp > X_SCHAR_MAX || *tp < X_SCHAR_MIN)
  2025. status = NC_ERANGE;
  2026. *xp++ = (schar) *tp++;
  2027. }
  2028. if(rndup)
  2029. {
  2030. (void) memcpy(xp, nada, rndup);
  2031. xp += rndup;
  2032. }
  2033. *xpp = (void *)xp;
  2034. return status;
  2035. }
  2036. /* short */
  2037. int
  2038. ncx_getn_short_schar(const void **xpp, size_t nelems, schar *tp)
  2039. {
  2040. const char *xp = (const char *) *xpp;
  2041. int status = ENOERR;
  2042. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2043. {
  2044. const int lstatus = ncx_get_short_schar(xp, tp);
  2045. if(lstatus != ENOERR)
  2046. status = lstatus;
  2047. }
  2048. *xpp = (const void *)xp;
  2049. return status;
  2050. }
  2051. int
  2052. ncx_getn_short_uchar(const void **xpp, size_t nelems, uchar *tp)
  2053. {
  2054. const char *xp = (const char *) *xpp;
  2055. int status = ENOERR;
  2056. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2057. {
  2058. const int lstatus = ncx_get_short_uchar(xp, tp);
  2059. if(lstatus != ENOERR)
  2060. status = lstatus;
  2061. }
  2062. *xpp = (const void *)xp;
  2063. return status;
  2064. }
  2065. #if X_SIZEOF_SHORT == SIZEOF_SHORT
  2066. /* optimized version */
  2067. int
  2068. ncx_getn_short_short(const void **xpp, size_t nelems, short *tp)
  2069. {
  2070. # if WORDS_BIGENDIAN
  2071. (void) memcpy(tp, *xpp, nelems * sizeof(short));
  2072. # else
  2073. swapn2b(tp, *xpp, nelems);
  2074. # endif
  2075. *xpp = (const void *)((const char *)(*xpp) + nelems * X_SIZEOF_SHORT);
  2076. return ENOERR;
  2077. }
  2078. #else
  2079. int
  2080. ncx_getn_short_short(const void **xpp, size_t nelems, short *tp)
  2081. {
  2082. const char *xp = (const char *) *xpp;
  2083. int status = ENOERR;
  2084. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2085. {
  2086. const int lstatus = ncx_get_short_short(xp, tp);
  2087. if(lstatus != ENOERR)
  2088. status = lstatus;
  2089. }
  2090. *xpp = (const void *)xp;
  2091. return status;
  2092. }
  2093. #endif
  2094. int
  2095. ncx_getn_short_int(const void **xpp, size_t nelems, int *tp)
  2096. {
  2097. const char *xp = (const char *) *xpp;
  2098. int status = ENOERR;
  2099. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2100. {
  2101. const int lstatus = ncx_get_short_int(xp, tp);
  2102. if(lstatus != ENOERR)
  2103. status = lstatus;
  2104. }
  2105. *xpp = (const void *)xp;
  2106. return status;
  2107. }
  2108. int
  2109. ncx_getn_short_long(const void **xpp, size_t nelems, long *tp)
  2110. {
  2111. const char *xp = (const char *) *xpp;
  2112. int status = ENOERR;
  2113. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2114. {
  2115. const int lstatus = ncx_get_short_long(xp, tp);
  2116. if(lstatus != ENOERR)
  2117. status = lstatus;
  2118. }
  2119. *xpp = (const void *)xp;
  2120. return status;
  2121. }
  2122. int
  2123. ncx_getn_short_float(const void **xpp, size_t nelems, float *tp)
  2124. {
  2125. const char *xp = (const char *) *xpp;
  2126. int status = ENOERR;
  2127. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2128. {
  2129. const int lstatus = ncx_get_short_float(xp, tp);
  2130. if(lstatus != ENOERR)
  2131. status = lstatus;
  2132. }
  2133. *xpp = (const void *)xp;
  2134. return status;
  2135. }
  2136. int
  2137. ncx_getn_short_double(const void **xpp, size_t nelems, double *tp)
  2138. {
  2139. const char *xp = (const char *) *xpp;
  2140. int status = ENOERR;
  2141. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2142. {
  2143. const int lstatus = ncx_get_short_double(xp, tp);
  2144. if(lstatus != ENOERR)
  2145. status = lstatus;
  2146. }
  2147. *xpp = (const void *)xp;
  2148. return status;
  2149. }
  2150. int
  2151. ncx_pad_getn_short_schar(const void **xpp, size_t nelems, schar *tp)
  2152. {
  2153. const size_t rndup = nelems % 2;
  2154. const char *xp = (const char *) *xpp;
  2155. int status = ENOERR;
  2156. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2157. {
  2158. const int lstatus = ncx_get_short_schar(xp, tp);
  2159. if(lstatus != ENOERR)
  2160. status = lstatus;
  2161. }
  2162. if(rndup != 0)
  2163. xp += X_SIZEOF_SHORT;
  2164. *xpp = (void *)xp;
  2165. return status;
  2166. }
  2167. int
  2168. ncx_pad_getn_short_uchar(const void **xpp, size_t nelems, uchar *tp)
  2169. {
  2170. const size_t rndup = nelems % 2;
  2171. const char *xp = (const char *) *xpp;
  2172. int status = ENOERR;
  2173. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2174. {
  2175. const int lstatus = ncx_get_short_uchar(xp, tp);
  2176. if(lstatus != ENOERR)
  2177. status = lstatus;
  2178. }
  2179. if(rndup != 0)
  2180. xp += X_SIZEOF_SHORT;
  2181. *xpp = (void *)xp;
  2182. return status;
  2183. }
  2184. int
  2185. ncx_pad_getn_short_short(const void **xpp, size_t nelems, short *tp)
  2186. {
  2187. const size_t rndup = nelems % 2;
  2188. const char *xp = (const char *) *xpp;
  2189. int status = ENOERR;
  2190. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2191. {
  2192. const int lstatus = ncx_get_short_short(xp, tp);
  2193. if(lstatus != ENOERR)
  2194. status = lstatus;
  2195. }
  2196. if(rndup != 0)
  2197. xp += X_SIZEOF_SHORT;
  2198. *xpp = (void *)xp;
  2199. return status;
  2200. }
  2201. int
  2202. ncx_pad_getn_short_int(const void **xpp, size_t nelems, int *tp)
  2203. {
  2204. const size_t rndup = nelems % 2;
  2205. const char *xp = (const char *) *xpp;
  2206. int status = ENOERR;
  2207. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2208. {
  2209. const int lstatus = ncx_get_short_int(xp, tp);
  2210. if(lstatus != ENOERR)
  2211. status = lstatus;
  2212. }
  2213. if(rndup != 0)
  2214. xp += X_SIZEOF_SHORT;
  2215. *xpp = (void *)xp;
  2216. return status;
  2217. }
  2218. int
  2219. ncx_pad_getn_short_long(const void **xpp, size_t nelems, long *tp)
  2220. {
  2221. const size_t rndup = nelems % 2;
  2222. const char *xp = (const char *) *xpp;
  2223. int status = ENOERR;
  2224. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2225. {
  2226. const int lstatus = ncx_get_short_long(xp, tp);
  2227. if(lstatus != ENOERR)
  2228. status = lstatus;
  2229. }
  2230. if(rndup != 0)
  2231. xp += X_SIZEOF_SHORT;
  2232. *xpp = (void *)xp;
  2233. return status;
  2234. }
  2235. int
  2236. ncx_pad_getn_short_float(const void **xpp, size_t nelems, float *tp)
  2237. {
  2238. const size_t rndup = nelems % 2;
  2239. const char *xp = (const char *) *xpp;
  2240. int status = ENOERR;
  2241. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2242. {
  2243. const int lstatus = ncx_get_short_float(xp, tp);
  2244. if(lstatus != ENOERR)
  2245. status = lstatus;
  2246. }
  2247. if(rndup != 0)
  2248. xp += X_SIZEOF_SHORT;
  2249. *xpp = (void *)xp;
  2250. return status;
  2251. }
  2252. int
  2253. ncx_pad_getn_short_double(const void **xpp, size_t nelems, double *tp)
  2254. {
  2255. const size_t rndup = nelems % 2;
  2256. const char *xp = (const char *) *xpp;
  2257. int status = ENOERR;
  2258. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2259. {
  2260. const int lstatus = ncx_get_short_double(xp, tp);
  2261. if(lstatus != ENOERR)
  2262. status = lstatus;
  2263. }
  2264. if(rndu

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