xdr_float.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /* @(#)xdr_float.c 2.1 88/07/29 4.0 RPCSRC */
  2. /*
  3. * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  4. * unrestricted use provided that this legend is included on all tape
  5. * media and as a part of the software program in whole or part. Users
  6. * may copy or modify Sun RPC without charge, but are not authorized
  7. * to license or distribute it to anyone else except as part of a product or
  8. * program developed by the user.
  9. *
  10. * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  11. * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  12. * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  13. *
  14. * Sun RPC is provided with no support and without any obligation on the
  15. * part of Sun Microsystems, Inc. to assist in its use, correction,
  16. * modification or enhancement.
  17. *
  18. * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  19. * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  20. * OR ANY PART THEREOF.
  21. *
  22. * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  23. * or profits or other special, indirect and consequential damages, even if
  24. * Sun has been advised of the possibility of such damages.
  25. *
  26. * Sun Microsystems, Inc.
  27. * 2550 Garcia Avenue
  28. * Mountain View, California 94043
  29. */
  30. #if 0
  31. static char sccsid[] = "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro";
  32. #endif
  33. /*
  34. * xdr_float.c, Generic XDR routines implementation.
  35. *
  36. * Copyright (C) 1984, Sun Microsystems, Inc.
  37. *
  38. * These are the "floating point" xdr routines used to (de)serialize
  39. * most common data items. See xdr.h for more info on the interface to
  40. * xdr.
  41. */
  42. #define __FORCE_GLIBC
  43. #include <features.h>
  44. #include <stdio.h>
  45. #include <endian.h>
  46. #include <rpc/types.h>
  47. #include <rpc/xdr.h>
  48. /*
  49. * NB: Not portable.
  50. * This routine works on Suns (Sky / 68000's) and Vaxen.
  51. */
  52. #define LSW (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
  53. #ifdef vax
  54. /* What IEEE single precision floating point looks like on a Vax */
  55. struct ieee_single {
  56. unsigned int mantissa: 23;
  57. unsigned int exp : 8;
  58. unsigned int sign : 1;
  59. };
  60. /* Vax single precision floating point */
  61. struct vax_single {
  62. unsigned int mantissa1 : 7;
  63. unsigned int exp : 8;
  64. unsigned int sign : 1;
  65. unsigned int mantissa2 : 16;
  66. };
  67. #define VAX_SNG_BIAS 0x81
  68. #define IEEE_SNG_BIAS 0x7f
  69. static struct sgl_limits {
  70. struct vax_single s;
  71. struct ieee_single ieee;
  72. } sgl_limits[2] = {
  73. {{ 0x7f, 0xff, 0x0, 0xffff }, /* Max Vax */
  74. { 0x0, 0xff, 0x0 }}, /* Max IEEE */
  75. {{ 0x0, 0x0, 0x0, 0x0 }, /* Min Vax */
  76. { 0x0, 0x0, 0x0 }} /* Min IEEE */
  77. };
  78. #endif /* vax */
  79. bool_t
  80. xdr_float(xdrs, fp)
  81. XDR *xdrs;
  82. float *fp;
  83. {
  84. #ifdef vax
  85. struct ieee_single is;
  86. struct vax_single vs, *vsp;
  87. struct sgl_limits *lim;
  88. int i;
  89. #endif
  90. switch (xdrs->x_op) {
  91. case XDR_ENCODE:
  92. #ifdef vax
  93. vs = *((struct vax_single *)fp);
  94. for (i = 0, lim = sgl_limits;
  95. i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
  96. i++, lim++) {
  97. if ((vs.mantissa2 == lim->s.mantissa2) &&
  98. (vs.exp == lim->s.exp) &&
  99. (vs.mantissa1 == lim->s.mantissa1)) {
  100. is = lim->ieee;
  101. goto shipit;
  102. }
  103. }
  104. is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
  105. is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
  106. shipit:
  107. is.sign = vs.sign;
  108. return (XDR_PUTLONG(xdrs, (long *)&is));
  109. #else
  110. if (sizeof(float) == sizeof(long))
  111. return (XDR_PUTLONG(xdrs, (long *)fp));
  112. else if (sizeof(float) == sizeof(int)) {
  113. long tmp = *(int *)fp;
  114. return (XDR_PUTLONG(xdrs, &tmp));
  115. }
  116. break;
  117. #endif
  118. case XDR_DECODE:
  119. #ifdef vax
  120. vsp = (struct vax_single *)fp;
  121. if (!XDR_GETLONG(xdrs, (long *)&is))
  122. return (FALSE);
  123. for (i = 0, lim = sgl_limits;
  124. i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
  125. i++, lim++) {
  126. if ((is.exp == lim->ieee.exp) &&
  127. (is.mantissa == lim->ieee.mantissa)) {
  128. *vsp = lim->s;
  129. goto doneit;
  130. }
  131. }
  132. vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
  133. vsp->mantissa2 = is.mantissa;
  134. vsp->mantissa1 = (is.mantissa >> 16);
  135. doneit:
  136. vsp->sign = is.sign;
  137. return (TRUE);
  138. #else
  139. if (sizeof(float) == sizeof(long))
  140. return (XDR_GETLONG(xdrs, (long *)fp));
  141. else if (sizeof(float) == sizeof(int)) {
  142. long tmp;
  143. if (XDR_GETLONG(xdrs, &tmp)) {
  144. *(int *)fp = tmp;
  145. return (TRUE);
  146. }
  147. }
  148. break;
  149. #endif
  150. case XDR_FREE:
  151. return (TRUE);
  152. }
  153. return (FALSE);
  154. }
  155. /*
  156. * This routine works on Suns (Sky / 68000's) and Vaxen.
  157. */
  158. #ifdef vax
  159. /* What IEEE double precision floating point looks like on a Vax */
  160. struct ieee_double {
  161. unsigned int mantissa1 : 20;
  162. unsigned int exp : 11;
  163. unsigned int sign : 1;
  164. unsigned int mantissa2 : 32;
  165. };
  166. /* Vax double precision floating point */
  167. struct vax_double {
  168. unsigned int mantissa1 : 7;
  169. unsigned int exp : 8;
  170. unsigned int sign : 1;
  171. unsigned int mantissa2 : 16;
  172. unsigned int mantissa3 : 16;
  173. unsigned int mantissa4 : 16;
  174. };
  175. #define VAX_DBL_BIAS 0x81
  176. #define IEEE_DBL_BIAS 0x3ff
  177. #define MASK(nbits) ((1 << nbits) - 1)
  178. static struct dbl_limits {
  179. struct vax_double d;
  180. struct ieee_double ieee;
  181. } dbl_limits[2] = {
  182. {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */
  183. { 0x0, 0x7ff, 0x0, 0x0 }}, /* Max IEEE */
  184. {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */
  185. { 0x0, 0x0, 0x0, 0x0 }} /* Min IEEE */
  186. };
  187. #endif /* vax */
  188. bool_t
  189. xdr_double(xdrs, dp)
  190. XDR *xdrs;
  191. double *dp;
  192. {
  193. #ifdef vax
  194. struct ieee_double id;
  195. struct vax_double vd;
  196. register struct dbl_limits *lim;
  197. int i;
  198. #endif
  199. switch (xdrs->x_op) {
  200. case XDR_ENCODE:
  201. #ifdef vax
  202. vd = *((struct vax_double *)dp);
  203. for (i = 0, lim = dbl_limits;
  204. i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
  205. i++, lim++) {
  206. if ((vd.mantissa4 == lim->d.mantissa4) &&
  207. (vd.mantissa3 == lim->d.mantissa3) &&
  208. (vd.mantissa2 == lim->d.mantissa2) &&
  209. (vd.mantissa1 == lim->d.mantissa1) &&
  210. (vd.exp == lim->d.exp)) {
  211. id = lim->ieee;
  212. goto shipit;
  213. }
  214. }
  215. id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
  216. id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
  217. id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
  218. (vd.mantissa3 << 13) |
  219. ((vd.mantissa4 >> 3) & MASK(13));
  220. shipit:
  221. id.sign = vd.sign;
  222. dp = (double *)&id;
  223. #endif
  224. if (2*sizeof(long) == sizeof(double)) {
  225. long *lp = (long *)dp;
  226. return (XDR_PUTLONG(xdrs, lp+!LSW) &&
  227. XDR_PUTLONG(xdrs, lp+LSW));
  228. } else if (2*sizeof(int) == sizeof(double)) {
  229. int *ip = (int *)dp;
  230. long tmp[2];
  231. tmp[0] = ip[!LSW];
  232. tmp[1] = ip[LSW];
  233. return (XDR_PUTLONG(xdrs, tmp) &&
  234. XDR_PUTLONG(xdrs, tmp+1));
  235. }
  236. break;
  237. case XDR_DECODE:
  238. #ifdef vax
  239. lp = (long *)&id;
  240. if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp))
  241. return (FALSE);
  242. for (i = 0, lim = dbl_limits;
  243. i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
  244. i++, lim++) {
  245. if ((id.mantissa2 == lim->ieee.mantissa2) &&
  246. (id.mantissa1 == lim->ieee.mantissa1) &&
  247. (id.exp == lim->ieee.exp)) {
  248. vd = lim->d;
  249. goto doneit;
  250. }
  251. }
  252. vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
  253. vd.mantissa1 = (id.mantissa1 >> 13);
  254. vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
  255. (id.mantissa2 >> 29);
  256. vd.mantissa3 = (id.mantissa2 >> 13);
  257. vd.mantissa4 = (id.mantissa2 << 3);
  258. doneit:
  259. vd.sign = id.sign;
  260. *dp = *((double *)&vd);
  261. return (TRUE);
  262. #else
  263. if (2*sizeof(long) == sizeof(double)) {
  264. long *lp = (long *)dp;
  265. return (XDR_GETLONG(xdrs, lp+!LSW) &&
  266. XDR_GETLONG(xdrs, lp+LSW));
  267. } else if (2*sizeof(int) == sizeof(double)) {
  268. int *ip = (int *)dp;
  269. long tmp[2];
  270. if (XDR_GETLONG(xdrs, tmp+!LSW) &&
  271. XDR_GETLONG(xdrs, tmp+LSW)) {
  272. ip[0] = tmp[0];
  273. ip[1] = tmp[1];
  274. return (TRUE);
  275. }
  276. }
  277. break;
  278. #endif
  279. case XDR_FREE:
  280. return (TRUE);
  281. }
  282. return (FALSE);
  283. }