xdr_float.c 6.7 KB

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