xdr_float.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 !defined(lint) && defined(SCCSIDS)
  31. static char sccsid[] =
  32. "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro";
  33. #endif
  34. /*
  35. * xdr_float.c, Generic XDR routines impelmentation.
  36. *
  37. * Copyright (C) 1984, Sun Microsystems, Inc.
  38. *
  39. * These are the "floating point" xdr routines used to (de)serialize
  40. * most common data items. See xdr.h for more info on the interface to
  41. * xdr.
  42. */
  43. #include <stdio.h>
  44. #include <rpc/types.h>
  45. #include <rpc/xdr.h>
  46. /*
  47. * NB: Not portable.
  48. * This routine works on Suns (Sky / 68000's) and Vaxen.
  49. */
  50. #ifdef __linux__
  51. /* cheat big time */
  52. #define sparc
  53. #endif
  54. #ifdef vax
  55. /* What IEEE single precision floating point looks like on a Vax */
  56. struct ieee_single {
  57. unsigned int mantissa:23;
  58. unsigned int exp:8;
  59. unsigned int sign:1;
  60. };
  61. /* Vax single precision floating point */
  62. struct vax_single {
  63. unsigned int mantissa1:7;
  64. unsigned int exp:8;
  65. unsigned int sign:1;
  66. unsigned int mantissa2:16;
  67. };
  68. #define VAX_SNG_BIAS 0x81
  69. #define IEEE_SNG_BIAS 0x7f
  70. static struct sgl_limits {
  71. struct vax_single s;
  72. struct ieee_single ieee;
  73. } sgl_limits[2] = {
  74. { {
  75. 0x7f, 0xff, 0x0, 0xffff}, /* Max Vax */
  76. {
  77. 0x0, 0xff, 0x0}}, /* Max IEEE */
  78. { {
  79. 0x0, 0x0, 0x0, 0x0}, /* Min Vax */
  80. {
  81. 0x0, 0x0, 0x0}} /* Min IEEE */
  82. };
  83. #endif /* vax */
  84. bool_t xdr_float(xdrs, fp)
  85. register XDR *xdrs;
  86. register float *fp;
  87. {
  88. #if !defined(mc68000) && !defined(sparc)
  89. struct ieee_single is;
  90. struct vax_single vs, *vsp;
  91. struct sgl_limits *lim;
  92. int i;
  93. #endif
  94. switch (xdrs->x_op) {
  95. case XDR_ENCODE:
  96. #if defined(mc68000) || defined(sparc)
  97. return (XDR_PUTLONG(xdrs, (long *) fp));
  98. #else
  99. vs = *((struct vax_single *) fp);
  100. for (i = 0, lim = sgl_limits;
  101. i < sizeof(sgl_limits) / sizeof(struct sgl_limits);
  102. i++, lim++) {
  103. if ((vs.mantissa2 == lim->s.mantissa2) &&
  104. (vs.exp == lim->s.exp) &&
  105. (vs.mantissa1 == lim->s.mantissa1)) {
  106. is = lim->ieee;
  107. goto shipit;
  108. }
  109. }
  110. is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
  111. is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
  112. shipit:
  113. is.sign = vs.sign;
  114. return (XDR_PUTLONG(xdrs, (long *) &is));
  115. #endif
  116. case XDR_DECODE:
  117. #if defined(mc68000) || defined(sparc)
  118. return (XDR_GETLONG(xdrs, (long *) fp));
  119. #else
  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. #endif
  139. case XDR_FREE:
  140. return (TRUE);
  141. }
  142. return (FALSE);
  143. }
  144. /*
  145. * This routine works on Suns (Sky / 68000's) and Vaxen.
  146. */
  147. #ifdef vax
  148. /* What IEEE double precision floating point looks like on a Vax */
  149. struct ieee_double {
  150. unsigned int mantissa1:20;
  151. unsigned int exp:11;
  152. unsigned int sign:1;
  153. unsigned int mantissa2:32;
  154. };
  155. /* Vax double precision floating point */
  156. struct vax_double {
  157. unsigned int mantissa1:7;
  158. unsigned int exp:8;
  159. unsigned int sign:1;
  160. unsigned int mantissa2:16;
  161. unsigned int mantissa3:16;
  162. unsigned int mantissa4:16;
  163. };
  164. #define VAX_DBL_BIAS 0x81
  165. #define IEEE_DBL_BIAS 0x3ff
  166. #define MASK(nbits) ((1 << nbits) - 1)
  167. static struct dbl_limits {
  168. struct vax_double d;
  169. struct ieee_double ieee;
  170. } dbl_limits[2] = {
  171. { {
  172. 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff}, /* Max Vax */
  173. {
  174. 0x0, 0x7ff, 0x0, 0x0}}, /* Max IEEE */
  175. { {
  176. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */
  177. {
  178. 0x0, 0x0, 0x0, 0x0}} /* Min IEEE */
  179. };
  180. #endif /* vax */
  181. bool_t xdr_double(xdrs, dp)
  182. register XDR *xdrs;
  183. double *dp;
  184. {
  185. register long *lp;
  186. #if !defined(mc68000) && !defined(sparc)
  187. struct ieee_double id;
  188. struct vax_double vd;
  189. register struct dbl_limits *lim;
  190. int i;
  191. #endif
  192. switch (xdrs->x_op) {
  193. case XDR_ENCODE:
  194. #if defined(mc68000) || defined(sparc)
  195. lp = (long *) dp;
  196. #else
  197. vd = *((struct vax_double *) dp);
  198. for (i = 0, lim = dbl_limits;
  199. i < sizeof(dbl_limits) / sizeof(struct dbl_limits);
  200. i++, lim++) {
  201. if ((vd.mantissa4 == lim->d.mantissa4) &&
  202. (vd.mantissa3 == lim->d.mantissa3) &&
  203. (vd.mantissa2 == lim->d.mantissa2) &&
  204. (vd.mantissa1 == lim->d.mantissa1) &&
  205. (vd.exp == lim->d.exp)) {
  206. id = lim->ieee;
  207. goto shipit;
  208. }
  209. }
  210. id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
  211. id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
  212. id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
  213. (vd.mantissa3 << 13) | ((vd.mantissa4 >> 3) & MASK(13));
  214. shipit:
  215. id.sign = vd.sign;
  216. lp = (long *) &id;
  217. #endif
  218. return (XDR_PUTLONG(xdrs, lp++) && XDR_PUTLONG(xdrs, lp));
  219. case XDR_DECODE:
  220. #if defined(mc68000) || defined(sparc)
  221. lp = (long *) dp;
  222. return (XDR_GETLONG(xdrs, lp++) && XDR_GETLONG(xdrs, lp));
  223. #else
  224. lp = (long *) &id;
  225. if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp))
  226. return (FALSE);
  227. for (i = 0, lim = dbl_limits;
  228. i < sizeof(dbl_limits) / sizeof(struct dbl_limits);
  229. i++, lim++) {
  230. if ((id.mantissa2 == lim->ieee.mantissa2) &&
  231. (id.mantissa1 == lim->ieee.mantissa1) &&
  232. (id.exp == lim->ieee.exp)) {
  233. vd = lim->d;
  234. goto doneit;
  235. }
  236. }
  237. vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
  238. vd.mantissa1 = (id.mantissa1 >> 13);
  239. vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
  240. (id.mantissa2 >> 29);
  241. vd.mantissa3 = (id.mantissa2 >> 13);
  242. vd.mantissa4 = (id.mantissa2 << 3);
  243. doneit:
  244. vd.sign = id.sign;
  245. *dp = *((double *) &vd);
  246. return (TRUE);
  247. #endif
  248. case XDR_FREE:
  249. return (TRUE);
  250. }
  251. return (FALSE);
  252. }