xdr_float.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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[] = "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro";
  32. #endif
  33. /*
  34. * xdr_float.c, Generic XDR routines impelmentation.
  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. #include <stdio.h>
  43. #include <rpc/types.h>
  44. #include <rpc/xdr.h>
  45. /*
  46. * NB: Not portable.
  47. * This routine works on Suns (Sky / 68000's) and Vaxen.
  48. */
  49. #ifdef __linux__
  50. /* cheat big time */
  51. #define sparc
  52. #endif
  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. register XDR *xdrs;
  82. register float *fp;
  83. {
  84. #if !defined(mc68000) && !defined(sparc)
  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. #if defined(mc68000) || defined(sparc)
  93. return (XDR_PUTLONG(xdrs, (long *)fp));
  94. #else
  95. vs = *((struct vax_single *)fp);
  96. for (i = 0, lim = sgl_limits;
  97. i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
  98. i++, lim++) {
  99. if ((vs.mantissa2 == lim->s.mantissa2) &&
  100. (vs.exp == lim->s.exp) &&
  101. (vs.mantissa1 == lim->s.mantissa1)) {
  102. is = lim->ieee;
  103. goto shipit;
  104. }
  105. }
  106. is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
  107. is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
  108. shipit:
  109. is.sign = vs.sign;
  110. return (XDR_PUTLONG(xdrs, (long *)&is));
  111. #endif
  112. case XDR_DECODE:
  113. #if defined(mc68000) || defined(sparc)
  114. return (XDR_GETLONG(xdrs, (long *)fp));
  115. #else
  116. vsp = (struct vax_single *)fp;
  117. if (!XDR_GETLONG(xdrs, (long *)&is))
  118. return (FALSE);
  119. for (i = 0, lim = sgl_limits;
  120. i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
  121. i++, lim++) {
  122. if ((is.exp == lim->ieee.exp) &&
  123. (is.mantissa == lim->ieee.mantissa)) {
  124. *vsp = lim->s;
  125. goto doneit;
  126. }
  127. }
  128. vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
  129. vsp->mantissa2 = is.mantissa;
  130. vsp->mantissa1 = (is.mantissa >> 16);
  131. doneit:
  132. vsp->sign = is.sign;
  133. return (TRUE);
  134. #endif
  135. case XDR_FREE:
  136. return (TRUE);
  137. }
  138. return (FALSE);
  139. }
  140. /*
  141. * This routine works on Suns (Sky / 68000's) and Vaxen.
  142. */
  143. #ifdef vax
  144. /* What IEEE double precision floating point looks like on a Vax */
  145. struct ieee_double {
  146. unsigned int mantissa1 : 20;
  147. unsigned int exp : 11;
  148. unsigned int sign : 1;
  149. unsigned int mantissa2 : 32;
  150. };
  151. /* Vax double precision floating point */
  152. struct vax_double {
  153. unsigned int mantissa1 : 7;
  154. unsigned int exp : 8;
  155. unsigned int sign : 1;
  156. unsigned int mantissa2 : 16;
  157. unsigned int mantissa3 : 16;
  158. unsigned int mantissa4 : 16;
  159. };
  160. #define VAX_DBL_BIAS 0x81
  161. #define IEEE_DBL_BIAS 0x3ff
  162. #define MASK(nbits) ((1 << nbits) - 1)
  163. static struct dbl_limits {
  164. struct vax_double d;
  165. struct ieee_double ieee;
  166. } dbl_limits[2] = {
  167. {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */
  168. { 0x0, 0x7ff, 0x0, 0x0 }}, /* Max IEEE */
  169. {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */
  170. { 0x0, 0x0, 0x0, 0x0 }} /* Min IEEE */
  171. };
  172. #endif /* vax */
  173. bool_t
  174. xdr_double(xdrs, dp)
  175. register XDR *xdrs;
  176. double *dp;
  177. {
  178. register long *lp;
  179. #if !defined(mc68000) && !defined(sparc)
  180. struct ieee_double id;
  181. struct vax_double vd;
  182. register struct dbl_limits *lim;
  183. int i;
  184. #endif
  185. switch (xdrs->x_op) {
  186. case XDR_ENCODE:
  187. #if defined(mc68000) || defined(sparc)
  188. lp = (long *)dp;
  189. #else
  190. vd = *((struct vax_double *)dp);
  191. for (i = 0, lim = dbl_limits;
  192. i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
  193. i++, lim++) {
  194. if ((vd.mantissa4 == lim->d.mantissa4) &&
  195. (vd.mantissa3 == lim->d.mantissa3) &&
  196. (vd.mantissa2 == lim->d.mantissa2) &&
  197. (vd.mantissa1 == lim->d.mantissa1) &&
  198. (vd.exp == lim->d.exp)) {
  199. id = lim->ieee;
  200. goto shipit;
  201. }
  202. }
  203. id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
  204. id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
  205. id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
  206. (vd.mantissa3 << 13) |
  207. ((vd.mantissa4 >> 3) & MASK(13));
  208. shipit:
  209. id.sign = vd.sign;
  210. lp = (long *)&id;
  211. #endif
  212. return (XDR_PUTLONG(xdrs, lp++) && XDR_PUTLONG(xdrs, lp));
  213. case XDR_DECODE:
  214. #if defined(mc68000) || defined(sparc)
  215. lp = (long *)dp;
  216. return (XDR_GETLONG(xdrs, lp++) && XDR_GETLONG(xdrs, lp));
  217. #else
  218. lp = (long *)&id;
  219. if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp))
  220. return (FALSE);
  221. for (i = 0, lim = dbl_limits;
  222. i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
  223. i++, lim++) {
  224. if ((id.mantissa2 == lim->ieee.mantissa2) &&
  225. (id.mantissa1 == lim->ieee.mantissa1) &&
  226. (id.exp == lim->ieee.exp)) {
  227. vd = lim->d;
  228. goto doneit;
  229. }
  230. }
  231. vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
  232. vd.mantissa1 = (id.mantissa1 >> 13);
  233. vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
  234. (id.mantissa2 >> 29);
  235. vd.mantissa3 = (id.mantissa2 >> 13);
  236. vd.mantissa4 = (id.mantissa2 << 3);
  237. doneit:
  238. vd.sign = id.sign;
  239. *dp = *((double *)&vd);
  240. return (TRUE);
  241. #endif
  242. case XDR_FREE:
  243. return (TRUE);
  244. }
  245. return (FALSE);
  246. }