PageRenderTime 36ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://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
  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(rndup != 0)
  2265. xp += X_SIZEOF_SHORT;
  2266. *xpp = (void *)xp;
  2267. return status;
  2268. }
  2269. int
  2270. ncx_putn_short_schar(void **xpp, size_t nelems, const schar *tp)
  2271. {
  2272. char *xp = (char *) *xpp;
  2273. int status = ENOERR;
  2274. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2275. {
  2276. int lstatus = ncx_put_short_schar(xp, tp);
  2277. if(lstatus != ENOERR)
  2278. status = lstatus;
  2279. }
  2280. *xpp = (void *)xp;
  2281. return status;
  2282. }
  2283. int
  2284. ncx_putn_short_uchar(void **xpp, size_t nelems, const uchar *tp)
  2285. {
  2286. char *xp = (char *) *xpp;
  2287. int status = ENOERR;
  2288. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2289. {
  2290. int lstatus = ncx_put_short_uchar(xp, tp);
  2291. if(lstatus != ENOERR)
  2292. status = lstatus;
  2293. }
  2294. *xpp = (void *)xp;
  2295. return status;
  2296. }
  2297. #if X_SIZEOF_SHORT == SIZEOF_SHORT
  2298. /* optimized version */
  2299. int
  2300. ncx_putn_short_short(void **xpp, size_t nelems, const short *tp)
  2301. {
  2302. # if WORDS_BIGENDIAN
  2303. (void) memcpy(*xpp, tp, nelems * X_SIZEOF_SHORT);
  2304. # else
  2305. swapn2b(*xpp, tp, nelems);
  2306. # endif
  2307. *xpp = (void *)((char *)(*xpp) + nelems * X_SIZEOF_SHORT);
  2308. return ENOERR;
  2309. }
  2310. #else
  2311. int
  2312. ncx_putn_short_short(void **xpp, size_t nelems, const short *tp)
  2313. {
  2314. char *xp = (char *) *xpp;
  2315. int status = ENOERR;
  2316. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2317. {
  2318. int lstatus = ncx_put_short_short(xp, tp);
  2319. if(lstatus != ENOERR)
  2320. status = lstatus;
  2321. }
  2322. *xpp = (void *)xp;
  2323. return status;
  2324. }
  2325. #endif
  2326. int
  2327. ncx_putn_short_int(void **xpp, size_t nelems, const int *tp)
  2328. {
  2329. char *xp = (char *) *xpp;
  2330. int status = ENOERR;
  2331. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2332. {
  2333. int lstatus = ncx_put_short_int(xp, tp);
  2334. if(lstatus != ENOERR)
  2335. status = lstatus;
  2336. }
  2337. *xpp = (void *)xp;
  2338. return status;
  2339. }
  2340. int
  2341. ncx_putn_short_long(void **xpp, size_t nelems, const long *tp)
  2342. {
  2343. char *xp = (char *) *xpp;
  2344. int status = ENOERR;
  2345. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2346. {
  2347. int lstatus = ncx_put_short_long(xp, tp);
  2348. if(lstatus != ENOERR)
  2349. status = lstatus;
  2350. }
  2351. *xpp = (void *)xp;
  2352. return status;
  2353. }
  2354. int
  2355. ncx_putn_short_float(void **xpp, size_t nelems, const float *tp)
  2356. {
  2357. char *xp = (char *) *xpp;
  2358. int status = ENOERR;
  2359. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2360. {
  2361. int lstatus = ncx_put_short_float(xp, tp);
  2362. if(lstatus != ENOERR)
  2363. status = lstatus;
  2364. }
  2365. *xpp = (void *)xp;
  2366. return status;
  2367. }
  2368. int
  2369. ncx_putn_short_double(void **xpp, size_t nelems, const double *tp)
  2370. {
  2371. char *xp = (char *) *xpp;
  2372. int status = ENOERR;
  2373. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2374. {
  2375. int lstatus = ncx_put_short_double(xp, tp);
  2376. if(lstatus != ENOERR)
  2377. status = lstatus;
  2378. }
  2379. *xpp = (void *)xp;
  2380. return status;
  2381. }
  2382. int
  2383. ncx_pad_putn_short_schar(void **xpp, size_t nelems, const schar *tp)
  2384. {
  2385. const size_t rndup = nelems % 2;
  2386. char *xp = (char *) *xpp;
  2387. int status = ENOERR;
  2388. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2389. {
  2390. int lstatus = ncx_put_short_schar(xp, tp);
  2391. if(lstatus != ENOERR)
  2392. status = lstatus;
  2393. }
  2394. if(rndup != 0)
  2395. {
  2396. (void) memcpy(xp, nada, X_SIZEOF_SHORT);
  2397. xp += X_SIZEOF_SHORT;
  2398. }
  2399. *xpp = (void *)xp;
  2400. return status;
  2401. }
  2402. int
  2403. ncx_pad_putn_short_uchar(void **xpp, size_t nelems, const uchar *tp)
  2404. {
  2405. const size_t rndup = nelems % 2;
  2406. char *xp = (char *) *xpp;
  2407. int status = ENOERR;
  2408. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2409. {
  2410. int lstatus = ncx_put_short_uchar(xp, tp);
  2411. if(lstatus != ENOERR)
  2412. status = lstatus;
  2413. }
  2414. if(rndup != 0)
  2415. {
  2416. (void) memcpy(xp, nada, X_SIZEOF_SHORT);
  2417. xp += X_SIZEOF_SHORT;
  2418. }
  2419. *xpp = (void *)xp;
  2420. return status;
  2421. }
  2422. int
  2423. ncx_pad_putn_short_short(void **xpp, size_t nelems, const short *tp)
  2424. {
  2425. const size_t rndup = nelems % 2;
  2426. char *xp = (char *) *xpp;
  2427. int status = ENOERR;
  2428. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2429. {
  2430. int lstatus = ncx_put_short_short(xp, tp);
  2431. if(lstatus != ENOERR)
  2432. status = lstatus;
  2433. }
  2434. if(rndup != 0)
  2435. {
  2436. (void) memcpy(xp, nada, X_SIZEOF_SHORT);
  2437. xp += X_SIZEOF_SHORT;
  2438. }
  2439. *xpp = (void *)xp;
  2440. return status;
  2441. }
  2442. int
  2443. ncx_pad_putn_short_int(void **xpp, size_t nelems, const int *tp)
  2444. {
  2445. const size_t rndup = nelems % 2;
  2446. char *xp = (char *) *xpp;
  2447. int status = ENOERR;
  2448. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2449. {
  2450. int lstatus = ncx_put_short_int(xp, tp);
  2451. if(lstatus != ENOERR)
  2452. status = lstatus;
  2453. }
  2454. if(rndup != 0)
  2455. {
  2456. (void) memcpy(xp, nada, X_SIZEOF_SHORT);
  2457. xp += X_SIZEOF_SHORT;
  2458. }
  2459. *xpp = (void *)xp;
  2460. return status;
  2461. }
  2462. int
  2463. ncx_pad_putn_short_long(void **xpp, size_t nelems, const long *tp)
  2464. {
  2465. const size_t rndup = nelems % 2;
  2466. char *xp = (char *) *xpp;
  2467. int status = ENOERR;
  2468. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2469. {
  2470. int lstatus = ncx_put_short_long(xp, tp);
  2471. if(lstatus != ENOERR)
  2472. status = lstatus;
  2473. }
  2474. if(rndup != 0)
  2475. {
  2476. (void) memcpy(xp, nada, X_SIZEOF_SHORT);
  2477. xp += X_SIZEOF_SHORT;
  2478. }
  2479. *xpp = (void *)xp;
  2480. return status;
  2481. }
  2482. int
  2483. ncx_pad_putn_short_float(void **xpp, size_t nelems, const float *tp)
  2484. {
  2485. const size_t rndup = nelems % 2;
  2486. char *xp = (char *) *xpp;
  2487. int status = ENOERR;
  2488. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2489. {
  2490. int lstatus = ncx_put_short_float(xp, tp);
  2491. if(lstatus != ENOERR)
  2492. status = lstatus;
  2493. }
  2494. if(rndup != 0)
  2495. {
  2496. (void) memcpy(xp, nada, X_SIZEOF_SHORT);
  2497. xp += X_SIZEOF_SHORT;
  2498. }
  2499. *xpp = (void *)xp;
  2500. return status;
  2501. }
  2502. int
  2503. ncx_pad_putn_short_double(void **xpp, size_t nelems, const double *tp)
  2504. {
  2505. const size_t rndup = nelems % 2;
  2506. char *xp = (char *) *xpp;
  2507. int status = ENOERR;
  2508. for( ; nelems != 0; nelems--, xp += X_SIZEOF_SHORT, tp++)
  2509. {
  2510. int lstatus = ncx_put_short_double(xp, tp);
  2511. if(lstatus != ENOERR)
  2512. status = lstatus;
  2513. }
  2514. if(rndup != 0)
  2515. {
  2516. (void) memcpy(xp, nada, X_SIZEOF_SHORT);
  2517. xp += X_SIZEOF_SHORT;
  2518. }
  2519. *xpp = (void *)xp;
  2520. return status;
  2521. }
  2522. /* int */
  2523. int
  2524. ncx_getn_int_schar(const void **xpp, size_t nelems, schar *tp)
  2525. {
  2526. const char *xp = (const char *) *xpp;
  2527. int status = ENOERR;
  2528. for( ; nelems != 0; nelems--, xp += X_SIZEOF_INT, tp++)
  2529. {
  2530. const int lstatus = ncx_get_int_schar(xp, tp);
  2531. if(lstatus != ENOERR)
  2532. status = lstatus;
  2533. }
  2534. *xpp = (const void *)xp;
  2535. return status;
  2536. }
  2537. int
  2538. ncx_getn_int_uchar(const void **xpp, size_t nelems, uchar *tp)
  2539. {
  2540. const char *xp = (const char *) *xpp;
  2541. int status = ENOERR;
  2542. for( ; nelems != 0; nelems--, xp += X_SIZEOF_INT, tp++)
  2543. {
  2544. const int lstatus = ncx_get_int_uchar(xp, tp);
  2545. if(lstatus != ENOERR)
  2546. status = lstatus;
  2547. }
  2548. *xpp = (const void *)xp;
  2549. return status;
  2550. }
  2551. int
  2552. ncx_getn_int_short(const void **xpp, size_t nelems, short *tp)
  2553. {
  2554. const char *xp = (const char *) *xpp;
  2555. int status = ENOERR;
  2556. for( ; nelems != 0; nelems--, xp += X_SIZEOF_INT, tp++)
  2557. {
  2558. const int lstatus = ncx_get_int_short(xp, tp);
  2559. if(lstatus != ENOERR)
  2560. status = lstatus;
  2561. }
  2562. *xpp = (const void *)xp;
  2563. return status;
  2564. }
  2565. #if X_SIZEOF_INT == SIZEOF_INT
  2566. /* optimized version */
  2567. int
  2568. ncx_getn_int_int(const void **xpp, size_t nelems, int *tp)
  2569. {
  2570. # if WORDS_BIGENDIAN
  2571. (void) memcpy(tp, *xpp, nelems * sizeof(int));
  2572. # else
  2573. swapn4b(tp, *xpp, nelems);
  2574. # endif
  2575. *xpp = (const void *)((const char *)(*xpp) + nelems * X_SIZEOF_INT);
  2576. return ENOERR;
  2577. }
  2578. #else
  2579. int
  2580. ncx_getn_int_int(const void **xpp, size_t nelems, int *tp)
  2581. {
  2582. const char *xp = (const char *) *xpp;
  2583. int status = ENOERR;
  2584. for( ; nelems != 0; nelems--, xp += X_SIZEOF_INT, tp++)
  2585. {
  2586. const int lstatus = ncx_get_int_int(xp, tp);
  2587. if(lstatus != ENOERR)
  2588. status = lstatus;
  2589. }
  2590. *xpp = (const void *)xp;
  2591. return status;
  2592. }
  2593. #endif
  2594. #if X_SIZEOF_INT == SIZEOF_LONG
  2595. /* optimized version */
  2596. int
  2597. ncx_getn_int_long(const void **xpp, size_t nelems, long *tp)
  2598. {
  2599. # if WORDS_BIGENDIAN
  2600. (void) memcpy(tp, *xpp, nelems * sizeof(long));
  2601. # else
  2602. swapn4b(tp, *xpp, nelems);
  2603. # endif
  2604. *xpp = (const void *)((const char *)(*xpp) + nelems * X_SIZEOF_INT);
  2605. return ENOERR;
  2606. }
  2607. #else
  2608. int
  2609. ncx_getn_int_long(const void **xpp, size_t nelems, long *tp)
  2610. {
  2611. const char *xp = (const char *) *xpp;
  2612. int status = ENOERR;
  2613. for( ; nelems != 0; nelems--, xp += X_SIZEOF_INT, tp++)
  2614. {
  2615. const int lstatus = ncx_get_int_long(xp, tp);
  2616. if(lstatus != ENOERR)
  2617. status = lstatus;
  2618. }
  2619. *xpp = (const void *)xp;
  2620. return status;
  2621. }
  2622. #endif
  2623. int
  2624. ncx_getn_int_float(const void **xpp, size_t nelems, float *tp)
  2625. {
  2626. const char *xp = (const char *) *xpp;
  2627. int status = ENOERR;
  2628. for( ; nelems != 0; nelems--, xp += X_SIZEOF_INT, tp++)
  2629. {
  2630. const int lstatus = ncx_get_int_float(xp, tp);
  2631. if(lstatus != ENOERR)
  2632. status = lstatus;
  2633. }
  2634. *xpp = (const void *)xp;
  2635. return status;
  2636. }
  2637. int
  2638. ncx_getn_int_double(const void **xpp, size_t nelems, double *tp)
  2639. {
  2640. const char *xp = (const char *) *xpp;
  2641. int status = ENOERR;
  2642. for( ; nelems != 0; nelems--, xp += X_SIZEOF_INT, tp++)
  2643. {
  2644. const int lstatus = ncx_get_int_double(xp, tp);
  2645. if(lstatus != ENOERR)
  2646. status = lstatus;
  2647. }
  2648. *xpp = (const void *)xp;
  2649. return status;
  2650. }
  2651. int
  2652. ncx_putn_int_schar(void **xpp, size_t nelems, const schar *tp)
  2653. {
  2654. char *xp = (char *) *xpp;
  2655. int status = ENOERR;
  2656. for( ; nelems != 0; nelems--, xp += X_SIZEOF_INT, tp++)
  2657. {
  2658. int lstatus = ncx_put_int_schar(xp, tp);
  2659. if(lstatus != ENOERR)
  2660. status = lstatus;
  2661. }
  2662. *xpp = (void *)xp;
  2663. return status;
  2664. }
  2665. int
  2666. ncx_putn_int_uchar(void **xpp, size_t nelems, const uchar *tp)
  2667. {
  2668. char *xp = (char *) *xpp;
  2669. int status = ENOERR;
  2670. for( ; nelems != 0; nelems--, xp += X_SIZEOF_INT, tp++)
  2671. {
  2672. int lstatus = ncx_put_int_uchar(xp, tp);
  2673. if(lstatus != ENOERR)
  2674. status = lstatus;
  2675. }
  2676. *xpp = (void *)xp;
  2677. return status;
  2678. }
  2679. int
  2680. ncx_putn_int_short(void **xpp, size_t nelems, const short *tp)
  2681. {
  2682. char *xp = (char *) *xpp;
  2683. int status = ENOERR;
  2684. for( ; nelems != 0; nelems--, xp += X_SIZEOF_INT, tp++)
  2685. {
  2686. int lstatus = ncx_put_int_short(xp, tp);
  2687. if(lstatus != ENOERR)
  2688. status = lstatus;
  2689. }
  2690. *xpp = (void *)xp;
  2691. return status;
  2692. }
  2693. #if X_SIZEOF_INT == SIZEOF_INT
  2694. /* optimized version */
  2695. int
  2696. ncx_putn_int_int(void **xpp, size_t nelems, const int *tp)
  2697. {
  2698. # if WORDS_BIGENDIAN
  2699. (void) memcpy(*xpp, tp, nelems * X_SIZEOF_INT);
  2700. # else
  2701. swapn4b(*xpp, tp, nelems);
  2702. # endif
  2703. *xpp = (void *)((char *)(*xpp) + nelems * X_SIZEOF_INT);
  2704. return ENOERR;
  2705. }
  2706. #else
  2707. int
  2708. ncx_putn_int_int(void **xpp, size_t nelems, const int *tp)
  2709. {
  2710. char *xp = (char *) *xpp;
  2711. int status = ENOERR;
  2712. for( ; nelems != 0; nelems--, xp += X_SIZEOF_INT, tp++)
  2713. {
  2714. int lstatus = ncx_put_int_int(xp, tp);
  2715. if(lstatus != ENOERR)
  2716. status = lstatus;
  2717. }
  2718. *xpp = (void *)xp;
  2719. return status;
  2720. }
  2721. #endif
  2722. #if X_SIZEOF_INT == SIZEOF_LONG
  2723. /* optimized version */
  2724. int
  2725. ncx_putn_int_long(void **xpp, size_t nelems, const long *tp)
  2726. {
  2727. # if WORDS_BIGENDIAN
  2728. (void) memcpy(*xpp, tp, nelems * X_SIZEOF_INT);
  2729. # else
  2730. swapn4b(*xpp, tp, nelems);
  2731. # endif
  2732. *xpp = (void *)((char *)(*xpp) + nelems * X_SIZEOF_INT);
  2733. return ENOERR;
  2734. }
  2735. #else
  2736. int
  2737. ncx_putn_int_long(void **xpp, size_t nelems, const long *tp)
  2738. {
  2739. char *xp = (char *) *xpp;
  2740. int status = ENOERR;
  2741. for( ; nelems != 0; nelems--, xp += X_SIZEOF_INT, tp++)
  2742. {
  2743. int lstatus = ncx_put_int_long(xp, tp);
  2744. if(lstatus != ENOERR)
  2745. status = lstatus;
  2746. }
  2747. *xpp = (void *)xp;
  2748. return status;
  2749. }
  2750. #endif
  2751. int
  2752. ncx_putn_int_float(void **xpp, size_t nelems, const float *tp)
  2753. {
  2754. char *xp = (char *) *xpp;
  2755. int status = ENOERR;
  2756. for( ; nelems != 0; nelems--, xp += X_SIZEOF_INT, tp++)
  2757. {
  2758. int lstatus = ncx_put_int_float(xp, tp);
  2759. if(lstatus != ENOERR)
  2760. status = lstatus;
  2761. }
  2762. *xpp = (void *)xp;
  2763. return status;
  2764. }
  2765. int
  2766. ncx_putn_int_double(void **xpp, size_t nelems, const double *tp)
  2767. {
  2768. char *xp = (char *) *xpp;
  2769. int status = ENOERR;
  2770. for( ; nelems != 0; nelems--, xp += X_SIZEOF_INT, tp++)
  2771. {
  2772. int lstatus = ncx_put_int_double(xp, tp);
  2773. if(lstatus != ENOERR)
  2774. status = lstatus;
  2775. }
  2776. *xpp = (void *)xp;
  2777. return status;
  2778. }
  2779. /* float */
  2780. int
  2781. ncx_getn_float_schar(const void **xpp, size_t nelems, schar *tp)
  2782. {
  2783. const char *xp = (const char *) *xpp;
  2784. int status = ENOERR;
  2785. for( ; nelems != 0; nelems--, xp += X_SIZEOF_FLOAT, tp++)
  2786. {
  2787. const int lstatus = ncx_get_float_schar(xp, tp);
  2788. if(lstatus != ENOERR)
  2789. status = lstatus;
  2790. }
  2791. *xpp = (const void *)xp;
  2792. return status;
  2793. }
  2794. int
  2795. ncx_getn_float_uchar(const void **xpp, size_t nelems, uchar *tp)
  2796. {
  2797. const char *xp = (const char *) *xpp;
  2798. int status = ENOERR;
  2799. for( ; nelems != 0; nelems--, xp += X_SIZEOF_FLOAT, tp++)
  2800. {
  2801. const int lstatus = ncx_get_float_uchar(xp, tp);
  2802. if(lstatus != ENOERR)
  2803. status = lstatus;
  2804. }
  2805. *xpp = (const void *)xp;
  2806. return status;
  2807. }
  2808. int
  2809. ncx_getn_float_short(const void **xpp, size_t nelems, short *tp)
  2810. {
  2811. const char *xp = (const char *) *xpp;
  2812. int status = ENOERR;
  2813. for( ; nelems != 0; nelems--, xp += X_SIZEOF_FLOAT, tp++)
  2814. {
  2815. const int lstatus = ncx_get_float_short(xp, tp);
  2816. if(lstatus != ENOERR)
  2817. status = lstatus;
  2818. }
  2819. *xpp = (const void *)xp;
  2820. return status;
  2821. }
  2822. int
  2823. ncx_getn_float_int(const void **xpp, size_t nelems, int *tp)
  2824. {
  2825. const char *xp = (const char *) *xpp;
  2826. int status = ENOERR;
  2827. for( ; nelems != 0; nelems--, xp += X_SIZEOF_FLOAT, tp++)
  2828. {
  2829. const int lstatus = ncx_get_float_int(xp, tp);
  2830. if(lstatus != ENOERR)
  2831. status = lstatus;
  2832. }
  2833. *xpp = (const void *)xp;
  2834. return status;
  2835. }
  2836. int
  2837. ncx_getn_float_long(const void **xpp, size_t nelems, long *tp)
  2838. {
  2839. const char *xp = (const char *) *xpp;
  2840. int status = ENOERR;
  2841. for( ; nelems != 0; nelems--, xp += X_SIZEOF_FLOAT, tp++)
  2842. {
  2843. const int lstatus = ncx_get_float_long(xp, tp);
  2844. if(lstatus != ENOERR)
  2845. status = lstatus;
  2846. }
  2847. *xpp = (const void *)xp;
  2848. return status;
  2849. }
  2850. #if X_SIZEOF_FLOAT == SIZEOF_FLOAT && !defined(NO_IEEE_FLOAT)
  2851. /* optimized version */
  2852. int
  2853. ncx_getn_float_float(const void **xpp, size_t nelems, float *tp)
  2854. {
  2855. # if WORDS_BIGENDIAN
  2856. (void) memcpy(tp, *xpp, nelems * sizeof(float));
  2857. # else
  2858. swapn4b(tp, *xpp, nelems);
  2859. # endif
  2860. *xpp = (const void *)((const char *)(*xpp) + nelems * X_SIZEOF_FLOAT);
  2861. return ENOERR;
  2862. }
  2863. #elif vax
  2864. int
  2865. ncx_getn_float_float(const void **xpp, size_t nfloats, float *ip)
  2866. {
  2867. float *const end = ip + nfloats;
  2868. while(ip < end)
  2869. {
  2870. struct vax_single *const vsp = (struct vax_single *) ip;
  2871. const struct ieee_single *const isp =
  2872. (const struct ieee_single *) (*xpp);
  2873. unsigned exp = isp->exp_hi << 1 | isp->exp_lo;
  2874. switch(exp) {
  2875. case 0 :
  2876. /* ieee subnormal */
  2877. if(isp->mant_hi == min.ieee.mant_hi
  2878. && isp->mant_lo_hi == min.ieee.mant_lo_hi
  2879. && isp->mant_lo_lo == min.ieee.mant_lo_lo)
  2880. {
  2881. *vsp = min.s;
  2882. }
  2883. else
  2884. {
  2885. unsigned mantissa = (isp->mant_hi << 16)
  2886. | isp->mant_lo_hi << 8
  2887. | isp->mant_lo_lo;
  2888. unsigned tmp = mantissa >> 20;
  2889. if(tmp >= 4) {
  2890. vsp->exp = 2;
  2891. } else if (tmp >= 2) {
  2892. vsp->exp = 1;
  2893. } else {
  2894. *vsp = min.s;
  2895. break;
  2896. } /* else */
  2897. tmp = mantissa - (1 << (20 + vsp->exp ));
  2898. tmp <<= 3 - vsp->exp;
  2899. vsp->mantissa2 = tmp;
  2900. vsp->mantissa1 = (tmp >> 16);
  2901. }
  2902. break;
  2903. case 0xfe :
  2904. case 0xff :
  2905. *vsp = max.s;
  2906. break;
  2907. default :
  2908. vsp->exp = exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
  2909. vsp->mantissa2 = isp->mant_lo_hi << 8 | isp->mant_lo_lo;
  2910. vsp->mantissa1 = isp->mant_hi;
  2911. }
  2912. vsp->sign = isp->sign;
  2913. ip++;
  2914. *xpp = (char *)(*xpp) + X_SIZEOF_FLOAT;
  2915. }
  2916. return ENOERR;
  2917. }
  2918. #elif _SX && _FLOAT2
  2919. int
  2920. ncx_getn_float_float(const void **xpp, size_t nelems, float *tp)
  2921. {
  2922. const char *const xp = *xpp;
  2923. const int ncnv = ie3_fl2(xp, tp, 4, 8, nelems);
  2924. *xpp = xp + nelems * X_SIZEOF_FLOAT;
  2925. return (nelems == ncnv ? ENOERR : NC_ERANGE);
  2926. }
  2927. #else
  2928. int
  2929. ncx_getn_float_float(const void **xpp, size_t nelems, float *tp)
  2930. {
  2931. const char *xp = *xpp;
  2932. int status = ENOERR;
  2933. for( ; nelems != 0; nelems--, xp += X_SIZEOF_FLOAT, tp++)
  2934. {
  2935. const int lstatus = ncx_get_float_float(xp, tp);
  2936. if(lstatus != ENOERR)
  2937. status = lstatus;
  2938. }
  2939. *xpp = (const void *)xp;
  2940. return status;
  2941. }
  2942. #endif
  2943. int
  2944. ncx_getn_float_double(const void **xpp, size_t nelems, double *tp)
  2945. {
  2946. const char *xp = (const char *) *xpp;
  2947. int status = ENOERR;
  2948. for( ; nelems != 0; nelems--, xp += X_SIZEOF_FLOAT, tp++)
  2949. {
  2950. const int lstatus = ncx_get_float_double(xp, tp);
  2951. if(lstatus != ENOERR)
  2952. status = lstatus;
  2953. }
  2954. *xpp = (const void *)xp;
  2955. return status;
  2956. }
  2957. int
  2958. ncx_putn_float_schar(void **xpp, size_t nelems, const schar *tp)
  2959. {
  2960. char *xp = (char *) *xpp;
  2961. int status = ENOERR;
  2962. for( ; nelems != 0; nelems--, xp += X_SIZEOF_FLOAT, tp++)
  2963. {
  2964. int lstatus = ncx_put_float_schar(xp, tp);
  2965. if(lstatus != ENOERR)
  2966. status = lstatus;
  2967. }
  2968. *xpp = (void *)xp;
  2969. return status;
  2970. }
  2971. int
  2972. ncx_putn_float_uchar(void **xpp, size_t nelems, const uchar *tp)
  2973. {
  2974. char *xp = (char *) *xpp;
  2975. int status = ENOERR;
  2976. for( ; nelems != 0; nelems--, xp += X_SIZEOF_FLOAT, tp++)
  2977. {
  2978. int lstatus = ncx_put_float_uchar(xp, tp);
  2979. if(lstatus != ENOERR)
  2980. status = lstatus;
  2981. }
  2982. *xpp = (void *)xp;
  2983. return status;
  2984. }
  2985. int
  2986. ncx_putn_float_short(void **xpp, size_t nelems, const short *tp)
  2987. {
  2988. char *xp = (char *) *xpp;
  2989. int status = ENOERR;
  2990. for( ; nelems != 0; nelems--, xp += X_SIZEOF_FLOAT, tp++)
  2991. {
  2992. int lstatus = ncx_put_float_short(xp, tp);
  2993. if(lstatus != ENOERR)
  2994. status = lstatus;
  2995. }
  2996. *xpp = (void *)xp;
  2997. return status;
  2998. }
  2999. int
  3000. ncx_putn_float_int(void **xpp, size_t nelems, const int *tp)
  3001. {
  3002. char *xp = (char *) *xpp;
  3003. int status = ENOERR;
  3004. for( ; nelems != 0; nelems--, xp += X_SIZEOF_FLOAT, tp++)
  3005. {
  3006. int lstatus = ncx_put_float_int(xp, tp);
  3007. if(lstatus != ENOERR)
  3008. status = lstatus;
  3009. }
  3010. *xpp = (void *)xp;
  3011. return status;
  3012. }
  3013. int
  3014. ncx_putn_float_long(void **xpp, size_t nelems, const long *tp)
  3015. {
  3016. char *xp = (char *) *xpp;
  3017. int status = ENOERR;
  3018. for( ; nelems != 0; nelems--, xp += X_SIZEOF_FLOAT, tp++)
  3019. {
  3020. int lstatus = ncx_put_float_long(xp, tp);
  3021. if(lstatus != ENOERR)
  3022. status = lstatus;
  3023. }
  3024. *xpp = (void *)xp;
  3025. return status;
  3026. }
  3027. #if X_SIZEOF_FLOAT == SIZEOF_FLOAT && !defined(NO_IEEE_FLOAT)
  3028. /* optimized version */
  3029. int
  3030. ncx_putn_float_float(void **xpp, size_t nelems, const float *tp)
  3031. {
  3032. # if WORDS_BIGENDIAN
  3033. (void) memcpy(*xpp, tp, nelems * X_SIZEOF_FLOAT);
  3034. # else
  3035. swapn4b(*xpp, tp, nelems);
  3036. # endif
  3037. *xpp = (void *)((char *)(*xpp) + nelems * X_SIZEOF_FLOAT);
  3038. return ENOERR;
  3039. }
  3040. #elif vax
  3041. int
  3042. ncx_putn_float_float(void **xpp, size_t nfloats, const float *ip)
  3043. {
  3044. const float *const end = ip + nfloats;
  3045. while(ip < end)
  3046. {
  3047. const struct vax_single *const vsp =
  3048. (const struct vax_single *)ip;
  3049. struct ieee_single *const isp = (struct ieee_single *) (*xpp);
  3050. switch(vsp->exp){
  3051. case 0 :
  3052. /* all vax float with zero exponent map to zero */
  3053. *isp = min.ieee;
  3054. break;
  3055. case 2 :
  3056. case 1 :
  3057. {
  3058. /* These will map to subnormals */
  3059. unsigned mantissa = (vsp->mantissa1 << 16)
  3060. | vsp->mantissa2;
  3061. mantissa >>= 3 - vsp->exp;
  3062. mantissa += (1 << (20 + vsp->exp));
  3063. isp->mant_lo_lo = mantissa;
  3064. isp->mant_lo_hi = mantissa >> 8;
  3065. isp->mant_hi = mantissa >> 16;
  3066. isp->exp_lo = 0;
  3067. isp->exp_hi = 0;
  3068. }
  3069. break;
  3070. case 0xff : /* max.s.exp */
  3071. if( vsp->mantissa2 == max.s.mantissa2
  3072. && vsp->mantissa1 == max.s.mantissa1)
  3073. {
  3074. /* map largest vax float to ieee infinity */
  3075. *isp = max.ieee;
  3076. break;
  3077. } /* else, fall thru */
  3078. default :
  3079. {
  3080. unsigned exp = vsp->exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
  3081. isp->exp_hi = exp >> 1;
  3082. isp->exp_lo = exp;
  3083. isp->mant_lo_lo = vsp->mantissa2;
  3084. isp->mant_lo_hi = vsp->mantissa2 >> 8;
  3085. isp->mant_hi = vsp->mantissa1;
  3086. }
  3087. }
  3088. isp->sign = vsp->sign;
  3089. ip++;
  3090. *xpp = (char *)(*xpp) + X_SIZEOF_FLOAT;
  3091. }
  3092. return ENOERR;
  3093. }
  3094. #elif _SX && _FLOAT2
  3095. int
  3096. ncx_putn_float_float(void **xpp, size_t nelems, const float *tp)
  3097. {
  3098. char *const xp = *xpp;
  3099. const int ncnv = fl2_ie3(tp, xp, 8, 4, nelems);
  3100. *xpp = xp + nelems * X_SIZEOF_FLOAT;
  3101. return (nelems == ncnv ? ENOERR : NC_ERANGE);
  3102. }
  3103. #else
  3104. int
  3105. ncx_putn_float_float(void **xpp, size_t nelems, const float *tp)
  3106. {
  3107. char *xp = *xpp;
  3108. int status = ENOERR;
  3109. for( ; nelems != 0; nelems--, xp += X_SIZEOF_FLOAT, tp++)
  3110. {
  3111. int lstatus = ncx_put_float_float(xp, tp);
  3112. if(lstatus != ENOERR)
  3113. status = lstatus;
  3114. }
  3115. *xpp = (void *)xp;
  3116. return status;
  3117. }
  3118. #endif
  3119. int
  3120. ncx_putn_float_double(void **xpp, size_t nelems, const double *tp)
  3121. {
  3122. char *xp = (char *) *xpp;
  3123. int status = ENOERR;
  3124. for( ; nelems != 0; nelems--, xp += X_SIZEOF_FLOAT, tp++)
  3125. {
  3126. int lstatus = ncx_put_float_double(xp, tp);
  3127. if(lstatus != ENOERR)
  3128. status = lstatus;
  3129. }
  3130. *xpp = (void *)xp;
  3131. return status;
  3132. }
  3133. /* double */
  3134. int
  3135. ncx_getn_double_schar(const void **xpp, size_t nelems, schar *tp)
  3136. {
  3137. const char *xp = (const char *) *xpp;
  3138. int status = ENOERR;
  3139. for( ; nelems != 0; nelems--, xp += X_SIZEOF_DOUBLE, tp++)
  3140. {
  3141. const int lstatus = ncx_get_double_schar(xp, tp);
  3142. if(lstatus != ENOERR)
  3143. status = lstatus;
  3144. }
  3145. *xpp = (const void *)xp;
  3146. return status;
  3147. }
  3148. int
  3149. ncx_getn_double_uchar(const void **xpp, size_t nelems, uchar *tp)
  3150. {
  3151. const char *xp = (const char *) *xpp;
  3152. int status = ENOERR;
  3153. for( ; nelems != 0; nelems--, xp += X_SIZEOF_DOUBLE, tp++)
  3154. {
  3155. const int lstatus = ncx_get_double_uchar(xp, tp);
  3156. if(lstatus != ENOERR)
  3157. status = lstatus;
  3158. }
  3159. *xpp = (const void *)xp;
  3160. return status;
  3161. }
  3162. int
  3163. ncx_getn_double_short(const void **xpp, size_t nelems, short *tp)
  3164. {
  3165. const char *xp = (const char *) *xpp;
  3166. int status = ENOERR;
  3167. for( ; nelems != 0; nelems--, xp += X_SIZEOF_DOUBLE, tp++)
  3168. {
  3169. const int lstatus = ncx_get_double_short(xp, tp);
  3170. if(lstatus != ENOERR)
  3171. status = lstatus;
  3172. }
  3173. *xpp = (const void *)xp;
  3174. return status;
  3175. }
  3176. int
  3177. ncx_getn_double_int(const void **xpp, size_t nelems, int *tp)
  3178. {
  3179. const char *xp = (const char *) *xpp;
  3180. int status = ENOERR;
  3181. for( ; nelems != 0; nelems--, xp += X_SIZEOF_DOUBLE, tp++)
  3182. {
  3183. const int lstatus = ncx_get_double_int(xp, tp);
  3184. if(lstatus != ENOERR)
  3185. status = lstatus;
  3186. }
  3187. *xpp = (const void *)xp;
  3188. return status;
  3189. }
  3190. int
  3191. ncx_getn_double_long(const void **xpp, size_t nelems, long *tp)
  3192. {
  3193. const char *xp = (const char *) *xpp;
  3194. int status = ENOERR;
  3195. for( ; nelems != 0; nelems--, xp += X_SIZEOF_DOUBLE, tp++)
  3196. {
  3197. const int lstatus = ncx_get_double_long(xp, tp);
  3198. if(lstatus != ENOERR)
  3199. status = lstatus;
  3200. }
  3201. *xpp = (const void *)xp;
  3202. return status;
  3203. }
  3204. int
  3205. ncx_getn_double_float(const void **xpp, size_t nelems, float *tp)
  3206. {
  3207. const char *xp = (const char *) *xpp;
  3208. int status = ENOERR;
  3209. for( ; nelems != 0; nelems--, xp += X_SIZEOF_DOUBLE, tp++)
  3210. {
  3211. const int lstatus = ncx_get_double_float(xp, tp);
  3212. if(lstatus != ENOERR)
  3213. status = lstatus;
  3214. }
  3215. *xpp = (const void *)xp;
  3216. return status;
  3217. }
  3218. #if X_SIZEOF_DOUBLE == SIZEOF_DOUBLE && !defined(NO_IEEE_FLOAT)
  3219. /* optimized version */
  3220. int
  3221. ncx_getn_double_double(const void **xpp, size_t nelems, double *tp)
  3222. {
  3223. # if WORDS_BIGENDIAN
  3224. (void) memcpy(tp, *xpp, nelems * sizeof(double));
  3225. # else
  3226. swapn8b(tp, *xpp, nelems);
  3227. # endif
  3228. *xpp = (const void *)((const char *)(*xpp) + nelems * X_SIZEOF_DOUBLE);
  3229. return ENOERR;
  3230. }
  3231. #elif vax
  3232. int
  3233. ncx_getn_double_double(const void **xpp, size_t ndoubles, double *ip)
  3234. {
  3235. double *const end = ip + ndoubles;
  3236. while(ip < end)
  3237. {
  3238. struct vax_double *const vdp =
  3239. (struct vax_double *)ip;
  3240. const struct ieee_double *const idp =
  3241. (const struct ieee_double *) (*xpp);
  3242. {
  3243. const struct dbl_limits *lim;
  3244. int ii;
  3245. for (ii = 0, lim = dbl_limits;
  3246. ii < sizeof(dbl_limits)/sizeof(struct dbl_limits);
  3247. ii++, lim++)
  3248. {
  3249. if ((idp->mant_lo == lim->ieee.mant_lo)
  3250. && (idp->mant_4 == lim->ieee.mant_4)
  3251. && (idp->mant_5 == lim->ieee.mant_5)
  3252. && (idp->mant_6 == lim->ieee.mant_6)
  3253. && (idp->exp_lo == lim->ieee.exp_lo)
  3254. && (idp->exp_hi == lim->ieee.exp_hi)
  3255. )
  3256. {
  3257. *vdp = lim->d;
  3258. goto doneit;
  3259. }
  3260. }
  3261. }
  3262. {
  3263. unsigned exp = idp->exp_hi << 4 | idp->exp_lo;
  3264. vdp->exp = exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
  3265. }
  3266. {
  3267. unsigned mant_hi = ((idp->mant_6 << 16)
  3268. | (idp->mant_5 << 8)
  3269. | idp->mant_4);
  3270. unsigned mant_lo = SWAP4(idp->mant_lo);
  3271. vdp->mantissa1 = (mant_hi >> 13);
  3272. vdp->mantissa2 = ((mant_hi & MASK(13)) << 3)
  3273. | (mant_lo >> 29);
  3274. vdp->mantissa3 = (mant_lo >> 13);
  3275. vdp->mantissa4 = (mant_lo << 3);
  3276. }
  3277. doneit:
  3278. vdp->sign = idp->sign;
  3279. ip++;
  3280. *xpp = (char *)(*xpp) + X_SIZEOF_DOUBLE;
  3281. }
  3282. return ENOERR;
  3283. }
  3284. /* vax */
  3285. #elif _SX && _FLOAT2
  3286. int
  3287. ncx_getn_double_double(const void **xpp, size_t nelems, double *tp)
  3288. {
  3289. const char *const xp = *xpp;
  3290. const int ncnv = ie3_fl2(xp, tp, 8, 8, nelems);
  3291. *xpp = xp + nelems * X_SIZEOF_DOUBLE;
  3292. return (nelems == ncnv ? ENOERR : NC_ERANGE);
  3293. }
  3294. #else
  3295. int
  3296. ncx_getn_double_double(const void **xpp, size_t nelems, double *tp)
  3297. {
  3298. const char *xp = *xpp;
  3299. int status = ENOERR;
  3300. for( ; nelems != 0; nelems--, xp += X_SIZEOF_DOUBLE, tp++)
  3301. {
  3302. const int lstatus = ncx_get_double_double(xp, tp);
  3303. if(lstatus != ENOERR)
  3304. status = lstatus;
  3305. }
  3306. *xpp = (const void *)xp;
  3307. return status;
  3308. }
  3309. #endif
  3310. int
  3311. ncx_putn_double_schar(void **xpp, size_t nelems, const schar *tp)
  3312. {
  3313. char *xp = (char *) *xpp;
  3314. int status = ENOERR;
  3315. for( ; nelems != 0; nelems--, xp += X_SIZEOF_DOUBLE, tp++)
  3316. {
  3317. int lstatus = ncx_put_double_schar(xp, tp);
  3318. if(lstatus != ENOERR)
  3319. status = lstatus;
  3320. }
  3321. *xpp = (void *)xp;
  3322. return status;
  3323. }
  3324. int
  3325. ncx_putn_double_uchar(void **xpp, size_t nelems, const uchar *tp)
  3326. {
  3327. char *xp = (char *) *xpp;
  3328. int status = ENOERR;
  3329. for( ; nelems != 0; nelems--, xp += X_SIZEOF_DOUBLE, tp++)
  3330. {
  3331. int lstatus = ncx_put_double_uchar(xp, tp);
  3332. if(lstatus != ENOERR)
  3333. status = lstatus;
  3334. }
  3335. *xpp = (void *)xp;
  3336. return status;
  3337. }
  3338. int
  3339. ncx_putn_double_short(void **xpp, size_t nelems, const short *tp)
  3340. {
  3341. char *xp = (char *) *xpp;
  3342. int status = ENOERR;
  3343. for( ; nelems != 0; nelems--, xp += X_SIZEOF_DOUBLE, tp++)
  3344. {
  3345. int lstatus = ncx_put_double_short(xp, tp);
  3346. if(lstatus != ENOERR)
  3347. status = lstatus;
  3348. }
  3349. *xpp = (void *)xp;
  3350. return status;
  3351. }
  3352. int
  3353. ncx_putn_double_int(void **xpp, size_t nelems, const int *tp)
  3354. {
  3355. char *xp = (char *) *xpp;
  3356. int status = ENOERR;
  3357. for( ; nelems != 0; nelems--, xp += X_SIZEOF_DOUBLE, tp++)
  3358. {
  3359. int lstatus = ncx_put_double_int(xp, tp);
  3360. if(lstatus != ENOERR)
  3361. status = lstatus;
  3362. }
  3363. *xpp = (void *)xp;
  3364. return status;
  3365. }
  3366. int
  3367. ncx_putn_double_long(void **xpp, size_t nelems, const long *tp)
  3368. {
  3369. char *xp = (char *) *xpp;
  3370. int status = ENOERR;
  3371. for( ; nelems != 0; nelems--, xp += X_SIZEOF_DOUBLE, tp++)
  3372. {
  3373. int lstatus = ncx_put_double_long(xp, tp);
  3374. if(lstatus != ENOERR)
  3375. status = lstatus;
  3376. }
  3377. *xpp = (void *)xp;
  3378. return status;
  3379. }
  3380. int
  3381. ncx_putn_double_float(void **xpp, size_t nelems, const float *tp)
  3382. {
  3383. char *xp = (char *) *xpp;
  3384. int status = ENOERR;
  3385. for( ; nelems != 0; nelems--, xp += X_SIZEOF_DOUBLE, tp++)
  3386. {
  3387. int lstatus = ncx_put_double_float(xp, tp);
  3388. if(lstatus != ENOERR)
  3389. status = lstatus;
  3390. }
  3391. *xpp = (void *)xp;
  3392. return status;
  3393. }
  3394. #if X_SIZEOF_DOUBLE == SIZEOF_DOUBLE && !defined(NO_IEEE_FLOAT)
  3395. /* optimized version */
  3396. int
  3397. ncx_putn_double_double(void **xpp, size_t nelems, const double *tp)
  3398. {
  3399. # if WORDS_BIGENDIAN
  3400. (void) memcpy(*xpp, tp, nelems * X_SIZEOF_DOUBLE);
  3401. # else
  3402. swapn8b(*xpp, tp, nelems);
  3403. # endif
  3404. *xpp = (void *)((char *)(*xpp) + nelems * X_SIZEOF_DOUBLE);
  3405. return ENOERR;
  3406. }
  3407. #elif vax
  3408. int
  3409. ncx_putn_double_double(void **xpp, size_t ndoubles, const double *ip)
  3410. {
  3411. const double *const end = ip + ndoubles;
  3412. while(ip < end)
  3413. {
  3414. const struct vax_double *const vdp =
  3415. (const struct vax_double *)ip;
  3416. struct ieee_double *const idp =
  3417. (struct ieee_double *) (*xpp);
  3418. if ((vdp->mantissa4 > (dbl_limits[0].d.mantissa4 - 3)) &&
  3419. (vdp->mantissa3 == dbl_limits[0].d.mantissa3) &&
  3420. (vdp->mantissa2 == dbl_limits[0].d.mantissa2) &&
  3421. (vdp->mantissa1 == dbl_limits[0].d.mantissa1) &&
  3422. (vdp->exp == dbl_limits[0].d.exp))
  3423. {
  3424. *idp = dbl_limits[0].ieee;
  3425. goto shipit;
  3426. }
  3427. if ((vdp->mantissa4 == dbl_limits[1].d.mantissa4) &&
  3428. (vdp->mantissa3 == dbl_limits[1].d.mantissa3) &&
  3429. (vdp->mantissa2 == dbl_limits[1].d.mantissa2) &&
  3430. (vdp->mantissa1 == dbl_limits[1].d.mantissa1) &&
  3431. (vdp->exp == dbl_limits[1].d.exp))
  3432. {
  3433. *idp = dbl_limits[1].ieee;
  3434. goto shipit;
  3435. }
  3436. {
  3437. unsigned exp = vdp->exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
  3438. unsigned mant_lo = ((vdp->mantissa2 & MASK(3)) << 29) |
  3439. (vdp->mantissa3 << 13) |
  3440. ((vdp->mantissa4 >> 3) & MASK(13));
  3441. unsigned mant_hi = (vdp->mantissa1 << 13)
  3442. | (vdp->mantissa2 >> 3);
  3443. if((vdp->mantissa4 & 7) > 4)
  3444. {
  3445. /* round up */
  3446. mant_lo++;
  3447. if(mant_lo == 0)
  3448. {
  3449. mant_hi++;
  3450. if(mant_hi > 0xffffff)
  3451. {
  3452. mant_hi = 0;
  3453. exp++;
  3454. }
  3455. }
  3456. }
  3457. idp->mant_lo = SWAP4(mant_lo);
  3458. idp->mant_6 = mant_hi >> 16;
  3459. idp->mant_5 = (mant_hi & 0xff00) >> 8;
  3460. idp->mant_4 = mant_hi;
  3461. idp->exp_hi = exp >> 4;
  3462. idp->exp_lo = exp;
  3463. }
  3464. shipit:
  3465. idp->sign = vdp->sign;
  3466. ip++;
  3467. *xpp = (char *)(*xpp) + X_SIZEOF_DOUBLE;
  3468. }
  3469. return ENOERR;
  3470. }
  3471. /* vax */
  3472. #elif _SX && _FLOAT2
  3473. int
  3474. ncx_putn_double_double(void **xpp, size_t nelems, const double *tp)
  3475. {
  3476. char *const xp = *xpp;
  3477. const int ncnv = fl2_ie3(tp, xp, 8, 8, nelems);
  3478. *xpp = xp + nelems * X_SIZEOF_DOUBLE;
  3479. return (nelems == ncnv ? ENOERR : NC_ERANGE);
  3480. }
  3481. #else
  3482. int
  3483. ncx_putn_double_double(void **xpp, size_t nelems, const double *tp)
  3484. {
  3485. char *xp = *xpp;
  3486. int status = ENOERR;
  3487. for( ; nelems != 0; nelems--, xp += X_SIZEOF_DOUBLE, tp++)
  3488. {
  3489. int lstatus = ncx_put_double_double(xp, tp);
  3490. if(lstatus != ENOERR)
  3491. status = lstatus;
  3492. }
  3493. *xpp = (void *)xp;
  3494. return status;
  3495. }
  3496. #endif
  3497. /*
  3498. * Other aggregate conversion functions.
  3499. */
  3500. /* text */
  3501. int
  3502. ncx_getn_text(const void **xpp, size_t nelems, char *tp)
  3503. {
  3504. (void) memcpy(tp, *xpp, nelems);
  3505. *xpp = (void *)((char *)(*xpp) + nelems);
  3506. return ENOERR;
  3507. }
  3508. int
  3509. ncx_pad_getn_text(const void **xpp, size_t nelems, char *tp)
  3510. {
  3511. size_t rndup = nelems % X_ALIGN;
  3512. if(rndup)
  3513. rndup = X_ALIGN - rndup;
  3514. (void) memcpy(tp, *xpp, nelems);
  3515. *xpp = (void *)((char *)(*xpp) + nelems + rndup);
  3516. return ENOERR;
  3517. }
  3518. int
  3519. ncx_putn_text(void **xpp, size_t nelems, const char *tp)
  3520. {
  3521. (void) memcpy(*xpp, tp, nelems);
  3522. *xpp = (void *)((char *)(*xpp) + nelems);
  3523. return ENOERR;
  3524. }
  3525. int
  3526. ncx_pad_putn_text(void **xpp, size_t nelems, const char *tp)
  3527. {
  3528. size_t rndup = nelems % X_ALIGN;
  3529. if(rndup)
  3530. rndup = X_ALIGN - rndup;
  3531. (void) memcpy(*xpp, tp, nelems);
  3532. *xpp = (void *)((char *)(*xpp) + nelems);
  3533. if(rndup)
  3534. {
  3535. (void) memcpy(*xpp, nada, rndup);
  3536. *xpp = (void *)((char *)(*xpp) + rndup);
  3537. }
  3538. return ENOERR;
  3539. }
  3540. /* opaque */
  3541. int
  3542. ncx_getn_void(const void **xpp, size_t nelems, void *tp)
  3543. {
  3544. (void) memcpy(tp, *xpp, nelems);
  3545. *xpp = (void *)((char *)(*xpp) + nelems);
  3546. return ENOERR;
  3547. }
  3548. int
  3549. ncx_pad_getn_void(const void **xpp, size_t nelems, void *tp)
  3550. {
  3551. size_t rndup = nelems % X_ALIGN;
  3552. if(rndup)
  3553. rndup = X_ALIGN - rndup;
  3554. (void) memcpy(tp, *xpp, nelems);
  3555. *xpp = (void *)((char *)(*xpp) + nelems + rndup);
  3556. return ENOERR;
  3557. }
  3558. int
  3559. ncx_putn_void(void **xpp, size_t nelems, const void *tp)
  3560. {
  3561. (void) memcpy(*xpp, tp, nelems);
  3562. *xpp = (void *)((char *)(*xpp) + nelems);
  3563. return ENOERR;
  3564. }
  3565. int
  3566. ncx_pad_putn_void(void **xpp, size_t nelems, const void *tp)
  3567. {
  3568. size_t rndup = nelems % X_ALIGN;
  3569. if(rndup)
  3570. rndup = X_ALIGN - rndup;
  3571. (void) memcpy(*xpp, tp, nelems);
  3572. *xpp = (void *)((char *)(*xpp) + nelems);
  3573. if(rndup)
  3574. {
  3575. (void) memcpy(*xpp, nada, rndup);
  3576. *xpp = (void *)((char *)(*xpp) + rndup);
  3577. }
  3578. return ENOERR;
  3579. }